KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > preprocessor > Rule


1 package persistence.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/license.html
6  *
7  */

8
9 import persistence.antlr.collections.impl.IndexedVector;
10
11 import java.util.Hashtable JavaDoc;
12 import java.util.Enumeration JavaDoc;
13
14 class Rule {
15     protected String JavaDoc name;
16     protected String JavaDoc block;
17     protected String JavaDoc args;
18     protected String JavaDoc returnValue;
19     protected String JavaDoc throwsSpec;
20     protected String JavaDoc initAction;
21     protected IndexedVector options;
22     protected String JavaDoc visibility;
23     protected Grammar enclosingGrammar;
24     protected boolean bang = false;
25
26     public Rule(String JavaDoc n, String JavaDoc b, IndexedVector options, Grammar gr) {
27         name = n;
28         block = b;
29         this.options = options;
30         setEnclosingGrammar(gr);
31     }
32
33     public String JavaDoc getArgs() {
34         return args;
35     }
36
37     public boolean getBang() {
38         return bang;
39     }
40
41     public String JavaDoc getName() {
42         return name;
43     }
44
45     public String JavaDoc getReturnValue() {
46         return returnValue;
47     }
48
49     public String JavaDoc getVisibility() {
50         return visibility;
51     }
52
53     /** If 'rule' narrows the visible of 'this', return true;
54      * For example, 'this' is public and 'rule' is private,
55      * true is returned. You cannot narrow the vis. of
56      * a rule.
57      */

58     public boolean narrowerVisibility(Rule rule) {
59         if (visibility.equals("public")) {
60             if (!rule.equals("public")) {
61                 return true; // everything narrower than public
62
}
63             return false;
64         }
65         else if (visibility.equals("protected")) {
66             if (rule.equals("private")) {
67                 return true; // private narrower than protected
68
}
69             return false;
70         }
71         else if (visibility.equals("private")) {
72             return false; // nothing is narrower than private
73
}
74         return false;
75     }
76
77     /** Two rules have the same signature if they have:
78      * same name
79      * same return value
80      * same args
81      * I do a simple string compare now, but later
82      * the type could be pulled out so it is insensitive
83      * to names of args etc...
84      */

85     public boolean sameSignature(Rule rule) {
86         boolean nSame = true;
87         boolean aSame = true;
88         boolean rSame = true;
89
90         nSame = name.equals(rule.getName());
91         if (args != null) {
92             aSame = args.equals(rule.getArgs());
93         }
94         if (returnValue != null) {
95             rSame = returnValue.equals(rule.getReturnValue());
96         }
97         return nSame && aSame && rSame;
98     }
99
100     public void setArgs(String JavaDoc a) {
101         args = a;
102     }
103
104     public void setBang() {
105         bang = true;
106     }
107
108     public void setEnclosingGrammar(Grammar g) {
109         enclosingGrammar = g;
110     }
111
112     public void setInitAction(String JavaDoc a) {
113         initAction = a;
114     }
115
116     public void setOptions(IndexedVector options) {
117         this.options = options;
118     }
119
120     public void setReturnValue(String JavaDoc ret) {
121         returnValue = ret;
122     }
123
124     public void setThrowsSpec(String JavaDoc t) {
125         throwsSpec = t;
126     }
127
128     public void setVisibility(String JavaDoc v) {
129         visibility = v;
130     }
131
132     public String JavaDoc toString() {
133         String JavaDoc s = "";
134         String JavaDoc retString = returnValue == null ? "" : "returns " + returnValue;
135         String JavaDoc argString = args == null ? "" : args;
136         String JavaDoc bang = getBang() ? "!" : "";
137
138         s += visibility == null ? "" : visibility + " ";
139         s += name + bang + argString + " " + retString + throwsSpec;
140         if (options != null) {
141             s += System.getProperty("line.separator") +
142                 "options {" +
143                 System.getProperty("line.separator");
144             for (Enumeration JavaDoc e = options.elements(); e.hasMoreElements();) {
145                 s += (Option)e.nextElement() + System.getProperty("line.separator");
146             }
147             s += "}" + System.getProperty("line.separator");
148         }
149         if (initAction != null) {
150             s += initAction + System.getProperty("line.separator");
151         }
152         s += block;
153         return s;
154     }
155 }
156
Popular Tags