use of org.antlr.v4.tool.DOTGenerator in project antlr4 by antlr.
the class BaseBrowserTest method checkRuleATN.
void checkRuleATN(Grammar g, String ruleName, String expecting) {
ParserATNFactory f = new ParserATNFactory(g);
ATN atn = f.createATN();
DOTGenerator dot = new DOTGenerator(g);
System.out.println(dot.getDOT(atn.ruleToStartState[g.getRule(ruleName).index]));
Rule r = g.getRule(ruleName);
ATNState startState = atn.ruleToStartState[r.index];
ATNPrinter serializer = new ATNPrinter(g, startState);
String result = serializer.asString();
//System.out.print(result);
assertEquals(expecting, result);
}
use of org.antlr.v4.tool.DOTGenerator in project antlr4 by tunnelvisionlabs.
the class TestATNInterpreter method checkMatchedAlt.
public void checkMatchedAlt(LexerGrammar lg, final Grammar g, String inputString, int expected) {
ATN lexatn = createATN(lg, true);
LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn);
IntegerList types = getTokenTypesViaATN(inputString, lexInterp);
System.out.println(types);
g.importVocab(lg);
ParserATNFactory f = new ParserATNFactory(g);
ATN atn = f.createATN();
IntTokenStream input = new IntTokenStream(types);
System.out.println("input=" + input.types);
ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, input);
ATNState startState = atn.ruleToStartState[g.getRule("a").index];
if (startState.transition(0).target instanceof BlockStartState) {
startState = startState.transition(0).target;
}
DOTGenerator dot = new DOTGenerator(g);
System.out.println(dot.getDOT(atn.ruleToStartState[g.getRule("a").index]));
Rule r = g.getRule("e");
if (r != null)
System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
int result = interp.matchATN(input, startState);
assertEquals(expected, result);
}
use of org.antlr.v4.tool.DOTGenerator in project antlr4 by tunnelvisionlabs.
the class TestATNLexerInterpreter method checkLexerMatches.
protected void checkLexerMatches(LexerGrammar lg, String inputString, String expecting) {
ATN atn = createATN(lg, true);
CharStream input = CharStreams.fromString(inputString);
ATNState startState = atn.modeNameToStartState.get("DEFAULT_MODE");
DOTGenerator dot = new DOTGenerator(lg);
System.out.println(dot.getDOT(startState, true));
List<String> tokenTypes = getTokenTypes(lg, atn, input);
String result = Utils.join(tokenTypes.iterator(), ", ");
System.out.println(tokenTypes);
assertEquals(expecting, result);
}
use of org.antlr.v4.tool.DOTGenerator in project antlr4 by tunnelvisionlabs.
the class TestATNSerialization method testNot.
@Test
public void testNot() throws Exception {
Grammar g = new Grammar("parser grammar T;\n" + "tokens {A, B, C}\n" + "a : ~A ;");
String expecting = "max type 3\n" + "0:RULE_START 0\n" + "1:RULE_STOP 0\n" + "2:BASIC 0\n" + "3:BASIC 0\n" + "4:BASIC 0\n" + "rule 0:0\n" + "0:A..A\n" + "0->2 EPSILON 0,0,0\n" + "2->3 NOT_SET 0,0,0\n" + "3->1 EPSILON 0,0,0\n";
ATN atn = createATN(g, true);
DOTGenerator gen = new DOTGenerator(g);
System.out.println(gen.getDOT(atn.ruleToStartState[0]));
String result = ATNSerializer.getDecoded(atn, Arrays.asList(g.getRuleNames()), Arrays.asList(g.getTokenNames()));
assertEquals(expecting, result);
}
use of org.antlr.v4.tool.DOTGenerator in project antlr4 by tunnelvisionlabs.
the class Tool method generateATNs.
public void generateATNs(Grammar g) {
DOTGenerator dotGenerator = new DOTGenerator(g);
List<Grammar> grammars = new ArrayList<Grammar>();
grammars.add(g);
List<Grammar> imported = g.getAllImportedGrammars();
if (imported != null)
grammars.addAll(imported);
for (Grammar ig : grammars) {
for (Rule r : ig.rules.values()) {
try {
String dot = dotGenerator.getDOT(g.atn.ruleToStartState[r.index], g.isLexer());
if (dot != null) {
writeDOTFile(g, r, dot);
}
} catch (IOException ioe) {
errMgr.toolError(ErrorType.CANNOT_WRITE_FILE, ioe);
}
}
}
}
Aggregations