Search in sources :

Example 26 with TokenStream

use of org.antlr.v4.runtime.TokenStream in project antlr4 by antlr.

the class TestUnbufferedTokenStream method testLookahead.

@Test
public void testLookahead() 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);
    TokenStream tokens = new UnbufferedTokenStream<Token>(lexEngine);
    assertEquals("x", tokens.LT(1).getText());
    assertEquals(" ", tokens.LT(2).getText());
    assertEquals("=", tokens.LT(3).getText());
    assertEquals(" ", tokens.LT(4).getText());
    assertEquals("302", tokens.LT(5).getText());
    assertEquals(";", tokens.LT(6).getText());
}
Also used : LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) TokenStream(org.antlr.v4.runtime.TokenStream) UnbufferedTokenStream(org.antlr.v4.runtime.UnbufferedTokenStream) StringReader(java.io.StringReader) LexerGrammar(org.antlr.v4.tool.LexerGrammar) UnbufferedTokenStream(org.antlr.v4.runtime.UnbufferedTokenStream) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

Example 27 with TokenStream

use of org.antlr.v4.runtime.TokenStream in project presto by prestodb.

the class TypeCalculation method parseTypeCalculation.

private static ParserRuleContext parseTypeCalculation(String calculation) {
    TypeCalculationLexer lexer = new TypeCalculationLexer(new CaseInsensitiveStream(new ANTLRInputStream(calculation)));
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    TypeCalculationParser parser = new TypeCalculationParser(tokenStream);
    lexer.removeErrorListeners();
    lexer.addErrorListener(ERROR_LISTENER);
    parser.removeErrorListeners();
    parser.addErrorListener(ERROR_LISTENER);
    ParserRuleContext tree;
    try {
        // first, try parsing with potentially faster SLL mode
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        tree = parser.typeCalculation();
    } catch (ParseCancellationException ex) {
        // if we fail, parse with LL mode
        // rewind input stream
        tokenStream.reset();
        parser.reset();
        parser.getInterpreter().setPredictionMode(PredictionMode.LL);
        tree = parser.typeCalculation();
    }
    return tree;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) CaseInsensitiveStream(com.facebook.presto.sql.parser.CaseInsensitiveStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 28 with TokenStream

use of org.antlr.v4.runtime.TokenStream in project sling by apache.

the class ExpressionParser method createParser.

private SightlyParser createParser(String string) {
    SightlyLexer lexer = new SightlyLexer(new ANTLRInputStream(string));
    lexer.removeErrorListeners();
    lexer.addErrorListener(new SightlyParserErrorListener());
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    SightlyParser parser = new SightlyParser(tokenStream);
    parser.removeErrorListeners();
    parser.addErrorListener(new SightlyParserErrorListener());
    return parser;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SightlyLexer(org.apache.sling.scripting.sightly.impl.parser.expr.generated.SightlyLexer) SightlyParser(org.apache.sling.scripting.sightly.impl.parser.expr.generated.SightlyParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)13 TokenStream (org.antlr.v4.runtime.TokenStream)11 CharStream (org.antlr.v4.runtime.CharStream)10 Test (org.junit.Test)7 BufferedTokenStream (org.antlr.v4.runtime.BufferedTokenStream)6 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)6 LexerGrammar (org.antlr.v4.tool.LexerGrammar)6 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)5 ATN (org.antlr.v4.runtime.atn.ATN)5 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)4 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)4 Token (org.antlr.v4.runtime.Token)4 ArrayList (java.util.ArrayList)3 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)3 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)3 UnbufferedTokenStream (org.antlr.v4.runtime.UnbufferedTokenStream)3 ATNDeserializer (org.antlr.v4.runtime.atn.ATNDeserializer)3 DFA (org.antlr.v4.runtime.dfa.DFA)3 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)3 AstNode (com.linkedin.pinot.pql.parsers.pql2.ast.AstNode)2