KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > preprocessor > Preprocessor


1 // $ANTLR : "preproc.g" -> "Preprocessor.java"$
2

3 package persistence.antlr.preprocessor;
4
5 import persistence.antlr.TokenBuffer;
6 import persistence.antlr.TokenStreamException;
7 import persistence.antlr.TokenStreamIOException;
8 import persistence.antlr.ANTLRException;
9 import persistence.antlr.LLkParser;
10 import persistence.antlr.Token;
11 import persistence.antlr.TokenStream;
12 import persistence.antlr.RecognitionException;
13 import persistence.antlr.NoViableAltException;
14 import persistence.antlr.MismatchedTokenException;
15 import persistence.antlr.SemanticException;
16 import persistence.antlr.ParserSharedInputState;
17 import persistence.antlr.collections.impl.BitSet;
18
19 import persistence.antlr.collections.impl.IndexedVector;
20 import java.util.Hashtable JavaDoc;
21 import persistence.antlr.preprocessor.Grammar;
22
23 public class Preprocessor extends persistence.antlr.LLkParser implements PreprocessorTokenTypes
24  {
25
26     // This chunk of error reporting code provided by Brian Smith
27

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

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

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

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

80     public void reportWarning(String JavaDoc s) {
81         if (getTool() != null) {
82             getTool().warning(s, getFilename(), -1, -1);
83         }
84         else {
85             super.reportWarning(s);
86         }
87     }
88
89 protected Preprocessor(TokenBuffer tokenBuf, int k) {
90   super(tokenBuf,k);
91   tokenNames = _tokenNames;
92 }
93
94 public Preprocessor(TokenBuffer tokenBuf) {
95   this(tokenBuf,1);
96 }
97
98 protected Preprocessor(TokenStream lexer, int k) {
99   super(lexer,k);
100   tokenNames = _tokenNames;
101 }
102
103 public Preprocessor(TokenStream lexer) {
104   this(lexer,1);
105 }
106
107 public Preprocessor(ParserSharedInputState state) {
108   super(state,1);
109   tokenNames = _tokenNames;
110 }
111
112     public final void grammarFile(
113         Hierarchy hier, String JavaDoc file
114     ) throws RecognitionException, TokenStreamException {
115         
116         Token hdr = null;
117         
118             Grammar gr;
119             IndexedVector opt=null;
120         
121         
122         try { // for error handling
123
{
124             _loop265:
125             do {
126                 if ((LA(1)==HEADER_ACTION)) {
127                     hdr = LT(1);
128                     match(HEADER_ACTION);
129                     hier.getFile(file).addHeaderAction(hdr.getText());
130                 }
131                 else {
132                     break _loop265;
133                 }
134                 
135             } while (true);
136             }
137             {
138             switch ( LA(1)) {
139             case OPTIONS_START:
140             {
141                 opt=optionSpec(null);
142                 break;
143             }
144             case EOF:
145             case ACTION:
146             case LITERAL_class:
147             {
148                 break;
149             }
150             default:
151             {
152                 throw new NoViableAltException(LT(1), getFilename());
153             }
154             }
155             }
156             {
157             _loop268:
158             do {
159                 if ((LA(1)==ACTION||LA(1)==LITERAL_class)) {
160                     gr=class_def(file, hier);
161                     
162                                 if ( gr!=null && opt!=null ) {
163                                     hier.getFile(file).setOptions(opt);
164                                 }
165                                 if ( gr!=null ) {
166                                     gr.setFileName(file);
167                                     hier.addGrammar(gr);
168                                 }
169                                 
170                 }
171                 else {
172                     break _loop268;
173                 }
174                 
175             } while (true);
176             }
177             match(Token.EOF_TYPE);
178         }
179         catch (RecognitionException ex) {
180             reportError(ex);
181             consume();
182             consumeUntil(_tokenSet_0);
183         }
184     }
185     
186     public final IndexedVector optionSpec(
187         Grammar gr
188     ) throws RecognitionException, TokenStreamException {
189         IndexedVector options;
190         
191         Token op = null;
192         Token rhs = null;
193         
194             options = new IndexedVector();
195         
196         
197         try { // for error handling
198
match(OPTIONS_START);
199             {
200             _loop280:
201             do {
202                 if ((LA(1)==ID)) {
203                     op = LT(1);
204                     match(ID);
205                     rhs = LT(1);
206                     match(ASSIGN_RHS);
207                     
208                                     Option newOp = new Option(op.getText(),rhs.getText(),gr);
209                                     options.appendElement(newOp.getName(),newOp);
210                                     if ( gr!=null && op.getText().equals("importVocab") ) {
211                                         gr.specifiedVocabulary = true;
212                                         gr.importVocab = rhs.getText();
213                                     }
214                                     else if ( gr!=null && op.getText().equals("exportVocab") ) {
215                                         // don't want ';' included in outputVocab.
216
// This is heinously inconsistent! Ugh.
217
gr.exportVocab = rhs.getText().substring(0,rhs.getText().length()-1);
218                                         gr.exportVocab = gr.exportVocab.trim();
219                                     }
220                                     
221                 }
222                 else {
223                     break _loop280;
224                 }
225                 
226             } while (true);
227             }
228             match(RCURLY);
229         }
230         catch (RecognitionException ex) {
231             reportError(ex);
232             consume();
233             consumeUntil(_tokenSet_1);
234         }
235         return options;
236     }
237     
238     public final Grammar class_def(
239         String JavaDoc file, Hierarchy hier
240     ) throws RecognitionException, TokenStreamException {
241         Grammar gr;
242         
243         Token preamble = null;
244         Token sub = null;
245         Token sup = null;
246         Token tk = null;
247         Token memberA = null;
248         
249             gr=null;
250             IndexedVector rules = new IndexedVector(100);
251             IndexedVector classOptions = null;
252             String JavaDoc sc = null;
253         
254         
255         try { // for error handling
256
{
257             switch ( LA(1)) {
258             case ACTION:
259             {
260                 preamble = LT(1);
261                 match(ACTION);
262                 break;
263             }
264             case LITERAL_class:
265             {
266                 break;
267             }
268             default:
269             {
270                 throw new NoViableAltException(LT(1), getFilename());
271             }
272             }
273             }
274             match(LITERAL_class);
275             sub = LT(1);
276             match(ID);
277             match(LITERAL_extends);
278             sup = LT(1);
279             match(ID);
280             {
281             switch ( LA(1)) {
282             case SUBRULE_BLOCK:
283             {
284                 sc=superClass();
285                 break;
286             }
287             case SEMI:
288             {
289                 break;
290             }
291             default:
292             {
293                 throw new NoViableAltException(LT(1), getFilename());
294             }
295             }
296             }
297             match(SEMI);
298             
299                         gr = (Grammar)hier.getGrammar(sub.getText());
300                         if ( gr!=null ) {
301             // antlr.Tool.toolError("redefinition of grammar "+gr.getName()+" ignored");
302
gr=null;
303                             throw new SemanticException("redefinition of grammar "+sub.getText(), file, sub.getLine(), sub.getColumn());
304                         }
305                         else {
306                             gr = new Grammar(hier.getTool(), sub.getText(), sup.getText(), rules);
307                             gr.superClass=sc;
308                             if ( preamble!=null ) {
309                                 gr.setPreambleAction(preamble.getText());
310                             }
311                         }
312                     
313             {
314             switch ( LA(1)) {
315             case OPTIONS_START:
316             {
317                 classOptions=optionSpec(gr);
318                 break;
319             }
320             case ACTION:
321             case ID:
322             case TOKENS_SPEC:
323             case LITERAL_protected:
324             case LITERAL_private:
325             case LITERAL_public:
326             {
327                 break;
328             }
329             default:
330             {
331                 throw new NoViableAltException(LT(1), getFilename());
332             }
333             }
334             }
335             
336                     if ( gr!=null ) {
337                         gr.setOptions(classOptions);
338                     }
339                     
340             {
341             switch ( LA(1)) {
342             case TOKENS_SPEC:
343             {
344                 tk = LT(1);
345                 match(TOKENS_SPEC);
346                 gr.setTokenSection(tk.getText());
347                 break;
348             }
349             case ACTION:
350             case ID:
351             case LITERAL_protected:
352             case LITERAL_private:
353             case LITERAL_public:
354             {
355                 break;
356             }
357             default:
358             {
359                 throw new NoViableAltException(LT(1), getFilename());
360             }
361             }
362             }
363             {
364             switch ( LA(1)) {
365             case ACTION:
366             {
367                 memberA = LT(1);
368                 match(ACTION);
369                 gr.setMemberAction(memberA.getText());
370                 break;
371             }
372             case ID:
373             case LITERAL_protected:
374             case LITERAL_private:
375             case LITERAL_public:
376             {
377                 break;
378             }
379             default:
380             {
381                 throw new NoViableAltException(LT(1), getFilename());
382             }
383             }
384             }
385             {
386             int _cnt277=0;
387             _loop277:
388             do {
389                 if ((_tokenSet_2.member(LA(1)))) {
390                     rule(gr);
391                 }
392                 else {
393                     if ( _cnt277>=1 ) { break _loop277; } else {throw new NoViableAltException(LT(1), getFilename());}
394                 }
395                 
396                 _cnt277++;
397             } while (true);
398             }
399         }
400         catch (RecognitionException ex) {
401             reportError(ex);
402             consume();
403             consumeUntil(_tokenSet_3);
404         }
405         return gr;
406     }
407     
408     public final String JavaDoc superClass() throws RecognitionException, TokenStreamException {
409         String JavaDoc sup;
410         
411         sup=LT(1).getText();
412         
413         try { // for error handling
414
match(SUBRULE_BLOCK);
415         }
416         catch (RecognitionException ex) {
417             reportError(ex);
418             consume();
419             consumeUntil(_tokenSet_4);
420         }
421         return sup;
422     }
423     
424     public final void rule(
425         Grammar gr
426     ) throws RecognitionException, TokenStreamException {
427         
428         Token r = null;
429         Token arg = null;
430         Token ret = null;
431         Token init = null;
432         Token blk = null;
433         
434             IndexedVector o = null; // options for rule
435
String JavaDoc vis = null;
436             boolean bang=false;
437             String JavaDoc eg=null, thr="";
438         
439         
440         try { // for error handling
441
{
442             switch ( LA(1)) {
443             case LITERAL_protected:
444             {
445                 match(LITERAL_protected);
446                 vis="protected";
447                 break;
448             }
449             case LITERAL_private:
450             {
451                 match(LITERAL_private);
452                 vis="private";
453                 break;
454             }
455             case LITERAL_public:
456             {
457                 match(LITERAL_public);
458                 vis="public";
459                 break;
460             }
461             case ID:
462             {
463                 break;
464             }
465             default:
466             {
467                 throw new NoViableAltException(LT(1), getFilename());
468             }
469             }
470             }
471             r = LT(1);
472             match(ID);
473             {
474             switch ( LA(1)) {
475             case BANG:
476             {
477                 match(BANG);
478                 bang=true;
479                 break;
480             }
481             case ACTION:
482             case OPTIONS_START:
483             case ARG_ACTION:
484             case LITERAL_returns:
485             case RULE_BLOCK:
486             case LITERAL_throws:
487             {
488                 break;
489             }
490             default:
491             {
492                 throw new NoViableAltException(LT(1), getFilename());
493             }
494             }
495             }
496             {
497             switch ( LA(1)) {
498             case ARG_ACTION:
499             {
500                 arg = LT(1);
501                 match(ARG_ACTION);
502                 break;
503             }
504             case ACTION:
505             case OPTIONS_START:
506             case LITERAL_returns:
507             case RULE_BLOCK:
508             case LITERAL_throws:
509             {
510                 break;
511             }
512             default:
513             {
514                 throw new NoViableAltException(LT(1), getFilename());
515             }
516             }
517             }
518             {
519             switch ( LA(1)) {
520             case LITERAL_returns:
521             {
522                 match(LITERAL_returns);
523                 ret = LT(1);
524                 match(ARG_ACTION);
525                 break;
526             }
527             case ACTION:
528             case OPTIONS_START:
529             case RULE_BLOCK:
530             case LITERAL_throws:
531             {
532                 break;
533             }
534             default:
535             {
536                 throw new NoViableAltException(LT(1), getFilename());
537             }
538             }
539             }
540             {
541             switch ( LA(1)) {
542             case LITERAL_throws:
543             {
544                 thr=throwsSpec();
545                 break;
546             }
547             case ACTION:
548             case OPTIONS_START:
549             case RULE_BLOCK:
550             {
551                 break;
552             }
553             default:
554             {
555                 throw new NoViableAltException(LT(1), getFilename());
556             }
557             }
558             }
559             {
560             switch ( LA(1)) {
561             case OPTIONS_START:
562             {
563                 o=optionSpec(null);
564                 break;
565             }
566             case ACTION:
567             case RULE_BLOCK:
568             {
569                 break;
570             }
571             default:
572             {
573                 throw new NoViableAltException(LT(1), getFilename());
574             }
575             }
576             }
577             {
578             switch ( LA(1)) {
579             case ACTION:
580             {
581                 init = LT(1);
582                 match(ACTION);
583                 break;
584             }
585             case RULE_BLOCK:
586             {
587                 break;
588             }
589             default:
590             {
591                 throw new NoViableAltException(LT(1), getFilename());
592             }
593             }
594             }
595             blk = LT(1);
596             match(RULE_BLOCK);
597             eg=exceptionGroup();
598             
599                     String JavaDoc rtext = blk.getText()+eg;
600                     Rule ppr = new Rule(r.getText(),rtext,o,gr);
601                     ppr.setThrowsSpec(thr);
602                     if ( arg!=null ) {
603                         ppr.setArgs(arg.getText());
604                     }
605                     if ( ret!=null ) {
606                         ppr.setReturnValue(ret.getText());
607                     }
608                     if ( init!=null ) {
609                         ppr.setInitAction(init.getText());
610                     }
611                     if ( bang ) {
612                         ppr.setBang();
613                     }
614                     ppr.setVisibility(vis);
615                     if ( gr!=null ) {
616                         gr.addRule(ppr);
617                     }
618                     
619         }
620         catch (RecognitionException ex) {
621             reportError(ex);
622             consume();
623             consumeUntil(_tokenSet_5);
624         }
625     }
626     
627     public final String JavaDoc throwsSpec() throws RecognitionException, TokenStreamException {
628         String JavaDoc t;
629         
630         Token a = null;
631         Token b = null;
632         t="throws ";
633         
634         try { // for error handling
635
match(LITERAL_throws);
636             a = LT(1);
637             match(ID);
638             t+=a.getText();
639             {
640             _loop291:
641             do {
642                 if ((LA(1)==COMMA)) {
643                     match(COMMA);
644                     b = LT(1);
645                     match(ID);
646                     t+=","+b.getText();
647                 }
648                 else {
649                     break _loop291;
650                 }
651                 
652             } while (true);
653             }
654         }
655         catch (RecognitionException ex) {
656             reportError(ex);
657             consume();
658             consumeUntil(_tokenSet_6);
659         }
660         return t;
661     }
662     
663     public final String JavaDoc exceptionGroup() throws RecognitionException, TokenStreamException {
664         String JavaDoc g;
665         
666         String JavaDoc e=null; g="";
667         
668         try { // for error handling
669
{
670             _loop294:
671             do {
672                 if ((LA(1)==LITERAL_exception)) {
673                     e=exceptionSpec();
674                     g += e;
675                 }
676                 else {
677                     break _loop294;
678                 }
679                 
680             } while (true);
681             }
682         }
683         catch (RecognitionException ex) {
684             reportError(ex);
685             consume();
686             consumeUntil(_tokenSet_5);
687         }
688         return g;
689     }
690     
691     public final String JavaDoc exceptionSpec() throws RecognitionException, TokenStreamException {
692         String JavaDoc es;
693         
694         Token aa = null;
695         String JavaDoc h=null;
696         es = System.getProperty("line.separator")+"exception ";
697         
698         
699         try { // for error handling
700
match(LITERAL_exception);
701             {
702             switch ( LA(1)) {
703             case ARG_ACTION:
704             {
705                 aa = LT(1);
706                 match(ARG_ACTION);
707                 es += aa.getText();
708                 break;
709             }
710             case EOF:
711             case ACTION:
712             case LITERAL_class:
713             case ID:
714             case LITERAL_protected:
715             case LITERAL_private:
716             case LITERAL_public:
717             case LITERAL_exception:
718             case LITERAL_catch:
719             {
720                 break;
721             }
722             default:
723             {
724                 throw new NoViableAltException(LT(1), getFilename());
725             }
726             }
727             }
728             {
729             _loop298:
730             do {
731                 if ((LA(1)==LITERAL_catch)) {
732                     h=exceptionHandler();
733                     es += h;
734                 }
735                 else {
736                     break _loop298;
737                 }
738                 
739             } while (true);
740             }
741         }
742         catch (RecognitionException ex) {
743             reportError(ex);
744             consume();
745             consumeUntil(_tokenSet_7);
746         }
747         return es;
748     }
749     
750     public final String JavaDoc exceptionHandler() throws RecognitionException, TokenStreamException {
751         String JavaDoc h;
752         
753         Token a1 = null;
754         Token a2 = null;
755         h=null;
756         
757         try { // for error handling
758
match(LITERAL_catch);
759             a1 = LT(1);
760             match(ARG_ACTION);
761             a2 = LT(1);
762             match(ACTION);
763             h = System.getProperty("line.separator")+
764                          "catch "+a1.getText()+" "+a2.getText();
765         }
766         catch (RecognitionException ex) {
767             reportError(ex);
768             consume();
769             consumeUntil(_tokenSet_8);
770         }
771         return h;
772     }
773     
774     
775     public static final String JavaDoc[] _tokenNames = {
776         "<0>",
777         "EOF",
778         "<2>",
779         "NULL_TREE_LOOKAHEAD",
780         "\"tokens\"",
781         "HEADER_ACTION",
782         "SUBRULE_BLOCK",
783         "ACTION",
784         "\"class\"",
785         "ID",
786         "\"extends\"",
787         "SEMI",
788         "TOKENS_SPEC",
789         "OPTIONS_START",
790         "ASSIGN_RHS",
791         "RCURLY",
792         "\"protected\"",
793         "\"private\"",
794         "\"public\"",
795         "BANG",
796         "ARG_ACTION",
797         "\"returns\"",
798         "RULE_BLOCK",
799         "\"throws\"",
800         "COMMA",
801         "\"exception\"",
802         "\"catch\"",
803         "ALT",
804         "ELEMENT",
805         "LPAREN",
806         "RPAREN",
807         "ID_OR_KEYWORD",
808         "CURLY_BLOCK_SCARF",
809         "WS",
810         "NEWLINE",
811         "COMMENT",
812         "SL_COMMENT",
813         "ML_COMMENT",
814         "CHAR_LITERAL",
815         "STRING_LITERAL",
816         "ESC",
817         "DIGIT",
818         "XDIGIT"
819     };
820     
821     private static final long[] mk_tokenSet_0() {
822         long[] data = { 2L, 0L};
823         return data;
824     }
825     public static final BitSet _tokenSet_0 = new BitSet(mk_tokenSet_0());
826     private static final long[] mk_tokenSet_1() {
827         long[] data = { 4658050L, 0L};
828         return data;
829     }
830     public static final BitSet _tokenSet_1 = new BitSet(mk_tokenSet_1());
831     private static final long[] mk_tokenSet_2() {
832         long[] data = { 459264L, 0L};
833         return data;
834     }
835     public static final BitSet _tokenSet_2 = new BitSet(mk_tokenSet_2());
836     private static final long[] mk_tokenSet_3() {
837         long[] data = { 386L, 0L};
838         return data;
839     }
840     public static final BitSet _tokenSet_3 = new BitSet(mk_tokenSet_3());
841     private static final long[] mk_tokenSet_4() {
842         long[] data = { 2048L, 0L};
843         return data;
844     }
845     public static final BitSet _tokenSet_4 = new BitSet(mk_tokenSet_4());
846     private static final long[] mk_tokenSet_5() {
847         long[] data = { 459650L, 0L};
848         return data;
849     }
850     public static final BitSet _tokenSet_5 = new BitSet(mk_tokenSet_5());
851     private static final long[] mk_tokenSet_6() {
852         long[] data = { 4202624L, 0L};
853         return data;
854     }
855     public static final BitSet _tokenSet_6 = new BitSet(mk_tokenSet_6());
856     private static final long[] mk_tokenSet_7() {
857         long[] data = { 34014082L, 0L};
858         return data;
859     }
860     public static final BitSet _tokenSet_7 = new BitSet(mk_tokenSet_7());
861     private static final long[] mk_tokenSet_8() {
862         long[] data = { 101122946L, 0L};
863         return data;
864     }
865     public static final BitSet _tokenSet_8 = new BitSet(mk_tokenSet_8());
866     
867     }
868
Popular Tags