Search in sources :

Example 26 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project yamcs-studio by yamcs.

the class FormulaAst method createParser.

/**
 * Creates a parser for the given text.
 *
 * @param text the string to be parsed
 * @return the new parser
 */
static FormulaParser createParser(String text) {
    CharStream stream = new ANTLRStringStream(text);
    FormulaLexer lexer = new FormulaLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    return new FormulaParser(tokenStream);
}
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 27 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project ceylon-compiler by ceylon.

the class IssuesTests_1500_1999 method benchmarkParse.

private void benchmarkParse(String file) throws Exception {
    String readSource = readFile(new File(getPackagePath(), file));
    String source = readSource.toString();
    char[] chars = source.toCharArray();
    LineMap map = Position.makeLineMap(chars, chars.length, false);
    System.err.println(map.hashCode());
    ANTLRStringStream input = new ANTLRStringStream(source);
    CeylonLexer lexer = new CeylonLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    CeylonParser parser = new CeylonParser(tokens);
//        CompilationUnit cu = parser.compilationUnit();
//        System.err.println(cu.hashCode());
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) LineMap(com.sun.tools.javac.util.Position.LineMap) CeylonLexer(com.redhat.ceylon.compiler.typechecker.parser.CeylonLexer) File(java.io.File) CeylonParser(com.redhat.ceylon.compiler.typechecker.parser.CeylonParser)

Example 28 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project che by eclipse.

the class ANTLRExpressionParser method parse.

private void parse() throws RecognitionException {
    JavaLexer lexer = new JavaLexer(new ANTLRStringStream(getExpression()));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JavaParser parser = new JavaParser(tokens);
    nodes = new CommonTreeNodeStream(parser.expression().getTree());
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 29 with CommonTokenStream

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

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

the class ParseUtils method parseSelect.

public static SelectSpec parseSelect(String selectExprStr) throws WindowingException {
    Windowing2Lexer lexer;
    CommonTokenStream tokens;
    Windowing2Parser parser = null;
    CommonTree t;
    CommonTreeNodeStream nodes;
    QSpecBuilder2 qSpecBldr = null;
    String err;
    try {
        lexer = new Windowing2Lexer(new ANTLRStringStream(selectExprStr));
        tokens = new CommonTokenStream(lexer);
        parser = new Windowing2Parser(tokens);
        parser.setTreeAdaptor(TranslateUtils.adaptor);
        t = (CommonTree) parser.select().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);
        SelectSpec selectSpec = qSpecBldr.select();
        err = qSpecBldr.getWindowingParseErrors();
        if (err != null) {
            throw new WindowingException(err);
        }
        return selectSpec;
    } 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) SelectSpec(com.sap.hadoop.windowing.query2.specification.SelectSpec) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

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