| Did you know ... | Search Documentation: |
| Predicate read_term/2 |
current_input
stream and variations that lack the
Options argument read using an empty option list. On reaching
end-of-file Term is unified with the atom end_of_file.
Syntax errors are reported using exception handling (see catch/3)
unless the syntax_errors option demands otherwise. The
argument order is according to the ISO standard.
[NOTE] You might have found this while looking for a predicate
to read input from a file or the user. Quite likely this is not what you
need in this case. These predicates read a Prolog term, which may
span multiple lines and must end in a full stop (dot character
followed by a layout character). The predicates for reading and writing
Prolog terms are particularly useful for storing Prolog data in a file
or transferring them over a network communication channel (socket) to
another Prolog process. The libraries provide a wealth of predicates to
read data in other formats. See e.g., library(readutil),
library(pure_input) or libraries from the extension
packages to read XML, JSON, YAML, etc.
This family of predicates is intended for reading Prolog terms from a stream. The predicate read_clause/3 reads a term the way the compiler does. Use read_term_from_atom/3, term_to_atom/2 or term_string/2 to read a term from text and read_term_with_history/2 to read a term with the history substitutions of the toplevel.
Valid options for read_term/2 and read_term/3 are:
true, read `...` to a string
object (see
section 5.2). The default
depends on the Prolog flag
back_quotes.<Type>(Arg,
... ), e.g.
<stream>(0x55c1e0). See section
12.4.10. Values are
dead, but first look for a blob that is alive in this
process and writes as this text. This is what the toplevel uses, so a
blob can be copied from earlier output and pasted back into a goal. Note
that this turns text into a live handle and must therefore only be used
on trusted input. It is also best effort because the original blob may
have been reclaimed and a new blob of the same type may have allocated a
new resource at the same address (known as the ABA problem).
Considering the toplevel, (small) answers are recorded for reuse as
$Var and blobs that are part of small answers are thus
referenced as long as the answer binding is not changed.
There is no index from the written form back to the blob. If the first argument is the address of the blob data, which is what the write() function of a blob normally emits, only the blob at this address is written to compare the result. If it is not, every live blob of Type is written. Either way the comparison is on the text, i.e., the blob must write exactly as it was read.
The notation is only unambiguous as long as
is not a prefix operator: if it is, <<a>(f)
is the legal term >(<(a),f). In a module that
declares such an operator the reader therefore leaves the text alone and
blobs cannot be read, regardless of Mode.
\ escape sequences in quoted atoms. See
the Prolog flag character_escapes
in current_prolog_flag/2.
(SWI-Prolog).true (default false), re-instantiate
templates as produced by the corresponding write_term/2
option. Note that the default is false to avoid
misinterpretation of @(Template, Substitutions), while the
default of write_term/2
is true because emitting cyclic terms without using the
template construct produces an infinitely large term (read: it will
generate an error after producing a huge amount of output).true (default false), read .(a,[])
as a list, even if lists are internally constructed a different functor
([|](Head,Tail)). This is primarily intended to read the
output from write_canonical/1
from other Prolog systems. See
section 5.1.user is used because new modules by
default inherit from userquasi_quotation(+Syntax,
+Quotation, +VarDict, -Result), where Syntax is the
term in
{|Syntax||..|}, Quotation is a list of character
codes that represent the quotation, VarDict is a list of
Name=Variable and Result is a variable
that shares with the place where the quotation must be inserted. This
option is intended to support tools that manipulate Prolog source text.variable_names, but only reports the variables occurring
only once in the Term read (ISO). If Vars is the
constant
warning, singleton variables are reported using print_message/2.
The variables appear in the order they have been read. The latter option
provides backward compatibility and is used to read terms from source
files. Not all singleton variables are reported as a warning. See
section 2.15.1.10
for the rules that apply for warning about a singleton variable.113As
of version 7.7.17, all variables starting with an underscore
except for the truly anonymous variable are returned in Vars.
Older versions only reported those that would have been reported if warning
is used.error (default), throw an exception on a syntax error.
Other values are fail, which causes a message to be printed
using
print_message/2,
after which the predicate fails, quiet which causes the
predicate to fail silently, and dec10 which causes syntax
errors to be printed, after which read_term/[2,3]
continues reading the next term. Using dec10, read_term/[2,3]
never fails. (Quintus, SICStus).").{...}, as used in DCG rules. Arg
describes the argument.|<TailTerm>, Tail
is unified with the term position of the tail, otherwise with the atom none.key_value_position/7 terms. The
key_value_position/7 terms appear in the order of the
input. Because maps do not preserve ordering, the key is provided in the
position description.term_position/5 that would
be created, except that the key and value positions do not need an
intermediate list and the key is provided in Key to enable
synchronisation of the file position data with the data structure.{|Syntax||Content|},
SyntaxTerm is the parsed term representation from Syntax,
e.g., {|string(X)||Hello {{X}}|} produces Syntax
string(X) and SyntaxPos describes the layout of
this term. ContentPos is always a term From-To
describing the character range of Content.114The
layout of the term produced by the quasi quotation parser is not
available. Future versions may provide an interface that allows
contributing a layout term.true, demand variables to start with an underscore. See
section 2.15.1.8.unicode_atoms option, or the
unicode_atoms
flag), library(unicode) is loaded automatically. If the
library is unavailable the underlying
use_module/1
call propagates its
existence_error(source_sink, library(unicode)).syntax_error(non_nfc_atom) when an unquoted atom
contains text that is not in NFC. Uses the kernel hook for an exact NFC
test when library(unicode) is loaded; otherwise falls back
to rejecting any code point with wcwidth less than 1 (combining
marks, zero-width and non-printable characters), which is conservative
but accurate enough for source-code review.syntax_error(non_ascii_atom) when an unquoted atom
contains any non-ASCII code point. Independent of library(unicode).
Quoted atoms and string literals are always byte-faithful, regardless
of the mode. This is the canonical description of the four values; the
same vocabulary applies to the Prolog flag
unicode_atoms, set_stream/2,
the unicode_atoms option of open/4,
and the unicode_atoms property of
stream_property/2.