Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 545850aa04bbee77363d7e614c4ca0fa59b0ad74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
package org.eclipse.cdt.internal.core.newparser;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

/**
 * This is an attempt at a copyright clean parser.  The grammar is based
 * on the ISO C++ standard
 */
public class Parser {

	private IParserCallback callback;
	private boolean quickParse = false;
	private boolean parsePassed = true;
	
	// TO DO: convert to a real symbol table
	private Map currRegion = new HashMap();
	
	public Parser(IScanner s, IParserCallback c, boolean quick) throws Exception {
		callback = c;
		scanner = s;
		quickParse = quick;
		scanner.setQuickScan(quick);
		scanner.setCallback(c);
		//fetchToken();
	}

	public Parser(IScanner s, IParserCallback c) throws Exception {
		this(s, c, false);
	}
	
	public Parser( IScanner s) throws Exception {
		this(s, new NullParserCallback(), false);
	}
	
	public Parser(String code) throws Exception {
		this(new Scanner().initialize( new StringReader( code ), null
));
	}

	public Parser(String code, IParserCallback c) throws Exception {
		this(new Scanner().initialize( new StringReader( code ), null
), c, false);
	}

	public Parser(InputStream stream, IParserCallback c, boolean quick) throws Exception {
		this(new Scanner().initialize( new InputStreamReader(stream), null ), 
c, quick);
	}
	
	private static int parseCount = 0;
	
	public boolean parse() throws Exception {
		long startTime = System.currentTimeMillis();
		translationUnit();
		System.out.println("Parse " + (++parseCount) + ": "
			+ ( System.currentTimeMillis() - startTime ) + "ms"
			+ ( parsePassed ? "" : " - parse failure" ));
			
		return parsePassed;
	}
	
	/**
	 * translationUnit
	 * : (declaration)*
	 * 
	 */
	public void translationUnit() throws Exception {
		Object translationUnit = callback.translationUnitBegin();
		Token lastBacktrack = null;
		while (LT(1) != Token.tEOF) {
			try {
				declaration( translationUnit );
			} catch (Backtrack b) {
				// Mark as failure and try to reach a recovery point
				parsePassed = false;
				
				if (lastBacktrack != null && lastBacktrack == LA(1)) {
					// we haven't progressed from the last backtrack
					// try and find tne next definition
					for (int t = LT(1); t != Token.tEOF; t = LT(1)) {
						consume();
						// TO DO: we should really check for matching braces too
						if (t == Token.tSEMI)
							break;
					}
				} else {
					// start again from here
					lastBacktrack = LA(1);
				}
			}
		}
		callback.translationUnitEnd(translationUnit);
	}
	
	/**
	 * declaration
	 * : {"asm"} asmDefinition
	 * | {"namespace"} namespaceDefinition
	 * | {"using"} usingDeclaration
	 * | {"export"|"template"} templateDeclaration
	 * | {"extern"} linkageSpecification
	 * | simpleDeclaration
	 * 
	 * Notes:
	 * - folded in blockDeclaration
	 * - merged alternatives that required same LA
	 *   - functionDefinition into simpleDeclaration
	 *   - namespaceAliasDefinition into namespaceDefinition
	 *   - usingDirective into usingDeclaration
	 *   - explicitInstantiation and explicitSpecialization into
	 *       templateDeclaration
	 */
	public void declaration( Object container ) throws Exception {
		switch (LT(1)) {
			case Token.t_asm:
				// asmDefinition( );
				return; 
			case Token.t_namespace:
				// namespaceDefinition();
				return; 
			case Token.t_using:
				// usingDeclaration();
				return; 
			case Token.t_export:
			case Token.t_template:
				// templateDeclaration();
				return; 
			case Token.t_extern:
				if (LT(1) == Token.tSTRING)
				{
					// linkageSpecification();
					return; 
				}
					
				// else drop through
			default:
				simpleDeclaration( container ); 
		}
	}
	
	
	
	/**
	 * simpleDeclaration
	 * : (declSpecifier)* (initDeclarator ("," initDeclarator)*)? 
	 *     (";" | {"{"} functionBody)
	 * 
	 * Notes:
	 * - append functionDefinition stuff to end of this rule
	 * 
	 * To do:
	 * - work in ctorInitializer and functionTryBlock
	 */
	public void simpleDeclaration( Object container ) throws Exception {
		Object simpleDecl = callback.simpleDeclarationBegin( container, LA(1));
		declSpecifierSeq(simpleDecl);

		if (LT(1) != Token.tSEMI)
			try {
				initDeclarator(simpleDecl);
				
				while (LT(1) == Token.tCOMMA) {
					consume();
					
					try {
						initDeclarator(simpleDecl);
					} catch (Backtrack b) {
						throw b;
					}
				}
			} catch (Backtrack b) {
				// allowed to be empty
			}
		
		switch (LT(1)) {
			case Token.tSEMI:
				consume();
				break;
			case Token.tLBRACE:
				callback.functionBodyBegin();
				if (quickParse) {
					// speed up the parser by skiping the body
					// simply look for matching brace and return
					consume();
					int depth = 1;
					while (depth > 0) {
						switch (consume().getType()) {
							case Token.tRBRACE:
								--depth;
								break;
							case Token.tLBRACE:
								++depth;
								break;
							case Token.tEOF:
								// Oops, no match
								throw backtrack;
						}
					}
				} else {
					functionBody();
				}
				callback.functionBodyEnd();
				break;
			default:
				break;
		}
		
		callback.simpleDeclarationEnd(simpleDecl);
	}
	
	
	public void parameterDeclaration( Object containerObject ) throws Exception
	{
		Object parameterDecl = callback.parameterDeclarationBegin( containerObject, LA(1) );
		declSpecifierSeq( parameterDecl );
		
		if (LT(1) != Token.tSEMI)
			try {
				initDeclarator(parameterDecl);
				
			} catch (Backtrack b) {
				// allowed to be empty
			}
 		 
		callback.parameterDeclarationEnd( parameterDecl );
		 
	}
	
	/**
	 * declSpecifier
	 * : "auto" | "register" | "static" | "extern" | "mutable"
	 * | "inline" | "virtual" | "explicit"
	 * | "char" | "wchar_t" | "bool" | "short" | "int" | "long"
	 * | "signed" | "unsigned" | "float" | "double" | "void"
	 * | "const" | "volatile"
	 * | "friend" | "typedef"
	 * | ("typename")? name
	 * | {"class"|"struct"|"union"} classSpecifier
	 * | {"enum"} enumSpecifier
	 * 
	 * Notes:
	 * - folded in storageClassSpecifier, typeSpecifier, functionSpecifier
	 * - folded elaboratedTypeSpecifier into classSpecifier and enumSpecifier
	 * - find template names in name
	 */
	public void declSpecifierSeq( Object decl ) throws Exception {
		declSpecifiers:		
		for (;;) {
			switch (LT(1)) {
				case Token.t_auto:
				case Token.t_register:
				case Token.t_static:
				case Token.t_extern:
				case Token.t_mutable:
				case Token.t_inline:
				case Token.t_virtual:
				case Token.t_explicit:
				case Token.t_typedef:
				case Token.t_friend:
				case Token.t_const:
				case Token.t_volatile:
				case Token.t_char:
				case Token.t_wchar_t:
				case Token.t_bool:
				case Token.t_short:
				case Token.t_int:
				case Token.t_long:
				case Token.t_signed:
				case Token.t_unsigned:
				case Token.t_float:
				case Token.t_double:
				case Token.t_void:
					callback.simpleDeclSpecifier(decl, consume());
					break;
				case Token.t_typename:
					consume();
					name();
					break;
				case Token.tCOLONCOLON:
					consume();
					// handle nested later:
				case Token.tIDENTIFIER:
					// handle nested later:
					if (currRegion.get(LA(1).getImage()) != null) {
						consume();
						break;
					}
					else
						break declSpecifiers;
				case Token.t_class:
				case Token.t_struct:
				case Token.t_union:
					classSpecifier(decl);
					break;
				case Token.t_enum:
					// enumSpecifier();
					break;
				default:
					break declSpecifiers;
			}
		}
	}
	
	/**
	 * name
	 * : ("::")? name2 ("::" name2)*
	 * 
	 * name2
	 * : IDENTIFER
	 * 
	 * To Do:
	 * - Handle template ids
	 * - Handle unqualifiedId
	 */
	public boolean name() throws Exception {
		Token last = null;
		
		callback.nameBegin(LA(1));
		
		if (LT(1) == Token.tCOLONCOLON)
			last = consume();

		last = consume(Token.tIDENTIFIER);

		while (LT(1) == Token.tCOLONCOLON) {
			last = consume();
			
			last = consume(Token.tIDENTIFIER);
		}

		callback.nameEnd(last);

		return true;
	}

	/**
	 * cvQualifier
	 * : "const" | "volatile"
	 */
	public Object cvQualifier() throws Exception {
		switch (LT(1)) {
			case Token.t_const:
			case Token.t_volatile:
				consume();
				return null;
			default:
				throw backtrack;
		}
	}
	
	/**
	 * initDeclarator
	 * : declarator ("=" initializerClause | "(" expressionList ")")?
	 * 
	 * To Do:
	 * - handle initializers
	 */
	public void initDeclarator( Object owner ) throws Exception {
		declarator( owner );
	}
	
	/**
	 * declarator
	 * : (ptrOperator)* directDeclarator
	 * 
	 * directDeclarator
	 * : declaratorId
	 * | directDeclarator "(" parameterDeclarationClause ")" (cvQualifier)*
	 *     (exceptionSpecification)*
	 * | directDeclarator "[" (constantExpression)? "]"
	 * | "(" declarator")"
	 * 
	 * declaratorId
	 * : name
	 */
	public void declarator( Object container ) throws Exception {
		
		Object declarator = callback.declaratorBegin( container );
		
		for (;;) {
			try {
				ptrOperator();
			} catch (Backtrack b) {
				break;
			}
		}
		
		if (LT(1) == Token.tLPAREN) {
			consume();
			declarator(declarator);
			consume(Token.tRPAREN);
			return;
		}
		
		name();
		callback.declaratorId(declarator);
		
		for (;;) {
			switch (LT(1)) {
				case Token.tLPAREN:
					// parameterDeclarationClause
					Object clause = callback.argumentsBegin(declarator);
					consume();
					parameterDeclarationLoop:
					for (;;) {
						switch (LT(1)) {
							case Token.tRPAREN:
								consume();
								break parameterDeclarationLoop;
							case Token.tELIPSE:
								consume();
								break;
							case Token.tCOMMA:
								consume();
								break;
							default:
								parameterDeclaration( clause );  
						}
					}
					callback.argumentsEnd();
					break;
				case Token.tLBRACKET:
					consume();
					// constantExpression();
					consume(Token.tRBRACKET);
					continue;
			}
			break;
		}
		
		callback.declaratorEnd(declarator);
	}
	
	/**
	 * ptrOperator
	 * : "*" (cvQualifier)*
	 * | "&"
	 * | name "*" (cvQualifier)*
	 */
	public Object ptrOperator() throws Exception {
		int t = LT(1);
		
		if (t == Token.tAMPER) {					
			consume();
			return null;
		}
		
		Token mark = mark();
		if (t == Token.tIDENTIFIER || t == Token.tCOLONCOLON)
		name();

		if (t == Token.tSTAR) {
			consume();

			for (;;) {
				try {
					cvQualifier();
				} catch (Backtrack b) {
					break;
				}
			}			
			
			return null;
		}
		
		backup(mark);
		throw backtrack;
	}

	/**
	 * classSpecifier
	 * : classKey name (baseClause)? "{" (memberSpecification)* "}"
	 */
	public void classSpecifier( Object owner ) throws Exception {
		Token classKey = null;
		
		// class key
		switch (LT(1)) {
			case Token.t_class:
			case Token.t_struct:
			case Token.t_union:
				classKey = consume();
				break;
			default:
				throw backtrack;
		}

		Object classSpec = callback.classSpecifierBegin( owner, classKey);
		
		// class name
		if (LT(1) == Token.tIDENTIFIER) {
			name();
			callback.classSpecifierName(classSpec);			
		}

		//currRegion.put(name.getImage(), classKey);
		
		// base clause
		if (LT(1) == Token.tCOLON) {
			consume();
			baseSpecifier( classSpec );
		}
		
		// If we don't get a "{", assume elaborated type
		if (LT(1) == Token.tLBRACE) {
			consume();
			
			memberDeclarationLoop:
			while (LT(1) != Token.tRBRACE) {
				switch (LT(1)) {
					case Token.t_public:
						consume();
						consume(Token.tCOLON);
						break;
					case Token.t_protected:
						consume();
						consume(Token.tCOLON);
						break;
					case Token.t_private:
						consume();
						consume(Token.tCOLON);
						break;
					case Token.tRBRACE:
						consume(Token.tRBRACE);
						break memberDeclarationLoop;
					default:
						declaration(classSpec);
				}
			}
			// consume the }
			consume();
		}
		
		callback.classSpecifierEnd(classSpec);
	}

	public void baseSpecifier( Object classSpecOwner ) throws Exception {

		Object baseSpecifier = callback.baseSpecifierBegin( classSpecOwner ); 		
		
		baseSpecifierLoop:
		for (;;) {
			switch (LT(1)) {
				case Token.t_virtual:
					callback.baseSpecifierVirtual( baseSpecifier, true ); 
					consume();
					break;
				case Token.t_public:
				case Token.t_protected:
				case Token.t_private:
					callback.baseSpecifierVisibility( baseSpecifier, currToken );
					consume();					
					break;
				case Token.tCOLONCOLON:
				case Token.tIDENTIFIER:
					name();
					callback.baseSpecifierName( baseSpecifier ); 
					break;
				case Token.tCOMMA:
					callback.baseSpecifierEnd( baseSpecifier ); 
					baseSpecifier = callback.baseSpecifierBegin( classSpecOwner );
					consume(); 
					continue baseSpecifierLoop;
				default:
					break baseSpecifierLoop;
			}
		}
		callback.baseSpecifierEnd( baseSpecifier ); 
	}
	
	public void functionBody() throws Exception {
		compoundStatement();
	}
	
	// Statements
	public void statement() throws Exception {
		switch (LT(1)) {
			case Token.t_case:
				consume();
				constantExpression();
				consume(Token.tCOLON);
				statement();
				return;
			case Token.t_default:
				consume();
				consume(Token.tCOLON);
				statement();
				return;
			case Token.tLBRACE:
				compoundStatement();
				return;
			case Token.t_if:
				consume();
				consume(Token.tLPAREN);
				condition();
				consume(Token.tRPAREN);
				statement();
				if (LT(1) == Token.t_else) {
					consume();
					statement();
				}
				return;
			case Token.t_switch:
				consume();
				consume(Token.tLPAREN);
				condition();
				consume(Token.tRPAREN);
				statement();
				return;
			case Token.t_while:
				consume();
				consume(Token.tLPAREN);
				condition();
				consume(Token.tRPAREN);
				statement();
				return;
			case Token.t_do:
				consume();
				statement();
				consume(Token.t_while);
				consume(Token.tLPAREN);
				condition();
				consume(Token.tRPAREN);
				return;
			case Token.t_for:
				consume();
				consume(Token.tLPAREN);
				forInitStatement();
				if (LT(1) != Token.tSEMI)
					condition();
				consume(Token.tSEMI);
				if (LT(1) != Token.tRPAREN)
					expression();
				consume(Token.tRPAREN);
				statement();
				return;
			case Token.t_break:
				consume();
				consume(Token.tSEMI);
				return;
			case Token.t_continue:
				consume();
				consume(Token.tSEMI);
				return;
			case Token.t_return:
				consume();
				if (LT(1) != Token.tSEMI)
					expression();
				consume(Token.tSEMI);
				return;
			case Token.t_goto:
				consume();
				consume(Token.tIDENTIFIER);
				consume(Token.tSEMI);
				return;
			case Token.t_try:
				consume();
				compoundStatement();
				while (LT(1) == Token.t_catch) {
					consume();
					consume(Token.tLPAREN);
					declaration(null); // was exceptionDeclaration
					consume(Token.tRPAREN);
					compoundStatement();
				}
				return;
			case Token.tSEMI:
				consume();
				return;
			default:
				// can be many things:
				// label
				if (LT(1) == Token.tIDENTIFIER && LT(2) == Token.tCOLON) {
					consume();
					consume();
					statement();
					return;
				}
				
				// expressionStatement
				// Note: the function style cast ambiguity is handled in expression
				// Since it only happens when we are in a statement
				try {
					expression();
					consume(Token.tSEMI);
					return;
				} catch (Backtrack b) {
				}
				
				// declarationStatement
				declaration(null);
		}
	}
	
	public void condition() throws Exception {
		// TO DO
	}
	
	public void forInitStatement() throws Exception {
		// TO DO
	}
	
	public void compoundStatement() throws Exception {
		consume(Token.tLBRACE);
		while (LT(1) != Token.tRBRACE)
			statement();
		consume();
	}
	
	// Expressions
	public void constantExpression() throws Exception {
		conditionalExpression();
	}
	
	public void expression() throws Exception {
		assignmentExpression();
		
		while (LT(1) == Token.tCOMMA) {
			Token t = consume();
			assignmentExpression();
			callback.expressionOperator(t);
		}
	}
	
	public void assignmentExpression() throws Exception {
		if (LT(1) == Token.t_throw) {
			throwExpression();
			return;
		}
		
		// if the condition not taken, try assignment operators
		if (!conditionalExpression()) {
			switch (LT(1)) {
				case Token.tASSIGN:
				case Token.tSTARASSIGN:
				case Token.tDIVASSIGN:
				case Token.tMODASSIGN:
				case Token.tPLUSASSIGN:
				case Token.tMINUSASSIGN:
				case Token.tSHIFTRASSIGN:
				case Token.tSHIFTLASSIGN:
				case Token.tAMPERASSIGN:
				case Token.tXORASSIGN:
				case Token.tBITORASSIGN:
					Token t = consume();
					conditionalExpression();
					callback.expressionOperator(t);
					break;
			}
		}
	}
	
	public void throwExpression() throws Exception {
		consume(Token.t_throw);
		
		try {
			expression();
		} catch (Backtrack b) {
		}
	}
	
	public boolean conditionalExpression() throws Exception {
		logicalOrExpression();
		
		if (LT(1) == Token.tQUESTION) {
			consume();
			expression();
			consume(Token.tCOLON);
			assignmentExpression();
			return true;
		} else
			return false;
	}
	
	public void logicalOrExpression() throws Exception {
		logicalAndExpression();
		
		while (LT(1) == Token.tOR) {
			Token t = consume();
			logicalAndExpression();
			callback.expressionOperator(t);
		}
	}
	
	public void logicalAndExpression() throws Exception {
		inclusiveOrExpression();
		
		while (LT(1) == Token.tAND) {
			Token t = consume();
			inclusiveOrExpression();
			callback.expressionOperator(t);
		}
	}
	
	public void inclusiveOrExpression() throws Exception {
		exclusiveOrExpression();
		
		while (LT(1) == Token.tBITOR) {
			Token t = consume();
			exclusiveOrExpression();
			callback.expressionOperator(t);
		}
	}
	
	public void exclusiveOrExpression() throws Exception {
		andExpression();
		
		while (LT(1) == Token.tXOR) {
			Token t = consume();
			andExpression();
			callback.expressionOperator(t);
		}
	}
	
	public void andExpression() throws Exception {
		equalityExpression();
		
		while (LT(1) == Token.tAMPER) {
			Token t = consume();
			equalityExpression();
			callback.expressionOperator(t);
		}
	}
	
	public void equalityExpression() throws Exception {
		relationalExpression();
		
		for (;;) {
			switch (LT(1)) {
				case Token.tEQUAL:
				case Token.tNOTEQUAL:
					Token t = consume();
					relationalExpression();
					callback.expressionOperator(t);
					break;
				default:
					return;
			}
		}
	}
	
	public void relationalExpression() throws Exception {
		shiftExpression();
		
		for (;;) {
			switch (LT(1)) {
				case Token.tGT:
					// For template args, the GT means end of args
					//if (templateArgs)
					//	return;
				case Token.tLT:
				case Token.tLTEQUAL:
				case Token.tGTEQUAL:
					Token t = consume();
					shiftExpression();
					callback.expressionOperator(t);
					break;
				default:
					return;
			}
		}
	}
	
	public void shiftExpression() throws Exception {
		additiveExpression();
		
		for (;;) {
			switch (LT(1)) {
				case Token.tSHIFTL:
				case Token.tSHIFTR:
					Token t = consume();
					additiveExpression();
					callback.expressionOperator(t);
					break;
				default:
					return;
			}
		}
	}
	
	public void additiveExpression() throws Exception {
		multiplicativeExpression();
		
		for (;;) {
			switch (LT(1)) {
				case Token.tPLUS:
				case Token.tMINUS:
					Token t = consume();
					multiplicativeExpression();
					callback.expressionOperator(t);
					break;
				default:
					return;
			}
		}
	}
	
	public void multiplicativeExpression() throws Exception {
		pmExpression();
		
		for (;;) {
			switch (LT(1)) {
				case Token.tSTAR:
				case Token.tDIV:
				case Token.tMOD:
					Token t = consume();
					pmExpression();
					callback.expressionOperator(t);
					break;
				default:
					return;
			}
		}
	}
	
	public void pmExpression() throws Exception {
		castExpression();
		
		for (;;) {
			switch (LT(1)) {
				case Token.tDOTSTAR:
				case Token.tARROWSTAR:
					Token t = consume();
					castExpression();
					callback.expressionOperator(t);
					break;
				default:
					return;
			}
		}
	}
	
	/**
	 * castExpression
	 * : unaryExpression
	 * | "(" typeId ")" castExpression
	 */
	public void castExpression() throws Exception {
		// TO DO: we need proper symbol checkint to ensure type name
		if (false && LT(1) == Token.tLPAREN) {
			Token mark = mark();
			consume();
			
			// If this isn't a type name, then we shouldn't be here
			try {
				typeId();
				consume(Token.tRPAREN);
				castExpression();
				return;
			} catch (Backtrack b) {
				backup(mark);
			}
		}

		unaryExpression();
	}
	
	public void typeId() throws Exception {
		try {
			name();
			return;
		} catch (Backtrack b) {
		}
	}
	
	public void deleteExpression() throws Exception {
		if (LT(1) == Token.tCOLONCOLON) {
			// global scope
			consume();
		}
		
		consume(Token.t_delete);
		
		if (LT(1) == Token.tLBRACKET) {
			// array delete
			consume();
			consume(Token.tRBRACKET);
		}
		
		castExpression();
	}
	
	public void newExpression() throws Exception {
		if (LT(1) == Token.tCOLONCOLON) {
			// global scope
			consume();
		}
		
		consume (Token.t_new);
		
		// TO DO: finish this horrible mess...
	}
	
	public void unaryExpression() throws Exception {
		switch (LT(1)) {
			case Token.tSTAR:
			case Token.tAMPER:
			case Token.tPLUS:
			case Token.tMINUS:
			case Token.tNOT:
			case Token.tCOMPL:
			case Token.tINCR:
			case Token.tDECR:
				Token t = consume();
				castExpression();
				callback.expressionOperator(t);
				return;
			case Token.t_sizeof:
				if (LT(1) == Token.tLPAREN) {
					consume();
					typeId();
					consume(Token.tRPAREN);
				} else {
					unaryExpression();
				}
				return;
			case Token.t_new:
				newExpression();
				return;
			case Token.t_delete:
				deleteExpression();
				return;
			case Token.tCOLONCOLON:
				switch (LT(2)) {
					case Token.t_new:
						newExpression();
						return;
					case Token.t_delete:
						deleteExpression();
						return;
					default:
						postfixExpression();
						return;			
				}
			default:
				postfixExpression();
				return;
		}
	}

	public void postfixExpression() throws Exception {
		switch (LT(1)) {
			case Token.t_typename:
				consume();
				// TO DO: this
				break;
			case Token.t_dynamic_cast:
			case Token.t_static_cast:
			case Token.t_reinterpret_cast:
			case Token.t_const_cast:
				consume();
				consume(Token.tLT);
				typeId();
				consume(Token.tGT);
				consume(Token.tLPAREN);
				expression();
				consume(Token.tRPAREN);
				break;
			case Token.t_typeid:
				consume();
				consume(Token.tLPAREN);
				try {
					typeId();
				} catch (Backtrack b) {
					expression();
				}
				consume(Token.tRPAREN);
				break;
			default:
				// TO DO: try simpleTypeSpecifier "(" expressionList ")"
				primaryExpression();
		}
		
		for (;;) {
			switch (LT(1)) {
				case Token.tLBRACKET:
					// array access
					consume();
					expression();
					consume(Token.tRBRACKET);
					break;
				case Token.tLPAREN:
					// function call
					consume();
					// Note: since expressionList and expression are the same...
					expression();
					consume(Token.tRPAREN);
					break;
				case Token.tINCR:
				case Token.tDECR:
					// post incr/decr
					consume();
					break;
				case Token.tDOT:
				case Token.tARROW:
					// member access
					consume();
					// TO DO: handle this
					//varName();
					break;
				default:
					return;
			}
		}
	}
	
	public void primaryExpression() throws Exception {
		switch (LT(1)) {
			// TO DO: we need more literals...
			case Token.tINTEGER:
				callback.expressionTerminal(consume());
				return;
			case Token.tSTRING:
				callback.expressionTerminal(consume());
				return;
			case Token.t_this:
				consume();
				return;
			case Token.tLPAREN:
				consume();
				expression();
				consume(Token.tRPAREN);
				return;
			default:
				// TO DO: idExpression which yeilds a variable
				//idExpression();
				return;
		}
	}
	
	public void varName() throws Exception {
		if (LT(1) == Token.tCOLONCOLON)
			consume();
		
		for (;;) {
			switch (LT(1)) {
				case Token.tIDENTIFIER:
					consume();
					//if (isTemplateArgs()) {
					//	rTemplateArgs();
					//}
					
					if (LT(1) == Token.tCOLONCOLON) {
						switch (LT(2)) {
							case Token.tIDENTIFIER:
							case Token.tCOMPL:
							case Token.t_operator:
								consume();
								break;
							default:
								return;
						}
					} else
						return;
					break;
				case Token.tCOMPL:
					consume();
					consume(Token.tIDENTIFIER);
					return;
				case Token.t_operator:
					consume();
					//rOperatorName();
					return;
				default:
					throw backtrack;
			}
		}
	}
	
	// Backtracking
	private static class Backtrack extends Exception {
	}
	
	private static Backtrack backtrack = new Backtrack();
	
	// Token management
	private IScanner scanner;
	private Token currToken;
	
	private Token fetchToken() {
		try {
			return scanner.nextToken();
		} catch (Exception e) {
			return null;
		}
	}

	protected Token LA(int i) {
		if (i < 1)
			// can't go backwards
			return null;

		if (currToken == null)
			currToken = fetchToken();
		
		Token retToken = currToken;
		 
		for (; i > 1; --i) {
			if (retToken.getNext() == null)
				fetchToken();
			retToken = retToken.getNext();
		}
		
		return retToken;
	}

	protected int LT(int i) {
		return LA(i).type;
	}
	
	protected Token consume() {
		if (currToken.getNext() == null)
			fetchToken();
		Token retToken = currToken;
		currToken = currToken.getNext();
		return retToken;
	}
	
	protected Token consume(int type) throws Exception {
		if (LT(1) == type)
			return consume();
		else
			throw backtrack;
	}
	
	protected Token mark() {
		return currToken;
	}
	
	protected void backup(Token mark) {
		currToken = mark;
	}

	// Utility routines that require a knowledge of the grammar
	public static String generateName(Token startToken) throws Exception {
		Token currToken = startToken.getNext();
		
		if (currToken == null || currToken.getType() != Token.tCOLONCOLON)
			return startToken.getImage();
		
		StringBuffer buff = new StringBuffer(startToken.getImage());
		while (currToken != null && currToken.getType() == Token.tCOLONCOLON) {
			currToken = currToken.getNext();
			if (currToken == null || currToken.getType() != Token.tIDENTIFIER)
				// Not good
				throw new ParserException(startToken);
			buff.append("::");
			buff.append(currToken.getImage());
			currToken = currToken.getNext();
		}
		
		return buff.toString();
	}
}

Back to the top