Search in sources :

Example 36 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 37 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)

Example 38 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project drill by apache.

the class PhysicalOpUnitTestBase method parseExpr.

@Override
protected LogicalExpression parseExpr(String expr) {
    ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ExprParser parser = new ExprParser(tokens);
    try {
        return parser.parse().e;
    } catch (RecognitionException e) {
        throw new RuntimeException("Error parsing expression: " + expr);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ExprLexer(org.apache.drill.common.expression.parser.ExprLexer) ExprParser(org.apache.drill.common.expression.parser.ExprParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 39 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project drill by apache.

the class TestEvaluationVisitor method getExpr.

private LogicalExpression getExpr(String expr) throws Exception {
    ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    //    tokens.fill();
    //    for(Token t : (List<Token>) tokens.getTokens()){
    //      System.out.println(t + "" + t.getType());
    //    }
    //    tokens.rewind();
    ExprParser parser = new ExprParser(tokens);
    parse_return ret = parser.parse();
    return ret.e;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ExprLexer(org.apache.drill.common.expression.parser.ExprLexer) ExprParser.parse_return(org.apache.drill.common.expression.parser.ExprParser.parse_return) ExprParser(org.apache.drill.common.expression.parser.ExprParser)

Example 40 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project drill by apache.

the class ExecTest method parseExpr.

protected LogicalExpression parseExpr(String expr) throws RecognitionException {
    final ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final ExprParser parser = new ExprParser(tokens);
    final ExprParser.parse_return ret = parser.parse();
    return ret.e;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ExprLexer(org.apache.drill.common.expression.parser.ExprLexer) ExprParser(org.apache.drill.common.expression.parser.ExprParser)

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