sets at top level work now: s : A | B ; in lexer and parser.

This commit is contained in:
Terence Parr 2012-08-05 10:17:00 -07:00
parent 799b7afc1c
commit 64c050f233
4 changed files with 9 additions and 3 deletions

View File

@ -1,5 +1,6 @@
grammar T;
s : (A | B) ;
A : ('a'|'c');
s : A | B ;
x : (A|B);
A : 'a'|'c';
B : 'b' ;

View File

@ -64,7 +64,10 @@ boolean inLexer = Character.isUpperCase(currentRuleName.charAt(0));
@after {
GrammarTransformPipeline.setGrammarPtr(g, $tree);
}
: {!inContext("RULE")}? // if not rule block and > 1 alt
: {inContext("RULE")}? // top-level: rule block and > 1 alt
^(BLOCK ^(ALT setElement[inLexer]) ( ^(ALT setElement[inLexer]) )+)
-> ^(BLOCK<BlockAST>[$BLOCK.token] ^(ALT<AltAST>[$BLOCK.token,"ALT"] ^(SET[$BLOCK.token, "SET"] setElement+)))
| {!inContext("RULE")}? // if not rule block and > 1 alt
^(BLOCK ^(ALT setElement[inLexer]) ( ^(ALT setElement[inLexer]) )+)
-> ^(SET[$BLOCK.token, "SET"] setElement+)
;

View File

@ -114,6 +114,7 @@ public class GrammarTransformPipeline {
/** Utility visitor that sets grammar ptr in each node */
public static void setGrammarPtr(final Grammar g, GrammarAST tree) {
if ( tree==null ) return;
// ensure each node has pointer to surrounding grammar
TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor());
v.visit(tree, new TreeVisitorAction() {

View File

@ -56,6 +56,7 @@ public class AltAST extends GrammarAST {
public AltAST(Token t) { super(t); }
public AltAST(int type) { super(type); }
public AltAST(int type, Token t) { super(type, t); }
public AltAST(int type, Token t, String text) { super(type,t,text); }
@Override
public Tree dupNode() { return new AltAST(this); }