Posted By:
Jonathan_Shapiro
Posted On:
Tuesday, December 18, 2001 04:55 AM
This has to be a FAQ, but I cannot locate it. I have a production that looks like: enumDecl : "enum"^ nameDef LCURLY! enumConst ( COMMA! enumConst )* RCURLY! SEMI! ; The nameDef node is currently a singleton identifier, which I wish to rewrite for reasons of tree consistency, to get: #(ND_NAMEDEF, DUMMYTYPE_enumTag, #nameDef) That is, I want to inject a subtree that wraps the original nameDef node. I'm having trouble with the rewrite, because (a) I don't understand how to reach in and do the rewrite on the one element of the AST that is already built, and (b) given the * pattern I don't know how to c
More>>
This has to be a FAQ, but I cannot locate it.
I have a production that looks like:
enumDecl
: "enum"^ nameDef LCURLY!
enumConst
( COMMA! enumConst )*
RCURLY! SEMI!
;
The nameDef node is currently a singleton identifier, which I wish to rewrite for reasons of tree consistency, to get:
#(ND_NAMEDEF, DUMMYTYPE_enumTag, #nameDef)
That is, I want to inject a subtree that wraps the original nameDef node.
I'm having trouble with the rewrite, because (a) I don't understand how to reach in and do the rewrite on the one element of the AST that is already built, and (b) given the * pattern I don't know how to construct the desired output AST by hand. I tried labeling the internal enumConsts as e1 and e2 and using (... , #e1, #e2), but the #e2 only gets expanded once even if that part of the pattern matches multiple times.
I'ld be willing to separate out the enumConsts as a subproduction, but if I do that I really need a way to splice the subordinate AST members into the middle of the parent AST.
Given the number of questions I see along similar lines to these, I'm struck by the thought that the AST pattern matching language really needs to be generalized. Providing list manipulation built-ins
cons, nth, nthcdr
and list splicing would go a very long way here toward addressing the kinds of things that seem to be confusing people.
In any case, can what I want be done within the AST synthesis language? How?
<<Less