1 package antlr.preprocessor;
2 
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.cs.usfca.edu
5  * Software rights: http://www.antlr.org/license.html
6  *
7  * $Id: //depot/code/org.antlr/release/antlr-2.7.7/antlr/preprocessor/Rule.java#2 $
8  */
9 
10 import antlr.collections.impl.IndexedVector;
11 
12 import java.util.Hashtable;
13 import java.util.Enumeration;
14 
15 class Rule {
16     protected String name;
17     protected String block;
18     protected String args;
19     protected String returnValue;
20     protected String throwsSpec;
21     protected String initAction;
22     protected IndexedVector options;
23     protected String visibility;
24     protected Grammar enclosingGrammar;
25     protected boolean bang = false;
26 
Rule(String n, String b, IndexedVector options, Grammar gr)27     public Rule(String n, String b, IndexedVector options, Grammar gr) {
28         name = n;
29         block = b;
30         this.options = options;
31         setEnclosingGrammar(gr);
32     }
33 
getArgs()34     public String getArgs() {
35         return args;
36     }
37 
getBang()38     public boolean getBang() {
39         return bang;
40     }
41 
getName()42     public String getName() {
43         return name;
44     }
45 
getReturnValue()46     public String getReturnValue() {
47         return returnValue;
48     }
49 
getVisibility()50     public String getVisibility() {
51         return visibility;
52     }
53 
54     /** If 'rule' narrows the visible of 'this', return true;
55      *  For example, 'this' is public and 'rule' is private,
56      *  true is returned.  You cannot narrow the vis. of
57      *  a rule.
58      */
narrowerVisibility(Rule rule)59     public boolean narrowerVisibility(Rule rule) {
60         if (visibility.equals("public")) {
61             if (!rule.equals("public")) {
62                 return true;	// everything narrower than public
63             }
64             return false;
65         }
66         else if (visibility.equals("protected")) {
67             if (rule.equals("private")) {
68                 return true;	// private narrower than protected
69             }
70             return false;
71         }
72         else if (visibility.equals("private")) {
73             return false;	// nothing is narrower than private
74         }
75         return false;
76     }
77 
78     /** Two rules have the same signature if they have:
79      *  	same name
80      *		same return value
81      *		same args
82      *	I do a simple string compare now, but later
83      *	the type could be pulled out so it is insensitive
84      *	to names of args etc...
85      */
sameSignature(Rule rule)86     public boolean sameSignature(Rule rule) {
87         boolean nSame = true;
88         boolean aSame = true;
89         boolean rSame = true;
90 
91         nSame = name.equals(rule.getName());
92         if (args != null) {
93             aSame = args.equals(rule.getArgs());
94         }
95         if (returnValue != null) {
96             rSame = returnValue.equals(rule.getReturnValue());
97         }
98         return nSame && aSame && rSame;
99     }
100 
setArgs(String a)101     public void setArgs(String a) {
102         args = a;
103     }
104 
setBang()105     public void setBang() {
106         bang = true;
107     }
108 
setEnclosingGrammar(Grammar g)109     public void setEnclosingGrammar(Grammar g) {
110         enclosingGrammar = g;
111     }
112 
setInitAction(String a)113     public void setInitAction(String a) {
114         initAction = a;
115     }
116 
setOptions(IndexedVector options)117     public void setOptions(IndexedVector options) {
118         this.options = options;
119     }
120 
setReturnValue(String ret)121     public void setReturnValue(String ret) {
122         returnValue = ret;
123     }
124 
setThrowsSpec(String t)125     public void setThrowsSpec(String t) {
126         throwsSpec = t;
127     }
128 
setVisibility(String v)129     public void setVisibility(String v) {
130         visibility = v;
131     }
132 
toString()133     public String toString() {
134         String s = "";
135         String retString = returnValue == null ? "" : "returns " + returnValue;
136         String argString = args == null ? "" : args;
137         String bang = getBang() ? "!" : "";
138 
139         s += visibility == null ? "" : visibility + " ";
140         s += name + bang + argString + " " + retString + throwsSpec;
141         if (options != null) {
142             s += System.getProperty("line.separator") +
143                 "options {" +
144                 System.getProperty("line.separator");
145             for (Enumeration e = options.elements(); e.hasMoreElements();) {
146                 s += (Option)e.nextElement() + System.getProperty("line.separator");
147             }
148             s += "}" + System.getProperty("line.separator");
149         }
150         if (initAction != null) {
151             s += initAction + System.getProperty("line.separator");
152         }
153         s += block;
154         return s;
155     }
156 }
157