Search in sources :

Example 1 with CommonTreeNodeStream

use of org.antlr.runtime.tree.CommonTreeNodeStream in project SQLWindowing by hbutani.

the class ParseUtils method parse.

public static QuerySpec parse(String query) throws WindowingException {
    Windowing2Lexer lexer;
    CommonTokenStream tokens;
    Windowing2Parser parser = null;
    CommonTree t;
    CommonTreeNodeStream nodes;
    QSpecBuilder2 qSpecBldr = null;
    String err;
    try {
        lexer = new Windowing2Lexer(new ANTLRStringStream(query));
        tokens = new CommonTokenStream(lexer);
        parser = new Windowing2Parser(tokens);
        parser.setTreeAdaptor(TranslateUtils.adaptor);
        t = (CommonTree) parser.query().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);
        qSpecBldr.query();
        err = qSpecBldr.getWindowingParseErrors();
        if (err != null) {
            throw new WindowingException(err);
        }
        return qSpecBldr.getQuerySpec();
    } 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) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 2 with CommonTreeNodeStream

use of org.antlr.runtime.tree.CommonTreeNodeStream in project wso2-synapse by wso2.

the class StreamingXPATHCompiler method parse.

/**
 * This will create the Custom XPATH Parser Components for a given XPATH. This will Use the Antlr Grammar XPATH1.0 for this process.
 *
 * @param source is the String for XPATH Expression
 * @return A Custom XPATH Parser Component Chain
 * @throws RecognitionException
 */
public static StreamingParser parse(String source) throws RecognitionException {
    XPath1Lexer lexer = new XPath1Lexer();
    lexer.setCharStream(new ANTLRStringStream(source));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    XPath1Parser parser = new XPath1Parser(tokens);
    org.apache.synapse.util.streaming_xpath.compiler.XPath1Parser.xpath_return r = parser.xpath();
    CommonTree t = (CommonTree) r.getTree();
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
    nodes.setTokenStream(tokens);
    XPath1Walker walker = new XPath1Walker(nodes);
    walker.xpath = source;
    return walker.xpath();
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 3 with CommonTreeNodeStream

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

the class ParserATNFactory method _createATN.

protected void _createATN(@NotNull Collection<Rule> rules) {
    createRuleStartAndStopATNStates();
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
    for (Rule r : rules) {
        // find rule's block
        GrammarAST blk = (GrammarAST) r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk);
        ATNBuilder b = new ATNBuilder(nodes, this);
        try {
            setCurrentRuleName(r.name);
            Handle h = b.ruleBlock(null);
            rule(r.ast, r.name, h);
        } catch (RecognitionException re) {
            ErrorManager.fatalInternalError("bad grammar AST structure", re);
        }
    }
}
Also used : ATNBuilder(org.antlr.v4.parse.ATNBuilder) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) RecognitionException(org.antlr.runtime.RecognitionException) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 4 with CommonTreeNodeStream

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

the class OutputModelController method buildNormalRuleFunction.

public void buildNormalRuleFunction(Rule r, RuleFunction function) {
    CodeGenerator gen = delegate.getGenerator();
    // TRIGGER factory functions for rule alts, elements
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream());
    GrammarAST blk = (GrammarAST) r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk);
    walker = new SourceGenTriggers(nodes, this);
    try {
        // walk AST of rule alts/elements
        function.code = DefaultOutputModelFactory.list(walker.block(null, null));
        function.hasLookaheadBlock = walker.hasLookaheadBlock;
    } catch (org.antlr.runtime.RecognitionException e) {
        e.printStackTrace(System.err);
    }
    function.ctxType = delegate.getTarget().getRuleFunctionContextStructName(function);
    function.postamble = rulePostamble(function, r);
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 5 with CommonTreeNodeStream

use of org.antlr.runtime.tree.CommonTreeNodeStream in project atlas by alibaba.

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();
    return dexGen.getNumberOfSyntaxErrors() == 0;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenSource(org.antlr.runtime.TokenSource) InputStreamReader(java.io.InputStreamReader) CommonTree(org.antlr.runtime.tree.CommonTree) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Token(org.antlr.runtime.Token) org.jf.smali.smaliFlexLexer(org.jf.smali.smaliFlexLexer) FileInputStream(java.io.FileInputStream) org.jf.smali.smaliParser(org.jf.smali.smaliParser) org.jf.smali.smaliTreeWalker(org.jf.smali.smaliTreeWalker) LexerErrorInterface(org.jf.smali.LexerErrorInterface) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Aggregations

CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)20 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)10 CommonTokenStream (org.antlr.runtime.CommonTokenStream)9 CommonTree (org.antlr.runtime.tree.CommonTree)9 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)4 RecognitionException (org.antlr.runtime.RecognitionException)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 FileInputStream (java.io.FileInputStream)3 InputStreamReader (java.io.InputStreamReader)3 WindowingException (com.sap.hadoop.windowing.WindowingException)2 CharStream (org.antlr.runtime.CharStream)2 Token (org.antlr.runtime.Token)2 TokenSource (org.antlr.runtime.TokenSource)2 FrequencySet (org.antlr.v4.misc.FrequencySet)2 ATNBuilder (org.antlr.v4.parse.ATNBuilder)2 BlockSetTransformer (org.antlr.v4.parse.BlockSetTransformer)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 Rule (org.antlr.v4.tool.Rule)2 LexerErrorInterface (org.jf.smali.LexerErrorInterface)2 org.jf.smali.smaliFlexLexer (org.jf.smali.smaliFlexLexer)2