KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > ANTLRTokdefParser


1 // $ANTLR : "tokdef.g" -> "ANTLRTokdefParser.java"$
2
package persistence.antlr;
3 import persistence.antlr.TokenBuffer;
4 import persistence.antlr.TokenStreamException;
5 import persistence.antlr.TokenStreamIOException;
6 import persistence.antlr.ANTLRException;
7 import persistence.antlr.LLkParser;
8 import persistence.antlr.Token;
9 import persistence.antlr.TokenStream;
10 import persistence.antlr.RecognitionException;
11 import persistence.antlr.NoViableAltException;
12 import persistence.antlr.MismatchedTokenException;
13 import persistence.antlr.SemanticException;
14 import persistence.antlr.ParserSharedInputState;
15 import persistence.antlr.collections.impl.BitSet;
16
17 /** Simple lexer/parser for reading token definition files
18   in support of the import/export vocab option for grammars.
19  */

20 public class ANTLRTokdefParser extends persistence.antlr.LLkParser implements ANTLRTokdefParserTokenTypes
21  {
22
23     // This chunk of error reporting code provided by Brian Smith
24

25     private persistence.antlr.Tool antlrTool;
26
27     /** In order to make it so existing subclasses don't break, we won't require
28      * that the persistence.antlr.Tool instance be passed as a constructor element. Instead,
29      * the persistence.antlr.Tool instance should register itself via {@link #initTool(antlr.Tool)}
30      * @throws IllegalStateException if a tool has already been registered
31      * @since 2.7.2
32      */

33     public void setTool(persistence.antlr.Tool tool) {
34         if (antlrTool == null) {
35             antlrTool = tool;
36         }
37         else {
38             throw new IllegalStateException JavaDoc("persistence.antlr.Tool already registered");
39         }
40     }
41
42     /** @since 2.7.2 */
43     protected persistence.antlr.Tool getTool() {
44         return antlrTool;
45     }
46
47     /** Delegates the error message to the tool if any was registered via
48      * {@link #initTool(persistence.antlr.Tool)}
49      * @since 2.7.2
50      */

51     public void reportError(String JavaDoc s) {
52         if (getTool() != null) {
53             getTool().error(s, getFilename(), -1, -1);
54         }
55         else {
56             super.reportError(s);
57         }
58     }
59
60     /** Delegates the error message to the tool if any was registered via
61      * {@link #initTool(persistence.antlr.Tool)}
62      * @since 2.7.2
63      */

64     public void reportError(RecognitionException e) {
65         if (getTool() != null) {
66             getTool().error(e.getErrorMessage(), e.getFilename(), e.getLine(), e.getColumn());
67         }
68         else {
69             super.reportError(e);
70         }
71     }
72
73     /** Delegates the warning message to the tool if any was registered via
74      * {@link #initTool(persistence.antlr.Tool)}
75      * @since 2.7.2
76      */

77     public void reportWarning(String JavaDoc s) {
78         if (getTool() != null) {
79             getTool().warning(s, getFilename(), -1, -1);
80         }
81         else {
82             super.reportWarning(s);
83         }
84     }
85
86 protected ANTLRTokdefParser(TokenBuffer tokenBuf, int k) {
87   super(tokenBuf,k);
88   tokenNames = _tokenNames;
89 }
90
91 public ANTLRTokdefParser(TokenBuffer tokenBuf) {
92   this(tokenBuf,3);
93 }
94
95 protected ANTLRTokdefParser(TokenStream lexer, int k) {
96   super(lexer,k);
97   tokenNames = _tokenNames;
98 }
99
100 public ANTLRTokdefParser(TokenStream lexer) {
101   this(lexer,3);
102 }
103
104 public ANTLRTokdefParser(ParserSharedInputState state) {
105   super(state,3);
106   tokenNames = _tokenNames;
107 }
108
109     public final void file(
110         ImportVocabTokenManager tm
111     ) throws RecognitionException, TokenStreamException {
112         
113         Token name = null;
114         
115         try { // for error handling
116
name = LT(1);
117             match(ID);
118             {
119             _loop225:
120             do {
121                 if ((LA(1)==ID||LA(1)==STRING)) {
122                     line(tm);
123                 }
124                 else {
125                     break _loop225;
126                 }
127                 
128             } while (true);
129             }
130         }
131         catch (RecognitionException ex) {
132             reportError(ex);
133             consume();
134             consumeUntil(_tokenSet_0);
135         }
136     }
137     
138     public final void line(
139         ImportVocabTokenManager tm
140     ) throws RecognitionException, TokenStreamException {
141         
142         Token s1 = null;
143         Token lab = null;
144         Token s2 = null;
145         Token id = null;
146         Token para = null;
147         Token id2 = null;
148         Token i = null;
149         Token t=null; Token s=null;
150         
151         try { // for error handling
152
{
153             if ((LA(1)==STRING)) {
154                 s1 = LT(1);
155                 match(STRING);
156                 s = s1;
157             }
158             else if ((LA(1)==ID) && (LA(2)==ASSIGN) && (LA(3)==STRING)) {
159                 lab = LT(1);
160                 match(ID);
161                 t = lab;
162                 match(ASSIGN);
163                 s2 = LT(1);
164                 match(STRING);
165                 s = s2;
166             }
167             else if ((LA(1)==ID) && (LA(2)==LPAREN)) {
168                 id = LT(1);
169                 match(ID);
170                 t=id;
171                 match(LPAREN);
172                 para = LT(1);
173                 match(STRING);
174                 match(RPAREN);
175             }
176             else if ((LA(1)==ID) && (LA(2)==ASSIGN) && (LA(3)==INT)) {
177                 id2 = LT(1);
178                 match(ID);
179                 t=id2;
180             }
181             else {
182                 throw new NoViableAltException(LT(1), getFilename());
183             }
184             
185             }
186             match(ASSIGN);
187             i = LT(1);
188             match(INT);
189             
190                     Integer JavaDoc value = Integer.valueOf(i.getText());
191                     // if literal found, define as a string literal
192
if ( s!=null ) {
193                         tm.define(s.getText(), value.intValue());
194                         // if label, then label the string and map label to token symbol also
195
if ( t!=null ) {
196                             StringLiteralSymbol sl =
197                                 (StringLiteralSymbol) tm.getTokenSymbol(s.getText());
198                             sl.setLabel(t.getText());
199                             tm.mapToTokenSymbol(t.getText(), sl);
200                         }
201                     }
202                     // define token (not a literal)
203
else if ( t!=null ) {
204                         tm.define(t.getText(), value.intValue());
205                         if ( para!=null ) {
206                             TokenSymbol ts = tm.getTokenSymbol(t.getText());
207                             ts.setParaphrase(
208                                 para.getText()
209                             );
210                         }
211                     }
212                     
213         }
214         catch (RecognitionException ex) {
215             reportError(ex);
216             consume();
217             consumeUntil(_tokenSet_1);
218         }
219     }
220     
221     
222     public static final String JavaDoc[] _tokenNames = {
223         "<0>",
224         "EOF",
225         "<2>",
226         "NULL_TREE_LOOKAHEAD",
227         "ID",
228         "STRING",
229         "ASSIGN",
230         "LPAREN",
231         "RPAREN",
232         "INT",
233         "WS",
234         "SL_COMMENT",
235         "ML_COMMENT",
236         "ESC",
237         "DIGIT",
238         "XDIGIT"
239     };
240     
241     private static final long[] mk_tokenSet_0() {
242         long[] data = { 2L, 0L};
243         return data;
244     }
245     public static final BitSet _tokenSet_0 = new BitSet(mk_tokenSet_0());
246     private static final long[] mk_tokenSet_1() {
247         long[] data = { 50L, 0L};
248         return data;
249     }
250     public static final BitSet _tokenSet_1 = new BitSet(mk_tokenSet_1());
251     
252     }
253
Popular Tags