KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > preprocessor > Preprocessor


1 // $ANTLR 2.7.2a1 (20011228-1): "preproc.g" -> "Preprocessor.java"$
2

3 package antlr.preprocessor;
4
5 import antlr.TokenBuffer;
6 import antlr.TokenStreamException;
7 import antlr.TokenStreamIOException;
8 import antlr.ANTLRException;
9 import antlr.LLkParser;
10 import antlr.Token;
11 import antlr.TokenStream;
12 import antlr.RecognitionException;
13 import antlr.NoViableAltException;
14 import antlr.MismatchedTokenException;
15 import antlr.SemanticException;
16 import antlr.ParserSharedInputState;
17 import antlr.collections.impl.BitSet;
18 import antlr.collections.AST;
19 import antlr.ASTPair;
20 import antlr.collections.impl.ASTArray;
21
22 import antlr.collections.impl.IndexedVector;
23 import java.util.Hashtable JavaDoc;
24 import antlr.preprocessor.Grammar;
25
26 public class Preprocessor extends antlr.LLkParser
27        implements PreprocessorTokenTypes
28  {
29
30 protected Preprocessor(TokenBuffer tokenBuf, int k) {
31   super(tokenBuf,k);
32   tokenNames = _tokenNames;
33 }
34
35 public Preprocessor(TokenBuffer tokenBuf) {
36   this(tokenBuf,1);
37 }
38
39 protected Preprocessor(TokenStream lexer, int k) {
40   super(lexer,k);
41   tokenNames = _tokenNames;
42 }
43
44 public Preprocessor(TokenStream lexer) {
45   this(lexer,1);
46 }
47
48 public Preprocessor(ParserSharedInputState state) {
49   super(state,1);
50   tokenNames = _tokenNames;
51 }
52
53     public final void grammarFile(
54         Hierarchy hier, String JavaDoc file
55     ) throws RecognitionException, TokenStreamException {
56         
57         Token hdr = null;
58         
59             Grammar gr;
60             IndexedVector opt=null;
61         
62         
63         try { // for error handling
64
{
65             _loop3:
66             do {
67                 if ((LA(1)==HEADER_ACTION)) {
68                     hdr = LT(1);
69                     match(HEADER_ACTION);
70                     hier.getFile(file).addHeaderAction(hdr.getText());
71                 }
72                 else {
73                     break _loop3;
74                 }
75                 
76             } while (true);
77             }
78             {
79             switch ( LA(1)) {
80             case OPTIONS_START:
81             {
82                 opt=optionSpec(null);
83                 break;
84             }
85             case EOF:
86             case ACTION:
87             case LITERAL_class:
88             {
89                 break;
90             }
91             default:
92             {
93                 throw new NoViableAltException(LT(1), getFilename());
94             }
95             }
96             }
97             {
98             _loop6:
99             do {
100                 if ((LA(1)==ACTION||LA(1)==LITERAL_class)) {
101                     gr=class_def(file, hier);
102                     
103                                 if ( gr!=null && opt!=null ) {
104                                     hier.getFile(file).setOptions(opt);
105                                 }
106                                 if ( gr!=null ) {
107                                     gr.setFileName(file);
108                                     hier.addGrammar(gr);
109                                 }
110                                 
111                 }
112                 else {
113                     break _loop6;
114                 }
115                 
116             } while (true);
117             }
118             match(Token.EOF_TYPE);
119         }
120         catch (RecognitionException ex) {
121             reportError(ex);
122             consume();
123             consumeUntil(_tokenSet_0);
124         }
125     }
126     
127     public final IndexedVector optionSpec(
128         Grammar gr
129     ) throws RecognitionException, TokenStreamException {
130         IndexedVector options;
131         
132         Token op = null;
133         Token rhs = null;
134         
135             options = new IndexedVector();
136         
137         
138         try { // for error handling
139
match(OPTIONS_START);
140             {
141             _loop16:
142             do {
143                 if ((LA(1)==ID)) {
144                     op = LT(1);
145                     match(ID);
146                     rhs = LT(1);
147                     match(ASSIGN_RHS);
148                     
149                                     Option newOp = new Option(op.getText(),rhs.getText(),gr);
150                                     options.appendElement(newOp.getName(),newOp);
151                                     if ( gr!=null && op.getText().equals("importVocab") ) {
152                                         gr.specifiedVocabulary = true;
153                                         gr.importVocab = rhs.getText();
154                                     }
155                                     else if ( gr!=null && op.getText().equals("exportVocab") ) {
156                                         // don't want ';' included in outputVocab.
157
// This is heinously inconsistent! Ugh.
158
gr.exportVocab = rhs.getText().substring(0,rhs.getText().length()-1);
159                                     }
160                                     
161                 }
162                 else {
163                     break _loop16;
164                 }
165                 
166             } while (true);
167             }
168             match(RCURLY);
169         }
170         catch (RecognitionException ex) {
171             reportError(ex);
172             consume();
173             consumeUntil(_tokenSet_1);
174         }
175         return options;
176     }
177     
178     public final Grammar class_def(
179         String JavaDoc file, Hierarchy hier
180     ) throws RecognitionException, TokenStreamException {
181         Grammar gr;
182         
183         Token preamble = null;
184         Token sub = null;
185         Token sup = null;
186         Token tk = null;
187         Token memberA = null;
188         
189             gr=null;
190             IndexedVector rules = new IndexedVector(100);
191             IndexedVector classOptions = null;
192         
193         
194         try { // for error handling
195
{
196             switch ( LA(1)) {
197             case ACTION:
198             {
199                 preamble = LT(1);
200                 match(ACTION);
201                 break;
202             }
203             case LITERAL_class:
204             {
205                 break;
206             }
207             default:
208             {
209                 throw new NoViableAltException(LT(1), getFilename());
210             }
211             }
212             }
213             match(LITERAL_class);
214             sub = LT(1);
215             match(ID);
216             match(LITERAL_extends);
217             sup = LT(1);
218             match(ID);
219             match(SEMI);
220             
221                         gr = (Grammar)hier.getGrammar(sub.getText());
222                         if ( gr!=null ) {
223             // antlr.Tool.toolError("redefinition of grammar "+gr.getName()+" ignored");
224
gr=null;
225                             throw new SemanticException("redefinition of grammar "+sub.getText(), file, sub.getLine(), sub.getColumn());
226                         }
227                         else {
228                             gr = new Grammar(hier.getTool(), sub.getText(), sup.getText(), rules);
229                             if ( preamble!=null ) {
230                                 gr.setPreambleAction(preamble.getText());
231                             }
232                         }
233                     
234             {
235             switch ( LA(1)) {
236             case OPTIONS_START:
237             {
238                 classOptions=optionSpec(gr);
239                 break;
240             }
241             case ACTION:
242             case ID:
243             case TOKENS_SPEC:
244             case LITERAL_protected:
245             case LITERAL_private:
246             case LITERAL_public:
247             {
248                 break;
249             }
250             default:
251             {
252                 throw new NoViableAltException(LT(1), getFilename());
253             }
254             }
255             }
256             
257                     if ( gr!=null ) {
258                         gr.setOptions(classOptions);
259                     }
260                     
261             {
262             switch ( LA(1)) {
263             case TOKENS_SPEC:
264             {
265                 tk = LT(1);
266                 match(TOKENS_SPEC);
267                 gr.setTokenSection(tk.getText());
268                 break;
269             }
270             case ACTION:
271             case ID:
272             case LITERAL_protected:
273             case LITERAL_private:
274             case LITERAL_public:
275             {
276                 break;
277             }
278             default:
279             {
280                 throw new NoViableAltException(LT(1), getFilename());
281             }
282             }
283             }
284             {
285             switch ( LA(1)) {
286             case ACTION:
287             {
288                 memberA = LT(1);
289                 match(ACTION);
290                 gr.setMemberAction(memberA.getText());
291                 break;
292             }
293             case ID:
294             case LITERAL_protected:
295             case LITERAL_private:
296             case LITERAL_public:
297             {
298                 break;
299             }
300             default:
301             {
302                 throw new NoViableAltException(LT(1), getFilename());
303             }
304             }
305             }
306             {
307             int _cnt13=0;
308             _loop13:
309             do {
310                 if ((_tokenSet_2.member(LA(1)))) {
311                     rule(gr);
312                 }
313                 else {
314                     if ( _cnt13>=1 ) { break _loop13; } else {throw new NoViableAltException(LT(1), getFilename());}
315                 }
316                 
317                 _cnt13++;
318             } while (true);
319             }
320         }
321         catch (RecognitionException ex) {
322             reportError(ex);
323             consume();
324             consumeUntil(_tokenSet_3);
325         }
326         return gr;
327     }
328     
329     public final void rule(
330         Grammar gr
331     ) throws RecognitionException, TokenStreamException {
332         
333         Token r = null;
334         Token arg = null;
335         Token ret = null;
336         Token init = null;
337         Token blk = null;
338         
339             IndexedVector o = null; // options for rule
340
String JavaDoc vis = null;
341             boolean bang=false;
342             String JavaDoc eg=null, thr="";
343         
344         
345         try { // for error handling
346
{
347             switch ( LA(1)) {
348             case LITERAL_protected:
349             {
350                 match(LITERAL_protected);
351                 vis="protected";
352                 break;
353             }
354             case LITERAL_private:
355             {
356                 match(LITERAL_private);
357                 vis="private";
358                 break;
359             }
360             case LITERAL_public:
361             {
362                 match(LITERAL_public);
363                 vis="public";
364                 break;
365             }
366             case ID:
367             {
368                 break;
369             }
370             default:
371             {
372                 throw new NoViableAltException(LT(1), getFilename());
373             }
374             }
375             }
376             r = LT(1);
377             match(ID);
378             {
379             switch ( LA(1)) {
380             case BANG:
381             {
382                 match(BANG);
383                 bang=true;
384                 break;
385             }
386             case ACTION:
387             case OPTIONS_START:
388             case ARG_ACTION:
389             case LITERAL_returns:
390             case RULE_BLOCK:
391             case LITERAL_throws:
392             {
393                 break;
394             }
395             default:
396             {
397                 throw new NoViableAltException(LT(1), getFilename());
398             }
399             }
400             }
401             {
402             switch ( LA(1)) {
403             case ARG_ACTION:
404             {
405                 arg = LT(1);
406                 match(ARG_ACTION);
407                 break;
408             }
409             case ACTION:
410             case OPTIONS_START:
411             case LITERAL_returns:
412             case RULE_BLOCK:
413             case LITERAL_throws:
414             {
415                 break;
416             }
417             default:
418             {
419                 throw new NoViableAltException(LT(1), getFilename());
420             }
421             }
422             }
423             {
424             switch ( LA(1)) {
425             case LITERAL_returns:
426             {
427                 match(LITERAL_returns);
428                 ret = LT(1);
429                 match(ARG_ACTION);
430                 break;
431             }
432             case ACTION:
433             case OPTIONS_START:
434             case RULE_BLOCK:
435             case LITERAL_throws:
436             {
437                 break;
438             }
439             default:
440             {
441                 throw new NoViableAltException(LT(1), getFilename());
442             }
443             }
444             }
445             {
446             switch ( LA(1)) {
447             case LITERAL_throws:
448             {
449                 thr=throwsSpec();
450                 break;
451             }
452             case ACTION:
453             case OPTIONS_START:
454             case RULE_BLOCK:
455             {
456                 break;
457             }
458             default:
459             {
460                 throw new NoViableAltException(LT(1), getFilename());
461             }
462             }
463             }
464             {
465             switch ( LA(1)) {
466             case OPTIONS_START:
467             {
468                 o=optionSpec(null);
469                 break;
470             }
471             case ACTION:
472             case RULE_BLOCK:
473             {
474                 break;
475             }
476             default:
477             {
478                 throw new NoViableAltException(LT(1), getFilename());
479             }
480             }
481             }
482             {
483             switch ( LA(1)) {
484             case ACTION:
485             {
486                 init = LT(1);
487                 match(ACTION);
488                 break;
489             }
490             case RULE_BLOCK:
491             {
492                 break;
493             }
494             default:
495             {
496                 throw new NoViableAltException(LT(1), getFilename());
497             }
498             }
499             }
500             blk = LT(1);
501             match(RULE_BLOCK);
502             eg=exceptionGroup();
503             
504                     String JavaDoc rtext = blk.getText()+eg;
505                     Rule ppr = new Rule(r.getText(),rtext,o,gr);
506                     ppr.setThrowsSpec(thr);
507                     if ( arg!=null ) {
508                         ppr.setArgs(arg.getText());
509                     }
510                     if ( ret!=null ) {
511                         ppr.setReturnValue(ret.getText());
512                     }
513                     if ( init!=null ) {
514                         ppr.setInitAction(init.getText());
515                     }
516                     if ( bang ) {
517                         ppr.setBang();
518                     }
519                     ppr.setVisibility(vis);
520                     if ( gr!=null ) {
521                         gr.addRule(ppr);
522                     }
523                     
524         }
525         catch (RecognitionException ex) {
526             reportError(ex);
527             consume();
528             consumeUntil(_tokenSet_4);
529         }
530     }
531     
532     public final String JavaDoc throwsSpec() throws RecognitionException, TokenStreamException {
533         String JavaDoc t;
534         
535         Token a = null;
536         Token b = null;
537         t="throws ";
538         
539         try { // for error handling
540
match(LITERAL_throws);
541             a = LT(1);
542             match(ID);
543             t+=a.getText();
544             {
545             _loop27:
546             do {
547                 if ((LA(1)==COMMA)) {
548                     match(COMMA);
549                     b = LT(1);
550                     match(ID);
551                     t+=","+b.getText();
552                 }
553                 else {
554                     break _loop27;
555                 }
556                 
557             } while (true);
558             }
559         }
560         catch (RecognitionException ex) {
561             reportError(ex);
562             consume();
563             consumeUntil(_tokenSet_5);
564         }
565         return t;
566     }
567     
568     public final String JavaDoc exceptionGroup() throws RecognitionException, TokenStreamException {
569         String JavaDoc g;
570         
571         String JavaDoc e=null; g="";
572         
573         try { // for error handling
574
{
575             _loop30:
576             do {
577                 if ((LA(1)==LITERAL_exception)) {
578                     e=exceptionSpec();
579                     g += e;
580                 }
581                 else {
582                     break _loop30;
583                 }
584                 
585             } while (true);
586             }
587         }
588         catch (RecognitionException ex) {
589             reportError(ex);
590             consume();
591             consumeUntil(_tokenSet_4);
592         }
593         return g;
594     }
595     
596     public final String JavaDoc exceptionSpec() throws RecognitionException, TokenStreamException {
597         String JavaDoc es;
598         
599         Token aa = null;
600         String JavaDoc h=null;
601         es = System.getProperty("line.separator")+"exception ";
602         
603         
604         try { // for error handling
605
match(LITERAL_exception);
606             {
607             switch ( LA(1)) {
608             case ARG_ACTION:
609             {
610                 aa = LT(1);
611                 match(ARG_ACTION);
612                 es += aa.getText();
613                 break;
614             }
615             case EOF:
616             case ACTION:
617             case LITERAL_class:
618             case ID:
619             case LITERAL_protected:
620             case LITERAL_private:
621             case LITERAL_public:
622             case LITERAL_exception:
623             case LITERAL_catch:
624             {
625                 break;
626             }
627             default:
628             {
629                 throw new NoViableAltException(LT(1), getFilename());
630             }
631             }
632             }
633             {
634             _loop34:
635             do {
636                 if ((LA(1)==LITERAL_catch)) {
637                     h=exceptionHandler();
638                     es += h;
639                 }
640                 else {
641                     break _loop34;
642                 }
643                 
644             } while (true);
645             }
646         }
647         catch (RecognitionException ex) {
648             reportError(ex);
649             consume();
650             consumeUntil(_tokenSet_6);
651         }
652         return es;
653     }
654     
655     public final String JavaDoc exceptionHandler() throws RecognitionException, TokenStreamException {
656         String JavaDoc h;
657         
658         Token a1 = null;
659         Token a2 = null;
660         h=null;
661         
662         try { // for error handling
663
match(LITERAL_catch);
664             a1 = LT(1);
665             match(ARG_ACTION);
666             a2 = LT(1);
667             match(ACTION);
668             h = System.getProperty("line.separator")+
669                          "catch "+a1.getText()+" "+a2.getText();
670         }
671         catch (RecognitionException ex) {
672             reportError(ex);
673             consume();
674             consumeUntil(_tokenSet_7);
675         }
676         return h;
677     }
678     
679     
680     public static final String JavaDoc[] _tokenNames = {
681         "<0>",
682         "EOF",
683         "<2>",
684         "NULL_TREE_LOOKAHEAD",
685         "\"tokens\"",
686         "HEADER_ACTION",
687         "ACTION",
688         "\"class\"",
689         "ID",
690         "\"extends\"",
691         "SEMI",
692         "TOKENS_SPEC",
693         "OPTIONS_START",
694         "ASSIGN_RHS",
695         "RCURLY",
696         "\"protected\"",
697         "\"private\"",
698         "\"public\"",
699         "BANG",
700         "ARG_ACTION",
701         "\"returns\"",
702         "RULE_BLOCK",
703         "\"throws\"",
704         "COMMA",
705         "\"exception\"",
706         "\"catch\"",
707         "SUBRULE_BLOCK",
708         "ALT",
709         "ELEMENT",
710         "ID_OR_KEYWORD",
711         "CURLY_BLOCK_SCARF",
712         "WS",
713         "NEWLINE",
714         "COMMENT",
715         "SL_COMMENT",
716         "ML_COMMENT",
717         "CHAR_LITERAL",
718         "STRING_LITERAL",
719         "ESC",
720         "DIGIT",
721         "XDIGIT"
722     };
723     
724     private static final long[] mk_tokenSet_0() { long[] data = { 2L, 0L }; return data; }
725     public static final BitSet _tokenSet_0 = new BitSet(mk_tokenSet_0());
726     private static final long[] mk_tokenSet_1() { long[] data = { 2329026L, 0L }; return data; }
727     public static final BitSet _tokenSet_1 = new BitSet(mk_tokenSet_1());
728     private static final long[] mk_tokenSet_2() { long[] data = { 229632L, 0L }; return data; }
729     public static final BitSet _tokenSet_2 = new BitSet(mk_tokenSet_2());
730     private static final long[] mk_tokenSet_3() { long[] data = { 194L, 0L }; return data; }
731     public static final BitSet _tokenSet_3 = new BitSet(mk_tokenSet_3());
732     private static final long[] mk_tokenSet_4() { long[] data = { 229826L, 0L }; return data; }
733     public static final BitSet _tokenSet_4 = new BitSet(mk_tokenSet_4());
734     private static final long[] mk_tokenSet_5() { long[] data = { 2101312L, 0L }; return data; }
735     public static final BitSet _tokenSet_5 = new BitSet(mk_tokenSet_5());
736     private static final long[] mk_tokenSet_6() { long[] data = { 17007042L, 0L }; return data; }
737     public static final BitSet _tokenSet_6 = new BitSet(mk_tokenSet_6());
738     private static final long[] mk_tokenSet_7() { long[] data = { 50561474L, 0L }; return data; }
739     public static final BitSet _tokenSet_7 = new BitSet(mk_tokenSet_7());
740     
741     }
742
Popular Tags