001: package antlr.preprocessor;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.cs.usfca.edu
005: * Software rights: http://www.antlr.org/license.html
006: */
007:
008: import antlr.collections.impl.IndexedVector;
009:
010: import java.util.Hashtable;
011: import java.util.Enumeration;
012:
013: class Rule {
014: protected String name;
015: protected String block;
016: protected String args;
017: protected String returnValue;
018: protected String throwsSpec;
019: protected String initAction;
020: protected IndexedVector options;
021: protected String visibility;
022: protected Grammar enclosingGrammar;
023: protected boolean bang = false;
024:
025: public Rule(String n, String b, IndexedVector options, Grammar gr) {
026: name = n;
027: block = b;
028: this .options = options;
029: setEnclosingGrammar(gr);
030: }
031:
032: public String getArgs() {
033: return args;
034: }
035:
036: public boolean getBang() {
037: return bang;
038: }
039:
040: public String getName() {
041: return name;
042: }
043:
044: public String getReturnValue() {
045: return returnValue;
046: }
047:
048: public String getVisibility() {
049: return visibility;
050: }
051:
052: /** If 'rule' narrows the visible of 'this', return true;
053: * For example, 'this' is public and 'rule' is private,
054: * true is returned. You cannot narrow the vis. of
055: * a rule.
056: */
057: public boolean narrowerVisibility(Rule rule) {
058: if (visibility.equals("public")) {
059: if (!rule.equals("public")) {
060: return true; // everything narrower than public
061: }
062: return false;
063: } else if (visibility.equals("protected")) {
064: if (rule.equals("private")) {
065: return true; // private narrower than protected
066: }
067: return false;
068: } else if (visibility.equals("private")) {
069: return false; // nothing is narrower than private
070: }
071: return false;
072: }
073:
074: /** Two rules have the same signature if they have:
075: * same name
076: * same return value
077: * same args
078: * I do a simple string compare now, but later
079: * the type could be pulled out so it is insensitive
080: * to names of args etc...
081: */
082: public boolean sameSignature(Rule rule) {
083: boolean nSame = true;
084: boolean aSame = true;
085: boolean rSame = true;
086:
087: nSame = name.equals(rule.getName());
088: if (args != null) {
089: aSame = args.equals(rule.getArgs());
090: }
091: if (returnValue != null) {
092: rSame = returnValue.equals(rule.getReturnValue());
093: }
094: return nSame && aSame && rSame;
095: }
096:
097: public void setArgs(String a) {
098: args = a;
099: }
100:
101: public void setBang() {
102: bang = true;
103: }
104:
105: public void setEnclosingGrammar(Grammar g) {
106: enclosingGrammar = g;
107: }
108:
109: public void setInitAction(String a) {
110: initAction = a;
111: }
112:
113: public void setOptions(IndexedVector options) {
114: this .options = options;
115: }
116:
117: public void setReturnValue(String ret) {
118: returnValue = ret;
119: }
120:
121: public void setThrowsSpec(String t) {
122: throwsSpec = t;
123: }
124:
125: public void setVisibility(String v) {
126: visibility = v;
127: }
128:
129: public String toString() {
130: String s = "";
131: String retString = returnValue == null ? "" : "returns "
132: + returnValue;
133: String argString = args == null ? "" : args;
134: String bang = getBang() ? "!" : "";
135:
136: s += visibility == null ? "" : visibility + " ";
137: s += name + bang + argString + " " + retString + throwsSpec;
138: if (options != null) {
139: s += System.getProperty("line.separator") + "options {"
140: + System.getProperty("line.separator");
141: for (Enumeration e = options.elements(); e
142: .hasMoreElements();) {
143: s += (Option) e.nextElement()
144: + System.getProperty("line.separator");
145: }
146: s += "}" + System.getProperty("line.separator");
147: }
148: if (initAction != null) {
149: s += initAction + System.getProperty("line.separator");
150: }
151: s += block;
152: return s;
153: }
154: }
|