1	package antlr.preprocessor;
2	
3	import antlr.collections.impl.IndexedVector;
4	import java.util.Hashtable;
5	import java.util.Enumeration;
6	
7	class Rule {
8		protected String name;
9		protected String block;
10		protected String args;
11		protected String returnValue;
12		protected String initAction;
13	  	protected IndexedVector options;
14		protected String visibility;
15		protected Grammar enclosingGrammar;
16		protected boolean bang = false;
17	
18		public Rule(String n, String b, IndexedVector options, Grammar gr) {
19			name = n;
20			block = b;
21			this.options = options;
22			setEnclosingGrammar(gr);
23		}
24		public String getArgs() { return args; }
25		public boolean getBang() { return bang; }
26		public String getName() { return name; }
27		public String getReturnValue() { return returnValue; }
28		public String getVisibility() { return visibility; }
29		/** If 'rule' narrows the visible of 'this', return true;
30		 *  For example, 'this' is public and 'rule' is private,
31		 *  true is returned.  You cannot narrow the vis. of
32		 *  a rule.
33		 */
34		public boolean narrowerVisibility(Rule rule) {
35			if ( visibility.equals("public") ) {
36				if ( !rule.equals("public") ) {
37					return true;	// everything narrower than public
38				}
39				return false;
40			}
41			else if ( visibility.equals("protected") ) {
42				if ( rule.equals("private") ) {
43					return true;	// private narrower than protected
44				}
45				return false;
46			}
47			else if ( visibility.equals("private") ) {
48				return false;	// nothing is narrower than private
49			}
50			return false;
51		}
52		/** Two rules have the same signature if they have:
53		 *  	same name
54		 *		same return value
55		 *		same args
56		 *	I do a simple string compare now, but later
57		 *	the type could be pulled out so it is insensitive
58		 *	to names of args etc...
59		 */
60		public boolean sameSignature(Rule rule) {
61			boolean nSame=true;
62			boolean aSame=true;
63			boolean rSame=true;
64	
65			nSame = name.equals(rule.getName());
66			if ( args!=null ) {
67				aSame = args.equals(rule.getArgs());
68			}
69			if ( returnValue!=null ) {
70				rSame = returnValue.equals(rule.getReturnValue());
71			}
72			return nSame && aSame && rSame;
73		}
74		public void setArgs(String a) { args=a; }
75		public void setBang() {bang=true;}
76		public void setEnclosingGrammar(Grammar g) { enclosingGrammar=g; }
77		public void setInitAction(String a) {initAction = a;}
78		public void setOptions(IndexedVector options) {
79			this.options = options;
80		}
81		public void setReturnValue(String ret) { returnValue=ret; }
82		public void setVisibility(String v) { visibility=v; }
83		public String toString() {
84			String s="";
85			String retString = returnValue==null ? "" : "returns "+returnValue;
86			String argString = args==null ? "" : args;
87			String bang = getBang() ? "!" : "";
88	
89			s += visibility==null ? "" : visibility+" ";
90			s += name+bang+argString+" "+retString;
91			if ( options!=null ) {
92				s += System.getProperty("line.separator")+
93					 "options {"+
94					 System.getProperty("line.separator");
95				for (Enumeration e = options.elements() ; e.hasMoreElements() ;) {
96					s += (Option)e.nextElement()+System.getProperty("line.separator");
97				}
98				s += "}"+System.getProperty("line.separator");
99			}
100			if ( initAction!=null ) {
101				s+=initAction+System.getProperty("line.separator");
102			}	
103			s += block;
104			return s;
105		}
106	}
107