forked from jasder/antlr
test profiler with generated code. dump profile information using toString as dump() no longer available.
This commit is contained in:
parent
4aa3556f1c
commit
a1c0319b41
|
@ -1025,6 +1025,7 @@ public abstract class BaseTest {
|
||||||
"import org.antlr.v4.runtime.*;\n" +
|
"import org.antlr.v4.runtime.*;\n" +
|
||||||
"import org.antlr.v4.runtime.tree.*;\n" +
|
"import org.antlr.v4.runtime.tree.*;\n" +
|
||||||
"import org.antlr.v4.runtime.atn.*;\n" +
|
"import org.antlr.v4.runtime.atn.*;\n" +
|
||||||
|
"import java.util.Arrays;\n"+
|
||||||
"\n" +
|
"\n" +
|
||||||
"public class Test {\n" +
|
"public class Test {\n" +
|
||||||
" public static void main(String[] args) throws Exception {\n" +
|
" public static void main(String[] args) throws Exception {\n" +
|
||||||
|
@ -1035,7 +1036,7 @@ public abstract class BaseTest {
|
||||||
" parser.setBuildParseTree(true);\n" +
|
" parser.setBuildParseTree(true);\n" +
|
||||||
" <profile>\n"+
|
" <profile>\n"+
|
||||||
" ParserRuleContext tree = parser.<parserStartRuleName>();\n" +
|
" ParserRuleContext tree = parser.<parserStartRuleName>();\n" +
|
||||||
" <if(profile)>profiler.dump();<endif>\n" +
|
" <if(profile)>System.out.println(Arrays.toString(profiler.getDecisionInfo()));<endif>\n" +
|
||||||
" ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);\n" +
|
" ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);\n" +
|
||||||
" }\n" +
|
" }\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
|
|
|
@ -208,6 +208,7 @@ public class TestParserProfiler extends BaseTest {
|
||||||
|
|
||||||
// pred forces to
|
// pred forces to
|
||||||
// ambig and ('+' e)* tail recursion forces lookahead to fall out of e
|
// ambig and ('+' e)* tail recursion forces lookahead to fall out of e
|
||||||
|
// any non-precedence predicates are always evaluated as true by the interpreter
|
||||||
DecisionInfo[] info = interpAndGetDecisionInfo(lg, g, "s", "a+b+c;");
|
DecisionInfo[] info = interpAndGetDecisionInfo(lg, g, "s", "a+b+c;");
|
||||||
// at "+b" it uses k=1 and enters loop then calls e for b...
|
// at "+b" it uses k=1 and enters loop then calls e for b...
|
||||||
// e matches and d=2 uses "+c;" for k=3
|
// e matches and d=2 uses "+c;" for k=3
|
||||||
|
@ -220,6 +221,32 @@ public class TestParserProfiler extends BaseTest {
|
||||||
assertEquals(expecting, Arrays.toString(info));
|
assertEquals(expecting, Arrays.toString(info));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test public void testProfilerGeneratedCode() throws Exception {
|
||||||
|
String grammar =
|
||||||
|
"grammar T;\n" +
|
||||||
|
"s : a+ ID EOF ;\n" +
|
||||||
|
"a : ID ';'{}\n" +
|
||||||
|
" | ID '.'\n" +
|
||||||
|
" ;\n"+
|
||||||
|
"WS : [ \\r\\t\\n]+ -> channel(HIDDEN) ;\n" +
|
||||||
|
"SEMI : ';' ;\n" +
|
||||||
|
"DOT : '.' ;\n" +
|
||||||
|
"ID : [a-zA-Z]+ ;\n" +
|
||||||
|
"INT : [0-9]+ ;\n" +
|
||||||
|
"PLUS : '+' ;\n" +
|
||||||
|
"MULT : '*' ;\n";
|
||||||
|
|
||||||
|
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s",
|
||||||
|
"xyz;abc;z.q", false, true);
|
||||||
|
String expecting =
|
||||||
|
"[{decision=0, contextSensitivities=0, errors=0, ambiguities=0, SLL_lookahead=6, SLL_ATNTransitions=4, " +
|
||||||
|
"SLL_DFATransitions=2, LL_Fallback=0, LL_lookahead=0, LL_ATNTransitions=0}," +
|
||||||
|
" {decision=1, contextSensitivities=0, errors=0, ambiguities=0, SLL_lookahead=6, " +
|
||||||
|
"SLL_ATNTransitions=3, SLL_DFATransitions=3, LL_Fallback=0, LL_lookahead=0, LL_ATNTransitions=0}]\n";
|
||||||
|
assertEquals(expecting, found);
|
||||||
|
assertEquals(null, stderrDuringParse);
|
||||||
|
}
|
||||||
|
|
||||||
public DecisionInfo[] interpAndGetDecisionInfo(
|
public DecisionInfo[] interpAndGetDecisionInfo(
|
||||||
LexerGrammar lg, Grammar g,
|
LexerGrammar lg, Grammar g,
|
||||||
String startRule, String... input)
|
String startRule, String... input)
|
||||||
|
|
Loading…
Reference in New Issue