Search in sources :

Example 1 with RuleRefAST

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

the class BasicSemanticChecks method checkElementOptions.

/** Check option is appropriate for elem; parent of ID is ELEMENT_OPTIONS */
boolean checkElementOptions(GrammarASTWithOptions elem, GrammarAST ID, GrammarAST valueAST) {
    if (checkAssocElementOption && ID != null && "assoc".equals(ID.getText())) {
        if (elem.getType() != ANTLRParser.ALT) {
            Token optionID = ID.token;
            String fileName = optionID.getInputStream().getSourceName();
            g.tool.errMgr.grammarError(ErrorType.UNRECOGNIZED_ASSOC_OPTION, fileName, optionID, currentRuleName);
        }
    }
    if (elem instanceof RuleRefAST) {
        return checkRuleRefOptions((RuleRefAST) elem, ID, valueAST);
    }
    if (elem instanceof TerminalAST) {
        return checkTokenOptions((TerminalAST) elem, ID, valueAST);
    }
    if (elem.getType() == ANTLRParser.ACTION) {
        return false;
    }
    if (elem.getType() == ANTLRParser.SEMPRED) {
        Token optionID = ID.token;
        String fileName = optionID.getInputStream().getSourceName();
        if (valueAST != null && !Grammar.semPredOptions.contains(optionID.getText())) {
            g.tool.errMgr.grammarError(ErrorType.ILLEGAL_OPTION, fileName, optionID, optionID.getText());
            return false;
        }
    }
    return false;
}
Also used : RuleRefAST(org.antlr.v4.tool.ast.RuleRefAST) Token(org.antlr.runtime.Token) TerminalAST(org.antlr.v4.tool.ast.TerminalAST)

Example 2 with RuleRefAST

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

the class LeftRecursiveRuleAnalyzer method addPrecedenceArgToRules.

public AltAST addPrecedenceArgToRules(AltAST t, int prec) {
    if (t == null)
        return null;
    // get all top-level rule refs from ALT
    List<GrammarAST> outerAltRuleRefs = t.getNodesWithTypePreorderDFS(IntervalSet.of(RULE_REF));
    for (GrammarAST x : outerAltRuleRefs) {
        RuleRefAST rref = (RuleRefAST) x;
        boolean recursive = rref.getText().equals(ruleName);
        boolean rightmost = rref == outerAltRuleRefs.get(outerAltRuleRefs.size() - 1);
        if (recursive && rightmost) {
            GrammarAST dummyValueNode = new GrammarAST(new CommonToken(ANTLRParser.INT, "" + prec));
            rref.setOption(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME, dummyValueNode);
        }
    }
    return t;
}
Also used : RuleRefAST(org.antlr.v4.tool.ast.RuleRefAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) CommonToken(org.antlr.runtime.CommonToken)

Aggregations

RuleRefAST (org.antlr.v4.tool.ast.RuleRefAST)2 CommonToken (org.antlr.runtime.CommonToken)1 Token (org.antlr.runtime.Token)1 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)1 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)1