Esrap is maintained courtesy of Steel Bank Studio Ltd by Nikodemus Siivola.
Esrap is maintained in Git:
git clone git://github.com/nikodemus/esrap.git
will get you a local copy.
is the GitHub project page.
Esrap is licenced under an MIT-style licence.
For more on packrat parsing, see http://pdos.csail.mit.edu/~baford/packrat/thesis/ for Bryan Ford's 2002 thesis: “Packrat Parsing: a Practical Linear Time Algorithm with Backtracking”.
Parsing proceeds by matching text against parsing expressions. Matching has three components: success vs failure, consumption of input, and associated production.
Parsing expressions that fail never consume input. Parsing expressions that succeed may or may not consume input.
A parsing expressions can be:
A terminal is a character or a string of length one, which succeeds and consumes a single character if that character matches the terminal.
Additionally, Esrap supports some pseudoterminals.
character always succeeds, consuming
and producing a single character.
(character-ranges range ...) match a
single character from the given range(s), consuming and producing that
character. A range is can be either a list of the form
(#\start_char #\stop_char) or a single character.
"foo" succeeds and consumes input as if
(and #\f #\o
#\o). Produces the consumed string.
(string length) can be used to specify
sequences of arbitrary characters:
(string 2) succeeds and
consumes input as if
(and character character). Produces the
Nonterminals are specified using symbols. A nonterminal symbol succeeds if the parsing expression associated with it succeeds, and consumes whatever the input that expression consumes.
The production of a nonterminal depends on the associated expression and an optional transformation rule.
Nonterminals are defined using
Note: Currently all rules share the same namespace, so you should not use symbols in the COMMON-LISP package or other shared packages to name your rules unless you are certain there are no other Esrap using components in your Lisp image. In a future version of Esrap grammar objects will be introduced to allow multiple definitions of nonterminals. Symbols in the COMMON-LISP package are specifically reserved for use by Esrap.
(and subexpression ...)
A sequence succeeds if all subexpressions succeed, and consumes all input consumed by the subexpressions. A sequence produces the productions of its subexpressions as a list.
(or subexpression ...)
An ordered choice succeeds if any of the subexpressions succeeds, and consumes all the input consumed by the successful subexpression. An ordered choice produces whatever the successful subexpression produces.
Subexpressions are checked strictly in the specified order, and once a subexpression succeeds no further ones will be tried.
A negation succeeds if the subexpression fails, and consumes one character of input. A negation produces the character it consumes.
A greedy repetition always succeeds, consuming all input consumed by applying subexpression repeatedly as long as it succeeds.
A greedy repetition produces the productions of the subexpression as a list.
A greedy repetition succeeds if subexpression succeeds at least once, and consumes all input consumed by applying subexpression repeatedly as long as it succeeds. A greedy positive repetition produces the productions of the subexpression as a list.
Optionals always succeed, and consume whatever input the subexpression
consumes. An optional produces whatever the subexpression produces, or
nil if the subexpression does not succeed.
A followed-by predicate succeeds if the subexpression succeeds, and consumes no input. A followed-by predicate produces whatever the subexpression produces.
A not-followed-by predicate succeeds if the subexpression does not
succeed, and consumes no input. A not-followed-by predicate
predicate-name is a symbol naming a global function. A
semantic predicate succeeds if subsexpression succeeds and the
named function returns true for the production of the subexpression. A
semantic predicate produces whatever the subexpression produces.
Note: semantic predicates may change in the future to produce whatever the predicate function returns.
optionscan be specified:
The rule is active only when
testevaluates to true. This can be used to specify optional extensions to a grammar.
No matter what input is consumed or what
expressionproduces, the production of the rule is always
If provided the production of the expression is transformed using
functioncan be a function name or a lambda-expression.
If true, the production of expression is used as-is, as if
(:function identity)has been specified. If no production option is specified, this is the default.
If true, the production of expression is flattened and concatenated into a string as if by
(:function text)has been specified.
(:lambda lambda-list &body body)
If provided, same as using the corresponding lambda-expression with
As an extension of the standard lambda list syntax,
lambda-listaccepts the optional pseudo lambda-list keyword
(1)must appear after all standard lambda list keywords.
(2)can be followed by one or two variables to which bounding indexes of the matching substring are bound.
(standard-lambda-list-elements [&bounds start [end]])
(:destructure destructuring-lambda-list &body body)
If provided, same as using a lambda-expression that destructures its argument using
destructuring-bindand the provided lambda-list with
esrap:&boundsin the same way as described for
(:around ([&bounds start [end]]) &body body)
If provided, execute
bodyaround the construction of the production of the rule.
bodyhas to call
esrap:call-transformto trigger the computation of the production. Any transformation provided via
:destructureis executed inside the call to
esrap:call-transform. As a result, modification to the dynamic state are visible within the transform.
esrap:&boundscan be used in the same way as described for
This option can be used to safely track nesting depth, manage symbol tables or for other stack-like operations.
Signaled when an Esrap parse fails. Use
esrap-error-textto obtain the string that was being parsed, and
esrap-error-positionthe position at which the error occurred.
Signaled when left recursion is detected during Esrap parsing.
left-recursion-nonterminalnames the symbol for which left recursion was detected, and
left-recursion-pathlists nonterminals of which the left recursion cycle consists.