Search in sources :

Example 6 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project gradle by gradle.

the class GrammarTest method canUseGeneratedGrammar.

@Test
public void canUseGeneratedGrammar() throws Exception {
    ANTLRStringStream in = new ANTLRStringStream("1+2");
    CalculatorLexer lexer = new CalculatorLexer(in);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    CalculatorParser parser = new CalculatorParser(tokens);
    parser.add();
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) Test(org.junit.Test)

Example 7 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project SQLWindowing by hbutani.

the class ParseUtils method parse.

public static QuerySpec parse(String query) throws WindowingException {
    Windowing2Lexer lexer;
    CommonTokenStream tokens;
    Windowing2Parser parser = null;
    CommonTree t;
    CommonTreeNodeStream nodes;
    QSpecBuilder2 qSpecBldr = null;
    String err;
    try {
        lexer = new Windowing2Lexer(new ANTLRStringStream(query));
        tokens = new CommonTokenStream(lexer);
        parser = new Windowing2Parser(tokens);
        parser.setTreeAdaptor(TranslateUtils.adaptor);
        t = (CommonTree) parser.query().getTree();
        err = parser.getWindowingParseErrors();
        if (err != null) {
            throw new WindowingException(err);
        }
    } catch (WindowingException we) {
        throw we;
    } catch (Throwable te) {
        err = parser.getWindowingParseErrors();
        if (err != null) {
            throw new WindowingException(err);
        }
        throw new WindowingException("Parse Error:" + te.toString(), te);
    }
    TranslateUtils.unescapeStringLiterals((ASTNode) t);
    try {
        nodes = new CommonTreeNodeStream(t);
        nodes.setTokenStream(tokens);
        qSpecBldr = new QSpecBuilder2(nodes);
        qSpecBldr.query();
        err = qSpecBldr.getWindowingParseErrors();
        if (err != null) {
            throw new WindowingException(err);
        }
        return qSpecBldr.getQuerySpec();
    } catch (WindowingException we) {
        throw we;
    } catch (Throwable te) {
        err = qSpecBldr.getWindowingParseErrors();
        if (err != null) {
            throw new WindowingException(err);
        }
        throw new WindowingException("Parse Error:" + te.toString(), te);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) WindowingException(com.sap.hadoop.windowing.WindowingException) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 8 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project binnavi by google.

the class CFilterRuleParser method parse.

/**
   * Parses a filter string.
   * 
   * @param filterString The filter string to parse.
   * 
   * @return The root node of the parsed AST.
   * 
   * @throws RecognitionException Thrown if parsing the filter string failed.
   */
public static IAbstractNode parse(final String filterString) throws RecognitionException {
    final CharStream charStream = new ANTLRStringStream(filterString);
    final FilterLexer lexer = new FilterLexer(charStream);
    final CommonTokenStream tokens = new CommonTokenStream();
    tokens.setTokenSource(lexer);
    final FilterParser parser = new FilterParser(tokens);
    parser.setTreeAdaptor(adaptor);
    final FilterParser.prog_return parserResult = parser.prog();
    final CommonTree ast = (CommonTree) parserResult.getTree();
    return convert(ast);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) FilterParser(com.google.security.zynamics.binnavi.parsers.filter.FilterParser) CharStream(org.antlr.runtime.CharStream) FilterLexer(com.google.security.zynamics.binnavi.parsers.filter.FilterLexer)

Example 9 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project binnavi by google.

the class BreakpointConditionParser method parse.

/**
   * Parses a breakpoint condition string.
   *
   * @param conditionString The condition string to parse.
   *
   * @return The parsed breakpoint condition tree.
   *
   * @throws RecognitionException Thrown if the condition string could not be parsed.
   * @throws MaybeNullException Thrown if an empty condition string is passed to the function.
   */
public static ConditionNode parse(final String conditionString) throws RecognitionException, MaybeNullException {
    if (conditionString.trim().isEmpty()) {
        throw new MaybeNullException();
    }
    final CharStream charStream = new ANTLRStringStream(conditionString);
    final ConditionLexer lexer = new ConditionLexer(charStream);
    final CommonTokenStream tokens = new CommonTokenStream();
    tokens.setTokenSource(lexer);
    final ConditionParser parser = new ConditionParser(tokens);
    parser.setTreeAdaptor(adaptor);
    try {
        final ConditionParser.prog_return parserResult = parser.prog();
        final CommonTree ast = (CommonTree) parserResult.getTree();
        if (parser.input.index() < parser.input.size()) {
            throw new RecognitionException();
        }
        return convert(ast);
    } catch (final IllegalArgumentException e) {
        throw new RecognitionException();
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) ConditionParser(com.google.security.zynamics.binnavi.parsers.BreakpointCondition.ConditionParser) CommonTree(org.antlr.runtime.tree.CommonTree) ConditionLexer(com.google.security.zynamics.binnavi.parsers.BreakpointCondition.ConditionLexer) CharStream(org.antlr.runtime.CharStream) RecognitionException(org.antlr.runtime.RecognitionException)

Example 10 with ANTLRStringStream

use of org.antlr.runtime.ANTLRStringStream in project cassandra by apache.

the class CqlParserTest method testRemoveErrorListener.

@Test
public void testRemoveErrorListener() throws Exception {
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();
    CharStream stream = new ANTLRStringStream("SELECT * FORM test;");
    CqlLexer lexer = new CqlLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(firstCounter);
    parser.addErrorListener(secondCounter);
    parser.removeErrorListener(secondCounter);
    parser.query();
    assertEquals(1, firstCounter.count);
    assertEquals(0, secondCounter.count);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenStream(org.antlr.runtime.TokenStream) CharStream(org.antlr.runtime.CharStream) Test(org.junit.Test)

Aggregations

ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)60 CommonTokenStream (org.antlr.runtime.CommonTokenStream)36 Test (org.junit.Test)20 CommonToken (org.antlr.runtime.CommonToken)15 Token (org.antlr.runtime.Token)13 CharStream (org.antlr.runtime.CharStream)11 Lexer (org.eclipse.xtext.parser.antlr.Lexer)10 RecognitionException (org.antlr.runtime.RecognitionException)9 InternalSimpleExpressionsTestLanguageLexer (org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer)8 CommonTree (org.antlr.runtime.tree.CommonTree)6 ActionSplitter (org.antlr.v4.parse.ActionSplitter)6 TokenStream (org.antlr.runtime.TokenStream)5 InternalXtendLexer (org.eclipse.xtend.core.parser.antlr.internal.InternalXtendLexer)4 WindowingException (com.sap.hadoop.windowing.WindowingException)3 ExprLexer (org.apache.drill.common.expression.parser.ExprLexer)3 ExprParser (org.apache.drill.common.expression.parser.ExprParser)3 CeylonLexer (com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer)2 CeylonParser (com.redhat.ceylon.compiler.typechecker.parser.CeylonParser)2 LineMap (com.sun.tools.javac.util.Position.LineMap)2 File (java.io.File)2