Action code is normal Rust code with the addition of the following special variables:
$nrefer to the respective symbol in the production, numbered from 1 (i.e.
$1refers to the first symbol in the production). If the symbol references a rule
Rthen an instance of
R's type will be stored in the
$ivariable. If the symbol references a lexeme then a
Result<Lexeme<StorageT>, Lexeme<StorageT>>instance is returned where the
Okvariant is used for lexemes that are directly derived from the user's input and the
Errvariant is used for lexemes that have been inserted by error recovery.
$lexerallows access to the lexer and its various functions. The most commonly used of these is the
span_strfunction, which allows us to extract
&'input strs from a
Span(e.g. to extract the string represented by a
Lexeme, we would use
$lexer.span_str(lexeme.span())). As this may suggest, actions may also reference the special lifetime
$prefix), which allows strings to be returned / stored by the grammar without copying memory.
lrpar::Spantuple (with both elements of type
usize) which captures how much of the user's input the current production matched.
$$is equivalent to
$in normal Rust code.
Any other variables beginning with
$ are treated as errors.
Productions' return types can be any arbitrary Rust type. You may in addition make use of the following:
The generic parameter
StorageTreferences the type of lexemes and is typically used with the
Lexeme<StorageT>. This allows you to return lexemes from rules.
'inputallows you to extract strings whose lifetime is tied to the lexer and return them from rules / store them in structs without copying.
Lexer::span_strreturns such strings and the typical idiom of use is