From f69f3f2c0981dc21b75a129bc2044f5c624f14a3 Mon Sep 17 00:00:00 2001 From: parrt Date: Sun, 23 May 2010 09:05:05 -0800 Subject: [PATCH] refactored [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6885] --- .../v4/tool/templates/codegen/Java/Java.stg | 8 +++---- tool/src/org/antlr/v4/codegen/src/Choice.java | 16 +++++++++----- .../org/antlr/v4/codegen/src/LL1Choice.java | 14 +++++------- .../src/org/antlr/v4/codegen/src/LL1Loop.java | 10 ++------- .../src/LL1OptionalBlockSingleAlt.java | 12 +++++----- .../antlr/v4/codegen/src/LL1PlusBlock.java | 22 ++++--------------- .../antlr/v4/codegen/src/LL1StarBlock.java | 13 +++-------- 7 files changed, 34 insertions(+), 61 deletions(-) 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 6800a1524..2ebf1c6df 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 @@ -121,7 +121,7 @@ else { } >> -LL1StarBlock(choice, alts, error, sync) ::= << +LL1StarBlock(choice, alts, sync) ::= << : while (true) { switch ( state.input.LA(1) ) { @@ -135,7 +135,7 @@ while (true) { } >> -LL1StarBlockSingleAlt(choice, expr, alts, preamble, iteration, error, sync) ::= << +LL1StarBlockSingleAlt(choice, expr, alts, preamble, iteration, sync) ::= << while ( ) { @@ -144,7 +144,7 @@ while ( ) { } >> -LL1PlusBlock(choice, alts, error, earlyExitError, sync) ::= << +LL1PlusBlock(choice, alts, earlyExitError, sync) ::= << int = 0; : @@ -162,7 +162,7 @@ while (true) { >> LL1PlusBlockSingleAlt(choice, expr, alts, preamble, iteration, - error, earlyExitError, sync) ::= + earlyExitError, sync) ::= << diff --git a/tool/src/org/antlr/v4/codegen/src/Choice.java b/tool/src/org/antlr/v4/codegen/src/Choice.java index da122e643..bd642b057 100644 --- a/tool/src/org/antlr/v4/codegen/src/Choice.java +++ b/tool/src/org/antlr/v4/codegen/src/Choice.java @@ -15,8 +15,6 @@ public abstract class Choice extends SrcOp { public List alts; public List preamble; public IntervalSet expecting; - public ThrowNoViableAlt error; - public Sync sync; public Choice(OutputModelFactory factory, GrammarAST blkOrEbnfRootAST, List alts) { super(factory, blkOrEbnfRootAST); @@ -26,10 +24,7 @@ public abstract class Choice extends SrcOp { LinearApproximator approx = new LinearApproximator(factory.g, decision); NFAState decisionState = ast.nfaState; expecting = approx.LOOK(decisionState); - System.out.println(blkOrEbnfRootAST.toStringTree()+" loop expecting="+expecting); - -// this.error = new ThrowNoViableAlt(factory, blkOrEbnfRootAST, expecting); - this.sync = new Sync(factory, blkOrEbnfRootAST, expecting); + System.out.println(blkOrEbnfRootAST.toStringTree()+" choice expecting="+expecting); } public void addPreambleOp(SrcOp op) { @@ -43,4 +38,13 @@ public abstract class Choice extends SrcOp { // return new ArrayList() {{ if ( sup!=null ) addAll(sup); // add("alts"); add("preamble"); add("error"); }}; // } + + public List getAltLookaheadAsStringLists(IntervalSet[] altLookSets) { + List altLook = new ArrayList(); + for (int a=1; a altLook; - /** Lookahead for each alt 1..n */ - public IntervalSet[] altLookSets; + public ThrowNoViableAlt error; public LL1Choice(OutputModelFactory factory, GrammarAST blkAST, List alts) { super(factory, blkAST, alts); this.decision = ((DecisionState)blkAST.nfaState).decision; DFA dfa = factory.g.decisionDFAs.get(decision); - altLookSets = LinearApproximator.getLL1LookaheadSets(dfa); - altLook = new ArrayList(); - for (int a=1; a iteration; + public Sync sync; public LL1Loop(OutputModelFactory factory, GrammarAST blkAST, List alts) { super(factory, blkAST, alts); + this.sync = new Sync(factory, blkAST, expecting); } public void addIterationOp(SrcOp op) { @@ -33,12 +35,4 @@ public abstract class LL1Loop extends Choice { } } -// @Override -// public List getChildren() { -// final List sup = super.getChildren(); -// return new ArrayList() {{ -// if ( sup!=null ) addAll(sup); add("expr"); add("iteration"); -// }}; -// } - } diff --git a/tool/src/org/antlr/v4/codegen/src/LL1OptionalBlockSingleAlt.java b/tool/src/org/antlr/v4/codegen/src/LL1OptionalBlockSingleAlt.java index 26c7a0808..243ea508b 100644 --- a/tool/src/org/antlr/v4/codegen/src/LL1OptionalBlockSingleAlt.java +++ b/tool/src/org/antlr/v4/codegen/src/LL1OptionalBlockSingleAlt.java @@ -1,5 +1,8 @@ package org.antlr.v4.codegen.src; +import org.antlr.v4.analysis.LinearApproximator; +import org.antlr.v4.automata.BlockStartState; +import org.antlr.v4.automata.DFA; import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.misc.IntervalSet; import org.antlr.v4.tool.GrammarAST; @@ -11,6 +14,9 @@ public class LL1OptionalBlockSingleAlt extends LL1Choice { public Object expr; public LL1OptionalBlockSingleAlt(OutputModelFactory factory, GrammarAST blkAST, List alts) { super(factory, blkAST, alts); + DFA dfa = factory.g.decisionDFAs.get(((BlockStartState)blkAST.nfaState).decision); + /** Lookahead for each alt 1..n */ + IntervalSet[] altLookSets = LinearApproximator.getLL1LookaheadSets(dfa); IntervalSet look = altLookSets[1]; expr = factory.getLL1Test(look, blkAST); if ( expr instanceof TestSetInline ) { @@ -19,10 +25,4 @@ public class LL1OptionalBlockSingleAlt extends LL1Choice { addPreambleOp(nextToken); } } - -// @Override -// public List getChildren() { -// final List sup = super.getChildren(); -// return new ArrayList() {{ if ( sup!=null ) addAll(sup); add("expr"); }}; -// } } diff --git a/tool/src/org/antlr/v4/codegen/src/LL1PlusBlock.java b/tool/src/org/antlr/v4/codegen/src/LL1PlusBlock.java index f3dfccf85..f10cb98c7 100644 --- a/tool/src/org/antlr/v4/codegen/src/LL1PlusBlock.java +++ b/tool/src/org/antlr/v4/codegen/src/LL1PlusBlock.java @@ -8,16 +8,13 @@ import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.misc.IntervalSet; import org.antlr.v4.tool.GrammarAST; -import java.util.ArrayList; import java.util.List; /** */ public class LL1PlusBlock extends LL1Loop { /** Token names for each alt 0..n-1 */ public List altLook; - /** Lookahead for each alt 1..n */ - public IntervalSet[] altLookSets; - + public String loopLabel; public String loopCounterVar; public String[] exitLook; @@ -29,12 +26,9 @@ public class LL1PlusBlock extends LL1Loop { BlockStartState blkStart = (BlockStartState)plus.transition(0).target; DFA dfa = factory.g.decisionDFAs.get(blkStart.decision); - altLookSets = LinearApproximator.getLL1LookaheadSets(dfa); - altLook = new ArrayList(); - for (int a=1; a getChildren() { -// final List sup = super.getChildren(); -// return new ArrayList() {{ -// if ( sup!=null ) addAll(sup); add("earlyExitError"); -// }}; -// } } diff --git a/tool/src/org/antlr/v4/codegen/src/LL1StarBlock.java b/tool/src/org/antlr/v4/codegen/src/LL1StarBlock.java index ed897c65d..f13526ead 100644 --- a/tool/src/org/antlr/v4/codegen/src/LL1StarBlock.java +++ b/tool/src/org/antlr/v4/codegen/src/LL1StarBlock.java @@ -8,15 +8,12 @@ import org.antlr.v4.codegen.OutputModelFactory; import org.antlr.v4.misc.IntervalSet; import org.antlr.v4.tool.GrammarAST; -import java.util.ArrayList; import java.util.List; /** */ public class LL1StarBlock extends LL1Loop { /** Token names for each alt 0..n-1 */ public List altLook; - /** Lookahead for each alt 1..n */ - public IntervalSet[] altLookSets; public String loopLabel; public String[] exitLook; @@ -26,15 +23,11 @@ public class LL1StarBlock extends LL1Loop { StarBlockStartState star = (StarBlockStartState)blkAST.nfaState; int enterExitDecision = star.decision; BlockStartState blkStart = (BlockStartState)star.transition(0).target; - //this.decision = blkStart.decision; DFA dfa = factory.g.decisionDFAs.get(blkStart.decision); - altLookSets = LinearApproximator.getLL1LookaheadSets(dfa); - altLook = new ArrayList(); - for (int a=1; a