Search in sources :

Example 1 with CharStream

use of org.antlr.runtime.CharStream in project coffeescript-eclipse by adamschmideg.

the class FirstCommentIncludingXtextTokenStream method toString.

@Override
public String toString() {
    if (p == -1) {
        fillBuffer();
    }
    if (tokenSource instanceof Lexer) {
        if (tokens.isEmpty())
            return "";
        Token lastToken = (Token) tokens.get(tokens.size() - 1);
        if (lastToken instanceof CommonToken) {
            CommonToken commonStop = (CommonToken) lastToken;
            CharStream charStream = ((Lexer) tokenSource).getCharStream();
            String result = charStream.substring(0, commonStop.getStopIndex());
            return result;
        }
    }
    return super.toString();
}
Also used : Lexer(org.antlr.runtime.Lexer) Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) CommonToken(org.antlr.runtime.CommonToken) CharStream(org.antlr.runtime.CharStream)

Example 2 with CharStream

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

the class GrammarAST method dupTree.

public GrammarAST dupTree() {
    GrammarAST t = this;
    CharStream input = this.token.getInputStream();
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input);
    return (GrammarAST) adaptor.dupTree(t);
}
Also used : GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) CharStream(org.antlr.runtime.CharStream)

Example 3 with CharStream

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

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

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

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