use of org.antlr.v4.runtime.ANTLRInputStream 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.ANTLRInputStream 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.ANTLRInputStream 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.ANTLRInputStream in project L42 by ElvisResearchGroup.
the class Parser method getParser.
public static antlrGenerated.L42Parser getParser(String s) {
ANTLRInputStream in = new ANTLRInputStream(s);
antlrGenerated.L42Lexer l = new antlrGenerated.L42Lexer(in);
/*{
@Override public void recover(LexerNoViableAltException e) {
throw new IllegalArgumentException(e); // Bail out
}
@Override public void recover(RecognitionException re) {
throw new IllegalArgumentException(re); // Bail out
}};*/
CommonTokenStream t = new CommonTokenStream(l);
antlrGenerated.L42Parser p = new antlrGenerated.L42Parser(t);
// p.setErrorHandler(new BailErrorStrategy());
return p;
}
use of org.antlr.v4.runtime.ANTLRInputStream in project antlr4 by antlr.
the class TestUnbufferedTokenStream method testMarkStart.
@Test
public void testMarkStart() 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 = 302;
CharStream input = new ANTLRInputStream(new StringReader("x = 302;"));
LexerInterpreter lexEngine = g.createLexerInterpreter(input);
TestingUnbufferedTokenStream<Token> tokens = new TestingUnbufferedTokenStream<Token>(lexEngine);
int m = tokens.mark();
assertEquals("[[@0,0:0='x',<1>,1:0]]", tokens.getBuffer().toString());
assertEquals("x", tokens.LT(1).getText());
// consume x
tokens.consume();
assertEquals("[[@0,0:0='x',<1>,1:0], [@1,1:1=' ',<7>,1:1]]", tokens.getBuffer().toString());
// ' '
tokens.consume();
// =
tokens.consume();
// ' '
tokens.consume();
// 302
tokens.consume();
// ;
tokens.consume();
assertEquals("[[@0,0:0='x',<1>,1:0], [@1,1:1=' ',<7>,1:1]," + " [@2,2:2='=',<4>,1:2], [@3,3:3=' ',<7>,1:3]," + " [@4,4:6='302',<2>,1:4], [@5,7:7=';',<3>,1:7]," + " [@6,8:7='<EOF>',<-1>,1:8]]", tokens.getBuffer().toString());
}
Aggregations