-
Notifications
You must be signed in to change notification settings - Fork 1
Actions
Actions appear in two places: the end of a lexical rule and the middle or end of a grammar rule. They are executed when a lexical rule is matched or reduce a grammar rule.
An action can simply be a code block
{ [action code] }
or an array of some predefined internal actions parenthesised by []
:
[ [action] (, [action])* ]
where [action]
could be one of the following:
Action syntax | Description |
---|---|
+[state] |
Push a lexical state on to the state stack, where [state] is the name of the lexical state. |
- |
Pop one lexical state from the state stack. |
=>[state] |
Change top of the lexical state stack to [state] . |
="[content]" |
Set currently matched input string to [content] , and reset line and column information. |
{[action code]} |
A code block. |
Action code blocks are bounded by {}
. These two brackets must match each other in the block, including those in strings. If you do need unmatched ones, you may use \
to escape them.
There are some built-in variables and functions you can use in the action code:
Variable | Description |
---|---|
$$ |
In lexical rule, this is the sematic value of the token, in grammar rule, is the sematic value of the left hand side non terminal of this rule. In both case you can assign values to it. |
$token |
Currently matched token object. Use of this variable only make sense in lexical action. |
$matched |
Currently matched string. Use of this variable only make sense in lexical action. In rules that emit a token, this variable will be an empty string, You should use $token.val instead. |
$emit<[token]> |
Emit a token manually. If emit from a lexical rule that also emits a token, this emitted token would follow that one. Value and line information about this token will be empty. This function allows the sematic analyser to affect the parser, and can be used to handle some context sensitive grammars such as heredoc in shell and php, or expressions like x * y in C++, which can either be treated as x times y or a pointer y of type x, depends on whether x is a type name. |
All of these variables or functions start with $
. So if you want any of these variable or function as literal in your code, like $token
, you may use \$token
,i.e., just escape the begining dollar sign.
Action code blocks can be disabled via disableBlocks()
of the parser. If an action code block needs to be always enabled, use %always
directive before it.