Inspired by Haskell, SWI-Prolog support quasi quotation. Quasi quotation allows for embedding (long) strings using the syntax of an external language (e.g., HTML, SQL) in Prolog text and syntax-aware embedding of Prolog variables in this syntax. At the same time, quasi quotation provides an alternative to represent long strings and atoms in Prolog.
The basic form of a quasi quotation is defined below. Here, Syntax
is an arbitrary Prolog term that must parse into a callable (atom
or compound) term and Quotation is an arbitrary sequence of characters,
not including the sequence |}
. If this sequence needs to be
embedded, it must be escaped according to the rules of the target
language or the‘quoter’must provide an escaping mechanism.
{|Syntax||Quotation|}
While reading a Prolog term, and if the Prolog flag quasi_quotes
is set to true
(which is the case if this library is
loaded), the parser collects quasi quotations. After reading the final
full stop, the parser makes the call below. Here, SyntaxName
is the functor name of Syntax above and SyntaxArgs
is a list holding the arguments, i.e., Syntax =.. [SyntaxName|SyntaxArgs]
.
Splitting the syntax into its name and arguments is done to make the
quasi quotation parser a predicate with a consistent arity 4, regardless
of the number of additional arguments.
call(+SyntaxName, +Content, +SyntaxArgs, +VariableNames, -Result)
The arguments are defined as
..., {|html(Name, Address)|| <tr><td>Name<td>Address</tr> |}
variable_names
. It is a list of terms Name = Var
.library(http/html_write)
). Examples of languages that may
be embedded for processing in Prolog are SPARQL, RuleML or regular
expressions.
The file library(http/html_quasiquotations)
provides
the, suprisingly simple, quasi quotation parser for HTML.
utf8
encoding.Goal | is executed as once(Goal) . Goal
must succeed. Failure or exceptions from Goal are interpreted
as syntax errors. |
syntax_error(Error)
using the current location in
the quasi quoted input parser.
error(syntax_error(Error), Position)