quasi_quotations.pl -- Define Quasi Quotation syntax
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
- SyntaxName is the principal functor of the quasi quotation syntax. This must be declared using quasi_quotation_syntax/1 and there must be a predicate SyntaxName/4.
- Content is an opaque term that carries the content of the quasi quoted material and position information about the source code. It is passed to with_quasi_quote_input/3.
- SyntaxArgs carries the additional arguments of the Syntax. These are
commonly used to make the parameter passing between the clause and the
quasi quotation explicit. For example:
..., {|html(Name, Address)|| <tr><td>Name<td>Address</tr> |}
- VariableNames is the complete variable dictionary of the clause as
it is made available throug read_term/3 with the option
variable_names
. It is a list of termsName = Var
. - Result is a variable that must be unified to resulting term. Typically, this term is structured Prolog tree that carries a (partial) representation of the abstract syntax tree with embedded variables that pass the Prolog parameters. This term is normally either passed to a predicate that serializes the abstract syntax tree, or a predicate that processes the result in Prolog. For example, HTML is commonly embedded for writing HTML documents (see 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.
- with_quasi_quotation_input(+Content, -Stream, :Goal) is det
- Process the quasi-quoted Content using Stream parsed by Goal.
Stream is a temporary stream with the following properties:
- Its initial position represents the position of the start of the quoted material.
- It is a text stream, using
utf8
encoding. - It allows for repositioning
- It will be closed after Goal completes.
- phrase_from_quasi_quotation(:Grammar, +Content) is det
- Process the quasi quotation using the DCG Grammar. Failure of the grammer is interpreted as a syntax error.
- quasi_quotation_syntax(:SyntaxName) is det
- Declare the predicate SyntaxName/4 to implement the the quasi quote syntax SyntaxName. Normally used as a directive.
- quasi_quotation_syntax_error(+Error)
- Report
syntax_error(Error)
using the current location in the quasi quoted input parser.