1/* 2 [The "BSD license"] 3 Copyright (c) 2010 Terence Parr 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 2. Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 3. The name of the author may not be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*/ 28 29/** We need to set Rule.referencedPredefinedRuleAttributes before 30 * code generation. This filter looks at an action in context of 31 * its rule and outer alternative number and figures out which 32 * rules have predefined prefs referenced. I need this so I can 33 * remove unusued labels. This also tracks, for labeled rules, 34 * which are referenced by actions. 35 */ 36lexer grammar ActionAnalysis; 37options { 38 filter=true; // try all non-fragment rules in order specified 39} 40 41@header { 42package org.antlr.grammar.v3; 43import org.antlr.runtime.*; 44import org.antlr.tool.*; 45} 46 47@members { 48Rule enclosingRule; 49Grammar grammar; 50Token actionToken; 51int outerAltNum = 0; 52 53 public ActionAnalysis(Grammar grammar, String ruleName, GrammarAST actionAST) 54 { 55 this(new ANTLRStringStream(actionAST.token.getText())); 56 this.grammar = grammar; 57 this.enclosingRule = grammar.getLocallyDefinedRule(ruleName); 58 this.actionToken = actionAST.token; 59 this.outerAltNum = actionAST.outerAltNum; 60 } 61 62public void analyze() { 63 // System.out.println("###\naction="+actionToken); 64 Token t; 65 do { 66 t = nextToken(); 67 } while ( t.getType()!= Token.EOF ); 68} 69} 70 71/** $x.y x is enclosing rule or rule ref or rule label 72 * y is a return value, parameter, or predefined property. 73 */ 74X_Y : '$' x=ID '.' y=ID {enclosingRule!=null}? 75 { 76 AttributeScope scope = null; 77 String refdRuleName = null; 78 if ( $x.text.equals(enclosingRule.name) ) { 79 // ref to enclosing rule. 80 refdRuleName = $x.text; 81 scope = enclosingRule.getLocalAttributeScope($y.text); 82 } 83 else if ( enclosingRule.getRuleLabel($x.text)!=null ) { 84 // ref to rule label 85 Grammar.LabelElementPair pair = enclosingRule.getRuleLabel($x.text); 86 pair.actionReferencesLabel = true; 87 refdRuleName = pair.referencedRuleName; 88 Rule refdRule = grammar.getRule(refdRuleName); 89 if ( refdRule!=null ) { 90 scope = refdRule.getLocalAttributeScope($y.text); 91 } 92 } 93 else if ( enclosingRule.getRuleRefsInAlt(x.getText(), outerAltNum)!=null ) { 94 // ref to rule referenced in this alt 95 refdRuleName = $x.text; 96 Rule refdRule = grammar.getRule(refdRuleName); 97 if ( refdRule!=null ) { 98 scope = refdRule.getLocalAttributeScope($y.text); 99 } 100 } 101 if ( scope!=null && 102 (scope.isPredefinedRuleScope||scope.isPredefinedLexerRuleScope) ) 103 { 104 grammar.referenceRuleLabelPredefinedAttribute(refdRuleName); 105 //System.out.println("referenceRuleLabelPredefinedAttribute for "+refdRuleName); 106 } 107 } 108 ; 109 110/** $x x is an isolated rule label. Just record that the label was referenced */ 111X : '$' x=ID {enclosingRule!=null && enclosingRule.getRuleLabel($x.text)!=null}? 112 { 113 Grammar.LabelElementPair pair = enclosingRule.getRuleLabel($x.text); 114 pair.actionReferencesLabel = true; 115 } 116 ; 117 118/** $y y is a return value, parameter, or predefined property of current rule */ 119Y : '$' ID {enclosingRule!=null && enclosingRule.getLocalAttributeScope($ID.text)!=null}? 120 { 121 AttributeScope scope = enclosingRule.getLocalAttributeScope($ID.text); 122 if ( scope!=null && 123 (scope.isPredefinedRuleScope||scope.isPredefinedLexerRuleScope) ) 124 { 125 grammar.referenceRuleLabelPredefinedAttribute(enclosingRule.name); 126 //System.out.println("referenceRuleLabelPredefinedAttribute for "+$ID.text); 127 } 128 } 129 ; 130 131fragment 132ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* 133 ; 134