use of org.antlr.v4.tool.LexerGrammar in project antlr4 by antlr.
the class BasePythonTest method createATN.
protected ATN createATN(Grammar g, boolean useSerializer) {
if (g.atn == null) {
semanticProcess(g);
assertEquals(0, g.tool.getNumErrors());
ParserATNFactory f;
if (g.isLexer()) {
f = new LexerATNFactory((LexerGrammar) g);
} else {
f = new ParserATNFactory(g);
}
g.atn = f.createATN();
assertEquals(0, g.tool.getNumErrors());
}
ATN atn = g.atn;
if (useSerializer) {
char[] serialized = ATNSerializer.getSerializedAsChars(atn);
return new ATNDeserializer().deserialize(serialized);
}
return atn;
}
use of org.antlr.v4.tool.LexerGrammar in project antlr4 by antlr.
the class BasePythonTest method testActions.
public void testActions(String templates, String actionName, String action, String expected) throws org.antlr.runtime.RecognitionException {
int lp = templates.indexOf('(');
String name = templates.substring(0, lp);
STGroup group = new STGroupString(templates);
ST st = group.getInstanceOf(name);
st.add(actionName, action);
String grammar = st.render();
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(grammar, equeue);
if (g.ast != null && !g.ast.hasErrors) {
SemanticPipeline sem = new SemanticPipeline(g);
sem.process();
ATNFactory factory = new ParserATNFactory(g);
if (g.isLexer())
factory = new LexerATNFactory((LexerGrammar) g);
g.atn = factory.createATN();
CodeGenerator gen = new CodeGenerator(g);
ST outputFileST = gen.generateParser();
String output = outputFileST.render();
//System.out.println(output);
String b = "#" + actionName + "#";
int start = output.indexOf(b);
String e = "#end-" + actionName + "#";
int end = output.indexOf(e);
String snippet = output.substring(start + b.length(), end);
assertEquals(expected, snippet);
}
if (equeue.size() > 0) {
System.err.println(equeue.toString());
}
}
use of org.antlr.v4.tool.LexerGrammar in project antlr4 by antlr.
the class BasePythonTest method checkLexerDFA.
List<ANTLRMessage> checkLexerDFA(String gtext, String modeName, String expecting) throws Exception {
ErrorQueue equeue = new ErrorQueue();
LexerGrammar g = new LexerGrammar(gtext, equeue);
g.atn = createATN(g, false);
// return equeue.all;
return null;
}
use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.
the class TestATNInterpreter method testMustTrackPreviousGoodAlt3.
@Test
public void testMustTrackPreviousGoodAlt3() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "D : 'd' ;\n");
Grammar g = new Grammar("parser grammar T;\n" + "a : A B | A | A B C ;");
checkMatchedAlt(lg, g, "a", 2);
checkMatchedAlt(lg, g, "ab", 1);
checkMatchedAlt(lg, g, "abc", 3);
checkMatchedAlt(lg, g, "ad", 2);
checkMatchedAlt(lg, g, "abd", 1);
checkMatchedAlt(lg, g, "abcd", 3);
}
use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.
the class TestATNInterpreter method testMustTrackPreviousGoodAlt2WithEOF.
@Test(expected = NoViableAltException.class)
public void testMustTrackPreviousGoodAlt2WithEOF() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "D : 'd' ;\n");
Grammar g = new Grammar("parser grammar T;\n" + "a : (A | A B | A B C) EOF;");
checkMatchedAlt(lg, g, "a", 1);
checkMatchedAlt(lg, g, "ab", 2);
checkMatchedAlt(lg, g, "abc", 3);
try {
checkMatchedAlt(lg, g, "abd", 1);
} catch (NoViableAltException re) {
assertEquals(2, re.getOffendingToken().getTokenIndex());
assertEquals(4, re.getOffendingToken().getType());
throw re;
}
}
Aggregations