« Prev 4.1 User Code Blocks | Table of Contents | Next » 4.3 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
structure containing
the entire parse tree for the input.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 ItemsMore; Items -> ; ItemsMore -> comma Item ItemsMore; ItemsMore -> ; Item -> a; Item -> b; Item -> lparen 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.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); ItemsMore * 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); assert_eq(TOKEN_b, itemsmore.pItem.pItem.pItem.pToken1.token); assert_eq(22, itemsmore.pItem.pItem.pItem.pToken1.pvalue); assert(itemsmore.pItemsMore !is null); itemsmore = itemsmore.pItemsMore; assert(itemsmore.pItem !is null); assert(itemsmore.pItem.pToken1 !is null); assert_eq(TOKEN_b, itemsmore.pItem.pToken1.token); assert_eq(22, itemsmore.pItem.pToken1.pvalue); assert(itemsmore.pItemsMore is null);
« Prev 4.1 User Code Blocks | Table of Contents | Next » 4.3 Specifying tokens - the token statement |