Search in sources :

Example 6 with GrammarASTAdaptor

use of org.antlr.v4.parse.GrammarASTAdaptor in project antlr4 by antlr.

the class ParserATNFactory method _createATN.

protected void _createATN(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 7 with GrammarASTAdaptor

use of org.antlr.v4.parse.GrammarASTAdaptor 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 8 with GrammarASTAdaptor

use of org.antlr.v4.parse.GrammarASTAdaptor 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)

Example 9 with GrammarASTAdaptor

use of org.antlr.v4.parse.GrammarASTAdaptor in project antlr4 by antlr.

the class LeftRecursiveRuleTransformer method translateLeftRecursiveRules.

public void translateLeftRecursiveRules() {
    String language = g.getOptionString("language");
    // translate all recursive rules
    List<String> leftRecursiveRuleNames = new ArrayList<String>();
    for (Rule r : rules) {
        if (!Grammar.isTokenName(r.name)) {
            if (LeftRecursiveRuleAnalyzer.hasImmediateRecursiveRuleRefs(r.ast, r.name)) {
                boolean fitsPattern = translateLeftRecursiveRule(ast, (LeftRecursiveRule) r, language);
                if (fitsPattern) {
                    leftRecursiveRuleNames.add(r.name);
                } else {
                    // better given an error that non-conforming left-recursion exists
                    tool.errMgr.grammarError(ErrorType.NONCONFORMING_LR_RULE, g.fileName, ((GrammarAST) r.ast.getChild(0)).token, r.name);
                }
            }
        }
    }
    // update all refs to recursive rules to have [0] argument
    for (GrammarAST r : ast.getNodesWithType(ANTLRParser.RULE_REF)) {
        // must be rule def
        if (r.getParent().getType() == ANTLRParser.RULE)
            continue;
        // already has arg; must be in rewritten rule
        if (((GrammarASTWithOptions) r).getOptionString(PRECEDENCE_OPTION_NAME) != null)
            continue;
        if (leftRecursiveRuleNames.contains(r.getText())) {
            // found ref to recursive rule not already rewritten with arg
            ((GrammarASTWithOptions) r).setOption(PRECEDENCE_OPTION_NAME, (GrammarAST) new GrammarASTAdaptor().create(ANTLRParser.INT, "0"));
        }
    }
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ArrayList(java.util.ArrayList) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) GrammarASTWithOptions(org.antlr.v4.tool.ast.GrammarASTWithOptions)

Example 10 with GrammarASTAdaptor

use of org.antlr.v4.parse.GrammarASTAdaptor in project antlr4 by antlr.

the class Grammar method getStringLiteralAliasesFromLexerRules.

/** Return list of (TOKEN_NAME node, 'literal' node) pairs */
public static List<Pair<GrammarAST, GrammarAST>> getStringLiteralAliasesFromLexerRules(GrammarRootAST ast) {
    String[] patterns = { "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL)))", "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL ACTION)))", "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL SEMPRED)))", "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) .)))", "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) . .)))", "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) (LEXER_ACTION_CALL . .))))", "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) . (LEXER_ACTION_CALL . .))))", "(RULE %name:TOKEN_REF (BLOCK (LEXER_ALT_ACTION (ALT %lit:STRING_LITERAL) (LEXER_ACTION_CALL . .) .)))" };
    GrammarASTAdaptor adaptor = new GrammarASTAdaptor(ast.token.getInputStream());
    org.antlr.runtime.tree.TreeWizard wiz = new org.antlr.runtime.tree.TreeWizard(adaptor, ANTLRParser.tokenNames);
    List<Pair<GrammarAST, GrammarAST>> lexerRuleToStringLiteral = new ArrayList<Pair<GrammarAST, GrammarAST>>();
    List<GrammarAST> ruleNodes = ast.getNodesWithType(ANTLRParser.RULE);
    if (ruleNodes == null || ruleNodes.isEmpty())
        return null;
    for (GrammarAST r : ruleNodes) {
        //tool.log("grammar", r.toStringTree());
        //			System.out.println("chk: "+r.toStringTree());
        org.antlr.runtime.tree.Tree name = r.getChild(0);
        if (name.getType() == ANTLRParser.TOKEN_REF) {
            // check rule against patterns
            boolean isLitRule;
            for (String pattern : patterns) {
                isLitRule = defAlias(r, pattern, wiz, lexerRuleToStringLiteral);
                if (isLitRule)
                    break;
            }
        //				if ( !isLitRule ) System.out.println("no pattern matched");
        }
    }
    return lexerRuleToStringLiteral;
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ArrayList(java.util.ArrayList) GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) Pair(org.antlr.v4.runtime.misc.Pair)

Aggregations

GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)14 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)9 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)5 ArrayList (java.util.ArrayList)4 RecognitionException (org.antlr.runtime.RecognitionException)4 Pair (org.antlr.v4.runtime.misc.Pair)3 CharStream (org.antlr.runtime.CharStream)2 CommonTokenStream (org.antlr.runtime.CommonTokenStream)2 ParserRuleReturnScope (org.antlr.runtime.ParserRuleReturnScope)2 TreeVisitor (org.antlr.runtime.tree.TreeVisitor)2 TreeVisitorAction (org.antlr.runtime.tree.TreeVisitorAction)2 ToolANTLRParser (org.antlr.v4.parse.ToolANTLRParser)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 Rule (org.antlr.v4.tool.Rule)2 GrammarASTWithOptions (org.antlr.v4.tool.ast.GrammarASTWithOptions)2 GrammarRootAST (org.antlr.v4.tool.ast.GrammarRootAST)2 RuleAST (org.antlr.v4.tool.ast.RuleAST)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1