Search in sources :

Example 6 with CommonTreeNodeStream

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

use of org.antlr.runtime.tree.CommonTreeNodeStream in project Apktool by iBotPeaches.

the class SmaliMod method assembleSmaliFile.

public static boolean assembleSmaliFile(File smaliFile, DexBuilder dexBuilder, boolean verboseErrors, boolean printTokens) throws IOException, RecognitionException {
    CommonTokenStream tokens;
    LexerErrorInterface lexer;
    InputStream is = new FileInputStream(smaliFile);
    InputStreamReader reader = new InputStreamReader(is, "UTF-8");
    lexer = new smaliFlexLexer(reader);
    ((smaliFlexLexer) lexer).setSourceFile(smaliFile);
    tokens = new CommonTokenStream((TokenSource) lexer);
    if (printTokens) {
        tokens.getTokens();
        for (int i = 0; i < tokens.size(); i++) {
            Token token = tokens.get(i);
            if (token.getChannel() == smaliParser.HIDDEN) {
                continue;
            }
            System.out.println(smaliParser.tokenNames[token.getType()] + ": " + token.getText());
        }
    }
    smaliParser parser = new smaliParser(tokens);
    parser.setVerboseErrors(verboseErrors);
    smaliParser.smali_file_return result = parser.smali_file();
    if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfSyntaxErrors() > 0) {
        return false;
    }
    CommonTree t = (CommonTree) result.getTree();
    CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
    treeStream.setTokenStream(tokens);
    smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
    dexGen.setVerboseErrors(verboseErrors);
    dexGen.setDexBuilder(dexBuilder);
    dexGen.smali_file();
    is.close();
    reader.close();
    return dexGen.getNumberOfSyntaxErrors() == 0;
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 8 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 9 with CommonTreeNodeStream

use of org.antlr.runtime.tree.CommonTreeNodeStream in project smali by JesusFreke.

the class SmaliTestUtils method compileSmali.

public static ClassDef compileSmali(String smaliText, int apiLevel) throws RecognitionException, IOException {
    CommonTokenStream tokens;
    LexerErrorInterface lexer;
    DexBuilder dexBuilder = new DexBuilder(Opcodes.forApi(apiLevel));
    Reader reader = new StringReader(smaliText);
    lexer = new smaliFlexLexer(reader);
    tokens = new CommonTokenStream((TokenSource) lexer);
    smaliParser parser = new smaliParser(tokens);
    parser.setVerboseErrors(true);
    parser.setAllowOdex(false);
    parser.setApiLevel(apiLevel);
    smaliParser.smali_file_return result = parser.smali_file();
    if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfSyntaxErrors() > 0) {
        throw new RuntimeException("Error occured while compiling text");
    }
    CommonTree t = result.getTree();
    CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
    treeStream.setTokenStream(tokens);
    smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
    dexGen.setApiLevel(apiLevel);
    dexGen.setVerboseErrors(true);
    dexGen.setDexBuilder(dexBuilder);
    dexGen.smali_file();
    if (dexGen.getNumberOfSyntaxErrors() > 0) {
        throw new RuntimeException("Error occured while compiling text");
    }
    MemoryDataStore dataStore = new MemoryDataStore();
    dexBuilder.writeTo(dataStore);
    DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.forApi(apiLevel), dataStore.getData());
    return Iterables.getFirst(dexFile.getClasses(), null);
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenSource(org.antlr.runtime.TokenSource) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) CommonTree(org.antlr.runtime.tree.CommonTree) MemoryDataStore(org.jf.dexlib2.writer.io.MemoryDataStore) Reader(java.io.Reader) StringReader(java.io.StringReader) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder) StringReader(java.io.StringReader) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 10 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)

Aggregations

CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)14 CommonTokenStream (org.antlr.runtime.CommonTokenStream)8 CommonTree (org.antlr.runtime.tree.CommonTree)8 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)5 FileInputStream (java.io.FileInputStream)3 InputStreamReader (java.io.InputStreamReader)3 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)3 WindowingException (com.sap.hadoop.windowing.WindowingException)2 RecognitionException (org.antlr.runtime.RecognitionException)2 Token (org.antlr.runtime.Token)2 TokenSource (org.antlr.runtime.TokenSource)2 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)2 LexerErrorInterface (org.jf.smali.LexerErrorInterface)2 org.jf.smali.smaliFlexLexer (org.jf.smali.smaliFlexLexer)2 org.jf.smali.smaliParser (org.jf.smali.smaliParser)2 org.jf.smali.smaliTreeWalker (org.jf.smali.smaliTreeWalker)2 SelectSpec (com.sap.hadoop.windowing.query2.specification.SelectSpec)1 SmaliDexException (com.wuntee.oter.exception.SmaliDexException)1 SmaliSyntaxException (com.wuntee.oter.exception.SmaliSyntaxException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1