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 language=Java; 39 filter=true; // try all non-fragment rules in order specified 40} 41 42@header { 43package org.antlr.grammar.v3; 44import org.antlr.runtime.*; 45import org.antlr.tool.*; 46} 47 48@members { 49Rule enclosingRule; 50Grammar grammar; 51Token actionToken; 52int outerAltNum = 0; 53 54 public ActionAnalysis(Grammar grammar, String ruleName, GrammarAST actionAST) 55 { 56 this(new ANTLRStringStream(actionAST.token.getText())); 57 this.grammar = grammar; 58 this.enclosingRule = grammar.getLocallyDefinedRule(ruleName); 59 this.actionToken = actionAST.token; 60 this.outerAltNum = actionAST.outerAltNum; 61 } 62 63public void analyze() { 64 // System.out.println("###\naction="+actionToken); 65 Token t; 66 do { 67 t = nextToken(); 68 } while ( t.getType()!= Token.EOF ); 69} 70} 71 72/** $x.y x is enclosing rule or rule ref or rule label 73 * y is a return value, parameter, or predefined property. 74 */ 75X_Y : '$' x=ID '.' y=ID {enclosingRule!=null}? 76 { 77 AttributeScope scope = null; 78 String refdRuleName = null; 79 if ( $x.text.equals(enclosingRule.name) ) { 80 // ref to enclosing rule. 81 refdRuleName = $x.text; 82 scope = enclosingRule.getLocalAttributeScope($y.text); 83 } 84 else if ( enclosingRule.getRuleLabel($x.text)!=null ) { 85 // ref to rule label 86 Grammar.LabelElementPair pair = enclosingRule.getRuleLabel($x.text); 87 pair.actionReferencesLabel = true; 88 refdRuleName = pair.referencedRuleName; 89 Rule refdRule = grammar.getRule(refdRuleName); 90 if ( refdRule!=null ) { 91 scope = refdRule.getLocalAttributeScope($y.text); 92 } 93 } 94 else if ( enclosingRule.getRuleRefsInAlt(x.getText(), outerAltNum)!=null ) { 95 // ref to rule referenced in this alt 96 refdRuleName = $x.text; 97 Rule refdRule = grammar.getRule(refdRuleName); 98 if ( refdRule!=null ) { 99 scope = refdRule.getLocalAttributeScope($y.text); 100 } 101 } 102 if ( scope!=null && 103 (scope.isPredefinedRuleScope||scope.isPredefinedLexerRuleScope) ) 104 { 105 grammar.referenceRuleLabelPredefinedAttribute(refdRuleName); 106 //System.out.println("referenceRuleLabelPredefinedAttribute for "+refdRuleName); 107 } 108 } 109 ; 110 111/** $x x is an isolated rule label. Just record that the label was referenced */ 112X : '$' x=ID {enclosingRule!=null && enclosingRule.getRuleLabel($x.text)!=null}? 113 { 114 Grammar.LabelElementPair pair = enclosingRule.getRuleLabel($x.text); 115 pair.actionReferencesLabel = true; 116 } 117 ; 118 119/** $y y is a return value, parameter, or predefined property of current rule */ 120Y : '$' ID {enclosingRule!=null && enclosingRule.getLocalAttributeScope($ID.text)!=null}? 121 { 122 AttributeScope scope = enclosingRule.getLocalAttributeScope($ID.text); 123 if ( scope!=null && 124 (scope.isPredefinedRuleScope||scope.isPredefinedLexerRuleScope) ) 125 { 126 grammar.referenceRuleLabelPredefinedAttribute(enclosingRule.name); 127 //System.out.println("referenceRuleLabelPredefinedAttribute for "+$ID.text); 128 } 129 } 130 ; 131 132fragment 133ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')* 134 ; 135