A.43 library(quasi_quotations): Define Quasi Quotation syntax

author
Jan Wielemaker. Introduction of Quasi Quotation was suggested by Michael Hendricks.
See also
Why it's nice to be quoted: quasiquoting for haskell

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

The file library(http/html_quasiquotations) provides the, suprisingly simple, quasi quotation parser for HTML.

[det]with_quasi_quotation_input(+Content, -Stream, :Goal)
Process the quasi-quoted Content using Stream parsed by Goal. Stream is a temporary stream with the following properties:

Goal is executed as once(Goal). Goal must succeed. Failure or exceptions from Goal are interpreted as syntax errors.
See also
phrase_from_quasi_quotation/2 can be used to process a quotation using a grammar.
[det]phrase_from_quasi_quotation(:Grammar, +Content)
Process the quasi quotation using the DCG Grammar. Failure of the grammar is interpreted as a syntax error.
See also
with_quasi_quotation_input/3 for processing quotations from stream.
[det]quasi_quotation_syntax(:SyntaxName)
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.
throws
error(syntax_error(Error), Position)