• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

A The SWI-Prolog library
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • The SWI-Prolog library
        • library(aggregate): Aggregation operators on backtrackable predicates
        • library(ansi_term): Print decorated text to ANSI consoles
        • library(apply): Apply predicates on a list
        • library(assoc): Association lists
        • library(broadcast): Broadcast and receive event notifications
        • library(charsio): I/O on Lists of Character Codes
        • library(check): Consistency checking
        • library(clpb): CLP(B): Constraint Logic Programming over Boolean Variables
        • library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
        • library(clpqr): Constraint Logic Programming over Rationals and Reals
        • library(csv): Process CSV (Comma-Separated Values) data
        • library(dcg/basics): Various general DCG utilities
        • library(dcg/high_order): High order grammar operations
        • library(debug): Print debug messages and test assertions
        • library(dicts): Dict utilities
        • library(error): Error generating support
        • library(fastrw): Fast reading and writing of terms
        • library(gensym): Generate unique symbols
        • library(heaps): heaps/priority queues
        • library(increval): Incremental dynamic predicate modification
        • library(intercept): Intercept and signal interface
        • library(iostream): Utilities to deal with streams
        • library(listing): List programs and pretty print clauses
        • library(lists): List Manipulation
        • library(main): Provide entry point for scripts
        • library(nb_set): Non-backtrackable set
        • library(www_browser): Open a URL in the users browser
        • library(occurs): Finding and counting sub-terms
        • library(option): Option list processing
        • library(optparse): command line parsing
        • library(ordsets): Ordered set manipulation
        • library(pairs): Operations on key-value lists
        • library(persistency): Provide persistent dynamic predicates
        • library(pio): Pure I/O
        • library(portray_text): Portray text
        • library(predicate_options): Declare option-processing of predicates
        • library(prolog_debug): User level debugging tools
        • library(prolog_jiti): Just In Time Indexing (JITI) utilities
        • library(prolog_pack): A package manager for Prolog
        • library(prolog_xref): Prolog cross-referencer data collection
        • library(quasi_quotations): Define Quasi Quotation syntax
          • with_quasi_quotation_input/3
          • phrase_from_quasi_quotation/2
          • quasi_quotation_syntax/1
          • quasi_quotation_syntax_error/1
        • library(random): Random numbers
        • library(rbtrees): Red black trees
        • library(readutil): Read utilities
        • library(record): Access named fields in a term
        • library(registry): Manipulating the Windows registry
        • library(settings): Setting management
        • library(statistics): Get information about resource usage
        • library(strings): String utilities
        • library(simplex): Solve linear programming problems
        • library(solution_sequences): Modify solution sequences
        • library(tables): XSB interface to tables
        • library(terms): Term manipulation
        • library(thread): High level thread primitives
        • library(thread_pool): Resource bounded thread management
        • library(ugraphs): Graph manipulation library
        • library(url): Analysing and constructing URL
        • library(varnumbers): Utilities for numbered terms
        • library(yall): Lambda expressions
    • Packages

A.42 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

  • 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 terms Name = 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.

[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:

  • 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.
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)

ClioPatria (version V3.1.1-42-gd6a756b-DIRTY)