KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > RuleBlock


1 package persistence.antlr;
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.Vector;
10
11 import java.util.Hashtable JavaDoc;
12
13 /**A list of alternatives and info contained in
14  * the rule definition.
15  */

16 public class RuleBlock extends AlternativeBlock {
17     protected String JavaDoc ruleName;
18     protected String JavaDoc argAction = null; // string for rule arguments [...]
19
protected String JavaDoc throwsSpec = null;
20     protected String JavaDoc returnAction = null;// string for rule return type(s) <...>
21
protected RuleEndElement endNode; // which node ends this rule?
22

23     // Generate literal-testing code for lexer rule?
24
protected boolean testLiterals = false;
25
26     Vector labeledElements; // List of labeled elements found in this rule
27
// This is a list of AlternativeElement (or subclass)
28

29     protected boolean[] lock; // for analysis; used to avoid infinite loops
30
// 1..k
31
protected Lookahead cache[];// Each rule can cache it's lookahead computation.
32

33     // This cache contains an epsilon
34
// imaginary token if the FOLLOW is required. No
35
// FOLLOW information is cached here.
36
// The FIRST(rule) is stored in this cache; 1..k
37
// This set includes FIRST of all alts.
38

39     Hashtable JavaDoc exceptionSpecs; // table of String-to-ExceptionSpec.
40

41     // grammar-settable options
42
protected boolean defaultErrorHandler = true;
43     protected String JavaDoc ignoreRule = null;
44
45     /** Construct a named rule. */
46     public RuleBlock(Grammar g, String JavaDoc r) {
47         super(g);
48         ruleName = r;
49         labeledElements = new Vector();
50         cache = new Lookahead[g.maxk + 1];
51         exceptionSpecs = new Hashtable JavaDoc();
52         setAutoGen(g instanceof ParserGrammar);
53     }
54
55     /** Construct a named rule with line number information */
56     public RuleBlock(Grammar g, String JavaDoc r, int line, boolean doAutoGen_) {
57         this(g, r);
58         this.line = line;
59         setAutoGen(doAutoGen_);
60     }
61
62     public void addExceptionSpec(ExceptionSpec ex) {
63         if (findExceptionSpec(ex.label) != null) {
64             if (ex.label != null) {
65                 grammar.antlrTool.error("Rule '" + ruleName + "' already has an exception handler for label: " + ex.label);
66             }
67             else {
68                 grammar.antlrTool.error("Rule '" + ruleName + "' already has an exception handler");
69             }
70         }
71         else {
72             exceptionSpecs.put((ex.label == null ? "" : ex.label.getText()), ex);
73         }
74     }
75
76     public ExceptionSpec findExceptionSpec(Token label) {
77         return (ExceptionSpec)exceptionSpecs.get(label == null ? "" : label.getText());
78     }
79
80     public ExceptionSpec findExceptionSpec(String JavaDoc label) {
81         return (ExceptionSpec)exceptionSpecs.get(label == null ? "" : label);
82     }
83
84     public void generate() {
85         grammar.generator.gen(this);
86     }
87
88     public boolean getDefaultErrorHandler() {
89         return defaultErrorHandler;
90     }
91
92     public RuleEndElement getEndElement() {
93         return endNode;
94     }
95
96     public String JavaDoc getIgnoreRule() {
97         return ignoreRule;
98     }
99
100     public String JavaDoc getRuleName() {
101         return ruleName;
102     }
103
104     public boolean getTestLiterals() {
105         return testLiterals;
106     }
107
108     public boolean isLexerAutoGenRule() {
109         return ruleName.equals("nextToken");
110     }
111
112     public Lookahead look(int k) {
113         return grammar.theLLkAnalyzer.look(k, this);
114     }
115
116     public void prepareForAnalysis() {
117         super.prepareForAnalysis();
118         lock = new boolean[grammar.maxk + 1];
119     }
120
121     // rule option values
122
public void setDefaultErrorHandler(boolean value) {
123         defaultErrorHandler = value;
124     }
125
126     public void setEndElement(RuleEndElement re) {
127         endNode = re;
128     }
129
130     public void setOption(Token key, Token value) {
131         if (key.getText().equals("defaultErrorHandler")) {
132             if (value.getText().equals("true")) {
133                 defaultErrorHandler = true;
134             }
135             else if (value.getText().equals("false")) {
136                 defaultErrorHandler = false;
137             }
138             else {
139                 grammar.antlrTool.error("Value for defaultErrorHandler must be true or false", grammar.getFilename(), key.getLine(), key.getColumn());
140             }
141         }
142         else if (key.getText().equals("testLiterals")) {
143             if (!(grammar instanceof LexerGrammar)) {
144                 grammar.antlrTool.error("testLiterals option only valid for lexer rules", grammar.getFilename(), key.getLine(), key.getColumn());
145             }
146             else {
147                 if (value.getText().equals("true")) {
148                     testLiterals = true;
149                 }
150                 else if (value.getText().equals("false")) {
151                     testLiterals = false;
152                 }
153                 else {
154                     grammar.antlrTool.error("Value for testLiterals must be true or false", grammar.getFilename(), key.getLine(), key.getColumn());
155                 }
156             }
157         }
158         else if (key.getText().equals("ignore")) {
159             if (!(grammar instanceof LexerGrammar)) {
160                 grammar.antlrTool.error("ignore option only valid for lexer rules", grammar.getFilename(), key.getLine(), key.getColumn());
161             }
162             else {
163                 ignoreRule = value.getText();
164             }
165         }
166         else if (key.getText().equals("paraphrase")) {
167             if (!(grammar instanceof LexerGrammar)) {
168                 grammar.antlrTool.error("paraphrase option only valid for lexer rules", grammar.getFilename(), key.getLine(), key.getColumn());
169             }
170             else {
171                 // find token def associated with this rule
172
TokenSymbol ts = grammar.tokenManager.getTokenSymbol(ruleName);
173                 if (ts == null) {
174                     grammar.antlrTool.panic("cannot find token associated with rule " + ruleName);
175                 }
176                 ts.setParaphrase(value.getText());
177             }
178         }
179         else if (key.getText().equals("generateAmbigWarnings")) {
180             if (value.getText().equals("true")) {
181                 generateAmbigWarnings = true;
182             }
183             else if (value.getText().equals("false")) {
184                 generateAmbigWarnings = false;
185             }
186             else {
187                 grammar.antlrTool.error("Value for generateAmbigWarnings must be true or false", grammar.getFilename(), key.getLine(), key.getColumn());
188             }
189         }
190         else {
191             grammar.antlrTool.error("Invalid rule option: " + key.getText(), grammar.getFilename(), key.getLine(), key.getColumn());
192         }
193     }
194
195     public String JavaDoc toString() {
196         String JavaDoc s = " FOLLOW={";
197         Lookahead cache[] = endNode.cache;
198         int k = grammar.maxk;
199         boolean allNull = true;
200         for (int j = 1; j <= k; j++) {
201             if (cache[j] == null) continue;
202             s += cache[j].toString(",", grammar.tokenManager.getVocabulary());
203             allNull = false;
204             if (j < k && cache[j + 1] != null) s += ";";
205         }
206         s += "}";
207         if (allNull) s = "";
208         return ruleName + ": " + super.toString() + " ;" + s;
209     }
210 }
211
Popular Tags