Search in sources :

Example 16 with GrammarAST

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

the class SymbolChecks method checkForLabelConflict.

public void checkForLabelConflict(Rule r, GrammarAST labelID) {
    String name = labelID.getText();
    if (nameToRuleMap.containsKey(name)) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_RULE;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (tokenIDs.contains(name)) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_TOKEN;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (r.args != null && r.args.get(name) != null) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_ARG;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (r.retvals != null && r.retvals.get(name) != null) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_RETVAL;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
    if (r.locals != null && r.locals.get(name) != null) {
        ErrorType etype = ErrorType.LABEL_CONFLICTS_WITH_LOCAL;
        errMgr.grammarError(etype, g.fileName, labelID.token, name, r.name);
    }
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType)

Example 17 with GrammarAST

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

the class UseDefAnalyzer method getRuleDependencies.

public static Map<Rule, Set<Rule>> getRuleDependencies(Grammar g, Collection<Rule> rules) {
    Map<Rule, Set<Rule>> dependencies = new HashMap<Rule, Set<Rule>>();
    for (Rule r : rules) {
        List<GrammarAST> tokenRefs = r.ast.getNodesWithType(ANTLRParser.TOKEN_REF);
        for (GrammarAST tref : tokenRefs) {
            Set<Rule> calls = dependencies.get(r);
            if (calls == null) {
                calls = new HashSet<Rule>();
                dependencies.put(r, calls);
            }
            calls.add(g.getRule(tref.getText()));
        }
    }
    return dependencies;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule)

Example 18 with GrammarAST

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

the class ParserFactory method getComplexEBNFBlock.

@Override
public Choice getComplexEBNFBlock(GrammarAST ebnfRoot, List<CodeBlockForAlt> alts) {
    int ebnf = 0;
    if (ebnfRoot != null)
        ebnf = ebnfRoot.getType();
    Choice c = null;
    switch(ebnf) {
        case ANTLRParser.OPTIONAL:
            c = new OptionalBlock(this, ebnfRoot, alts);
            break;
        case ANTLRParser.CLOSURE:
            c = new StarBlock(this, ebnfRoot, alts);
            break;
        case ANTLRParser.POSITIVE_CLOSURE:
            c = new PlusBlock(this, ebnfRoot, alts);
            break;
    }
    return c;
}
Also used : Choice(org.antlr.v4.codegen.model.Choice) StarBlock(org.antlr.v4.codegen.model.StarBlock) LL1OptionalBlock(org.antlr.v4.codegen.model.LL1OptionalBlock) OptionalBlock(org.antlr.v4.codegen.model.OptionalBlock) PlusBlock(org.antlr.v4.codegen.model.PlusBlock)

Example 19 with GrammarAST

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

the class ParserFactory method getAddToListOpIfListLabelPresent.

public AddToLabelList getAddToListOpIfListLabelPresent(LabeledOp op, GrammarAST label) {
    AddToLabelList labelOp = null;
    if (label != null && label.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
        String listLabel = gen.getTarget().getListLabel(label.getText());
        labelOp = new AddToLabelList(this, listLabel, op.getLabels().get(0));
    }
    return labelOp;
}
Also used : AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 20 with GrammarAST

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

the class ParserFactory method needsImplicitLabel.

@Override
public boolean needsImplicitLabel(GrammarAST ID, LabeledOp op) {
    Alternative currentOuterMostAlt = getCurrentOuterMostAlt();
    boolean actionRefsAsToken = currentOuterMostAlt.tokenRefsInActions.containsKey(ID.getText());
    boolean actionRefsAsRule = currentOuterMostAlt.ruleRefsInActions.containsKey(ID.getText());
    return op.getLabels().isEmpty() && (actionRefsAsToken || actionRefsAsRule);
}
Also used : Alternative(org.antlr.v4.tool.Alternative)

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