Rule r = new Rule(this, ruleName, composite.ruleIndex, numAlts); nameToRuleMap.put(ruleName, r); setRuleAST(ruleName, tree); r.setOptions(options, ruleToken); r.argActionAST = argActionAST; composite.ruleIndexToRuleList.setSize(composite.ruleIndex+1);
/** Define a label defined in a rule r; check the validity then ask the * Rule object to actually define it. */ protected void defineLabel(Rule r, Token label, GrammarAST element, int type) { boolean err = nameSpaceChecker.checkForLabelTypeMismatch(r, label, type); if ( err ) { return; } r.defineLabel(label, element, type); }
/** Before generating code, we examine all actions that can have * $x.y and $y stuff in them because some code generation depends on * Rule.referencedPredefinedRuleAttributes. I need to remove unused * rule labels for example. */ protected void examineAllExecutableActions() { Collection rules = getRules(); for (Iterator it = rules.iterator(); it.hasNext();) { Rule r = (Rule) it.next(); // walk all actions within the rule elements, args, and exceptions List<GrammarAST> actions = r.getInlineActions(); for (int i = 0; i < actions.size(); i++) { GrammarAST actionAST = (GrammarAST) actions.get(i); ActionAnalysis sniffer = new ActionAnalysis(this, r.name, actionAST); sniffer.analyze(); } // walk any named actions like @init, @after Collection<GrammarAST> namedActions = r.getActions().values(); for (Iterator it2 = namedActions.iterator(); it2.hasNext();) { GrammarAST actionAST = (GrammarAST) it2.next(); ActionAnalysis sniffer = new ActionAnalysis(this, r.name, actionAST); sniffer.analyze(); } } }
Grammar.LabelElementPair label = enclosingRule.getRuleLabel(x); AttributeScope scope = enclosingRule.getAttributeScope(x); enclosingRule.getRuleRefsInAlt(x, outerAltNum)!=null || enclosingRule.name.equals(x) )
.getRuleRefsInAlt(ruleRefName,outerAltNum)==null ) String stName = "rewriteTokenRef"; Rule rule = grammar.getRule(currentRuleName); Collection<String> tokenRefsInAlt = rule.getTokenRefsInAlt(outerAltNum); boolean createNewNode = !tokenRefsInAlt.contains(tokenName) || arg!=null; if ( createNewNode ) String labelName = (LABEL21!=null?LABEL21.getText():null); Rule rule = grammar.getRule(currentRuleName); Grammar.LabelElementPair pair = rule.getLabel(labelName); if ( labelName.equals(currentRuleName) ) if ( rule.hasRewrite(outerAltNum) && rule.getRuleRefsInAlt(outerAltNum).contains(labelName) )
scope = enclosingRule.getLocalAttributeScope((y!=null?y.getText():null)); else if ( enclosingRule.getRuleLabel((x!=null?x.getText():null))!=null ) { Grammar.LabelElementPair pair = enclosingRule.getRuleLabel((x!=null?x.getText():null)); pair.actionReferencesLabel = true; refdRuleName = pair.referencedRuleName; Rule refdRule = grammar.getRule(refdRuleName); if ( refdRule!=null ) { scope = refdRule.getLocalAttributeScope((y!=null?y.getText():null)); else if ( enclosingRule.getRuleRefsInAlt(x.getText(), outerAltNum)!=null ) { scope = refdRule.getLocalAttributeScope((y!=null?y.getText():null));
pair = enclosingRule.getRuleLabel((x!=null?x.getText():null)); refdRuleName = (x!=null?x.getText():null); if ( pair!=null ) { if ( !(((enclosingRule.getRuleLabel((x!=null?x.getText():null))!=null || isRuleRefInAlt((x!=null?x.getText():null))) && getRuleLabelAttribute(enclosingRule.getRuleLabel((x!=null?x.getText():null))!=null?enclosingRule.getRuleLabel((x!=null?x.getText():null)).referencedRuleName:(x!=null?x.getText():null),(y!=null?y.getText():null))!=null)) ) { if (state.backtracking>0) {state.failed=true; return ;} throw new FailedPredicateException(input, "RULE_SCOPE_ATTR", "(enclosingRule.getRuleLabel($x.text)!=null || isRuleRefInAlt($x.text)) &&\n\t getRuleLabelAttribute(enclosingRule.getRuleLabel($x.text)!=null?enclosingRule.getRuleLabel($x.text).referencedRuleName:$x.text,$y.text)!=null"); label = enclosingRule.getElementLabel((x!=null?x.getText():null), outerAltNum, generator); if ( label==null ) { ErrorManager.grammarError(ErrorManager.MSG_FORWARD_ELEMENT_REF, AttributeScope scope = refdRule.getLocalAttributeScope((y!=null?y.getText():null)); if ( scope.isPredefinedRuleScope ) { st = template("ruleLabelPropertyRef_"+(y!=null?y.getText():null));
/** Return the scope containing name */ public AttributeScope getAttributeScope(String name) { AttributeScope scope = getLocalAttributeScope(name); if ( scope!=null ) { return scope; } if ( ruleScope!=null && ruleScope.getAttribute(name)!=null ) { scope = ruleScope; } return scope; }
public Grammar.LabelElementPair getElementLabel(String id) { return enclosingRule.getLabel(id); }
if ( encRule!=null && encRule.hasRewrite(outerAltNum) && astSuffix!=null ) { ErrorManager.grammarError(ErrorManager.MSG_AST_OP_IN_ALT_WITH_REWRITE, grammar, if ( !rdef.getHasReturnValue() ) { labelText = null;
public final void mX() throws RecognitionException { try { int _type = X; int _channel = DEFAULT_TOKEN_CHANNEL; CommonToken x=null; // org/antlr/grammar/v3/ActionAnalysis.g:111:3: ( '$' x= ID {...}?) // org/antlr/grammar/v3/ActionAnalysis.g:111:5: '$' x= ID {...}? { match('$'); if (state.failed) return ; int xStart76 = getCharIndex(); mID(); if (state.failed) return ; x = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, xStart76, getCharIndex()-1); if ( !((enclosingRule!=null && enclosingRule.getRuleLabel((x!=null?x.getText():null))!=null)) ) { if (state.backtracking>0) {state.failed=true; return ;} throw new FailedPredicateException(input, "X", "enclosingRule!=null && enclosingRule.getRuleLabel($x.text)!=null"); } if ( state.backtracking==1 ) { Grammar.LabelElementPair pair = enclosingRule.getRuleLabel((x!=null?x.getText():null)); pair.actionReferencesLabel = true; } } state.type = _type; state.channel = _channel; } finally { } } // $ANTLR end "X"
Rule.getRuleType(currentRuleName) == Grammar.LEXER) ) generator.translateActionAttributeReferencesForSingleScope( theRule, theRule.getActions() ); retval.code.add("ruleName", currentRuleName);
public void defineRuleListLabel(String ruleName, Token label, GrammarAST element) { Rule r = getLocallyDefinedRule(ruleName); if ( r!=null ) { if ( !r.getHasMultipleReturnValues() ) { ErrorManager.grammarError( ErrorManager.MSG_LIST_LABEL_INVALID_UNLESS_RETVAL_STRUCT,this, label,label.getText()); } defineLabel(r, label, element, RULE_LIST_LABEL); } }
/** A label on a rule is useless if the rule has no return value, no * tree or template output, and it is not referenced in an action. */ protected void removeUselessLabels(Map ruleToElementLabelPairMap) { if ( ruleToElementLabelPairMap==null ) { return; } Collection labels = ruleToElementLabelPairMap.values(); List kill = new ArrayList(); for (Iterator labelit = labels.iterator(); labelit.hasNext();) { LabelElementPair pair = (LabelElementPair) labelit.next(); Rule refdRule = getRule(pair.elementRef.getText()); if ( refdRule!=null && !refdRule.getHasReturnValue() && !pair.actionReferencesLabel ) { //System.out.println(pair.label.getText()+" is useless"); kill.add(pair.label.getText()); } } for (int i = 0; i < kill.size(); i++) { String labelToKill = (String) kill.get(i); // System.out.println("kill "+labelToKill); ruleToElementLabelPairMap.remove(labelToKill); } }
generator.translateActionAttributeReferencesForSingleScope( theRule, theRule.getActions() ); code.setAttribute("ruleName", r);
Grammar.LabelElementPair label = enclosingRule.getRuleLabel(x); AttributeScope scope = enclosingRule.getAttributeScope(x); enclosingRule.getRuleRefsInAlt(x, outerAltNum)!=null || enclosingRule.name.equals(x) )
.getRuleRefsInAlt(ruleRefName,outerAltNum)==null ) String stName = "rewriteTokenRef"; Rule rule = grammar.getRule(currentRuleName); Collection<String> tokenRefsInAlt = rule.getTokenRefsInAlt(outerAltNum); boolean createNewNode = !tokenRefsInAlt.contains(tokenName) || arg!=null; if ( createNewNode ) String labelName = (LABEL21!=null?LABEL21.getText():null); Rule rule = grammar.getRule(currentRuleName); Grammar.LabelElementPair pair = rule.getLabel(labelName); if ( labelName.equals(currentRuleName) ) if ( rule.hasRewrite(outerAltNum) && rule.getRuleRefsInAlt(outerAltNum).contains(labelName) )
scope = enclosingRule.getLocalAttributeScope((y!=null?y.getText():null)); else if ( enclosingRule.getRuleLabel((x!=null?x.getText():null))!=null ) { Grammar.LabelElementPair pair = enclosingRule.getRuleLabel((x!=null?x.getText():null)); pair.actionReferencesLabel = true; refdRuleName = pair.referencedRuleName; Rule refdRule = grammar.getRule(refdRuleName); if ( refdRule!=null ) { scope = refdRule.getLocalAttributeScope((y!=null?y.getText():null)); else if ( enclosingRule.getRuleRefsInAlt(x.getText(), outerAltNum)!=null ) { scope = refdRule.getLocalAttributeScope((y!=null?y.getText():null));
pair = enclosingRule.getRuleLabel((x!=null?x.getText():null)); refdRuleName = (x!=null?x.getText():null); if ( pair!=null ) { if ( !(((enclosingRule.getRuleLabel((x!=null?x.getText():null))!=null || isRuleRefInAlt((x!=null?x.getText():null))) && getRuleLabelAttribute(enclosingRule.getRuleLabel((x!=null?x.getText():null))!=null?enclosingRule.getRuleLabel((x!=null?x.getText():null)).referencedRuleName:(x!=null?x.getText():null),(y!=null?y.getText():null))!=null)) ) { if (state.backtracking>0) {state.failed=true; return;} throw new FailedPredicateException(input, "RULE_SCOPE_ATTR", "(enclosingRule.getRuleLabel($x.text)!=null || isRuleRefInAlt($x.text)) &&\n\t getRuleLabelAttribute(enclosingRule.getRuleLabel($x.text)!=null?enclosingRule.getRuleLabel($x.text).referencedRuleName:$x.text,$y.text)!=null"); label = enclosingRule.getElementLabel((x!=null?x.getText():null), outerAltNum, generator); if ( label==null ) { ErrorManager.grammarError(ErrorManager.MSG_FORWARD_ELEMENT_REF, AttributeScope scope = refdRule.getLocalAttributeScope((y!=null?y.getText():null)); if ( scope.isPredefinedRuleScope ) { st = template("ruleLabelPropertyRef_"+(y!=null?y.getText():null));
/** Return the scope containing name */ public AttributeScope getAttributeScope(String name) { AttributeScope scope = getLocalAttributeScope(name); if ( scope!=null ) { return scope; } if ( ruleScope!=null && ruleScope.getAttribute(name)!=null ) { scope = ruleScope; } return scope; }