use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestAmbigParseTrees method testInterpAtSpecificAlt.
void testInterpAtSpecificAlt(LexerGrammar lg, Grammar g, String startRule, int startAlt, String input, String expectedParseTree) {
LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
ParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
RuleStartState ruleStartState = g.atn.ruleToStartState[g.getRule(startRule).index];
Transition tr = ruleStartState.transition(0);
ATNState t2 = tr.target;
if (!(t2 instanceof BasicBlockStartState)) {
throw new IllegalArgumentException("rule has no decision: " + startRule);
}
parser.addDecisionOverride(((DecisionState) t2).decision, 0, startAlt);
ParseTree t = parser.parse(g.rules.get(startRule).index);
InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
assertEquals(expectedParseTree, Trees.toStringTree(t, nodeTextProvider));
}
use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestBufferedTokenStream method testCompleteBuffer.
@Test
public void testCompleteBuffer() throws Exception {
LexerGrammar g = new LexerGrammar("lexer grammar t;\n" + "ID : 'a'..'z'+;\n" + "INT : '0'..'9'+;\n" + "SEMI : ';';\n" + "ASSIGN : '=';\n" + "PLUS : '+';\n" + "MULT : '*';\n" + "WS : ' '+;\n");
// Tokens: 012345678901234567
// Input: x = 3 * 0 + 2 * 0;
CharStream input = new ANTLRInputStream("x = 3 * 0 + 2 * 0;");
LexerInterpreter lexEngine = g.createLexerInterpreter(input);
TokenStream tokens = createTokenStream(lexEngine);
int i = 1;
Token t = tokens.LT(i);
while (t.getType() != Token.EOF) {
i++;
t = tokens.LT(i);
}
// push it past end
tokens.LT(i++);
tokens.LT(i++);
String result = tokens.getText();
String expecting = "x = 3 * 0 + 2 * 0;";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestGrammarParserInterpreter method testInterp.
InterpreterRuleContext testInterp(LexerGrammar lg, Grammar g, String startRule, String input, String expectedParseTree) {
LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
ParseTree t = parser.parse(g.rules.get(startRule).index);
InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
String treeStr = Trees.toStringTree(t, nodeTextProvider);
// System.out.println("parse tree: "+treeStr);
assertEquals(expectedParseTree, treeStr);
return (InterpreterRuleContext) t;
}
use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testInsertThenReplaceLastIndex.
@Test
public void testInsertThenReplaceLastIndex() throws Exception {
LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
String input = "abc";
LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
CommonTokenStream stream = new CommonTokenStream(lexEngine);
stream.fill();
TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
tokens.insertBefore(2, "y");
tokens.replace(2, "x");
String result = tokens.getText();
String expecting = "abyx";
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.LexerInterpreter in project antlr4 by antlr.
the class TestTokenStreamRewriter method testReplaceThenInsertBeforeLastIndex.
@Test
public void testReplaceThenInsertBeforeLastIndex() throws Exception {
LexerGrammar g = new LexerGrammar("lexer grammar T;\n" + "A : 'a';\n" + "B : 'b';\n" + "C : 'c';\n");
String input = "abc";
LexerInterpreter lexEngine = g.createLexerInterpreter(new ANTLRInputStream(input));
CommonTokenStream stream = new CommonTokenStream(lexEngine);
stream.fill();
TokenStreamRewriter tokens = new TokenStreamRewriter(stream);
tokens.replace(2, "x");
tokens.insertBefore(2, "y");
String result = tokens.getText();
String expecting = "abyx";
assertEquals(expecting, result);
}
Aggregations