KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > RuleBlock


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/RuleBlock.java#8 $
8  */

9
10 import antlr.collections.impl.Vector;
11
12 import java.util.Hashtable JavaDoc;
13
14 /**A list of alternatives and info contained in
15  * the rule definition.
16  */

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

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

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

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

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

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