Search in sources :

Example 36 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project antlr4 by antlr.

the class BaseCppTest method writeParserTestFile.

protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug, boolean trace) {
    if (!parserStartRuleName.endsWith(")"))
        parserStartRuleName += "()";
    ST outputFileST = new ST("#include \\<iostream>\n" + "\n" + "#include \"antlr4-runtime.h\"\n" + "#include \"<lexerName>.h\"\n" + "#include \"<parserName>.h\"\n" + "\n" + "using namespace antlr4;\n" + "\n" + "class TreeShapeListener : public tree::ParseTreeListener {\n" + "public:\n" + "  void visitTerminal(tree::TerminalNode *) override {}\n" + "  void visitErrorNode(tree::ErrorNode *) override {}\n" + "  void exitEveryRule(ParserRuleContext *) override {}\n" + "  void enterEveryRule(ParserRuleContext *ctx) override {\n" + "    for (auto child : ctx->children) {\n" + "      tree::ParseTree *parent = child->parent;\n" + "      ParserRuleContext *rule = dynamic_cast\\<ParserRuleContext *>(parent);\n" + "      if (rule != ctx) {\n" + "        throw \"Invalid parse tree shape detected.\";\n" + "      }\n" + "    }\n" + "  }\n" + "};\n" + "\n" + "\n" + "int main(int argc, const char* argv[]) {\n" + "  ANTLRFileStream input(argv[1]);\n" + "  <lexerName> lexer(&input);\n" + "  CommonTokenStream tokens(&lexer);\n" + "<createParser>" + "\n" + "  tree::ParseTree *tree = parser.<parserStartRuleName>;\n" + "  TreeShapeListener listener;\n" + "  tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);\n" + "\n" + "  return 0;\n" + "}\n");
    String stSource = "  <parserName> parser(&tokens);\n";
    if (debug) {
        stSource += "  DiagnosticErrorListener errorListener;\n";
        stSource += "  parser.addErrorListener(&errorListener);\n";
    }
    if (trace)
        stSource += "  parser.setTrace(true);\n";
    ST createParserST = new ST(stSource);
    outputFileST.add("createParser", createParserST);
    outputFileST.add("parserName", parserName);
    outputFileST.add("lexerName", lexerName);
    outputFileST.add("listenerName", listenerName);
    outputFileST.add("visitorName", visitorName);
    outputFileST.add("parserStartRuleName", parserStartRuleName);
    writeFile(tmpdir, "Test.cpp", outputFileST.render());
}
Also used : ST(org.stringtemplate.v4.ST) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

Example 37 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project antlr4 by antlr.

the class TestRig method process.

protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, CharStream input) throws IOException, IllegalAccessException, InvocationTargetException, PrintException {
    lexer.setInputStream(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();
    if (showTokens) {
        for (Token tok : tokens.getTokens()) {
            if (tok instanceof CommonToken) {
                System.out.println(((CommonToken) tok).toString(lexer));
            } else {
                System.out.println(tok.toString());
            }
        }
    }
    if (startRuleName.equals(LEXER_START_RULE_NAME))
        return;
    if (diagnostics) {
        parser.addErrorListener(new DiagnosticErrorListener());
        parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
    }
    if (printTree || gui || psFile != null) {
        parser.setBuildParseTree(true);
    }
    if (SLL) {
        // overrides diagnostics
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    }
    parser.setTokenStream(tokens);
    parser.setTrace(trace);
    try {
        Method startRule = parserClass.getMethod(startRuleName);
        ParserRuleContext tree = (ParserRuleContext) startRule.invoke(parser, (Object[]) null);
        if (printTree) {
            System.out.println(tree.toStringTree(parser));
        }
        if (gui) {
            Trees.inspect(tree, parser);
        }
        if (psFile != null) {
            // Generate postscript
            Trees.save(tree, parser, psFile);
        }
    } catch (NoSuchMethodException nsme) {
        System.err.println("No method for rule " + startRuleName + " or it has arguments");
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DiagnosticErrorListener(org.antlr.v4.runtime.DiagnosticErrorListener) CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token) Method(java.lang.reflect.Method) CommonToken(org.antlr.v4.runtime.CommonToken)

Example 38 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project oxTrust by GluuFederation.

the class ScimFilterParser method expression.

private ExpressionContext expression(int _p) throws RecognitionException {
    ParserRuleContext _parentctx = _ctx;
    int _parentState = getState();
    ExpressionContext _localctx = new ExpressionContext(_ctx, _parentState);
    ExpressionContext _prevctx = _localctx;
    int _startState = 2;
    enterRecursionRule(_localctx, 2, RULE_expression, _p);
    int _la;
    try {
        int _alt;
        enterOuterAlt(_localctx, 1);
        {
            setState(92);
            _errHandler.sync(this);
            switch(getInterpreter().adaptivePredict(_input, 11, _ctx)) {
                case 1:
                    {
                        _localctx = new NOT_EXPRContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(17);
                        match(NOT);
                        setState(19);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(18);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(21);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 1, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(23);
                        expression(9);
                    }
                    break;
                case 2:
                    {
                        _localctx = new ATTR_OPER_EXPRContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(24);
                        match(ATTRNAME);
                        setState(26);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(25);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(28);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 2, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(30);
                        operator();
                        setState(32);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(31);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(34);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 3, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(36);
                        expression(4);
                    }
                    break;
                case 3:
                    {
                        _localctx = new ATTR_PRContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(38);
                        match(ATTRNAME);
                        setState(40);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(39);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(42);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 4, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(44);
                        match(PR);
                    }
                    break;
                case 4:
                    {
                        _localctx = new ATTR_OPER_CRITERIAContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(45);
                        match(ATTRNAME);
                        setState(47);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(46);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(49);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 5, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(51);
                        operator();
                        setState(53);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(52);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(55);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 6, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(57);
                        criteria();
                    }
                    break;
                case 5:
                    {
                        _localctx = new LPAREN_EXPR_RPARENContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(59);
                        match(LPAREN);
                        setState(63);
                        _errHandler.sync(this);
                        _alt = getInterpreter().adaptivePredict(_input, 7, _ctx);
                        while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
                            if (_alt == 1 + 1) {
                                {
                                    {
                                        setState(60);
                                        match(WS);
                                    }
                                }
                            }
                            setState(65);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 7, _ctx);
                        }
                        setState(66);
                        expression(0);
                        setState(70);
                        _errHandler.sync(this);
                        _alt = getInterpreter().adaptivePredict(_input, 8, _ctx);
                        while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
                            if (_alt == 1 + 1) {
                                {
                                    {
                                        setState(67);
                                        match(WS);
                                    }
                                }
                            }
                            setState(72);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 8, _ctx);
                        }
                        setState(73);
                        match(RPAREN);
                    }
                    break;
                case 6:
                    {
                        _localctx = new LBRAC_EXPR_RBRACContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(75);
                        match(ATTRNAME);
                        setState(76);
                        match(LBRAC);
                        setState(80);
                        _errHandler.sync(this);
                        _alt = getInterpreter().adaptivePredict(_input, 9, _ctx);
                        while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
                            if (_alt == 1 + 1) {
                                {
                                    {
                                        setState(77);
                                        match(WS);
                                    }
                                }
                            }
                            setState(82);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 9, _ctx);
                        }
                        setState(83);
                        expression(0);
                        setState(87);
                        _errHandler.sync(this);
                        _alt = getInterpreter().adaptivePredict(_input, 10, _ctx);
                        while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
                            if (_alt == 1 + 1) {
                                {
                                    {
                                        setState(84);
                                        match(WS);
                                    }
                                }
                            }
                            setState(89);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 10, _ctx);
                        }
                        setState(90);
                        match(RBRAC);
                    }
                    break;
            }
            _ctx.stop = _input.LT(-1);
            setState(136);
            _errHandler.sync(this);
            _alt = getInterpreter().adaptivePredict(_input, 19, _ctx);
            while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
                if (_alt == 1) {
                    if (_parseListeners != null)
                        triggerExitRuleEvent();
                    _prevctx = _localctx;
                    {
                        setState(134);
                        _errHandler.sync(this);
                        switch(getInterpreter().adaptivePredict(_input, 18, _ctx)) {
                            case 1:
                                {
                                    _localctx = new EXPR_AND_EXPRContext(new ExpressionContext(_parentctx, _parentState));
                                    pushNewRecursionContext(_localctx, _startState, RULE_expression);
                                    setState(94);
                                    if (!(precpred(_ctx, 8)))
                                        throw new FailedPredicateException(this, "precpred(_ctx, 8)");
                                    setState(96);
                                    _errHandler.sync(this);
                                    _alt = 1 + 1;
                                    do {
                                        switch(_alt) {
                                            case 1 + 1:
                                                {
                                                    {
                                                        setState(95);
                                                        match(WS);
                                                    }
                                                }
                                                break;
                                            default:
                                                throw new NoViableAltException(this);
                                        }
                                        setState(98);
                                        _errHandler.sync(this);
                                        _alt = getInterpreter().adaptivePredict(_input, 12, _ctx);
                                    } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                                    setState(100);
                                    match(AND);
                                    setState(102);
                                    _errHandler.sync(this);
                                    _alt = 1 + 1;
                                    do {
                                        switch(_alt) {
                                            case 1 + 1:
                                                {
                                                    {
                                                        setState(101);
                                                        match(WS);
                                                    }
                                                }
                                                break;
                                            default:
                                                throw new NoViableAltException(this);
                                        }
                                        setState(104);
                                        _errHandler.sync(this);
                                        _alt = getInterpreter().adaptivePredict(_input, 13, _ctx);
                                    } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                                    setState(106);
                                    expression(9);
                                }
                                break;
                            case 2:
                                {
                                    _localctx = new EXPR_OR_EXPRContext(new ExpressionContext(_parentctx, _parentState));
                                    pushNewRecursionContext(_localctx, _startState, RULE_expression);
                                    setState(107);
                                    if (!(precpred(_ctx, 7)))
                                        throw new FailedPredicateException(this, "precpred(_ctx, 7)");
                                    setState(109);
                                    _errHandler.sync(this);
                                    _alt = 1 + 1;
                                    do {
                                        switch(_alt) {
                                            case 1 + 1:
                                                {
                                                    {
                                                        setState(108);
                                                        match(WS);
                                                    }
                                                }
                                                break;
                                            default:
                                                throw new NoViableAltException(this);
                                        }
                                        setState(111);
                                        _errHandler.sync(this);
                                        _alt = getInterpreter().adaptivePredict(_input, 14, _ctx);
                                    } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                                    setState(113);
                                    match(OR);
                                    setState(115);
                                    _errHandler.sync(this);
                                    _la = _input.LA(1);
                                    do {
                                        {
                                            {
                                                setState(114);
                                                match(WS);
                                            }
                                        }
                                        setState(117);
                                        _errHandler.sync(this);
                                        _la = _input.LA(1);
                                    } while (_la == WS);
                                    setState(119);
                                    expression(8);
                                }
                                break;
                            case 3:
                                {
                                    _localctx = new EXPR_OPER_EXPRContext(new ExpressionContext(_parentctx, _parentState));
                                    pushNewRecursionContext(_localctx, _startState, RULE_expression);
                                    setState(120);
                                    if (!(precpred(_ctx, 6)))
                                        throw new FailedPredicateException(this, "precpred(_ctx, 6)");
                                    setState(122);
                                    _errHandler.sync(this);
                                    _alt = 1 + 1;
                                    do {
                                        switch(_alt) {
                                            case 1 + 1:
                                                {
                                                    {
                                                        setState(121);
                                                        match(WS);
                                                    }
                                                }
                                                break;
                                            default:
                                                throw new NoViableAltException(this);
                                        }
                                        setState(124);
                                        _errHandler.sync(this);
                                        _alt = getInterpreter().adaptivePredict(_input, 16, _ctx);
                                    } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                                    setState(126);
                                    operator();
                                    setState(128);
                                    _errHandler.sync(this);
                                    _alt = 1 + 1;
                                    do {
                                        switch(_alt) {
                                            case 1 + 1:
                                                {
                                                    {
                                                        setState(127);
                                                        match(WS);
                                                    }
                                                }
                                                break;
                                            default:
                                                throw new NoViableAltException(this);
                                        }
                                        setState(130);
                                        _errHandler.sync(this);
                                        _alt = getInterpreter().adaptivePredict(_input, 17, _ctx);
                                    } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                                    setState(132);
                                    expression(7);
                                }
                                break;
                        }
                    }
                }
                setState(138);
                _errHandler.sync(this);
                _alt = getInterpreter().adaptivePredict(_input, 19, _ctx);
            }
        }
    } catch (RecognitionException re) {
        _localctx.exception = re;
        _errHandler.reportError(this, re);
        _errHandler.recover(this, re);
    } finally {
        unrollRecursionContexts(_parentctx);
    }
    return _localctx;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) FailedPredicateException(org.antlr.v4.runtime.FailedPredicateException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 39 with ParserRuleContext

use of org.antlr.v4.runtime.ParserRuleContext in project drools by kiegroup.

the class ParserHelper method getOriginalText.

public static String getOriginalText(ParserRuleContext ctx) {
    int a = ctx.start.getStartIndex();
    int b = ctx.stop.getStopIndex();
    Interval interval = new Interval(a, b);
    return ctx.getStart().getInputStream().getText(interval);
}
Also used : Interval(org.antlr.v4.runtime.misc.Interval)

Aggregations

ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)24 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)6 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)5 ParseTree (org.antlr.v4.runtime.tree.ParseTree)5 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)4 SmaliParser (com.googlecode.d2j.smali.antlr4.SmaliParser)3 BitSet (java.util.BitSet)3 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)3 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)3 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)3 Token (org.antlr.v4.runtime.Token)3 ATN (org.antlr.v4.runtime.atn.ATN)3 DFAState (org.antlr.v4.runtime.dfa.DFAState)3 Interval (org.antlr.v4.runtime.misc.Interval)3 ErrorNode (org.antlr.v4.runtime.tree.ErrorNode)3 ArrayList (java.util.ArrayList)2 CommonToken (org.antlr.v4.runtime.CommonToken)2 ATNState (org.antlr.v4.runtime.atn.ATNState)2 DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)2 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)2