aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/antlr3_cpp_runtime/include/antlr3intstream.inl
blob: e9990786bb741e30bfcf275fe55c9c8723985b3f (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
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
namespace antlr3 {

template<class ImplTraits, class SuperType>
ANTLR_INLINE IntStream<ImplTraits, SuperType>::IntStream()
{
	m_lastMarker = 0;
	m_upper_case = false;
}

template<class ImplTraits, class SuperType>
ANTLR_INLINE typename IntStream<ImplTraits, SuperType>::StringType	IntStream<ImplTraits, SuperType>::getSourceName()
{
	return m_streamName;
}

template<class ImplTraits, class SuperType>
ANTLR_INLINE typename IntStream<ImplTraits, SuperType>::StringType& 	IntStream<ImplTraits, SuperType>::get_streamName()
{
	return m_streamName;
}

template<class ImplTraits, class SuperType>
ANTLR_INLINE const typename IntStream<ImplTraits, SuperType>::StringType& 	IntStream<ImplTraits, SuperType>::get_streamName() const
{
	return m_streamName;
}

template<class ImplTraits, class SuperType>
ANTLR_INLINE ANTLR_MARKER IntStream<ImplTraits, SuperType>::get_lastMarker() const
{
	return m_lastMarker;
}

template<class ImplTraits, class SuperType>
ANTLR_INLINE void	IntStream<ImplTraits, SuperType>::setUcaseLA(bool flag)
{
	m_upper_case = flag;
}

template<class ImplTraits, class SuperType>
ANTLR_INLINE SuperType* IntStream<ImplTraits, SuperType>::get_super()
{
	return static_cast<SuperType*>(this);
}

template<class ImplTraits, class SuperType>
void	IntStream<ImplTraits, SuperType>::consume()
{
	SuperType* input = this->get_super();

	const ANTLR_UINT8* nextChar = input->get_nextChar();
	const ANTLR_UINT8* data = input->get_data();
	ANTLR_UINT32 sizeBuf = input->get_sizeBuf();

    if	( nextChar < ( data + sizeBuf ) )
    {	
		/* Indicate one more character in this line
		 */
		input->inc_charPositionInLine();
	
		if  ((ANTLR_UCHAR)(*(nextChar)) == input->get_newlineChar() )
		{
			/* Reset for start of a new line of input
			 */
			input->inc_line();
			input->set_charPositionInLine(0);
			input->set_currentLine(nextChar + 1);
		}

		/* Increment to next character position
		 */
		input->set_nextChar( nextChar + 1 );
    }
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32	IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 la )
{
	SuperType* input = this->get_super();
	const ANTLR_UINT8* nextChar = input->get_nextChar();
	const ANTLR_UINT8* data = input->get_data();
	ANTLR_UINT32 sizeBuf = input->get_sizeBuf();

    if	(( nextChar + la - 1) >= (data + sizeBuf))
    {
		return	ANTLR_CHARSTREAM_EOF;
    }
    else
    {
		if( !m_upper_case )
			return	(ANTLR_UCHAR)(*(nextChar + la - 1));
		else
			return	(ANTLR_UCHAR)toupper(*(nextChar + la - 1));
    }
}

template<class ImplTraits, class SuperType>
ANTLR_MARKER IntStream<ImplTraits, SuperType>::mark()
{
	LexState<ImplTraits>*	    state;
    SuperType* input = this->get_super();

    /* New mark point 
     */
    input->inc_markDepth();

    /* See if we are revisiting a mark as we can just reuse the vector
     * entry if we are, otherwise, we need a new one
     */
    if	(input->get_markDepth() > input->get_markers().size() )
    {	
		input->get_markers().push_back( LexState<ImplTraits>() );
		LexState<ImplTraits>& state_r = input->get_markers().back();
		state = &state_r;
    }
    else
    {
		LexState<ImplTraits>& state_r = input->get_markers().at( input->get_markDepth() - 1 );
		state	= &state_r;

		/* Assume no errors for speed, it will just blow up if the table failed
		 * for some reasons, hence lots of unit tests on the tables ;-)
		 */
    }

    /* We have created or retrieved the state, so update it with the current
     * elements of the lexer state.
     */
    state->set_charPositionInLine( input->get_charPositionInLine() );
    state->set_currentLine( input->get_currentLine() );
    state->set_line( input->get_line() );
    state->set_nextChar( input->get_nextChar() );

    m_lastMarker = input->get_markDepth();

    /* And that's it
     */
    return  input->get_markDepth();
}

template<class ImplTraits, class SuperType>
ANTLR_MARKER	IntStream<ImplTraits, SuperType>::index()
{
	SuperType* input = this->get_super();
	return input->index_impl();
}

template<class ImplTraits, class SuperType>
void	IntStream<ImplTraits, SuperType>::rewind(ANTLR_MARKER mark)
{
    SuperType* input = this->get_super();

    /* Perform any clean up of the marks
     */
    this->release(mark);

    /* Find the supplied mark state 
     */
	ANTLR_UINT32 idx = static_cast<ANTLR_UINT32>( mark-1 );
    typename ImplTraits::LexStateType&   state = input->get_markers().at( idx );

    /* Seek input pointer to the requested point (note we supply the void *pointer
     * to whatever is implementing the int stream to seek).
     */
	this->seek( (ANTLR_MARKER)state.get_nextChar() );
    
    /* Reset to the reset of the information in the mark
     */
    input->set_charPositionInLine( state.get_charPositionInLine() );
    input->set_currentLine( state.get_currentLine() );
    input->set_line( state.get_line() );
    input->set_nextChar( state.get_nextChar() );

    /* And we are done
     */
}

template<class ImplTraits, class SuperType>
void	IntStream<ImplTraits, SuperType>::rewindLast()
{
	this->rewind(m_lastMarker);
}

template<class ImplTraits, class SuperType>
void	IntStream<ImplTraits, SuperType>::release(ANTLR_MARKER mark)
{
	SuperType* input = this->get_super();

	/* We don't do much here in fact as we never free any higher marks in
     * the hashtable as we just resuse any memory allocated for them.
     */
    input->set_markDepth( (ANTLR_UINT32)(mark - 1) );

}

template<class ImplTraits, class SuperType>
void IntStream<ImplTraits, SuperType>::setupIntStream(bool, bool)
{
}

template<class ImplTraits, class SuperType>
void	IntStream<ImplTraits, SuperType>::seek(ANTLR_MARKER seekPoint)
{
	ANTLR_INT32   count;
	SuperType* input = this->get_super();

	ANTLR_MARKER nextChar = (ANTLR_MARKER) input->get_nextChar();
	/* If the requested seek point is less than the current
	* input point, then we assume that we are resetting from a mark
	* and do not need to scan, but can just set to there.
	*/
	if	(seekPoint <= nextChar)
	{
		input->set_nextChar((ANTLR_UINT8*) seekPoint);
	}
	else
	{
		count	= (ANTLR_UINT32)(seekPoint - nextChar);

		while (count--)
		{
			this->consume();
		}
	}
}

template<class ImplTraits, class SuperType>
IntStream<ImplTraits, SuperType>::~IntStream()
{
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32	EBCDIC_IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 la)
{
	// EBCDIC to ASCII conversion table
	//
	// This for EBCDIC EDF04 translated to ISO-8859.1 which is the usually accepted POSIX
	// translation and the character tables are published all over the interweb.
	// 
	const ANTLR_UCHAR e2a[256] =
	{
		0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f,
		0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
		0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97,
		0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f,
		0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b,
		0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, 
		0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04,
		0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a,
		0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5,
		0xe7, 0xf1, 0x60, 0x2e, 0x3c, 0x28, 0x2b, 0x7c,
		0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef,
		0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x9f,
		0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5,
		0xc7, 0xd1, 0x5e, 0x2c, 0x25, 0x5f, 0x3e, 0x3f,
		0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf,
		0xcc, 0xa8, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22,
		0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
		0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1,
		0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
		0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4,
		0xb5, 0xaf, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
		0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0xdd, 0xde, 0xae,
		0xa2, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc,
		0xbd, 0xbe, 0xac, 0x5b, 0x5c, 0x5d, 0xb4, 0xd7,
		0xf9, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
		0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5,
		0xa6, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
		0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xdb, 0xfa, 0xff,
		0xd9, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
		0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5,
		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
		0x38, 0x39, 0xb3, 0x7b, 0xdc, 0x7d, 0xda, 0x7e
	};

	SuperType* input = this->get_super();

    if	(( input->get_nextChar() + la - 1) >= ( input->get_data() + input->get_sizeBuf() ))
    {
        return	ANTLR_CHARSTREAM_EOF;
    }
    else
    {
        // Translate the required character via the constant conversion table
        //
        return	e2a[(*(input->get_nextChar() + la - 1))];
    }
}

template<class ImplTraits, class SuperType>
void EBCDIC_IntStream<ImplTraits, SuperType>::setupIntStream()
{
	SuperType* super = this->get_super();
	super->set_charByteSize(1);
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32	UTF16_IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 i)
{
	return this->LA(i, ClassForwarder< typename ImplTraits::Endianness >() );
}

template<class ImplTraits, class SuperType>
void UTF16_IntStream<ImplTraits, SuperType>::consume()
{
	this->consume( ClassForwarder< typename ImplTraits::Endianness >() );
}

template<class ImplTraits, class SuperType>
ANTLR_MARKER	UTF16_IntStream<ImplTraits, SuperType>::index()
{
	SuperType* input = this->get_super();
    return  (ANTLR_MARKER)(input->get_nextChar());
}

template<class ImplTraits, class SuperType>
void UTF16_IntStream<ImplTraits, SuperType>::seek(ANTLR_MARKER seekPoint)
{
	SuperType* input = this->get_super();

	// If the requested seek point is less than the current
	// input point, then we assume that we are resetting from a mark
	// and do not need to scan, but can just set to there as rewind will
    // reset line numbers and so on.
	//
	if	(seekPoint <= (ANTLR_MARKER)(input->get_nextChar()))
	{
		input->set_nextChar( seekPoint );
	}
	else
	{
        // Call consume until we reach the asked for seek point or EOF
        //
        while( (this->LA(1) != ANTLR_CHARSTREAM_EOF) && (seekPoint < (ANTLR_MARKER)input->get_nextChar() ) )
	    {
			this->consume();
	    }
	}
}

template<class ImplTraits, class SuperType>
void IntStream<ImplTraits, SuperType>::findout_endian_spec(bool machineBigEndian, bool inputBigEndian)
{
	// We must install different UTF16 routines according to whether the input
	// is the same endianess as the machine we are executing upon or not. If it is not
	// then we must install methods that can convert the endianess on the fly as they go
	//

	if(machineBigEndian == true)
	{
		// Machine is Big Endian, if the input is also then install the 
		// methods that do not access input by bytes and reverse them.
		// Otherwise install endian aware methods.
		//
		if  (inputBigEndian == true) 
		{
			// Input is machine compatible
			//
			m_endian_spec = 1;
		}
		else
		{
			// Need to use methods that know that the input is little endian
			//
			m_endian_spec = 2;
		}
	}
	else
	{
		// Machine is Little Endian, if the input is also then install the 
		// methods that do not access input by bytes and reverse them.
		// Otherwise install endian aware methods.
		//
		if  (inputBigEndian == false) 
		{
			// Input is machine compatible
			//
			m_endian_spec =  1;
		}
		else
		{
			// Need to use methods that know that the input is Big Endian
			//
			m_endian_spec	= 3;
		}
	}
}

template<class ImplTraits, class SuperType>
void UTF16_IntStream<ImplTraits, SuperType>::setupIntStream(bool machineBigEndian, bool inputBigEndian)
{
	SuperType* super = this->get_super();
	super->set_charByteSize(2);

	this->findout_endian_spec( machineBigEndian, inputBigEndian );
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32 IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 i, ClassForwarder<RESOLVE_ENDIAN_AT_RUNTIME> )
{
	assert( (m_endian_spec >= 1) && (m_endian_spec <= 3));
	switch(m_endian_spec)
	{
	case 1:
		return this->LA(i, ClassForwarder<BYTE_AGNOSTIC>() );
		break;
	case 2:
		return this->LA(i, ClassForwarder<ANTLR_LITTLE_ENDIAN>() );
		break;
	case 3:
		return this->LA(i, ClassForwarder<ANTLR_BIG_ENDIAN>() );
		break;
	default:
		break;
	}
	return 0;
}

template<class ImplTraits, class SuperType>
void	IntStream<ImplTraits, SuperType>::consume( ClassForwarder<RESOLVE_ENDIAN_AT_RUNTIME> )
{
	assert( (m_endian_spec >= 1) && (m_endian_spec <= 3));
	switch(m_endian_spec)
	{
	case 1:
		this->consume( ClassForwarder<BYTE_AGNOSTIC>() );
		break;
	case 2:
		this->consume( ClassForwarder<ANTLR_LITTLE_ENDIAN>() );
		break;
	case 3:
		this->consume( ClassForwarder<ANTLR_BIG_ENDIAN>() );
		break;
	default:
		break;
	}
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32	UTF16_IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 la, ClassForwarder<BYTE_AGNOSTIC> )
{
	SuperType* input;
    UTF32   ch;
    UTF32   ch2;
    UTF16*	nextChar;

    // Find the input interface and where we are currently pointing to
    // in the input stream
    //
	input   = this->get_super;
	nextChar    = input->get_nextChar();

    // If a positive offset then advance forward, else retreat
    //
    if  (la >= 0)
    {
        while   (--la > 0 && (ANTLR_UINT8*)nextChar < ((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf() )
        {
            // Advance our copy of the input pointer
            //
            // Next char in natural machine byte order
            //
            ch  = *nextChar++;

            // If we have a surrogate pair then we need to consume
            // a following valid LO surrogate.
            //
            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) 
            {
                // If the 16 bits following the high surrogate are in the source buffer...
                //
                if	((ANTLR_UINT8*)(nextChar) < (((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf() ))
                {
                    // Next character is in natural machine byte order
                    //
                    ch2 = *nextChar;

                    // If it's a valid low surrogate, consume it
                    //
                    if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) 
                    {
                        // We consumed one 16 bit character
                        //
						nextChar++;
                    }
                    // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                    // it.
                    //
                } 
                // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                // it because the buffer ended
                //
            }
            // Note that we did not check for an invalid low surrogate here, or that fact that the
            // lo surrogate was missing. We just picked out one 16 bit character unless the character
            // was a valid hi surrogate, in whcih case we consumed two 16 bit characters.
            //
        }
    }
    else
    {
        // We need to go backwards from our input point
        //
        while   (la++ < 0 && (ANTLR_UINT8*)nextChar > (ANTLR_UINT8*)input->get_data() )
        {
            // Get the previous 16 bit character
            //
            ch = *--nextChar;

            // If we found a low surrogate then go back one more character if
            // the hi surrogate is there
            //
            if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) 
            {
                ch2 = *(nextChar-1);
                if (ch2 >= UNI_SUR_HIGH_START && ch2 <= UNI_SUR_HIGH_END) 
                {
                    // Yes, there is a high surrogate to match it so decrement one more and point to that
                    //
                    nextChar--;
                }
            }
        }
    }

    // Our local copy of nextChar is now pointing to either the correct character or end of file
    //
    // Input buffer size is always in bytes
    //
	if	( (ANTLR_UINT8*)nextChar >= (((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf() ))
	{
		return	ANTLR_CHARSTREAM_EOF;
	}
	else
	{
        // Pick up the next 16 character (native machine byte order)
        //
        ch = *nextChar++;

        // If we have a surrogate pair then we need to consume
        // a following valid LO surrogate.
        //
        if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) 
        {
            // If the 16 bits following the high surrogate are in the source buffer...
            //
            if	((ANTLR_UINT8*)(nextChar) < (((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf()))
            {
                // Next character is in natural machine byte order
                //
                ch2 = *nextChar;

                // If it's a valid low surrogate, consume it
                //
                if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) 
                {
                    // Construct the UTF32 code point
                    //
                    ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
								+ (ch2 - UNI_SUR_LOW_START) + halfBase;
                }
                // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                // it.
                //
            } 
            // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
            // it because the buffer ended
            //
        }
    }
    return ch;
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32	UTF16_IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 la, ClassForwarder<ANTLR_LITTLE_ENDIAN> )
{
	SuperType* input;
    UTF32           ch;
    UTF32           ch2;
    ANTLR_UCHAR*   nextChar;

    // Find the input interface and where we are currently pointing to
    // in the input stream
    //
	input       = this->get_super();
    nextChar    = input->get_nextChar();

    // If a positive offset then advance forward, else retreat
    //
    if  (la >= 0)
    {
        while   (--la > 0 && (ANTLR_UINT8*)nextChar < ((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf() )
        {
            // Advance our copy of the input pointer
            //
            // Next char in Little Endian byte order
            //
            ch  = (*nextChar) + (*(nextChar+1) << 8);
            nextChar += 2;

            // If we have a surrogate pair then we need to consume
            // a following valid LO surrogate.
            //
            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) 
            {
                // If the 16 bits following the high surrogate are in the source buffer...
                //
                if	((ANTLR_UINT8*)(nextChar) < (((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf() ))
                {
                    // Next character is in little endian byte order
                    //
                    ch2 = (*nextChar) + (*(nextChar+1) << 8);

                    // If it's a valid low surrogate, consume it
                    //
                    if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) 
                    {
                        // We consumed one 16 bit character
                        //
						nextChar += 2;
                    }
                    // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                    // it.
                    //
                } 
                // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                // it because the buffer ended
                //
            }
            // Note that we did not check for an invalid low surrogate here, or that fact that the
            // lo surrogate was missing. We just picked out one 16 bit character unless the character
            // was a valid hi surrogate, in whcih case we consumed two 16 bit characters.
            //
        }
    }
    else
    {
        // We need to go backwards from our input point
        //
        while   (la++ < 0 && (ANTLR_UINT8*)nextChar > (ANTLR_UINT8*)input->get_data() )
        {
            // Get the previous 16 bit character
            //
            ch = (*nextChar - 2) + ((*nextChar -1) << 8);
            nextChar -= 2;

            // If we found a low surrogate then go back one more character if
            // the hi surrogate is there
            //
            if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) 
            {
                ch2 = (*nextChar - 2) + ((*nextChar -1) << 8);
                if (ch2 >= UNI_SUR_HIGH_START && ch2 <= UNI_SUR_HIGH_END) 
                {
                    // Yes, there is a high surrogate to match it so decrement one more and point to that
                    //
                    nextChar -=2;
                }
            }
        }
    }

    // Our local copy of nextChar is now pointing to either the correct character or end of file
    //
    // Input buffer size is always in bytes
    //
	if	( (ANTLR_UINT8*)nextChar >= (((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf()))
	{
		return	ANTLR_CHARSTREAM_EOF;
	}
	else
	{
        // Pick up the next 16 character (little endian byte order)
        //
        ch = (*nextChar) + (*(nextChar+1) << 8);
        nextChar += 2;

        // If we have a surrogate pair then we need to consume
        // a following valid LO surrogate.
        //
        if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) 
        {
            // If the 16 bits following the high surrogate are in the source buffer...
            //
            if	((ANTLR_UINT8*)(nextChar) < (((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf()))
            {
                // Next character is in little endian byte order
                //
                ch2 = (*nextChar) + (*(nextChar+1) << 8);

                // If it's a valid low surrogate, consume it
                //
                if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) 
                {
                    // Construct the UTF32 code point
                    //
                    ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
								+ (ch2 - UNI_SUR_LOW_START) + halfBase;
                }
                // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                // it.
                //
            } 
            // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
            // it because the buffer ended
            //
        }
    }
    return ch;
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32	UTF16_IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 la, ClassForwarder<ANTLR_BIG_ENDIAN> )
{
	SuperType* input;
    UTF32           ch;
    UTF32           ch2;
    ANTLR_UCHAR*   nextChar;

    // Find the input interface and where we are currently pointing to
    // in the input stream
    //
	input       = this->get_super();
    nextChar    = input->get_nextChar();

    // If a positive offset then advance forward, else retreat
    //
    if  (la >= 0)
    {
        while   (--la > 0 && (ANTLR_UINT8*)nextChar < ((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf() )
        {
            // Advance our copy of the input pointer
            //
            // Next char in Big Endian byte order
            //
            ch  = ((*nextChar) << 8) + *(nextChar+1);
            nextChar += 2;

            // If we have a surrogate pair then we need to consume
            // a following valid LO surrogate.
            //
            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) 
            {
                // If the 16 bits following the high surrogate are in the source buffer...
                //
                if	((ANTLR_UINT8*)(nextChar) < (((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf()))
                {
                    // Next character is in big endian byte order
                    //
                    ch2 = ((*nextChar) << 8) + *(nextChar+1);

                    // If it's a valid low surrogate, consume it
                    //
                    if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) 
                    {
                        // We consumed one 16 bit character
                        //
						nextChar += 2;
                    }
                    // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                    // it.
                    //
                } 
                // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                // it because the buffer ended
                //
            }
            // Note that we did not check for an invalid low surrogate here, or that fact that the
            // lo surrogate was missing. We just picked out one 16 bit character unless the character
            // was a valid hi surrogate, in whcih case we consumed two 16 bit characters.
            //
        }
    }
    else
    {
        // We need to go backwards from our input point
        //
        while   (la++ < 0 && (ANTLR_UINT8*)nextChar > (ANTLR_UINT8*)input->get_data() )
        {
            // Get the previous 16 bit character
            //
            ch = ((*nextChar - 2) << 8) + (*nextChar -1);
            nextChar -= 2;

            // If we found a low surrogate then go back one more character if
            // the hi surrogate is there
            //
            if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) 
            {
                ch2 = ((*nextChar - 2) << 8) + (*nextChar -1);
                if (ch2 >= UNI_SUR_HIGH_START && ch2 <= UNI_SUR_HIGH_END) 
                {
                    // Yes, there is a high surrogate to match it so decrement one more and point to that
                    //
                    nextChar -=2;
                }
            }
        }
    }

    // Our local copy of nextChar is now pointing to either the correct character or end of file
    //
    // Input buffer size is always in bytes
    //
	if	( (ANTLR_UINT8*)nextChar >= (((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf()))
	{
		return	ANTLR_CHARSTREAM_EOF;
	}
	else
	{
        // Pick up the next 16 character (big endian byte order)
        //
        ch = ((*nextChar) << 8) + *(nextChar+1);
        nextChar += 2;

        // If we have a surrogate pair then we need to consume
        // a following valid LO surrogate.
        //
        if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) 
        {
            // If the 16 bits following the high surrogate are in the source buffer...
            //
            if	((ANTLR_UINT8*)(nextChar) < (((ANTLR_UINT8*)input->get_data()) + input->get_sizeBuf()))
            {
                // Next character is in big endian byte order
                //
                ch2 = ((*nextChar) << 8) + *(nextChar+1);

                // If it's a valid low surrogate, consume it
                //
                if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) 
                {
                    // Construct the UTF32 code point
                    //
                    ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
								+ (ch2 - UNI_SUR_LOW_START) + halfBase;
                }
                // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                // it.
                //
            } 
            // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
            // it because the buffer ended
            //
        }
    }
    return ch;
}

template<class ImplTraits, class SuperType>
void	UTF16_IntStream<ImplTraits, SuperType>::consume( ClassForwarder<BYTE_AGNOSTIC> )
{
	SuperType* input;
    UTF32   ch;
    UTF32   ch2;

	input   = this->get_super();

    // Buffer size is always in bytes
    //
	if(input->get_nextChar() < (input->get_data() + input->get_sizeBuf()/2) )
	{	
		// Indicate one more character in this line
		//
		input->inc_charPositionInLine();

		if  ((ANTLR_UCHAR)(*(input->get_nextChar())) == input->get_newlineChar())
		{
			// Reset for start of a new line of input
			//
			input->inc_line();
			input->set_charPositionInLine(0);
			input->set_currentLine( input->get_nextChar() + 1 );
		}

		// Increment to next character position, accounting for any surrogates
		//
        // Next char in natural machine byte order
        //
        ch  = *(input->get_nextChar());

        // We consumed one 16 bit character
        //
		input->set_nextChar( input->get_nextChar() + 1 );

        // If we have a surrogate pair then we need to consume
        // a following valid LO surrogate.
        //
        if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {

            // If the 16 bits following the high surrogate are in the source buffer...
            //
            if(input->get_nextChar() < (input->get_data() + input->get_sizeBuf()/2) )
            {
                // Next character is in natural machine byte order
                //
                ch2 = *(input->get_nextChar());

                // If it's a valid low surrogate, consume it
                //
                if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) 
                {
                    // We consumed one 16 bit character
                    //
					input->set_nextChar( input->get_nextChar() + 1 );
                }
                // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                // it.
                //
            } 
            // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
            // it because the buffer ended
            //
        } 
        // Note that we did not check for an invalid low surrogate here, or that fact that the
        // lo surrogate was missing. We just picked out one 16 bit character unless the character
        // was a valid hi surrogate, in whcih case we consumed two 16 bit characters.
        //
	}

}

template<class ImplTraits, class SuperType>
void	UTF16_IntStream<ImplTraits, SuperType>::consume( ClassForwarder<ANTLR_LITTLE_ENDIAN> )
{
	SuperType* input;
    UTF32   ch;
    UTF32   ch2;

	input   = this->get_super();

    // Buffer size is always in bytes
    //
	if(input->get_nextChar() < (input->get_data() + input->get_sizeBuf()/2) )
	{	
		// Indicate one more character in this line
		//
		input->inc_charPositionInLine();

		if  ((ANTLR_UCHAR)(*(input->get_nextChar())) == input->get_newlineChar())
		{
			// Reset for start of a new line of input
			//
			input->inc_line();
			input->set_charPositionInLine(0);
			input->set_currentLine(input->get_nextChar() + 1);
		}

		// Increment to next character position, accounting for any surrogates
		//
        // Next char in litle endian form
        //
        ch  = *((ANTLR_UINT8*)input->get_nextChar()) + (*((ANTLR_UINT8*)input->get_nextChar() + 1) <<8);

        // We consumed one 16 bit character
        //
		input->set_nextChar( input->get_nextChar() + 1);

        // If we have a surrogate pair then we need to consume
        // a following valid LO surrogate.
        //
        if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) 
		{
            // If the 16 bits following the high surrogate are in the source buffer...
            //
            if(input->get_nextChar() < (input->get_data() + input->get_sizeBuf()/2) )
            {
                ch2 = *((ANTLR_UINT8*)input->get_nextChar()) + (*((ANTLR_UINT8*)input->get_nextChar() + 1) <<8);

                // If it's a valid low surrogate, consume it
                //
                if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) 
                {
                    // We consumed one 16 bit character
                    //
					input->set_nextChar( input->get_nextChar() + 1);
                }
                // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                // it.
                //
            } 
            // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
            // it because the buffer ended
            //
        } 
        // Note that we did not check for an invalid low surrogate here, or that fact that the
        // lo surrogate was missing. We just picked out one 16 bit character unless the character
        // was a valid hi surrogate, in whcih case we consumed two 16 bit characters.
        //
	}
}

template<class ImplTraits, class SuperType>
void	UTF16_IntStream<ImplTraits, SuperType>::consume( ClassForwarder<ANTLR_BIG_ENDIAN> )
{
	SuperType* input;
    UTF32   ch;
    UTF32   ch2;

	input   = this->get_super();

    // Buffer size is always in bytes
    //
	if(input->get_nextChar() < (input->get_data() + input->get_sizeBuf()/2) )
	{	
		// Indicate one more character in this line
		//
		input->inc_charPositionInLine();

		if  ((ANTLR_UCHAR)(*(input->get_nextChar())) == input->get_newlineChar())
		{
			// Reset for start of a new line of input
			//
			input->inc_line();
			input->set_charPositionInLine(0);
			input->set_currentLine(input->get_nextChar() + 1);
		}

		// Increment to next character position, accounting for any surrogates
		//
        // Next char in big endian form
        //
        ch  = *((ANTLR_UINT8*)input->get_nextChar() + 1) + (*((ANTLR_UINT8*)input->get_nextChar() ) <<8);

        // We consumed one 16 bit character
        //
		input->set_nextChar( input->get_nextChar() + 1);

        // If we have a surrogate pair then we need to consume
        // a following valid LO surrogate.
        //
        if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) 
		{
            // If the 16 bits following the high surrogate are in the source buffer...
            //
            if(input->get_nextChar() < (input->get_data() + input->get_sizeBuf()/2) )
            {
                // Big endian
                //
                ch2 = *((ANTLR_UINT8*)input->get_nextChar() + 1) + (*((ANTLR_UINT8*)input->get_nextChar() ) <<8);

                // If it's a valid low surrogate, consume it
                //
                if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) 
                {
                    // We consumed one 16 bit character
                    //
					input->set_nextChar( input->get_nextChar() + 1);
                }
                // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
                // it.
                //
            } 
            // Note that we ignore a valid hi surrogate that has no lo surrogate to go with
            // it because the buffer ended
            //
        } 
        // Note that we did not check for an invalid low surrogate here, or that fact that the
        // lo surrogate was missing. We just picked out one 16 bit character unless the character
        // was a valid hi surrogate, in whcih case we consumed two 16 bit characters.
        //
	}
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32	UTF32_IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 i)
{
	return this->LA( i, ClassForwarder<typename ImplTraits::Endianness>() );
}

template<class ImplTraits, class SuperType>
ANTLR_MARKER	UTF32_IntStream<ImplTraits, SuperType>::index()
{
	SuperType* input = this->get_super();
    return  (ANTLR_MARKER)(input->get_nextChar());
}

template<class ImplTraits, class SuperType>
void UTF32_IntStream<ImplTraits, SuperType>::seek(ANTLR_MARKER seekPoint)
{
	SuperType* input;

	input   = this->get_super();

	// If the requested seek point is less than the current
	// input point, then we assume that we are resetting from a mark
	// and do not need to scan, but can just set to there as rewind will
        // reset line numbers and so on.
	//
	if	(seekPoint <= (ANTLR_MARKER)(input->get_nextChar()))
	{
		input->set_nextChar( static_cast<typename ImplTraits::DataType*>(seekPoint) );
	}
	else
	{
        // Call consume until we reach the asked for seek point or EOF
        //
        while( (this->LA(1) != ANTLR_CHARSTREAM_EOF) && (seekPoint < (ANTLR_MARKER)input->get_nextChar()) )
	    {
			this->consume();
	    }
	}

}

template<class ImplTraits, class SuperType>
void UTF32_IntStream<ImplTraits, SuperType>::setupIntStream(bool machineBigEndian, bool inputBigEndian)
{
	SuperType* super = this->get_super();
	super->set_charByteSize(4);

	this->findout_endian_spec(machineBigEndian, inputBigEndian);
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32	UTF32_IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 la, ClassForwarder<BYTE_AGNOSTIC> )
{
    SuperType* input = this->get_super();

    if	(( input->get_nextChar() + la - 1) >= (input->get_data() + input->get_sizeBuf()/4 ))
    {
		return	ANTLR_CHARSTREAM_EOF;
    }
    else
    {
		return	(ANTLR_UCHAR)(*(input->get_nextChar() + la - 1));
    }
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32	UTF32_IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 la, ClassForwarder<ANTLR_LITTLE_ENDIAN> )
{
	SuperType* input = this->get_super();

    if	(( input->get_nextChar() + la - 1) >= (input->get_data() + input->get_sizeBuf()/4 ))
    {
		return	ANTLR_CHARSTREAM_EOF;
    }
    else
    {
        ANTLR_UCHAR   c;

        c = (ANTLR_UCHAR)(*(input->get_nextChar() + la - 1));

        // Swap Endianess to Big Endian
        //
        return (c>>24) | ((c<<8) & 0x00FF0000) | ((c>>8) & 0x0000FF00) | (c<<24);
    }
}

template<class ImplTraits, class SuperType>
ANTLR_UINT32	UTF32_IntStream<ImplTraits, SuperType>::LA( ANTLR_INT32 la, ClassForwarder<ANTLR_BIG_ENDIAN> )
{
	SuperType* input = this->get_super();

    if	(( input->get_nextChar() + la - 1) >= (input->get_data() + input->get_sizeBuf()/4 ))
    {
		return	ANTLR_CHARSTREAM_EOF;
    }
    else
    {
        ANTLR_UCHAR   c;

        c = (ANTLR_UCHAR)(*(input->get_nextChar() + la - 1));

        // Swap Endianess to Little Endian
        //
        return (c>>24) | ((c<<8) & 0x00FF0000) | ((c>>8) & 0x0000FF00) | (c<<24);
    }
}

template<class ImplTraits, class SuperType>
void	UTF32_IntStream<ImplTraits, SuperType>::consume()
{
	SuperType* input = this->get_super();

    // SizeBuf is always in bytes
    //
	if	( input->get_nextChar()  < (input->get_data() + input->get_sizeBuf()/4 ))
    {	
		/* Indicate one more character in this line
		 */
		input->inc_charPositionInLine();
	
		if  ((ANTLR_UCHAR)(*(input->get_nextChar())) == input->get_newlineChar())
		{
			/* Reset for start of a new line of input
			 */
			input->inc_line();
			input->set_charPositionInLine(0);
			input->set_currentLine(	input->get_nextChar() + 1 );
		}

		/* Increment to next character position
		 */
		input->set_nextChar( input->get_nextChar() + 1 );
    }
}

template<class ImplTraits, class SuperType>
void UTF8_IntStream<ImplTraits, SuperType>::setupIntStream(bool, bool)
{
	SuperType* super = this->get_super();
	super->set_charByteSize(0);
}

// ------------------------------------------------------
// Following is from Unicode.org (see antlr3convertutf.c)
//

/// Index into the table below with the first byte of a UTF-8 sequence to
/// get the number of trailing bytes that are supposed to follow it.
/// Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
/// left as-is for anyone who may want to do such conversion, which was
/// allowed in earlier algorithms.
///
template<class ImplTraits, class SuperType>
const ANTLR_UINT32* UTF8_IntStream<ImplTraits, SuperType>::TrailingBytesForUTF8()
{
	static const ANTLR_UINT32 trailingBytesForUTF8[256] = {
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
		1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
		2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
	};

	return trailingBytesForUTF8;
}

/// Magic values subtracted from a buffer value during UTF8 conversion.
/// This table contains as many values as there might be trailing bytes
/// in a UTF-8 sequence.
///
template<class ImplTraits, class SuperType>
const UTF32* UTF8_IntStream<ImplTraits, SuperType>::OffsetsFromUTF8()
{
	static const UTF32 offsetsFromUTF8[6] = 
		{   0x00000000UL, 0x00003080UL, 0x000E2080UL, 
			0x03C82080UL, 0xFA082080UL, 0x82082080UL 
		};
	return 	offsetsFromUTF8;
}

// End of Unicode.org tables
// -------------------------


/** \brief Consume the next character in a UTF8 input stream
 *
 * \param input Input stream context pointer
 */
template<class ImplTraits, class SuperType>
void UTF8_IntStream<ImplTraits, SuperType>::consume()
{
    SuperType* input = this->get_super();
	const ANTLR_UINT32* trailingBytesForUTF8 = UTF8_IntStream::TrailingBytesForUTF8();
	const UTF32* offsetsFromUTF8 = UTF8_IntStream::OffsetsFromUTF8();

    ANTLR_UINT32           extraBytesToRead;
    ANTLR_UCHAR            ch;
    ANTLR_UINT8*           nextChar;

    nextChar = input->get_nextChar();

    if	(nextChar < (input->get_data() + input->get_sizeBuf()))
    {	
		// Indicate one more character in this line
		//
		input->inc_charPositionInLine();
	
        // Are there more bytes needed to make up the whole thing?
        //
        extraBytesToRead = trailingBytesForUTF8[*nextChar];

        if	((nextChar + extraBytesToRead) >= (input->get_data() + input->get_sizeBuf()))
        {
            input->set_nextChar( input->get_data() + input->get_sizeBuf() );
            return;
        }

        // Cases deliberately fall through (see note A in antlrconvertutf.c)
        // Legal UTF8 is only 4 bytes but 6 bytes could be used in old UTF8 so
        // we allow it.
        //
        ch  = 0;
       	switch (extraBytesToRead) 
		{
			case 5: ch += *nextChar++; ch <<= 6;
			case 4: ch += *nextChar++; ch <<= 6;
			case 3: ch += *nextChar++; ch <<= 6;
			case 2: ch += *nextChar++; ch <<= 6;
			case 1: ch += *nextChar++; ch <<= 6;
			case 0: ch += *nextChar++;
		}

        // Magically correct the input value
        //
		ch -= offsetsFromUTF8[extraBytesToRead];
		if  (ch == input->get_newlineChar())
		{
			/* Reset for start of a new line of input
			 */
			input->inc_line();
			input->set_charPositionInLine(0);
			input->set_currentLine(nextChar);
		}

        // Update input pointer
        //
        input->set_nextChar(nextChar);
    }
}

/** \brief Return the input element assuming a UTF8 input
 *
 * \param[in] input Input stream context pointer
 * \param[in] la 1 based offset of next input stream element
 *
 * \return Next input character in internal ANTLR3 encoding (UTF32)
 */
template<class ImplTraits, class SuperType>
ANTLR_UCHAR UTF8_IntStream<ImplTraits, SuperType>::LA(ANTLR_INT32 la)
{
    SuperType* input = this->get_super();
	const ANTLR_UINT32* trailingBytesForUTF8 = UTF8_IntStream::TrailingBytesForUTF8();
	const UTF32* offsetsFromUTF8 = UTF8_IntStream::OffsetsFromUTF8();
    ANTLR_UINT32           extraBytesToRead;
    ANTLR_UCHAR            ch;
    ANTLR_UINT8*           nextChar;

    nextChar = input->get_nextChar();

    // Do we need to traverse forwards or backwards?
    // - LA(0) is treated as LA(1) and we assume that the nextChar is
    //   already positioned.
    // - LA(n+) ; n>1 means we must traverse forward n-1 characters catering for UTF8 encoding
    // - LA(-n) means we must traverse backwards n chracters
    //
    if (la > 1) {

        // Make sure that we have at least one character left before trying to
        // loop through the buffer.
        //
        if	(nextChar < (input->get_data() + input->get_sizeBuf()))
        {	
            // Now traverse n-1 characters forward
            //
            while (--la > 0)
            {
                // Does the next character require trailing bytes?
                // If so advance the pointer by that many bytes as well as advancing
                // one position for what will be at least a single byte character.
                //
                nextChar += trailingBytesForUTF8[*nextChar] + 1;

                // Does that calculation take us past the byte length of the buffer?
                //
                if	(nextChar >= (input->get_data() + input->get_sizeBuf()))
                {
                    return ANTLR_CHARSTREAM_EOF;
                }
            }
        }
        else
        {
            return ANTLR_CHARSTREAM_EOF;
        }
    }
    else
    {
        // LA is negative so we decrease the pointer by n character positions
        //
        while   (nextChar > input->get_data() && la++ < 0)
        {
            // Traversing backwards in UTF8 means decermenting by one
            // then continuing to decrement while ever a character pattern
            // is flagged as being a trailing byte of an encoded code point.
            // Trailing UTF8 bytes always start with 10 in binary. We assumne that
            // the UTF8 is well formed and do not check boundary conditions
            //
            nextChar--;
            while ((*nextChar & 0xC0) == 0x80)
            {
                nextChar--;
            }
        }
    }

    // nextChar is now pointing at the UTF8 encoded character that we need to
    // decode and return.
    //
    // Are there more bytes needed to make up the whole thing?
    //
    extraBytesToRead = trailingBytesForUTF8[*nextChar];
    if	(nextChar + extraBytesToRead >= (input->get_data() + input->get_sizeBuf()))
    {
        return ANTLR_CHARSTREAM_EOF;
    }

    // Cases deliberately fall through (see note A in antlrconvertutf.c)
    // 
    ch  = 0;
    switch (extraBytesToRead) 
	{
        case 5: ch += *nextChar++; ch <<= 6;
        case 4: ch += *nextChar++; ch <<= 6;
        case 3: ch += *nextChar++; ch <<= 6;
        case 2: ch += *nextChar++; ch <<= 6;
        case 1: ch += *nextChar++; ch <<= 6;
        case 0: ch += *nextChar++;
    }

    // Magically correct the input value
    //
    ch -= offsetsFromUTF8[extraBytesToRead];

    return ch;
}

template<class ImplTraits>
TokenIntStream<ImplTraits>::TokenIntStream()
{
	m_cachedSize = 0;
}

template<class ImplTraits>
ANTLR_UINT32 TokenIntStream<ImplTraits>::get_cachedSize() const
{
	return m_cachedSize;
}

template<class ImplTraits>
void TokenIntStream<ImplTraits>::set_cachedSize( ANTLR_UINT32 cachedSize )
{
	m_cachedSize = cachedSize;
}

/** Move the input pointer to the next incoming token.  The stream
 *  must become active with LT(1) available.  consume() simply
 *  moves the input pointer so that LT(1) points at the next
 *  input symbol. Consume at least one token.
 *
 *  Walk past any token not on the channel the parser is listening to.
 */
template<class ImplTraits>
void TokenIntStream<ImplTraits>::consume()
{
	TokenStreamType* cts = static_cast<TokenStreamType*>(this);

    if((ANTLR_UINT32)cts->get_p() < m_cachedSize )
	{
		cts->inc_p();
		cts->set_p( cts->skipOffTokenChannels(cts->get_p()) );
	}
}
template<class ImplTraits>
void  TokenIntStream<ImplTraits>::consumeInitialHiddenTokens()
{
	ANTLR_MARKER	first;
	ANTLR_INT32	i;
	TokenStreamType*	ts;

	ts	    = this->get_super();
	first	= this->index();

	for	(i=0; i<first; i++)
	{
		ts->get_debugger()->consumeHiddenToken(ts->get(i));
	}

	ts->set_initialStreamState(false);
}


template<class ImplTraits>
ANTLR_UINT32	TokenIntStream<ImplTraits>::LA( ANTLR_INT32 i )
{
	const CommonTokenType*    tok;
	TokenStreamType*    ts	    = static_cast<TokenStreamType*>(this);

	tok	    =  ts->LT(i);

	if	(tok != NULL)
	{
		return	tok->get_type();
	}
	else
	{
		return	CommonTokenType::TOKEN_INVALID;
	}

}

template<class ImplTraits>
ANTLR_MARKER	TokenIntStream<ImplTraits>::mark()
{
    BaseType::m_lastMarker = this->index();
    return  BaseType::m_lastMarker;
}

template<class ImplTraits>
ANTLR_UINT32 TokenIntStream<ImplTraits>::size()
{
    if (this->get_cachedSize() > 0)
    {
		return  this->get_cachedSize();
    }
    TokenStreamType* cts   = this->get_super();

    this->set_cachedSize( static_cast<ANTLR_UINT32>(cts->get_tokens().size()) );
    return  this->get_cachedSize();
}

template<class ImplTraits>
void	TokenIntStream<ImplTraits>::release()
{
    return;
}

template<class ImplTraits>
ANTLR_MARKER   TokenIntStream<ImplTraits>::tindex()
{
	return this->get_super()->get_p();
}

template<class ImplTraits>
void	TokenIntStream<ImplTraits>::rewindLast()
{
    this->rewind( this->get_lastMarker() );
}

template<class ImplTraits>
void	TokenIntStream<ImplTraits>::rewind(ANTLR_MARKER marker)
{
	return this->seek(marker);
}

template<class ImplTraits>
void	TokenIntStream<ImplTraits>::seek(ANTLR_MARKER index)
{
    TokenStreamType* cts = static_cast<TokenStreamType*>(this);

    cts->set_p( static_cast<ANTLR_INT32>(index) );
}


/// Return a string that represents the name assoicated with the input source
///
/// /param[in] is The ANTLR3_INT_STREAM interface that is representing this token stream.
///
/// /returns 
/// /implements ANTLR3_INT_STREAM_struct::getSourceName()
///
template<class ImplTraits>
typename TokenIntStream<ImplTraits>::StringType
TokenIntStream<ImplTraits>::getSourceName()
{
	// Slightly convoluted as we must trace back to the lexer's input source
	// via the token source. The streamName that is here is not initialized
	// because this is a token stream, not a file or string stream, which are the
	// only things that have a context for a source name.
	//
	return this->get_super()->get_tokenSource()->get_fileName();
}

template<class ImplTraits>
void  TreeNodeIntStream<ImplTraits>::consume()
{
	TreeNodeStreamType* ctns = this->get_super();
	if( ctns->get_p() == -1 )
		ctns->fillBufferRoot();
	ctns->inc_p();
}
template<class ImplTraits>
ANTLR_MARKER		TreeNodeIntStream<ImplTraits>::tindex()
{
	TreeNodeStreamType* ctns = this->get_super();
	return (ANTLR_MARKER)(ctns->get_p());
}

template<class ImplTraits>
ANTLR_UINT32 TreeNodeIntStream<ImplTraits>::LA(ANTLR_INT32 i)
{
	TreeNodeStreamType* tns	    = this->get_super();

	// Ask LT for the 'token' at that position
	//
	TreeTypePtr t = tns->LT(i);

	if	(t == NULL)
	{
		return	CommonTokenType::TOKEN_INVALID;
	}

	// Token node was there so return the type of it
	//
	return  t->get_type();
}

template<class ImplTraits>
ANTLR_MARKER	TreeNodeIntStream<ImplTraits>::mark()
{
	TreeNodeStreamType* ctns	    = this->get_super();
	
	if	(ctns->get_p() == -1)
	{
		ctns->fillBufferRoot();
	}

	// Return the current mark point
	//
	this->set_lastMarker( this->index() );

	return this->get_lastMarker();

}

template<class ImplTraits>
void  TreeNodeIntStream<ImplTraits>::release(ANTLR_MARKER /*marker*/)
{

}

template<class ImplTraits>
void TreeNodeIntStream<ImplTraits>::rewindMark(ANTLR_MARKER marker)
{
	this->seek(marker);
}

template<class ImplTraits>
void TreeNodeIntStream<ImplTraits>::rewindLast()
{
	this->seek( this->get_lastMarker() );
}

template<class ImplTraits>
void	TreeNodeIntStream<ImplTraits>::seek(ANTLR_MARKER index)
{
	TreeNodeStreamType* ctns	    = this->get_super();
	ctns->set_p( ANTLR_UINT32_CAST(index) );
}

template<class ImplTraits>
ANTLR_UINT32	TreeNodeIntStream<ImplTraits>::size()
{
	TreeNodeStreamType* ctns	    = this->get_super();
	
	if	(ctns->get_p() == -1)
	{
		ctns->fillBufferRoot();
	}

	return ctns->get_nodes().size();
}


}