1.8 KiB
Action Execution Environment
Actions and predicates have these variables and functions available to them.
-
All variables and functions defined in the initializer at the beginning of the grammar are available.
-
Labels from preceding expressions are available as local variables, which will have the match result of the labelled expressions.
A label is only available after its labelled expression is matched:
rule = A:('a' B:'b' { /* B is available, A is not */ } )
A label in a sub-expression is only valid within the sub-expression:
rule = A:'a' (B: 'b') (C: 'b' { /* A and C are available, B is not */ })
-
options
is a variable that contains the parser options. -
error(message, where)
will report an error and throw an exception.where
is optional; the default is the value oflocation()
. -
expected(message, where)
is similar toerror
, but reports: Expected message but "other" found -
location()
returns an object like this:{ start: { offset: 23, line: 5, column: 6 }, end: { offset: 25, line: 5, column: 8 } }
For actions,
start
refers to the position at the beginning of the preceding expression, andend
refers to the position after the end of the preceding expression.For predicates,
start
andend
are the same, the location where the predicate is evaluated.offset
is a 0-based character index within the source text.line
andcolumn
are 1-based indices.Note that
line
andcolumn
are somewhat expensive to compute, so if you need location frequently, you might want to useoffset()
orrange()
instead. -
offset()
returns the start offset. -
range()
returns an array containing the start and end offsets, such as[23, 25]
. -
text()
returns the source text betweenstart
andend
(which will be "" for predicates).