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