Search in sources :

Example 21 with GrammarAST

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

the class ParserFactory method tokenRef.

@Override
public List<SrcOp> tokenRef(GrammarAST ID, GrammarAST labelAST, GrammarAST args) {
    MatchToken matchOp = new MatchToken(this, (TerminalAST) ID);
    if (labelAST != null) {
        String label = labelAST.getText();
        RuleFunction rf = getCurrentRuleFunction();
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            // add Token _X and List<Token> X decls
            // adds _X
            defineImplicitLabel(ID, matchOp);
            TokenListDecl l = getTokenListLabelDecl(label);
            rf.addContextDecl(ID.getAltLabel(), l);
        } else {
            Decl d = getTokenLabelDecl(label);
            matchOp.labels.add(d);
            rf.addContextDecl(ID.getAltLabel(), d);
        }
    //			Decl d = getTokenLabelDecl(label);
    //			((MatchToken)matchOp).labels.add(d);
    //			getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), d);
    //			if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) {
    //				TokenListDecl l = getTokenListLabelDecl(label);
    //				getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), l);
    //			}
    }
    if (controller.needsImplicitLabel(ID, matchOp))
        defineImplicitLabel(ID, matchOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST);
    return list(matchOp, listLabelOp);
}
Also used : TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) MatchToken(org.antlr.v4.codegen.model.MatchToken) RuleFunction(org.antlr.v4.codegen.model.RuleFunction) LeftRecursiveRuleFunction(org.antlr.v4.codegen.model.LeftRecursiveRuleFunction) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 22 with GrammarAST

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

the class ParserFactory method ruleRef.

@Override
public List<SrcOp> ruleRef(GrammarAST ID, GrammarAST label, GrammarAST args) {
    InvokeRule invokeOp = new InvokeRule(this, ID, label);
    // If no manual label and action refs as token/rule not label, we need to define implicit label
    if (controller.needsImplicitLabel(ID, invokeOp))
        defineImplicitLabel(ID, invokeOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(invokeOp, label);
    return list(invokeOp, listLabelOp);
}
Also used : InvokeRule(org.antlr.v4.codegen.model.InvokeRule) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 23 with GrammarAST

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

the class AttributeChecks method checkAllAttributeExpressions.

public static void checkAllAttributeExpressions(Grammar g) {
    for (ActionAST act : g.namedActions.values()) {
        AttributeChecks checker = new AttributeChecks(g, null, null, act, act.token);
        checker.examineAction();
    }
    for (Rule r : g.rules.values()) {
        for (ActionAST a : r.namedActions.values()) {
            AttributeChecks checker = new AttributeChecks(g, r, null, a, a.token);
            checker.examineAction();
        }
        for (int i = 1; i <= r.numberOfAlts; i++) {
            Alternative alt = r.alt[i];
            for (ActionAST a : alt.actions) {
                AttributeChecks checker = new AttributeChecks(g, r, alt, a, a.token);
                checker.examineAction();
            }
        }
        for (GrammarAST e : r.exceptions) {
            ActionAST a = (ActionAST) e.getChild(1);
            AttributeChecks checker = new AttributeChecks(g, r, null, a, a.token);
            checker.examineAction();
        }
        if (r.finallyAction != null) {
            AttributeChecks checker = new AttributeChecks(g, r, null, r.finallyAction, r.finallyAction.token);
            checker.examineAction();
        }
    }
}
Also used : Alternative(org.antlr.v4.tool.Alternative) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 24 with GrammarAST

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

the class BasicSemanticChecks method enterLabeledLexerElement.

@Override
protected void enterLabeledLexerElement(GrammarAST tree) {
    Token label = ((GrammarAST) tree.getChild(0)).getToken();
    g.tool.errMgr.grammarError(ErrorType.V3_LEXER_LABEL, g.fileName, label, label.getText());
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Token(org.antlr.runtime.Token)

Example 25 with GrammarAST

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

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