Search in sources :

Example 6 with CharStream

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

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

the class CqlParserTest method testAddErrorListener.

@Test
public void testAddErrorListener() throws Exception {
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();
    CharStream stream = new ANTLRStringStream("SELECT * FORM FROM test");
    CqlLexer lexer = new CqlLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(firstCounter);
    parser.addErrorListener(secondCounter);
    // By default CqlParser should recover from the syntax error by removing FORM
    // but as recoverFromMismatchedToken and recover have been overloaded, it will not
    // and the returned ParsedStatement will be null.
    assertNull(parser.query());
    // Only one error must be reported (mismatched: FORM).
    assertEquals(1, firstCounter.count);
    assertEquals(1, 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 8 with CharStream

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

the class DebuggerMemoryExpressionParser method parse.

/**
   * Parses a single memory expression string.
   *
   * @param memoryExpression The memory expression string to parse.
   *
   * @return The parsed memory expression tree.
   *
   * @throws RecognitionException Thrown if parsing failed.
   */
public static MemoryExpressionElement parse(final String memoryExpression) throws RecognitionException {
    final CharStream charStream = new ANTLRStringStream(memoryExpression);
    final MemoryExpressionLexer lexer = new MemoryExpressionLexer(charStream);
    final CommonTokenStream tokens = new CommonTokenStream();
    tokens.setTokenSource(lexer);
    final MemoryExpressionParser parser = new MemoryExpressionParser(tokens) {

        @Override
        public void recover(final IntStream input, final RecognitionException exception) {
        // Nothing to do
        }

        @Override
        public Object recoverFromMismatchedToken(final IntStream input, final int ttype, final org.antlr.runtime.BitSet follow) throws RecognitionException {
            throw new MismatchedTokenException(ttype, input);
        }

        @Override
        public void reportError(final RecognitionException exception) {
        // Nothing to do
        }
    };
    parser.setTreeAdaptor(adaptor);
    final MemoryExpressionParser.prog_return parserResult = parser.prog();
    final CommonTree ast = (CommonTree) parserResult.getTree();
    return convert(ast);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) MemoryExpressionParser(com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionParser) CommonTokenStream(org.antlr.runtime.CommonTokenStream) MemoryExpressionLexer(com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionLexer) CommonTree(org.antlr.runtime.tree.CommonTree) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) IntStream(org.antlr.runtime.IntStream) CharStream(org.antlr.runtime.CharStream) RecognitionException(org.antlr.runtime.RecognitionException)

Example 9 with CharStream

use of org.antlr.runtime.CharStream in project antlr4 by antlr.

the class GrammarAST method toTokenString.

public String toTokenString() {
    CharStream input = this.token.getInputStream();
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input);
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, this);
    StringBuilder buf = new StringBuilder();
    GrammarAST o = (GrammarAST) nodes.LT(1);
    int type = adaptor.getType(o);
    while (type != Token.EOF) {
        buf.append(" ");
        buf.append(o.getText());
        nodes.consume();
        o = (GrammarAST) nodes.LT(1);
        type = adaptor.getType(o);
    }
    return buf.toString();
}
Also used : GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) CharStream(org.antlr.runtime.CharStream) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Aggregations

CharStream (org.antlr.runtime.CharStream)9 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)6 CommonTokenStream (org.antlr.runtime.CommonTokenStream)6 TokenStream (org.antlr.runtime.TokenStream)3 CommonTree (org.antlr.runtime.tree.CommonTree)3 RecognitionException (org.antlr.runtime.RecognitionException)2 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)2 Test (org.junit.Test)2 MaybeNullException (com.google.security.zynamics.binnavi.Exceptions.MaybeNullException)1 ConditionLexer (com.google.security.zynamics.binnavi.parsers.BreakpointCondition.ConditionLexer)1 ConditionParser (com.google.security.zynamics.binnavi.parsers.BreakpointCondition.ConditionParser)1 FilterLexer (com.google.security.zynamics.binnavi.parsers.filter.FilterLexer)1 FilterParser (com.google.security.zynamics.binnavi.parsers.filter.FilterParser)1 MemoryExpressionLexer (com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionLexer)1 MemoryExpressionParser (com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionParser)1 CommonToken (org.antlr.runtime.CommonToken)1 IntStream (org.antlr.runtime.IntStream)1 Lexer (org.antlr.runtime.Lexer)1 MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)1 Token (org.antlr.runtime.Token)1