Search in sources :

Example 6 with ParserRuleReturnScope

use of org.antlr.runtime.ParserRuleReturnScope in project drools by kiegroup.

the class DSLMapParser method variable_reference.

// $ANTLR start "variable_reference"
// src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:228:1: variable_reference : lc= LEFT_CURLY name= variable_reference_expr rc= RIGHT_CURLY -> { hasSpaceBefore && hasSpaceAfter}? VT_SPACE ^( VT_VAR_REF LITERAL[$name.start,$name.text] ) VT_SPACE -> { hasSpaceBefore && !hasSpaceAfter}? VT_SPACE ^( VT_VAR_REF LITERAL[$name.start,$name.text] ) -> {!hasSpaceBefore && hasSpaceAfter}? ^( VT_VAR_REF LITERAL[$name.start,$name.text] ) VT_SPACE -> ^( VT_VAR_REF LITERAL[$name.start,$name.text] ) ;
public final DSLMapParser.variable_reference_return variable_reference() throws RecognitionException {
    DSLMapParser.variable_reference_return retval = new DSLMapParser.variable_reference_return();
    retval.start = input.LT(1);
    Object root_0 = null;
    Token lc = null;
    Token rc = null;
    ParserRuleReturnScope name = null;
    Object lc_tree = null;
    Object rc_tree = null;
    RewriteRuleTokenStream stream_RIGHT_CURLY = new RewriteRuleTokenStream(adaptor, "token RIGHT_CURLY");
    RewriteRuleTokenStream stream_LEFT_CURLY = new RewriteRuleTokenStream(adaptor, "token LEFT_CURLY");
    RewriteRuleSubtreeStream stream_variable_reference_expr = new RewriteRuleSubtreeStream(adaptor, "rule variable_reference_expr");
    boolean hasSpaceBefore = false;
    boolean hasSpaceAfter = false;
    String text = "";
    try {
        // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:234:5: (lc= LEFT_CURLY name= variable_reference_expr rc= RIGHT_CURLY -> { hasSpaceBefore && hasSpaceAfter}? VT_SPACE ^( VT_VAR_REF LITERAL[$name.start,$name.text] ) VT_SPACE -> { hasSpaceBefore && !hasSpaceAfter}? VT_SPACE ^( VT_VAR_REF LITERAL[$name.start,$name.text] ) -> {!hasSpaceBefore && hasSpaceAfter}? ^( VT_VAR_REF LITERAL[$name.start,$name.text] ) VT_SPACE -> ^( VT_VAR_REF LITERAL[$name.start,$name.text] ) )
        // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:234:7: lc= LEFT_CURLY name= variable_reference_expr rc= RIGHT_CURLY
        {
            lc = (Token) match(input, LEFT_CURLY, FOLLOW_LEFT_CURLY_in_variable_reference1389);
            if (state.failed)
                return retval;
            if (state.backtracking == 0)
                stream_LEFT_CURLY.add(lc);
            if (state.backtracking == 0) {
                CommonToken back2 = (CommonToken) input.LT(-2);
                if (back2 != null && back2.getStopIndex() < ((CommonToken) lc).getStartIndex() - 1)
                    hasSpaceBefore = true;
            }
            pushFollow(FOLLOW_variable_reference_expr_in_variable_reference1409);
            name = variable_reference_expr();
            state._fsp--;
            if (state.failed)
                return retval;
            if (state.backtracking == 0)
                stream_variable_reference_expr.add(name.getTree());
            rc = (Token) match(input, RIGHT_CURLY, FOLLOW_RIGHT_CURLY_in_variable_reference1413);
            if (state.failed)
                return retval;
            if (state.backtracking == 0)
                stream_RIGHT_CURLY.add(rc);
            if (state.backtracking == 0) {
                if (((CommonToken) rc).getStopIndex() < ((CommonToken) input.LT(1)).getStartIndex() - 1)
                    hasSpaceAfter = true;
            }
            // wildcard labels:
            if (state.backtracking == 0) {
                retval.tree = root_0;
                RewriteRuleSubtreeStream stream_retval = new RewriteRuleSubtreeStream(adaptor, "rule retval", retval != null ? retval.getTree() : null);
                root_0 = (Object) adaptor.nil();
                // 241:5: -> { hasSpaceBefore && hasSpaceAfter}? VT_SPACE ^( VT_VAR_REF LITERAL[$name.start,$name.text] ) VT_SPACE
                if (hasSpaceBefore && hasSpaceAfter) {
                    adaptor.addChild(root_0, (Object) adaptor.create(VT_SPACE, "VT_SPACE"));
                    // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:241:54: ^( VT_VAR_REF LITERAL[$name.start,$name.text] )
                    {
                        Object root_1 = (Object) adaptor.nil();
                        root_1 = (Object) adaptor.becomeRoot((Object) adaptor.create(VT_VAR_REF, "VT_VAR_REF"), root_1);
                        adaptor.addChild(root_1, (Object) adaptor.create(LITERAL, (name != null ? (name.start) : null), (name != null ? input.toString(name.start, name.stop) : null)));
                        adaptor.addChild(root_0, root_1);
                    }
                    adaptor.addChild(root_0, (Object) adaptor.create(VT_SPACE, "VT_SPACE"));
                } else // 242:5: -> { hasSpaceBefore && !hasSpaceAfter}? VT_SPACE ^( VT_VAR_REF LITERAL[$name.start,$name.text] )
                if (hasSpaceBefore && !hasSpaceAfter) {
                    adaptor.addChild(root_0, (Object) adaptor.create(VT_SPACE, "VT_SPACE"));
                    // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:242:54: ^( VT_VAR_REF LITERAL[$name.start,$name.text] )
                    {
                        Object root_1 = (Object) adaptor.nil();
                        root_1 = (Object) adaptor.becomeRoot((Object) adaptor.create(VT_VAR_REF, "VT_VAR_REF"), root_1);
                        adaptor.addChild(root_1, (Object) adaptor.create(LITERAL, (name != null ? (name.start) : null), (name != null ? input.toString(name.start, name.stop) : null)));
                        adaptor.addChild(root_0, root_1);
                    }
                } else // 243:5: -> {!hasSpaceBefore && hasSpaceAfter}? ^( VT_VAR_REF LITERAL[$name.start,$name.text] ) VT_SPACE
                if (!hasSpaceBefore && hasSpaceAfter) {
                    // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:243:54: ^( VT_VAR_REF LITERAL[$name.start,$name.text] )
                    {
                        Object root_1 = (Object) adaptor.nil();
                        root_1 = (Object) adaptor.becomeRoot((Object) adaptor.create(VT_VAR_REF, "VT_VAR_REF"), root_1);
                        adaptor.addChild(root_1, (Object) adaptor.create(LITERAL, (name != null ? (name.start) : null), (name != null ? input.toString(name.start, name.stop) : null)));
                        adaptor.addChild(root_0, root_1);
                    }
                    adaptor.addChild(root_0, (Object) adaptor.create(VT_SPACE, "VT_SPACE"));
                } else // 244:5: -> ^( VT_VAR_REF LITERAL[$name.start,$name.text] )
                {
                    // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:244:54: ^( VT_VAR_REF LITERAL[$name.start,$name.text] )
                    {
                        Object root_1 = (Object) adaptor.nil();
                        root_1 = (Object) adaptor.becomeRoot((Object) adaptor.create(VT_VAR_REF, "VT_VAR_REF"), root_1);
                        adaptor.addChild(root_1, (Object) adaptor.create(LITERAL, (name != null ? (name.start) : null), (name != null ? input.toString(name.start, name.stop) : null)));
                        adaptor.addChild(root_0, root_1);
                    }
                }
                retval.tree = root_0;
            }
        }
        retval.stop = input.LT(-1);
        if (state.backtracking == 0) {
            retval.tree = (Object) adaptor.rulePostProcessing(root_0);
            adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop);
        }
    } catch (RecognitionException re) {
        reportError(re);
        recover(input, re);
        retval.tree = (Object) adaptor.errorNode(input, retval.start, input.LT(-1), re);
    } finally {
    // do for sure before leaving
    }
    return retval;
}
Also used : RewriteRuleTokenStream(org.antlr.runtime.tree.RewriteRuleTokenStream) RewriteRuleSubtreeStream(org.antlr.runtime.tree.RewriteRuleSubtreeStream) Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) CommonToken(org.antlr.runtime.CommonToken) RecognitionException(org.antlr.runtime.RecognitionException)

Example 7 with ParserRuleReturnScope

use of org.antlr.runtime.ParserRuleReturnScope in project antlr4 by tunnelvisionlabs.

the class LeftRecursiveRuleTransformer method parseArtificialRule.

public RuleAST parseArtificialRule(final Grammar g, String ruleText) {
    ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText));
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(lexer.getCharStream());
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    lexer.tokens = tokens;
    ToolANTLRParser p = new ToolANTLRParser(tokens, tool);
    p.setTreeAdaptor(adaptor);
    Token ruleStart = null;
    try {
        ParserRuleReturnScope r = p.rule();
        RuleAST tree = (RuleAST) r.getTree();
        ruleStart = (Token) r.getStart();
        GrammarTransformPipeline.setGrammarPtr(g, tree);
        GrammarTransformPipeline.augmentTokensWithOriginalPosition(g, tree);
        return tree;
    } catch (Exception e) {
        tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, e, ruleStart, "error parsing rule created during left-recursion detection: " + ruleText);
    }
    return null;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) RuleAST(org.antlr.v4.tool.ast.RuleAST) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ANTLRLexer(org.antlr.v4.parse.ANTLRLexer) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) Token(org.antlr.runtime.Token) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) ToolANTLRParser(org.antlr.v4.parse.ToolANTLRParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 8 with ParserRuleReturnScope

use of org.antlr.runtime.ParserRuleReturnScope in project antlr4 by tunnelvisionlabs.

the class Tool method parse.

public GrammarRootAST parse(String fileName, CharStream in) {
    try {
        GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in);
        ToolANTLRLexer lexer = new ToolANTLRLexer(in, this);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        lexer.tokens = tokens;
        ToolANTLRParser p = new ToolANTLRParser(tokens, this);
        p.setTreeAdaptor(adaptor);
        try {
            ParserRuleReturnScope r = p.grammarSpec();
            GrammarAST root = (GrammarAST) r.getTree();
            if (root instanceof GrammarRootAST) {
                ((GrammarRootAST) root).hasErrors = lexer.getNumberOfSyntaxErrors() > 0 || p.getNumberOfSyntaxErrors() > 0;
                assert ((GrammarRootAST) root).tokenStream == tokens;
                if (grammarOptions != null) {
                    ((GrammarRootAST) root).cmdLineOptions = grammarOptions;
                }
                return ((GrammarRootAST) root);
            }
        } catch (v3TreeGrammarException e) {
            errMgr.grammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location);
        }
        return null;
    } catch (RecognitionException re) {
        // TODO: do we gen errors now?
        ErrorManager.internalError("can't generate this message at moment; antlr recovers");
    }
    return null;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) ToolANTLRLexer(org.antlr.v4.parse.ToolANTLRLexer) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) org.antlr.v4.parse.v3TreeGrammarException(org.antlr.v4.parse.v3TreeGrammarException) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) ToolANTLRParser(org.antlr.v4.parse.ToolANTLRParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 9 with ParserRuleReturnScope

use of org.antlr.runtime.ParserRuleReturnScope in project antlr4 by antlr.

the class Tool method parse.

public GrammarRootAST parse(String fileName, CharStream in) {
    try {
        GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in);
        ToolANTLRLexer lexer = new ToolANTLRLexer(in, this);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        lexer.tokens = tokens;
        ToolANTLRParser p = new ToolANTLRParser(tokens, this);
        p.setTreeAdaptor(adaptor);
        try {
            ParserRuleReturnScope r = p.grammarSpec();
            GrammarAST root = (GrammarAST) r.getTree();
            if (root instanceof GrammarRootAST) {
                ((GrammarRootAST) root).hasErrors = lexer.getNumberOfSyntaxErrors() > 0 || p.getNumberOfSyntaxErrors() > 0;
                assert ((GrammarRootAST) root).tokenStream == tokens;
                if (grammarOptions != null) {
                    ((GrammarRootAST) root).cmdLineOptions = grammarOptions;
                }
                return ((GrammarRootAST) root);
            }
        } catch (v3TreeGrammarException e) {
            errMgr.grammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location);
        }
        return null;
    } catch (RecognitionException re) {
        // TODO: do we gen errors now?
        ErrorManager.internalError("can't generate this message at moment; antlr recovers");
    }
    return null;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) ToolANTLRLexer(org.antlr.v4.parse.ToolANTLRLexer) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) org.antlr.v4.parse.v3TreeGrammarException(org.antlr.v4.parse.v3TreeGrammarException) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) ToolANTLRParser(org.antlr.v4.parse.ToolANTLRParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 10 with ParserRuleReturnScope

use of org.antlr.runtime.ParserRuleReturnScope in project antlr4 by antlr.

the class LeftRecursiveRuleTransformer method parseArtificialRule.

public RuleAST parseArtificialRule(final Grammar g, String ruleText) {
    ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText));
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(lexer.getCharStream());
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    lexer.tokens = tokens;
    ToolANTLRParser p = new ToolANTLRParser(tokens, tool);
    p.setTreeAdaptor(adaptor);
    Token ruleStart = null;
    try {
        ParserRuleReturnScope r = p.rule();
        RuleAST tree = (RuleAST) r.getTree();
        ruleStart = (Token) r.getStart();
        GrammarTransformPipeline.setGrammarPtr(g, tree);
        GrammarTransformPipeline.augmentTokensWithOriginalPosition(g, tree);
        return tree;
    } catch (Exception e) {
        tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, e, ruleStart, "error parsing rule created during left-recursion detection: " + ruleText);
    }
    return null;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) RuleAST(org.antlr.v4.tool.ast.RuleAST) CommonTokenStream(org.antlr.runtime.CommonTokenStream) ANTLRLexer(org.antlr.v4.parse.ANTLRLexer) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) Token(org.antlr.runtime.Token) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) ToolANTLRParser(org.antlr.v4.parse.ToolANTLRParser) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

ParserRuleReturnScope (org.antlr.runtime.ParserRuleReturnScope)35 RecognitionException (org.antlr.runtime.RecognitionException)35 RewriteRuleSubtreeStream (org.antlr.runtime.tree.RewriteRuleSubtreeStream)18 Token (org.antlr.runtime.Token)16 CommonToken (org.antlr.runtime.CommonToken)14 EarlyExitException (org.antlr.runtime.EarlyExitException)10 NoViableAltException (org.antlr.runtime.NoViableAltException)10 RewriteEarlyExitException (org.antlr.runtime.tree.RewriteEarlyExitException)10 RewriteRuleTokenStream (org.antlr.runtime.tree.RewriteRuleTokenStream)8 TokenRewriteStream (org.antlr.runtime.TokenRewriteStream)5 CommonTokenStream (org.antlr.runtime.CommonTokenStream)4 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)4 ToolANTLRParser (org.antlr.v4.parse.ToolANTLRParser)4 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)2 RewriteEmptyStreamException (org.antlr.runtime.tree.RewriteEmptyStreamException)2 ANTLRLexer (org.antlr.v4.parse.ANTLRLexer)2 ToolANTLRLexer (org.antlr.v4.parse.ToolANTLRLexer)2 org.antlr.v4.parse.v3TreeGrammarException (org.antlr.v4.parse.v3TreeGrammarException)2 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)2 GrammarRootAST (org.antlr.v4.tool.ast.GrammarRootAST)2