« Prev 4.1 User Code Blocks | Table of Contents | Next » 4.4 Specifying tokens - the token statement |
ast
statementTo activate AST generation mode, place the ast
statement in your grammar file:
ast;
It is recommended to place this statement early in the grammar.
In AST generation mode various aspects of propane's behavior are changed:
ptype
is allowed.p_result()
points to a Start
struct containing
the entire parse tree for the input. If the user has changed the start rule
with the start
grammar statement, the name of the start struct will be
given by the user-specified start rule instead of Start
.Example AST generation grammar:
ast; ptype int; token a << $$ = 11; >> token b << $$ = 22; >> token one /1/; token two /2/; token comma /,/ << $$ = 42; >> token lparen /\(/; token rparen /\)/; drop /\s+/; Start -> Items; Items -> Item:item ItemsMore; Items -> ; ItemsMore -> comma Item:item ItemsMore; ItemsMore -> ; Item -> a; Item -> b; Item -> lparen Item:item rparen; Item -> Dual; Dual -> One Two; Dual -> Two One; One -> one; Two -> two;
The following unit test describes the fields that will be present for an example parse:
string input = "a, ((b)), b"; p_context_t context; p_context_init(&context, input); assert_eq(P_SUCCESS, p_parse(&context)); Start * start = p_result(&context); assert(start.pItems1 !is null); assert(start.pItems !is null); Items * items = start.pItems; assert(items.item !is null); assert(items.item.pToken1 !is null); assert_eq(TOKEN_a, items.item.pToken1.token); assert_eq(11, items.item.pToken1.pvalue); assert(items.pItemsMore !is null); ItemsMore * itemsmore = items.pItemsMore; assert(itemsmore.item !is null); assert(itemsmore.item.item !is null); assert(itemsmore.item.item.item !is null); assert(itemsmore.item.item.item.pToken1 !is null); assert_eq(TOKEN_b, itemsmore.item.item.item.pToken1.token); assert_eq(22, itemsmore.item.item.item.pToken1.pvalue); assert(itemsmore.pItemsMore !is null); itemsmore = itemsmore.pItemsMore; assert(itemsmore.item !is null); assert(itemsmore.item.pToken1 !is null); assert_eq(TOKEN_b, itemsmore.item.pToken1.token); assert_eq(22, itemsmore.item.pToken1.pvalue); assert(itemsmore.pItemsMore is null);
ast_prefix
and ast_suffix
statementsIn AST generation mode, structure types are defined and named based on the
rules in the grammar.
Additionally, a structure type called Token
is generated to hold parsed
token information.
These structure names can be modified by using the ast_prefix
or ast_suffix
statements in the grammar file.
The field names that point to instances of the structures are not affected by
the ast_prefix
or ast_suffix
values.
For example, if the following two lines were added to the example above:
ast_prefix ABC; ast_suffix XYZ;
Then the types would be used as such instead:
string input = "a, ((b)), b"; p_context_t context; p_context_init(&context, input); assert_eq(P_SUCCESS, p_parse(&context)); ABCStartXYZ * start = p_result(&context); assert(start.pItems1 !is null); assert(start.pItems !is null); ABCItemsXYZ * items = start.pItems; assert(items.pItem !is null); assert(items.pItem.pToken1 !is null); assert_eq(TOKEN_a, items.pItem.pToken1.token); assert_eq(11, items.pItem.pToken1.pvalue); assert(items.pItemsMore !is null); ABCItemsMoreXYZ * itemsmore = items.pItemsMore; assert(itemsmore.pItem !is null); assert(itemsmore.pItem.pItem !is null); assert(itemsmore.pItem.pItem.pItem !is null); assert(itemsmore.pItem.pItem.pItem.pToken1 !is null);
« Prev 4.1 User Code Blocks | Table of Contents | Next » 4.4 Specifying tokens - the token statement |