Search in sources :

Example 31 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project binnavi by google.

the class DebuggerMemoryExpressionParser method parse.

/**
   * Parses a single memory expression string.
   *
   * @param memoryExpression The memory expression string to parse.
   *
   * @return The parsed memory expression tree.
   *
   * @throws RecognitionException Thrown if parsing failed.
   */
public static MemoryExpressionElement parse(final String memoryExpression) throws RecognitionException {
    final CharStream charStream = new ANTLRStringStream(memoryExpression);
    final MemoryExpressionLexer lexer = new MemoryExpressionLexer(charStream);
    final CommonTokenStream tokens = new CommonTokenStream();
    tokens.setTokenSource(lexer);
    final MemoryExpressionParser parser = new MemoryExpressionParser(tokens) {

        @Override
        public void recover(final IntStream input, final RecognitionException exception) {
        // Nothing to do
        }

        @Override
        public Object recoverFromMismatchedToken(final IntStream input, final int ttype, final org.antlr.runtime.BitSet follow) throws RecognitionException {
            throw new MismatchedTokenException(ttype, input);
        }

        @Override
        public void reportError(final RecognitionException exception) {
        // Nothing to do
        }
    };
    parser.setTreeAdaptor(adaptor);
    final MemoryExpressionParser.prog_return parserResult = parser.prog();
    final CommonTree ast = (CommonTree) parserResult.getTree();
    return convert(ast);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) MemoryExpressionParser(com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionParser) CommonTokenStream(org.antlr.runtime.CommonTokenStream) MemoryExpressionLexer(com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionLexer) CommonTree(org.antlr.runtime.tree.CommonTree) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) IntStream(org.antlr.runtime.IntStream) CharStream(org.antlr.runtime.CharStream) RecognitionException(org.antlr.runtime.RecognitionException)

Example 32 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project dex2jar by pxb1988.

the class Jasmin2JarCmd method assemble1.

private void assemble1(Path file, Path output) throws IOException {
    try (BufferedReader bufferedReader = Files.newBufferedReader(file, Charset.forName(encoding))) {
        ANTLRStringStream is = new ANTLRReaderStream(bufferedReader);
        is.name = file.toString();
        JasminLexer lexer = new JasminLexer(is);
        CommonTokenStream ts = new CommonTokenStream(lexer);
        JasminParser parser = new JasminParser(ts);
        parser.rebuildLine = autogenLines;
        ClassWriter cw = new ClassWriter(noComputeMax ? 0 : ClassWriter.COMPUTE_MAXS);
        ClassNode cn = parser.parse();
        if (cn.version == 0) {
            cn.version = versions[classVersion];
        }
        if (dump) {
            new JasminDumper(new PrintWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8), true)).dump(cn);
        }
        cn.accept(cw);
        Path clzFile = output.resolve(cn.name.replace('.', '/') + ".class");
        createParentDirectories(clzFile);
        Files.write(clzFile, cw.toByteArray());
    } catch (RecognitionException e) {
        System.err.println("Fail to assemble " + file);
        e.printStackTrace();
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ClassNode(org.objectweb.asm.tree.ClassNode) ClassWriter(org.objectweb.asm.ClassWriter) ANTLRReaderStream(org.antlr.runtime.ANTLRReaderStream) RecognitionException(org.antlr.runtime.RecognitionException)

Example 33 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream in project dex2jar by pxb1988.

the class Jasmins method parse.

public static ClassNode parse(String fileName, Reader bufferedReader) throws IOException, RecognitionException {
    ANTLRStringStream is = new ANTLRReaderStream(bufferedReader);
    is.name = fileName;
    JasminLexer lexer = new JasminLexer(is);
    CommonTokenStream ts = new CommonTokenStream(lexer);
    JasminParser parser = new JasminParser(ts);
    return parser.parse();
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ANTLRReaderStream(org.antlr.runtime.ANTLRReaderStream)

Example 34 with CommonTokenStream

use of org.antlr.runtime.CommonTokenStream 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 35 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)

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