Parsing answers.
Interface for operations needed for repetition collections.
Information about an active instance of left recursion.
An answer that is a left recursion record.
A marker of a position.
A marker of a position. Instaneces of this are used as placeholders when there are no other suitable values associated with a parse position.
A parser that is a memoising, left recursion-detecting encapsulation of a normal parser that returns a value of a particular type.
A parser that is a memoising, left recursion-detecting encapsulation
of a normal parser that returns a value of a particular type. This
type of parser must be used if any of the alternatives are left
recursive. Otherwise a plain Parser
can be used if memoisation is
not desired.
Note that it is non-trivial (impossible?) to combine this class with
Parser
. We need the latter to be covariant but this class can't be
because T
occurs in an invariant position in MemoEntry
.
A parser is a function from a string to a parser result.
A parser is a function from a string to a parser result. This kind
of parser cannot handle left recursive alternatives and does not
memoise its results so it may repeat work. If those properties are
desired use the PackratParser
type instead.
An answer that is a resolved parser result.
Common supertype for all rules (ie regardless of result type).
Special tuple class to match sequence combinator.
Convenience method for making a parser out of its body function, including adding support for whitespace prefix skipping and position recording.
Convenience method for making a parser out of its body function, including adding support for whitespace prefix skipping and position recording. All parsers should be created using this method so that they share the book-keeping.
A parser that matches any character, failing if the end of input is reached.
Wrap p
so that its failures become errors.
Wrap p
so that its failures become errors. See also nocut
.
Convenience conversion to allow arity two functions to be used directly in tree construction actions.
Convenience conversion to allow arity three functions to be used directly in tree construction actions.
Convenience conversion to allow arity four functions to be used directly in tree construction actions.
Convenience conversion to allow arity five functions to be used directly in tree construction actions.
Convenience conversion to allow arity six functions to be used directly in tree construction actions.
Parse digit strings that are constrained to fit into an Int
value.
Parse digit strings that are constrained to fit into an Int
value.
If the digit string is too big, a parse error results.
A parser that accepts just those characters that pass the given predicate.
A parser that accepts just those characters that pass the given predicate. The message is used to describe what was expected if an error occurs.
A parser that accepts just the given character.
A parser that always errors with the given message.
A parser that always fails with the given message.
Generic repetition zero or more times.
Generic repetition one or more times.
Generic repetition one or more times with separators.
Generic repetition zero or more times with separators.
A parser that succeeds iff its argument parser succeeds, but consumes no input in any circumstances.
Map between left input positions and active left recursion instances.
Parser for keywords.
Parser for keywords. The list of string arguments gives the text
of the keywords in a language. The regular expression gives the
possible extension of the keyword to stop the keyword being seen as
an identifier instead. For example, the keyword list might contain
"begin"
and "end"
and the extension regular expression might
be [^a-zA-Z0-9]
. Thus,
begin followed by something other than
a letter or digit is a keyword, but
beginfoo8 is an identifier.
This parser succeeds if any of the keywords is present, provided
that it's not immediately followed by something that extends it.
Record lack of success so that we can nicely handle the case where a phrase doesn't parse when looking for the end of input but there was a later lack of success for some other reason.
A parser that matches a literal string after skipping any whitespace.
A parser that matches a literal string after skipping any whitespace.
The form of the latter is defined by the whitespace
parser.
Mark a string parser so that its value is discarded but a marker is returned instead.
Mark a string parser so that its value is discarded but a marker is returned instead. That return value can then be used to set the position of another value. We can't use the string value itself since we are not guaranteed to have reference equality on strings.
(Implicit) conversion of non-memoising parser into a memoising one.
Suppress cuts in the parser p
.
Suppress cuts in the parser p
. I.e., errors produced by p
are
propagated as failures instead. See also commit
.
Invert the result of a parser without consuming any input.
Optional parsing.
Run a parser on a string to obtain its result.
Run a parser on all of a string to obtain its result.
Convenience conversion to lift parsers that return 2-tilde-tuples to parsers that return regular 2-tuples.
Convenience conversion to lift parsers that return 3-tilde-tuples to parsers that return regular 3-tuples.
Convenience conversion to lift parsers that return 4-tilde-tuples to parsers that return regular 4-tuples.
Convenience conversion to lift parsers that return 5-tilde-tuples to parsers that return regular 5-tuples.
Convenience conversion to lift parsers that return 6-tilde-tuples to parsers that return regular 6-tuples.
If we are parsing whitespace already, succeed with no progress so that we don't recurse.
If we are parsing whitespace already, succeed with no progress so that we don't recurse. If we are not already parsing whitespace, then apply the whitespace parser, swallowing any errors from it unless they occur at the end of the input. In other words, an error not at the end is treated as the absence of whitespace.
Are we currently parsing whitespace?
Phrases, i.e., parse and succeed with no input remaining, except possibly for whitespace at the end.
Phrases, i.e., parse and succeed with no input remaining, except possibly for whitespace at the end. If there is another later failure, prefer it over the failure due to end of input being expected since the later one is usually more informative.
A parser that matches a regex string after skipping any whitespace.
A parser that matches a regex string after skipping any whitespace.
The form of the latter is defined by the whitespace
parser.
Convert the digit string s
to an Int
if it's in range, but return an
error message if it's too big.
Succeed with a given value consuming no non-whitespace input.
Succeed with a given value consuming no non-whitespace input. The value is evaluated each time this parser is used.
A parser that skips whitespace (default: sequences of zero or more whitespace characters).
A parser that skips whitespace (default: sequences of zero or more whitespace characters). This definition can be overridden as long as the new definition succeeds at the end of the input.
Wrap a parser p
that produces a value of type T
to produce a
parser returning values of type U
.
Wrap a parser p
that produces a value of type T
to produce a
parser returning values of type U
.
The function f
is responsible for converting the T
value into
either a U
value or a string that indicates what went wrong.
In the latter case, the resulting parser will error at the
original position with the message, ignoring any other errors
at that position. Failures or errors of p
will be lifted to
the returned type.
Simple packrat parsing combinator suite. These combinators are largely source compatible with the Scala parser combinator library but the implementation is simpler and less general. Broadly speaking this library provides behaviour similar to the Scala library
RegexParsers
trait with sensible defaults and better integration with the rest of Kiama.The parameter
positions
provides the position store that this suite should use to track AST node positions. Usually the value passed in is shared with the rest of a program that reports errors etc.This library should not be used if efficiency is a concern. These combinators are essentially interpreting the grammar so the performance can't compare to a parser generator which compiles the grammar. Nevertheless, the library is perfectly fine for prototyping and for processing small to medium-sized inputs.
Some of the implementation details in this module are based on the implementation of the Scala parser combinator library but in a much simpler form.
The algorithms used here to handle left recursion are from "Packrat parsers can support left recursion" by Warth, Douglass and Millstein, ACM SIGPLAN Symposium on Partial Evaluation and Semantics-based Program Manipulation, 2008.