From 42f6b7cf86e55230de97125e7e789dcd90dd5655 Mon Sep 17 00:00:00 2001 From: parrt Date: Wed, 13 Jul 2011 17:29:20 -0800 Subject: [PATCH] after much bullshit, got sets working for ast stuff. [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8855] --- .../antlr/v4/runtime/atn/ATNInterpreter.java | 2 +- .../org/antlr/v4/runtime/atn/LL1Analyzer.java | 3 +- .../v4/runtime/atn/LexerInterpreter.java | 4 +- .../v4/runtime/atn/NotSetTransition.java | 12 +- .../v4/runtime/atn/ParserInterpreter.java | 9 +- .../antlr/v4/runtime/atn/SetTransition.java | 12 +- tool/playground/T.g | 3 +- .../v4/tool/templates/codegen/Java/Java.stg | 15 + tool/src/org/antlr/v4/Tool.java | 5 +- .../src/org/antlr/v4/automata/ATNFactory.java | 10 +- .../src/org/antlr/v4/automata/ATNPrinter.java | 5 + .../org/antlr/v4/automata/ATNSerializer.java | 2 +- .../antlr/v4/automata/LexerATNFactory.java | 23 ++ .../antlr/v4/automata/ParserATNFactory.java | 45 ++- .../v4/codegen/BlankOutputModelFactory.java | 2 + .../v4/codegen/CodeGeneratorExtension.java | 10 +- .../v4/codegen/OutputModelController.java | 22 +- .../antlr/v4/codegen/OutputModelFactory.java | 2 + .../antlr/v4/codegen/ParserASTExtension.java | 19 +- .../org/antlr/v4/codegen/ParserFactory.java | 38 ++- .../org/antlr/v4/codegen/SourceGenTriggers.g | 64 +++-- tool/src/org/antlr/v4/codegen/Target.java | 7 + .../org/antlr/v4/codegen/model/Choice.java | 3 +- .../antlr/v4/codegen/model/MatchNotSet.java | 39 +++ .../org/antlr/v4/codegen/model/MatchSet.java | 49 ++++ .../antlr/v4/codegen/model/MatchToken.java | 4 + .../antlr/v4/codegen/model/RuleElement.java | 1 + .../src/org/antlr/v4/codegen/model/SrcOp.java | 4 +- tool/src/org/antlr/v4/parse/ANTLRParser.g | 122 ++++---- tool/src/org/antlr/v4/parse/ASTVerifier.g | 44 ++- tool/src/org/antlr/v4/parse/ATNBuilder.g | 53 ++-- .../org/antlr/v4/parse/BlockSetTransformer.g | 68 +++++ .../antlr/v4/semantics/SemanticPipeline.java | 4 +- tool/src/org/antlr/v4/tool/Grammar.java | 3 + tool/src/org/antlr/v4/tool/GrammarAST.java | 1 + .../v4/tool/GrammarTransformPipeline.java | 54 ++++ tool/test/org/antlr/v4/test/TestASTOps.java | 3 +- .../org/antlr/v4/test/TestASTStructure.gunit | 18 +- .../org/antlr/v4/test/TestASTStructure.java | 264 ++++++++++-------- .../antlr/v4/test/TestATNConstruction.java | 108 ++++--- 40 files changed, 774 insertions(+), 382 deletions(-) create mode 100644 tool/src/org/antlr/v4/codegen/model/MatchNotSet.java create mode 100644 tool/src/org/antlr/v4/codegen/model/MatchSet.java create mode 100644 tool/src/org/antlr/v4/parse/BlockSetTransformer.g create mode 100644 tool/src/org/antlr/v4/tool/GrammarTransformPipeline.java diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNInterpreter.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNInterpreter.java index ae658dbb9..a6047cf81 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ATNInterpreter.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ATNInterpreter.java @@ -144,7 +144,7 @@ public abstract class ATNInterpreter { case Transition.FORCED_ACTION : return new ActionTransition(target, arg1, arg2); case Transition.SET : return new SetTransition(sets.get(arg1), target); case Transition.NOT_ATOM : return new NotAtomTransition(arg1, target); - case Transition.NOT_SET : return new NotSetTransition(sets.get(arg1), target); + case Transition.NOT_SET : return new NotSetTransition(null, sets.get(arg1), target); case Transition.WILDCARD : return new WildcardTransition(target); } return null; diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java index f774d8e20..89cadfd46 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java @@ -95,7 +95,8 @@ public class LL1Analyzer { } else { // System.out.println("adding "+ t); - look.addAll(t.label()); + IntervalSet set = t.label(); + look.addAll(set); } } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerInterpreter.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerInterpreter.java index 7d3e640e1..479a0ed32 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerInterpreter.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerInterpreter.java @@ -262,8 +262,8 @@ public class LexerInterpreter extends ATNInterpreter { else if ( trans instanceof SetTransition ) { SetTransition st = (SetTransition)trans; boolean not = trans instanceof NotSetTransition; - if ( !not && st.label.member(t) || not && !st.label.member(t) ) { - if ( debug ) System.out.println("match set "+st.label.toString()); + if ( !not && st.set.member(t) || not && !st.set.member(t) ) { + if ( debug ) System.out.println("match set "+st.set.toString()); return st.target; } } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/NotSetTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/NotSetTransition.java index 43b69d570..2538b59e3 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/NotSetTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/NotSetTransition.java @@ -32,14 +32,22 @@ package org.antlr.v4.runtime.atn; import org.antlr.v4.runtime.misc.IntervalSet; public class NotSetTransition extends SetTransition { - public NotSetTransition(IntervalSet label, ATNState target) { - super(label, target); + // keep both set, notSet; we can only compute at construction time + // since only then do we have grammar, which knows token set for complement. + public IntervalSet notSet; + + public NotSetTransition(IntervalSet set, IntervalSet notSet, ATNState target) { + super(set, target); + this.notSet = notSet; } public NotSetTransition(ATNState target) { super(target); } + @Override + public IntervalSet label() { return notSet; } + @Override public String toString() { return '~'+super.toString(); diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserInterpreter.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserInterpreter.java index c1dc2c35a..4b121f39f 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserInterpreter.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserInterpreter.java @@ -386,15 +386,15 @@ public class ParserInterpreter extends ATNInterpreter { public ATNState getReachableTarget(Transition trans, int ttype) { if ( trans instanceof AtomTransition ) { AtomTransition at = (AtomTransition)trans; - boolean not = trans instanceof NotAtomTransition; - if ( !not && at.label == ttype || not && at.label!=ttype ) { +// boolean not = trans instanceof NotAtomTransition; + if ( at.label == ttype ) { return at.target; } } else if ( trans instanceof SetTransition ) { SetTransition st = (SetTransition)trans; boolean not = trans instanceof NotSetTransition; - if ( !not && st.label.member(ttype) || not && !st.label.member(ttype) ) { + if ( !not && st.set.member(ttype) || not && !st.set.member(ttype) ) { return st.target; } } @@ -648,7 +648,8 @@ public class ParserInterpreter extends ATNInterpreter { } else if ( t instanceof SetTransition ) { SetTransition st = (SetTransition)t; - trans = "Set "+st.label.toString(); + boolean not = st instanceof NotSetTransition; + trans = (not?"~":"")+"Set "+st.set.toString(); } System.err.println(c.toString(parser, true)+":"+trans); } diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/SetTransition.java b/runtime/Java/src/org/antlr/v4/runtime/atn/SetTransition.java index be90f0e35..41571bf8c 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/SetTransition.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/SetTransition.java @@ -34,21 +34,21 @@ import org.antlr.v4.runtime.misc.IntervalSet; /** A transition containing a set of values */ public class SetTransition extends Transition { - public IntervalSet label; + public IntervalSet set; - public SetTransition(IntervalSet label, ATNState target) { + public SetTransition(IntervalSet set, ATNState target) { super(target); - if ( label==null ) label = IntervalSet.of(Token.INVALID_TYPE); - this.label = label; + if ( set == null ) set = IntervalSet.of(Token.INVALID_TYPE); + this.set = set; } public SetTransition(ATNState target) { super(target); } - public IntervalSet label() { return label; } + public IntervalSet label() { return set; } public String toString() { - return label.toString(); + return set.toString(); } } diff --git a/tool/playground/T.g b/tool/playground/T.g index 3dc7442f0..b1a132aed 100644 --- a/tool/playground/T.g +++ b/tool/playground/T.g @@ -1,10 +1,11 @@ grammar T; options {output=AST;} -a : (ID INT) ; +a : ('+' | '-')^ ; b returns [int i] : ID; ID : 'a'..'z'+ ; INT : '0'..'9'+; +PLUS : '+'; WS : (' '|'\n') {$channel=HIDDEN;} ; /* 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 1f57c2990..c13eeff7e 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 @@ -235,6 +235,8 @@ case : } >> +// TODO: we we need uniqueID? a single _alt might work + StarBlock(choice, alts, sync) ::= << int _alt = _interp.adaptivePredict(input,,_ctx); while ( _alt!= ) { @@ -285,6 +287,18 @@ _localctx.s = ; = }>(Token)match(); >> +MatchSet(m, expr, capture) ::= "" + +MatchNotSet(m, expr, capture) ::= "" + +CommonSetStuff(m, expr, capture, invert) ::= << +_localctx.s = ; + = }>input.LT(1); + +if ( !() ) throw new MismatchedSetException(this, input); +input.consume(); +>> + Wildcard(w) ::= << _localctx.s = ; = }>input.LT(1); input.consume(); @@ -354,6 +368,7 @@ ParserRuleContext() ::= "ParserRuleContext" RuleDynamicScopeStructName(ruleName) ::= "_stk" ImplicitTokenLabel(tokenName) ::= "_t" ImplicitRuleLabel(ruleName) ::= "_r" +ImplicitSetLabel(id) ::= "_tset" ListLabelName(label) ::= "