Search in sources :

Example 81 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream 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 82 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project n4js by eclipse.

the class NodeModelTokenSource method toPrefixToken.

/**
 * Produce an Antlr token for the prefix of the given leaf that overlaps the requested region
 *
 * @see #endOffset
 */
private Token toPrefixToken(ILeafNode leaf) {
    Lexer lexer = new InternalN4JSLexer();
    String text = leaf.getText();
    String prefix = text.substring(0, endOffset - leaf.getTotalOffset());
    ANTLRStringStream stream = new ANTLRStringStream(prefix);
    lexer.setCharStream(stream);
    Token nextToken = lexer.nextToken();
    // copy to get rid of the reference to the stream again
    return new CommonToken(nextToken.getType(), nextToken.getText());
}
Also used : InternalN4JSLexer(org.eclipse.n4js.ui.contentassist.antlr.lexer.InternalN4JSLexer) ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) Lexer(org.eclipse.xtext.ide.editor.contentassist.antlr.internal.Lexer) InternalN4JSLexer(org.eclipse.n4js.ui.contentassist.antlr.lexer.InternalN4JSLexer) Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) CommonToken(org.antlr.runtime.CommonToken)

Example 83 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream 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 84 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project n4js by eclipse.

the class AbstractSmokeTester method skipTokensInBetween.

private void skipTokensInBetween(CharSequence input) throws Exception {
    String string = input.toString();
    List<CommonToken> tokenList = Lists.newArrayList();
    {
        Lexer lexer = lexerProvider.get();
        lexer.setCharStream(new ANTLRStringStream(string));
        Token token = lexer.nextToken();
        while (token != Token.EOF_TOKEN) {
            tokenList.add((CommonToken) token);
            token = lexer.nextToken();
        }
    }
    for (CommonToken token : tokenList) {
        int start = token.getStartIndex();
        int length = token.getText().length();
        processFile(string.substring(0, start) + string.substring(start + length));
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) InternalN4JSLexer(org.eclipse.n4js.parser.antlr.lexer.InternalN4JSLexer) Lexer(org.antlr.runtime.Lexer) Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) CommonToken(org.antlr.runtime.CommonToken)

Example 85 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project drools by kiegroup.

the class DrlExprParser method parse.

/**
 * Parse an expression from text
 */
public ConstraintConnectiveDescr parse(final String text) {
    ConstraintConnectiveDescr constraint = null;
    try {
        DRLLexer lexer = DRLFactory.getDRLLexer(new ANTLRStringStream(text), languageLevel);
        CommonTokenStream input = new CommonTokenStream(lexer);
        RecognizerSharedState state = new RecognizerSharedState();
        helper = new ParserHelper(input, state, languageLevel);
        DRLExpressions parser = DRLFactory.getDRLExpressions(input, state, helper, languageLevel);
        parser.setBuildDescr(true);
        // setting initial value just in case
        parser.setLeftMostExpr(null);
        BaseDescr expr = parser.conditionalOrExpression();
        if (expr != null && !parser.hasErrors()) {
            constraint = ConstraintConnectiveDescr.newAnd();
            constraint.addOrMerge(expr);
        }
    } catch (RecognitionException e) {
        helper.reportError(e);
    }
    return constraint;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) DRLExpressions(org.drools.drl.parser.lang.DRLExpressions) DRLLexer(org.drools.drl.parser.lang.DRLLexer) ParserHelper(org.drools.drl.parser.lang.ParserHelper) RecognizerSharedState(org.antlr.runtime.RecognizerSharedState) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)117 CommonTokenStream (org.antlr.runtime.CommonTokenStream)63 Token (org.antlr.runtime.Token)30 Test (org.junit.Test)28 CharStream (org.antlr.runtime.CharStream)24 CommonToken (org.antlr.runtime.CommonToken)20 RecognitionException (org.antlr.runtime.RecognitionException)18 Lexer (org.eclipse.xtext.parser.antlr.Lexer)16 ActionSplitter (org.antlr.v4.parse.ActionSplitter)12 TokenStream (org.antlr.runtime.TokenStream)10 CommonTree (org.antlr.runtime.tree.CommonTree)10 ExprLexer (org.apache.drill.common.expression.parser.ExprLexer)10 ExprParser (org.apache.drill.common.expression.parser.ExprParser)10 InternalSimpleExpressionsTestLanguageLexer (org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer)8 File (java.io.File)4 ArrayList (java.util.ArrayList)4 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)4 Tree (org.antlr.runtime.tree.Tree)4 ExprParser.parse_return (org.apache.drill.common.expression.parser.ExprParser.parse_return)4 ApexLexer (apex.jorje.parser.impl.ApexLexer)3