diff --git a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java index e869bfb5f..7f5a7d1c3 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Recognizer.java @@ -226,7 +226,7 @@ public class Recognizer { public void action(RuleContext _localctx, int ruleIndex, int actionIndex) { } - public ParserRuleContext args(RuleContext _localctx, int s, int ruleIndex, int actionIndex) { + public ParserRuleContext newContext(RuleContext _localctx, int s, int ruleIndex, int actionIndex) { return new ParserRuleContext(_localctx, s); } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java index 01444e2c1..eb76417b4 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java @@ -94,8 +94,9 @@ public class ParserATNSimulator extends ATNSimulator { { if ( originalContext==null ) originalContext = RuleContext.EMPTY; RuleContext ctx = RuleContext.EMPTY; - if ( useContext ) ctx = originalContext; - OrderedHashSet s0_closure = computeStartState(dfa.atnStartState, ctx, originalContext); + if ( useContext ) ctx = originalContext; + OrderedHashSet s0_closure = + computeStartState(dfa.atnStartState, ctx, originalContext); dfa.s0 = addDFAState(dfa, s0_closure); if ( prevAccept!=null ) { dfa.s0.isAcceptState = true; @@ -300,7 +301,7 @@ public class ParserATNSimulator extends ATNSimulator { OrderedHashSet tmp = reach; reach = closure; closure = tmp; - reach.clear(); + reach.clear(); // THIS MIGHT BE SLOW! kills each element; realloc might be faster } while ( true ); if ( prevAccept==null ) { @@ -457,7 +458,7 @@ public class ParserATNSimulator extends ATNSimulator { RuleContext newContext; if ( parser != null ) { System.out.println("rule trans to rule "+parser.getRuleNames()[t.target.ruleIndex]); - newContext = parser.args(config.context, t.target.stateNumber, t.target.ruleIndex, -999); + newContext = parser.newContext(config.context, t.target.stateNumber, t.target.ruleIndex, -999); newContext.invokingState = p.stateNumber; System.out.println("new ctx type is "+newContext.getClass().getSimpleName()); } diff --git a/tool/playground/T.g b/tool/playground/T.g index 2329cc431..8030e5705 100644 --- a/tool/playground/T.g +++ b/tool/playground/T.g @@ -1,56 +1,14 @@ grammar T; -options {output=AST;} -s : e EOF ; -expressionList - : e (','! e)* - ; -e : '('! e ')'! - | 'this' - | 'super' - | INT - | ID - | type '.'^ 'class' - | e '.'^ ID - | e '.'^ 'this' - | e '.'^ 'super' '('^ expressionList? ')'! - | e '.'^ 'new'^ ID '('! expressionList? ')'! - | 'new'^ type ( '(' expressionList? ')'! | ('[' e ']'!)+) - | e '['^ e ']'! - | '('^ type ')'! e - | e ('++'^ | '--'^) - | e '('^ expressionList? ')'! - | ('+'^|'-'^|'++'^|'--'^) e - | ('~'^|'!'^) e - | e ('*'^|'/'^|'%'^) e - | e ('+'^|'-'^) e - | e ('<'^ '<' | '>'^ '>' '>' | '>'^ '>') e - | e ('<='^ | '>='^ | '>'^ | '<'^) e - | e 'instanceof'^ e - | e ('=='^ | '!='^) e - | e '&'^ e - | e '^'^ e - | e '|'^ e - | e '&&'^ e - | e '||'^ e - | e '?' e ':' e - | e ('='^ - |'+='^ - |'-='^ - |'*='^ - |'/='^ - |'&='^ - |'|='^ - |'^='^ - |'>>='^ - |'>>>='^ - |'<<='^ - |'%='^) e - ; -type: ID - | ID '['^ ']'! - | 'int' - | 'int' '['^ ']'! - ; -ID : ('a'..'z'|'A'..'Z'|'_'|'$')+; -INT : '0'..'9'+ ; -WS : (' '|'\n') {skip();} ; + +s : e[9] {true}? ';' ; + +e[int i] + : {$i>=0}? ID + | ID '!' + ; + +foo[int j] : e[8] {$j==2}? '$' ; // not called but in FOLLOW(e) + +ID : 'a'..'z'+; + +WS : ' '+ {skip();} ; diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index b63a3bc39..24a734843 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -65,10 +65,10 @@ public class extends Parser { dumpActions(recog, argFuncs, actionFuncs, sempredFuncs) ::= << - public ParserRuleContext args(RuleContext _localctx, int s, int ruleIndex, int actionIndex) { + public ParserRuleContext newContext(RuleContext _localctx, int s, int ruleIndex, int actionIndex) { switch ( ruleIndex ) { : return _arg(()_localctx, s, actionIndex);}; separator="\n"> +case : return _argEval(()_localctx, s, actionIndex);}; separator="\n"> } return new ParserRuleContext(_localctx, s); } @@ -104,7 +104,7 @@ public (TokenStream input) { RuleArgFunction(r, actions) ::= << /** arg computations for rules called FROM and evaluated in context of */ -public ParserRuleContext _arg(ParserRuleContext _localctx, int s, int actionIndex) { +public ParserRuleContext _argEval(ParserRuleContext _localctx, int s, int actionIndex) { switch ( actionIndex ) { : return new (_localctx, s, );}; separator="\n"> diff --git a/tool/src/org/antlr/v4/parse/GrammarTreeVisitor.g b/tool/src/org/antlr/v4/parse/GrammarTreeVisitor.g index 172219d88..08fdfd9bc 100644 --- a/tool/src/org/antlr/v4/parse/GrammarTreeVisitor.g +++ b/tool/src/org/antlr/v4/parse/GrammarTreeVisitor.g @@ -253,6 +253,7 @@ rule @init { List mods = new ArrayList(); List actions = new ArrayList(); // track roots +currentOuterAltNumber=0; } : ^( RULE ID {currentRuleName=$ID.text; currentRuleAST=$RULE;} DOC_COMMENT? (^(RULEMODIFIERS (m=ruleModifier{mods.add($m.start);})+))? @@ -308,8 +309,7 @@ ruleModifier ; ruleBlock - : {currentOuterAltNumber=0;} - ^( BLOCK + : ^( BLOCK ( { currentOuterAltRoot = (GrammarAST)input.LT(1); currentOuterAltNumber++;