Search in sources :

Example 6 with CommonTokenStream

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

use of org.antlr.runtime.CommonTokenStream 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 8 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream 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)

Example 9 with CommonTokenStream

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

the class CQLFragmentParser method parseAnyUnhandled.

/**
     * Just call a parser method in {@link CqlParser} - does not do any error handling.
     */
public static <R> R parseAnyUnhandled(CQLParserFunction<R> parserFunction, String input) throws RecognitionException {
    // Lexer and parser
    ErrorCollector errorCollector = new ErrorCollector(input);
    CharStream stream = new ANTLRStringStream(input);
    CqlLexer lexer = new CqlLexer(stream);
    lexer.addErrorListener(errorCollector);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(errorCollector);
    // Parse the query string to a statement instance
    R r = parserFunction.parse(parser);
    // The errorCollector has queue up any errors that the lexer and parser may have encountered
    // along the way, if necessary, we turn the last error into exceptions here.
    errorCollector.throwFirstSyntaxError();
    return r;
}
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)

Example 10 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project freud by LMAX-Exchange.

the class JavaSourceJdomParser method parseJavaSourceToDocument.

private static Document parseJavaSourceToDocument(final Reader javaSourceReader) throws RecognitionException, IOException {
    JavaParser parser = new JavaParser(new CommonTokenStream(new JavaLexer(new ANTLRReaderStream(javaSourceReader))));
    final JdomTreeAdaptor treeAdaptor = new JdomTreeAdaptor(JAVA_SOURCE_ROOT_ELEMENT_NAME, JAVA_SOURCE_TOKEN_TYPES);
    parser.setTreeAdaptor(treeAdaptor);
    parser.compilationUnit();
    return treeAdaptor.getDocument();
}
Also used : JavaParser(org.freud.analysed.javasource.parser.JavaParser) CommonTokenStream(org.antlr.runtime.CommonTokenStream) JdomTreeAdaptor(org.freud.core.parser.JdomTreeAdaptor) JavaLexer(org.freud.analysed.javasource.parser.JavaLexer) ANTLRReaderStream(org.antlr.runtime.ANTLRReaderStream)

Aggregations

CommonTokenStream (org.antlr.runtime.CommonTokenStream)47 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)36 RecognitionException (org.antlr.runtime.RecognitionException)12 CommonTree (org.antlr.runtime.tree.CommonTree)12 Test (org.junit.Test)11 CommonToken (org.antlr.runtime.CommonToken)9 CharStream (org.antlr.runtime.CharStream)8 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)8 InternalSimpleExpressionsTestLanguageLexer (org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer)8 TokenStream (org.antlr.runtime.TokenStream)5 File (java.io.File)4 InputStreamReader (java.io.InputStreamReader)4 WindowingException (com.sap.hadoop.windowing.WindowingException)3 FileInputStream (java.io.FileInputStream)3 ANTLRReaderStream (org.antlr.runtime.ANTLRReaderStream)3 Token (org.antlr.runtime.Token)3 TokenSource (org.antlr.runtime.TokenSource)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