Search in sources :

Example 16 with CommonTreeNodeStream

use of org.antlr.runtime.tree.CommonTreeNodeStream 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 17 with CommonTreeNodeStream

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

Example 18 with CommonTreeNodeStream

use of org.antlr.runtime.tree.CommonTreeNodeStream in project drools by kiegroup.

the class DSLTokenizedMappingFile method buildFileMappingWalker.

private DSLMapWalker buildFileMappingWalker(final List<ParserError> errors, CharStream stream) throws RecognitionException {
    DSLMapLexer lexer = new DSLMapLexer(stream);
    CommonTokenStream tokens = new CommonTokenStream();
    tokens.setTokenSource(lexer);
    DSLMapParser parser = new DSLMapParser(tokens);
    DSLMapParser.mapping_file_return example = parser.mapping_file();
    CommonTree tree = (CommonTree) example.getTree();
    // logger.info(tree.toStringTree());
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    DSLMapWalker walker = new DSLMapWalker(nodes);
    errors.addAll(lexer.getErrors());
    errors.addAll(parser.getErrors());
    return walker;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 19 with CommonTreeNodeStream

use of org.antlr.runtime.tree.CommonTreeNodeStream in project antlr4 by antlr.

the class GrammarTransformPipeline method reduceBlocksToSets.

public void reduceBlocksToSets(GrammarAST root) {
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(new GrammarASTAdaptor(), root);
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
    BlockSetTransformer transformer = new BlockSetTransformer(nodes, g);
    transformer.setTreeAdaptor(adaptor);
    transformer.downup(root);
}
Also used : BlockSetTransformer(org.antlr.v4.parse.BlockSetTransformer) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 20 with CommonTreeNodeStream

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

CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)27 CommonTokenStream (org.antlr.runtime.CommonTokenStream)14 CommonTree (org.antlr.runtime.tree.CommonTree)13 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)10 RecognitionException (org.antlr.runtime.RecognitionException)6 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)5 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 org.jf.smali.smaliFlexLexer (org.jf.smali.smaliFlexLexer)4 org.jf.smali.smaliParser (org.jf.smali.smaliParser)4 org.jf.smali.smaliTreeWalker (org.jf.smali.smaliTreeWalker)4 FileInputStream (java.io.FileInputStream)3 InputStreamReader (java.io.InputStreamReader)3 StringReader (java.io.StringReader)3 Token (org.antlr.runtime.Token)3 TokenSource (org.antlr.runtime.TokenSource)3 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)3 DexBuilder (org.jf.dexlib2.writer.builder.DexBuilder)3 WindowingException (com.sap.hadoop.windowing.WindowingException)2 File (java.io.File)2 Reader (java.io.Reader)2