Search in sources :

Example 61 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class Grammar method defAlias.

protected static boolean defAlias(GrammarAST r, String pattern, org.antlr.runtime.tree.TreeWizard wiz, List<Pair<GrammarAST, GrammarAST>> lexerRuleToStringLiteral) {
    HashMap<String, Object> nodes = new HashMap<String, Object>();
    if (wiz.parse(r, pattern, nodes)) {
        GrammarAST litNode = (GrammarAST) nodes.get("lit");
        GrammarAST nameNode = (GrammarAST) nodes.get("name");
        Pair<GrammarAST, GrammarAST> pair = new Pair<GrammarAST, GrammarAST>(nameNode, litNode);
        lexerRuleToStringLiteral.add(pair);
        return true;
    }
    return false;
}
Also used : OrderedHashMap(org.antlr.v4.misc.OrderedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Pair(org.antlr.v4.runtime.misc.Pair)

Example 62 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST 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)

Example 63 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class BasicSemanticChecks method checkNumRules.

void checkNumRules(GrammarAST rulesNode) {
    if (rulesNode.getChildCount() == 0) {
        GrammarAST root = (GrammarAST) rulesNode.getParent();
        GrammarAST IDNode = (GrammarAST) root.getChild(0);
        g.tool.errMgr.grammarError(ErrorType.NO_RULES, g.fileName, null, IDNode.getText(), g);
    }
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST)

Example 64 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class BasicSemanticChecks method finishPrequels.

@Override
public void finishPrequels(GrammarAST firstPrequel) {
    if (firstPrequel == null)
        return;
    GrammarAST parent = (GrammarAST) firstPrequel.parent;
    List<GrammarAST> options = parent.getAllChildrenWithType(OPTIONS);
    List<GrammarAST> imports = parent.getAllChildrenWithType(IMPORT);
    List<GrammarAST> tokens = parent.getAllChildrenWithType(TOKENS_SPEC);
    checkNumPrequels(options, imports, tokens);
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST)

Example 65 with GrammarAST

use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.

the class RuleCollector method discoverRule.

@Override
public void discoverRule(RuleAST rule, GrammarAST ID, List<GrammarAST> modifiers, ActionAST arg, ActionAST returns, GrammarAST thrws, GrammarAST options, ActionAST locals, List<GrammarAST> actions, GrammarAST block) {
    int numAlts = block.getChildCount();
    Rule r;
    if (LeftRecursiveRuleAnalyzer.hasImmediateRecursiveRuleRefs(rule, ID.getText())) {
        r = new LeftRecursiveRule(g, ID.getText(), rule);
    } else {
        r = new Rule(g, ID.getText(), rule, numAlts);
    }
    rules.put(r.name, r);
    if (arg != null) {
        r.args = ScopeParser.parseTypedArgList(arg, arg.getText(), g);
        r.args.type = AttributeDict.DictType.ARG;
        r.args.ast = arg;
        arg.resolver = r.alt[currentOuterAltNumber];
    }
    if (returns != null) {
        r.retvals = ScopeParser.parseTypedArgList(returns, returns.getText(), g);
        r.retvals.type = AttributeDict.DictType.RET;
        r.retvals.ast = returns;
    }
    if (locals != null) {
        r.locals = ScopeParser.parseTypedArgList(locals, locals.getText(), g);
        r.locals.type = AttributeDict.DictType.LOCAL;
        r.locals.ast = locals;
    }
    for (GrammarAST a : actions) {
        // a = ^(AT ID ACTION)
        ActionAST action = (ActionAST) a.getChild(1);
        r.namedActions.put(a.getChild(0).getText(), action);
        action.resolver = r;
    }
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Aggregations

GrammarAST (org.antlr.v4.tool.ast.GrammarAST)55 Rule (org.antlr.v4.tool.Rule)20 ATNState (org.antlr.v4.runtime.atn.ATNState)15 ArrayList (java.util.ArrayList)12 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)12 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)12 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)12 Grammar (org.antlr.v4.tool.Grammar)8 Decl (org.antlr.v4.codegen.model.decl.Decl)7 ActionAST (org.antlr.v4.tool.ast.ActionAST)7 AltAST (org.antlr.v4.tool.ast.AltAST)7 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)7 LinkedHashMap (java.util.LinkedHashMap)6 Token (org.antlr.runtime.Token)6 RuleAST (org.antlr.v4.tool.ast.RuleAST)6 HashMap (java.util.HashMap)5 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)5 Pair (org.antlr.v4.runtime.misc.Pair)5 LexerGrammar (org.antlr.v4.tool.LexerGrammar)5 GrammarASTWithOptions (org.antlr.v4.tool.ast.GrammarASTWithOptions)5