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