Search in sources :

Example 51 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 52 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)

Example 53 with CommonTokenStream

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

the class CssJdomParser method parseCssToDocument.

private static Document parseCssToDocument(final Reader reader) throws RecognitionException, IOException {
    CssParser parser = new CssParser(new CommonTokenStream(new CssLexer(new ANTLRReaderStream(reader))));
    final JdomTreeAdaptor treeAdaptor = new JdomTreeAdaptor(CSS_ROOT_ELEMENT_NAME, CSS_TOKEN_TYPES);
    parser.setTreeAdaptor(treeAdaptor);
    parser.stylesheet();
    return treeAdaptor.getDocument();
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) JdomTreeAdaptor(org.freud.core.parser.JdomTreeAdaptor) ANTLRReaderStream(org.antlr.runtime.ANTLRReaderStream) CssParser(org.freud.analysed.css.parser.CssParser) CssLexer(org.freud.analysed.css.parser.CssLexer)

Example 54 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project rsense by m2ym.

the class AnnotationHelper method parseAnnotation.

public static TypeAnnotation parseAnnotation(String annot, int lineno) {
    if (annot.startsWith("#%")) {
        ANTLRStringStream in = new ANTLRStringStream(annot.substring(2));
        in.setLine(lineno);
        TypeAnnotationLexer lex = new TypeAnnotationLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lex);
        TypeAnnotationParser parser = new TypeAnnotationParser(tokens);
        try {
            return parser.type();
        } catch (RecognitionException e) {
        }
    }
    return null;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) TypeAnnotationLexer(org.cx4a.rsense.parser.TypeAnnotationLexer) RecognitionException(org.antlr.runtime.RecognitionException) TypeAnnotationParser(org.cx4a.rsense.parser.TypeAnnotationParser)

Example 55 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project otertool by wuntee.

the class SmaliWorkshop method buildDex.

private static void buildDex(File smaliSourceDirectory, File destination) throws RecognitionException, SmaliSyntaxException, SmaliDexException, IOException {
    DexFile dexFile = new DexFile();
    // Load files into set
    logger.debug("Loading smali files");
    LinkedHashSet<File> filesToProcess = new LinkedHashSet<File>();
    getSmaliFilesInDir(smaliSourceDirectory, filesToProcess);
    // Process each file
    logger.debug("Processing files");
    for (File file : filesToProcess) {
        logger.debug("Processing: " + file.getAbsolutePath());
        FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
        LexerErrorInterface lexer = new smaliFlexLexer(reader);
        ((smaliFlexLexer) lexer).setSourceFile(file);
        CommonTokenStream tokens = new CommonTokenStream((TokenSource) lexer);
        smaliParser parser = new smaliParser(tokens);
        smaliParser.smali_file_return result = parser.smali_file();
        // Errors
        if (parser.getNumberOfSyntaxErrors() > 0) {
            throw new SmaliSyntaxException(file, parser.getNumberOfSyntaxErrors());
        }
        if (lexer.getNumberOfSyntaxErrors() > 0) {
            throw new SmaliSyntaxException(file, lexer.getNumberOfSyntaxErrors());
        }
        CommonTree t = (CommonTree) result.getTree();
        CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
        treeStream.setTokenStream(tokens);
        smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
        dexGen.dexFile = dexFile;
        dexGen.smali_file();
        if (dexGen.getNumberOfSyntaxErrors() > 0) {
            throw new SmaliDexException(file, dexGen.getNumberOfSyntaxErrors());
        }
    }
    // Calculate signatures and write file
    logger.debug("Writing dex file.");
    dexFile.place();
    ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput();
    dexFile.writeTo(out);
    byte[] bytes = out.toByteArray();
    DexFile.calcSignature(bytes);
    DexFile.calcChecksum(bytes);
    FileOutputStream fileOutputStream = new FileOutputStream(destination);
    fileOutputStream.write(bytes);
    fileOutputStream.close();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CommonTokenStream(org.antlr.runtime.CommonTokenStream) InputStreamReader(java.io.InputStreamReader) CommonTree(org.antlr.runtime.tree.CommonTree) SmaliDexException(com.wuntee.oter.exception.SmaliDexException) ByteArrayAnnotatedOutput(org.jf.dexlib.Util.ByteArrayAnnotatedOutput) SmaliSyntaxException(com.wuntee.oter.exception.SmaliSyntaxException) org.jf.smali.smaliFlexLexer(org.jf.smali.smaliFlexLexer) DexFile(org.jf.dexlib.DexFile) FileInputStream(java.io.FileInputStream) org.jf.smali.smaliParser(org.jf.smali.smaliParser) FileOutputStream(java.io.FileOutputStream) org.jf.smali.smaliTreeWalker(org.jf.smali.smaliTreeWalker) LexerErrorInterface(org.jf.smali.LexerErrorInterface) File(java.io.File) DexFile(org.jf.dexlib.DexFile) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Aggregations

CommonTokenStream (org.antlr.runtime.CommonTokenStream)93 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)62 RecognitionException (org.antlr.runtime.RecognitionException)25 CharStream (org.antlr.runtime.CharStream)22 CommonTree (org.antlr.runtime.tree.CommonTree)21 TokenStream (org.antlr.runtime.TokenStream)17 File (java.io.File)12 Test (org.junit.Test)12 CommonToken (org.antlr.runtime.CommonToken)10 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)10 ExprLexer (org.apache.drill.common.expression.parser.ExprLexer)10 ExprParser (org.apache.drill.common.expression.parser.ExprParser)10 FileInputStream (java.io.FileInputStream)9 InputStream (java.io.InputStream)8 ANTLRInputStream (org.antlr.runtime.ANTLRInputStream)8 InternalSimpleExpressionsTestLanguageLexer (org.eclipse.xtext.testlanguages.parser.antlr.internal.InternalSimpleExpressionsTestLanguageLexer)8 InputStreamReader (java.io.InputStreamReader)6 Token (org.antlr.runtime.Token)6 JPA2Lexer (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer)5 JPA2Parser (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser)5