Search in sources :

Example 66 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project drill by axbaretto.

the class TreeTest method parseExpression.

private LogicalExpression parseExpression(String expr) throws RecognitionException, IOException {
    ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // tokens.fill();
    // for(Token t : (List<Token>) tokens.getTokens()){
    // System.out.println(t + "" + t.getType());
    // }
    // tokens.rewind();
    ExprParser parser = new ExprParser(tokens);
    parse_return ret = parser.parse();
    return ret.e;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ExprParser.parse_return(org.apache.drill.common.expression.parser.ExprParser.parse_return)

Example 67 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project drill by axbaretto.

the class SchemaPath method parseFromString.

/**
 * Parses input string using the same rules which are used for the field in the query.
 * If a string contains dot outside back-ticks, or there are no backticks in the string,
 * will be created {@link SchemaPath} with the {@link NameSegment}
 * which contains one else {@link NameSegment}, etc.
 * If a string contains [] then {@link ArraySegment} will be created.
 *
 * @param expr input string to be parsed
 * @return {@link SchemaPath} instance
 */
public static SchemaPath parseFromString(String expr) {
    if (expr == null || expr.isEmpty()) {
        return null;
    }
    try {
        ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ExprParser parser = new ExprParser(tokens);
        parse_return ret = parser.parse();
        if (ret.e instanceof SchemaPath) {
            return (SchemaPath) ret.e;
        } else {
            throw new IllegalStateException("Schema path is not a valid format.");
        }
    } catch (RecognitionException e) {
        throw new RuntimeException(e);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ExprLexer(org.apache.drill.common.expression.parser.ExprLexer) ExprParser.parse_return(org.apache.drill.common.expression.parser.ExprParser.parse_return) ExprParser(org.apache.drill.common.expression.parser.ExprParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 68 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project vespa by vespa-engine.

the class Predicate method fromString.

public static Predicate fromString(String str) {
    ANTLRStringStream input = new ANTLRStringStream(str);
    PredicateLexer lexer = new PredicateLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    PredicateParser parser = new PredicateParser(tokens);
    try {
        return parser.predicate();
    } catch (RecognitionException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) PredicateLexer(com.yahoo.document.predicate.parser.PredicateLexer) PredicateParser(com.yahoo.document.predicate.parser.PredicateParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 69 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project cuba by cuba-platform.

the class Parser method createParser.

private static JPA2Parser createParser(String input) {
    if (input.contains("~"))
        throw new IllegalArgumentException("Input string cannot contain \"~\"");
    CharStream cs = new AntlrNoCaseStringStream(input);
    JPA2Lexer lexer = new JPA2Lexer(cs);
    TokenStream tstream = new CommonTokenStream(lexer);
    return new JPA2Parser(tstream);
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) JPA2Parser(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser) JPA2Lexer(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer) CharStream(org.antlr.runtime.CharStream)

Example 70 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project cuba by cuba-platform.

the class Jpa2GrammarTest method testTypeField.

@Test
public void testTypeField() throws Exception {
    String query = "where e.model.type = :component$filter.model_type89015";
    CharStream cs = new AntlrNoCaseStringStream(query);
    JPA2Lexer lexer = new JPA2Lexer(cs);
    TokenStream tstream = new CommonTokenStream(lexer);
    JPA2Parser jpa2Parser = new JPA2Parser(tstream);
    JPA2Parser.where_clause_return aReturn = jpa2Parser.where_clause();
    Assertions.assertTrue(isValid((CommonTree) aReturn.getTree()));
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) JPA2Parser(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser) JPA2Lexer(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer) CharStream(org.antlr.runtime.CharStream) Test(org.junit.jupiter.api.Test)

Aggregations

CommonTokenStream (org.antlr.runtime.CommonTokenStream)93 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)62 RecognitionException (org.antlr.runtime.RecognitionException)25 CharStream (org.antlr.runtime.CharStream)22 CommonTree (org.antlr.runtime.tree.CommonTree)21 TokenStream (org.antlr.runtime.TokenStream)17 File (java.io.File)12 Test (org.junit.Test)12 CommonToken (org.antlr.runtime.CommonToken)10 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)10 ExprLexer (org.apache.drill.common.expression.parser.ExprLexer)10 ExprParser (org.apache.drill.common.expression.parser.ExprParser)10 FileInputStream (java.io.FileInputStream)9 InputStream (java.io.InputStream)8 ANTLRInputStream (org.antlr.runtime.ANTLRInputStream)8 InternalSimpleExpressionsTestLanguageLexer (org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer)8 InputStreamReader (java.io.InputStreamReader)6 Token (org.antlr.runtime.Token)6 JPA2Lexer (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer)5 JPA2Parser (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser)5