Often it's useful to push some useful information into parse tree nodes, and later we can pull those data out of the nodes and use them inside a morph script.
1. Map data into a parse tree node in your input grammar file.g
ANTLRMorph implements its own parse tree builder (MorphParseTreeBuilder) and parse tree (MorphParseTree), and the moprh parse tree provides a map for storing arbitrary data.
Syntax (in Java) |
Description |
|---|---|
((MorphParseTreeBuilder)dbg).getCurrentTree().define(String key, Object value); |
map a key/value pair into the current parse tree node. |
2. Get data back from a parse tree node in your morph script file.morph
Syntax |
Description |
|---|---|
$ruleref.get("...") |
You may need to cast the value object into its original type. |
Example
In the following grammar fragment, we push a function scope object into the current parse tree node (func).
Later, in the morph script, we can pull the function scope out of the parse tree node as the code snippet shows below.