refactored

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6885]
This commit is contained in:
parrt 2010-05-23 09:05:05 -08:00
parent e1577a8071
commit f69f3f2c09
7 changed files with 34 additions and 61 deletions

View File

@ -121,7 +121,7 @@ else {
}
>>
LL1StarBlock(choice, alts, error, sync) ::= <<
LL1StarBlock(choice, alts, sync) ::= <<
<choice.loopLabel>:
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) ::= <<
<preamble; separator="\n">
while ( <expr> ) {
<alts; separator="\n">
@ -144,7 +144,7 @@ while ( <expr> ) {
}
>>
LL1PlusBlock(choice, alts, error, earlyExitError, sync) ::= <<
LL1PlusBlock(choice, alts, earlyExitError, sync) ::= <<
<sync>
int <choice.loopCounterVar> = 0;
<choice.loopLabel>:
@ -162,7 +162,7 @@ while (true) {
>>
LL1PlusBlockSingleAlt(choice, expr, alts, preamble, iteration,
error, earlyExitError, sync) ::=
earlyExitError, sync) ::=
<<
<sync>
<preamble; separator="\n">

View File

@ -15,8 +15,6 @@ public abstract class Choice extends SrcOp {
public List<CodeBlock> alts;
public List<SrcOp> preamble;
public IntervalSet expecting;
public ThrowNoViableAlt error;
public Sync sync;
public Choice(OutputModelFactory factory, GrammarAST blkOrEbnfRootAST, List<CodeBlock> 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<String>() {{ if ( sup!=null ) addAll(sup);
// add("alts"); add("preamble"); add("error"); }};
// }
public List<String[]> getAltLookaheadAsStringLists(IntervalSet[] altLookSets) {
List<String[]> altLook = new ArrayList<String[]>();
for (int a=1; a<altLookSets.length; a++) {
IntervalSet s = altLookSets[a];
altLook.add(factory.gen.target.getTokenTypesAsTargetLabels(factory.g, s.toArray()));
}
return altLook;
}
}

View File

@ -7,25 +7,21 @@ 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;
/** (A | B | C) */
public class LL1Choice extends Choice {
/** Token names for each alt 0..n-1 */
public List<String[]> altLook;
/** Lookahead for each alt 1..n */
public IntervalSet[] altLookSets;
public ThrowNoViableAlt error;
public LL1Choice(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> 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<String[]>();
for (int a=1; a<altLookSets.length; a++) {
IntervalSet s = altLookSets[a];
altLook.add(factory.gen.target.getTokenTypesAsTargetLabels(factory.g, s.toArray()));
}
/** Lookahead for each alt 1..n */
IntervalSet[] altLookSets = LinearApproximator.getLL1LookaheadSets(dfa);
altLook = getAltLookaheadAsStringLists(altLookSets);
this.error = new ThrowNoViableAlt(factory, blkAST, expecting);
}
}

View File

@ -11,9 +11,11 @@ import java.util.List;
public abstract class LL1Loop extends Choice {
public OutputModelObject expr;
public List<SrcOp> iteration;
public Sync sync;
public LL1Loop(OutputModelFactory factory, GrammarAST blkAST, List<CodeBlock> 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<String> getChildren() {
// final List<String> sup = super.getChildren();
// return new ArrayList<String>() {{
// if ( sup!=null ) addAll(sup); add("expr"); add("iteration");
// }};
// }
}

View File

@ -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<CodeBlock> 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<String> getChildren() {
// final List<String> sup = super.getChildren();
// return new ArrayList<String>() {{ if ( sup!=null ) addAll(sup); add("expr"); }};
// }
}

View File

@ -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 LL1PlusBlock extends LL1Loop {
/** Token names for each alt 0..n-1 */
public List<String[]> altLook;
/** Lookahead for each alt 1..n */
public IntervalSet[] altLookSets;
public String loopLabel;
public String loopCounterVar;
@ -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<String[]>();
for (int a=1; a<altLookSets.length; a++) {
IntervalSet s = altLookSets[a];
altLook.add(factory.gen.target.getTokenTypesAsTargetLabels(factory.g, s.toArray()));
}
/** Lookahead for each alt 1..n */
IntervalSet[] altLookSets = LinearApproximator.getLL1LookaheadSets(dfa);
altLook = getAltLookaheadAsStringLists(altLookSets);
dfa = factory.g.decisionDFAs.get(plus.loopBackState.decision);
IntervalSet exitLook = dfa.startState.edge(0).label;
@ -45,12 +39,4 @@ public class LL1PlusBlock extends LL1Loop {
this.earlyExitError = new ThrowEarlyExitException(factory, blkAST, expecting);
}
// @Override
// public List<String> getChildren() {
// final List<String> sup = super.getChildren();
// return new ArrayList<String>() {{
// if ( sup!=null ) addAll(sup); add("earlyExitError");
// }};
// }
}

View File

@ -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<String[]> 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<String[]>();
for (int a=1; a<altLookSets.length; a++) {
IntervalSet s = altLookSets[a];
altLook.add(factory.gen.target.getTokenTypesAsTargetLabels(factory.g, s.toArray()));
}
/** Lookahead for each alt 1..n */
IntervalSet[] altLookSets = LinearApproximator.getLL1LookaheadSets(dfa);
altLook = getAltLookaheadAsStringLists(altLookSets);
dfa = factory.g.decisionDFAs.get(enterExitDecision);
IntervalSet exitLook = dfa.startState.edge(1).label;