forked from jasder/antlr
rename atn sim, add error message to TestRig
This commit is contained in:
parent
c47c07299a
commit
894a77168d
|
@ -38,7 +38,7 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/** This is all the parsing support code essentially; most of it is error recovery stuff. */
|
/** This is all the parsing support code essentially; most of it is error recovery stuff. */
|
||||||
public abstract class Parser extends Recognizer<Token, v2ParserATNSimulator<Token>> {
|
public abstract class Parser extends Recognizer<Token, ParserATNSimulator<Token>> {
|
||||||
public class TraceListener implements ParseTreeListener<Token> {
|
public class TraceListener implements ParseTreeListener<Token> {
|
||||||
@Override
|
@Override
|
||||||
public void enterEveryRule(ParserRuleContext<Token> ctx) {
|
public void enterEveryRule(ParserRuleContext<Token> ctx) {
|
||||||
|
|
|
@ -43,7 +43,7 @@ import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class ParserATNPathFinder extends v2ParserATNSimulator {
|
public class ParserATNPathFinder extends ParserATNSimulator {
|
||||||
public ParserATNPathFinder(@Nullable Parser parser, @NotNull ATN atn) {
|
public ParserATNPathFinder(@Nullable Parser parser, @NotNull ATN atn) {
|
||||||
super(parser, atn);
|
super(parser, atn);
|
||||||
}
|
}
|
||||||
|
|
|
@ -225,7 +225,7 @@ import java.util.*;
|
||||||
* when closure operations fall off the end of the rule that
|
* when closure operations fall off the end of the rule that
|
||||||
* holds the decision were evaluating
|
* holds the decision were evaluating
|
||||||
*/
|
*/
|
||||||
public class v2ParserATNSimulator<Symbol> extends ATNSimulator {
|
public class ParserATNSimulator<Symbol> extends ATNSimulator {
|
||||||
public static boolean debug = false;
|
public static boolean debug = false;
|
||||||
public static boolean dfa_debug = false;
|
public static boolean dfa_debug = false;
|
||||||
public static boolean retry_debug = false;
|
public static boolean retry_debug = false;
|
||||||
|
@ -242,11 +242,11 @@ public class v2ParserATNSimulator<Symbol> extends ATNSimulator {
|
||||||
public final DFA[] decisionToDFA;
|
public final DFA[] decisionToDFA;
|
||||||
|
|
||||||
/** Testing only! */
|
/** Testing only! */
|
||||||
public v2ParserATNSimulator(@NotNull ATN atn) {
|
public ParserATNSimulator(@NotNull ATN atn) {
|
||||||
this(null, atn);
|
this(null, atn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public v2ParserATNSimulator(@Nullable Parser parser, @NotNull ATN atn) {
|
public ParserATNSimulator(@Nullable Parser parser, @NotNull ATN atn) {
|
||||||
super(atn);
|
super(atn);
|
||||||
this.parser = parser;
|
this.parser = parser;
|
||||||
// ctxToDFAs = new HashMap<RuleContext, DFA[]>();
|
// ctxToDFAs = new HashMap<RuleContext, DFA[]>();
|
|
@ -28,10 +28,8 @@
|
||||||
*/
|
*/
|
||||||
package org.antlr.v4.runtime.dfa;
|
package org.antlr.v4.runtime.dfa;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.Token;
|
|
||||||
import org.antlr.v4.runtime.TokenStream;
|
import org.antlr.v4.runtime.TokenStream;
|
||||||
import org.antlr.v4.runtime.atn.*;
|
import org.antlr.v4.runtime.atn.*;
|
||||||
import org.antlr.v4.runtime.misc.IntervalSet;
|
|
||||||
import org.antlr.v4.runtime.misc.NotNull;
|
import org.antlr.v4.runtime.misc.NotNull;
|
||||||
import org.antlr.v4.runtime.misc.Nullable;
|
import org.antlr.v4.runtime.misc.Nullable;
|
||||||
|
|
||||||
|
@ -83,7 +81,7 @@ public class DFA {
|
||||||
// return states;
|
// return states;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public List<Set<ATNState>> getATNStatesAlongPath(v2ParserATNSimulator atn,
|
public List<Set<ATNState>> getATNStatesAlongPath(ParserATNSimulator atn,
|
||||||
List<DFAState> dfaStates,
|
List<DFAState> dfaStates,
|
||||||
TokenStream input, int start, int stop)
|
TokenStream input, int start, int stop)
|
||||||
{
|
{
|
||||||
|
|
|
@ -60,8 +60,8 @@ public class TestRig {
|
||||||
String encoding = null;
|
String encoding = null;
|
||||||
if ( args.length < 2 ) {
|
if ( args.length < 2 ) {
|
||||||
System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName" +
|
System.err.println("java org.antlr.v4.runtime.misc.TestRig GrammarName startRuleName" +
|
||||||
" [-print] [-tokens] [-gui] [-encoding encodingname]" +
|
" [-tokens] [-print] [-gui] [-ps file.ps] [-encoding encodingname] [-trace]"+
|
||||||
" [-ps file.ps] [-trace] [input-filename]");
|
" [input-filename]");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i=0;
|
int i=0;
|
||||||
|
@ -155,17 +155,22 @@ public class TestRig {
|
||||||
|
|
||||||
parser.setTrace(trace);
|
parser.setTrace(trace);
|
||||||
|
|
||||||
Method startRule = parserClass.getMethod(startRuleName, (Class[])null);
|
try {
|
||||||
ParserRuleContext<Token> tree = (ParserRuleContext<Token>)startRule.invoke(parser, (Object[])null);
|
Method startRule = parserClass.getMethod(startRuleName, (Class[])null);
|
||||||
|
ParserRuleContext<Token> tree = (ParserRuleContext<Token>)startRule.invoke(parser, (Object[])null);
|
||||||
|
|
||||||
if ( printTree ) {
|
if ( printTree ) {
|
||||||
System.out.println(tree.toStringTree(parser));
|
System.out.println(tree.toStringTree(parser));
|
||||||
|
}
|
||||||
|
if ( gui ) {
|
||||||
|
tree.inspect(parser);
|
||||||
|
}
|
||||||
|
if ( psFile!=null ) {
|
||||||
|
tree.save(parser, psFile); // Generate postscript
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ( gui ) {
|
catch (NoSuchMethodException nsme) {
|
||||||
tree.inspect(parser);
|
System.err.println("No method for rule "+startRuleName+" or it has arguments");
|
||||||
}
|
|
||||||
if ( psFile!=null ) {
|
|
||||||
tree.save(parser, psFile); // Generate postscript
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
|
|
|
@ -33,14 +33,10 @@ import org.antlr.v4.Tool;
|
||||||
import org.antlr.v4.runtime.*;
|
import org.antlr.v4.runtime.*;
|
||||||
import org.antlr.v4.runtime.atn.*;
|
import org.antlr.v4.runtime.atn.*;
|
||||||
import org.antlr.v4.runtime.dfa.DFA;
|
import org.antlr.v4.runtime.dfa.DFA;
|
||||||
import org.antlr.v4.runtime.misc.IntervalSet;
|
|
||||||
import org.antlr.v4.runtime.misc.NotNull;
|
import org.antlr.v4.runtime.misc.NotNull;
|
||||||
import org.antlr.v4.runtime.misc.Nullable;
|
import org.antlr.v4.runtime.misc.Nullable;
|
||||||
import org.antlr.v4.tool.Grammar;
|
import org.antlr.v4.tool.Grammar;
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class ParserInterpreter {
|
public class ParserInterpreter {
|
||||||
class DummyParser extends Parser {
|
class DummyParser extends Parser {
|
||||||
public Grammar g;
|
public Grammar g;
|
||||||
|
@ -61,7 +57,7 @@ public class ParserInterpreter {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Grammar g;
|
protected Grammar g;
|
||||||
protected v2ParserATNSimulator<Token> atnSimulator;
|
protected ParserATNSimulator<Token> atnSimulator;
|
||||||
protected TokenStream input;
|
protected TokenStream input;
|
||||||
|
|
||||||
public ParserInterpreter(@NotNull Grammar g) {
|
public ParserInterpreter(@NotNull Grammar g) {
|
||||||
|
@ -71,7 +67,7 @@ public class ParserInterpreter {
|
||||||
public ParserInterpreter(@NotNull Grammar g, @NotNull TokenStream input) {
|
public ParserInterpreter(@NotNull Grammar g, @NotNull TokenStream input) {
|
||||||
Tool antlr = new Tool();
|
Tool antlr = new Tool();
|
||||||
antlr.process(g,false);
|
antlr.process(g,false);
|
||||||
atnSimulator = new v2ParserATNSimulator<Token>(new DummyParser(g, input), g.atn);
|
atnSimulator = new ParserATNSimulator<Token>(new DummyParser(g, input), g.atn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int predictATN(@NotNull DFA dfa, @NotNull SymbolStream<Token> input,
|
public int predictATN(@NotNull DFA dfa, @NotNull SymbolStream<Token> input,
|
||||||
|
@ -104,7 +100,7 @@ public class ParserInterpreter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public v2ParserATNSimulator<Token> getATNSimulator() {
|
public ParserATNSimulator<Token> getATNSimulator() {
|
||||||
return atnSimulator;
|
return atnSimulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ package org.antlr.v4.test;
|
||||||
import org.antlr.v4.automata.ATNSerializer;
|
import org.antlr.v4.automata.ATNSerializer;
|
||||||
import org.antlr.v4.misc.Utils;
|
import org.antlr.v4.misc.Utils;
|
||||||
import org.antlr.v4.runtime.atn.ATN;
|
import org.antlr.v4.runtime.atn.ATN;
|
||||||
import org.antlr.v4.runtime.atn.v2ParserATNSimulator;
|
import org.antlr.v4.runtime.atn.ParserATNSimulator;
|
||||||
import org.antlr.v4.tool.Grammar;
|
import org.antlr.v4.tool.Grammar;
|
||||||
import org.antlr.v4.tool.LexerGrammar;
|
import org.antlr.v4.tool.LexerGrammar;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -147,7 +147,7 @@ public class TestATNDeserialization extends BaseTest {
|
||||||
ATN atn = createATN(g);
|
ATN atn = createATN(g);
|
||||||
char[] data = Utils.toCharArray(ATNSerializer.getSerialized(g, atn));
|
char[] data = Utils.toCharArray(ATNSerializer.getSerialized(g, atn));
|
||||||
String atnData = ATNSerializer.getDecoded(g, atn);
|
String atnData = ATNSerializer.getDecoded(g, atn);
|
||||||
ATN atn2 = v2ParserATNSimulator.deserialize(data);
|
ATN atn2 = ParserATNSimulator.deserialize(data);
|
||||||
String atn2Data = ATNSerializer.getDecoded(g, atn2);
|
String atn2Data = ATNSerializer.getDecoded(g, atn2);
|
||||||
|
|
||||||
assertEquals(atnData, atn2Data);
|
assertEquals(atnData, atn2Data);
|
||||||
|
|
|
@ -541,7 +541,7 @@ public class TestATNParserPrediction extends BaseTest {
|
||||||
// System.out.println(dot.getDOT(atn.ruleToStartState.get(g.getRule("b"))));
|
// System.out.println(dot.getDOT(atn.ruleToStartState.get(g.getRule("b"))));
|
||||||
// System.out.println(dot.getDOT(atn.ruleToStartState.get(g.getRule("e"))));
|
// System.out.println(dot.getDOT(atn.ruleToStartState.get(g.getRule("e"))));
|
||||||
|
|
||||||
v2ParserATNSimulator interp = new v2ParserATNSimulator(atn);
|
ParserATNSimulator interp = new ParserATNSimulator(atn);
|
||||||
List<Integer> types = getTokenTypesViaATN(inputString, lexInterp);
|
List<Integer> types = getTokenTypesViaATN(inputString, lexInterp);
|
||||||
System.out.println(types);
|
System.out.println(types);
|
||||||
TokenStream input = new IntTokenStream(types);
|
TokenStream input = new IntTokenStream(types);
|
||||||
|
|
Loading…
Reference in New Issue