KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > preprocessor > Rule


1 package antlr.preprocessor;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/preprocessor/Rule.java#5 $
8  */

9
10 import antlr.collections.impl.IndexedVector;
11
12 import java.util.Hashtable JavaDoc;
13 import java.util.Enumeration JavaDoc;
14
15 class Rule {
16     protected String JavaDoc name;
17     protected String JavaDoc block;
18     protected String JavaDoc args;
19     protected String JavaDoc returnValue;
20     protected String JavaDoc throwsSpec;
21     protected String JavaDoc initAction;
22     protected IndexedVector options;
23     protected String JavaDoc visibility;
24     protected Grammar enclosingGrammar;
25     protected boolean bang = false;
26
27     public Rule(String JavaDoc n, String JavaDoc b, IndexedVector options, Grammar gr) {
28         name = n;
29         block = b;
30         this.options = options;
31         setEnclosingGrammar(gr);
32     }
33
34     public String JavaDoc getArgs() {
35         return args;
36     }
37
38     public boolean getBang() {
39         return bang;
40     }
41
42     public String JavaDoc getName() {
43         return name;
44     }
45
46     public String JavaDoc getReturnValue() {
47         return returnValue;
48     }
49
50     public String JavaDoc 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      */

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      */

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
101     public void setArgs(String JavaDoc a) {
102         args = a;
103     }
104
105     public void setBang() {
106         bang = true;
107     }
108
109     public void setEnclosingGrammar(Grammar g) {
110         enclosingGrammar = g;
111     }
112
113     public void setInitAction(String JavaDoc a) {
114         initAction = a;
115     }
116
117     public void setOptions(IndexedVector options) {
118         this.options = options;
119     }
120
121     public void setReturnValue(String JavaDoc ret) {
122         returnValue = ret;
123     }
124
125     public void setThrowsSpec(String JavaDoc t) {
126         throwsSpec = t;
127     }
128
129     public void setVisibility(String JavaDoc v) {
130         visibility = v;
131     }
132
133     public String JavaDoc toString() {
134         String JavaDoc s = "";
135         String JavaDoc retString = returnValue == null ? "" : "returns " + returnValue;
136         String JavaDoc argString = args == null ? "" : args;
137         String JavaDoc 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 JavaDoc 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
Popular Tags