-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgplexx.frame
1245 lines (1116 loc) · 44.7 KB
/
gplexx.frame
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
##
## DO NOT EDIT THIS FILE CARELESSLY!
## THE CONTENTS OF THIS FILE ARE STRONGLY LINKED TO THE
## CODE OF Dfsa.cs AND TaskState.cs, AND ALL MUST VARY
## TOGETHER
##
//
// gplexx.frame
// Version 1.0.0 of 01-November-2008
## Derived from gplex.frame version of 2-September-2006.
## Codepage support for files without a BOM.
## Left and Right Anchored state support.
## Start condition stack. Two generic params.
## Using fixed length context handling for right anchors
//
##-->defines
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
#if !STANDALONE
using gppg;
#endif
##-->version240
##-->usingDcl
{
/// <summary>
/// Summary Canonical example of GPLEX automaton
/// </summary>
#if STANDALONE
//
// These are the dummy declarations for stand-alone GPLEX applications
// normally these declarations would come from the parser.
// If you declare /noparser, or %option noparser then you get this.
//
public enum Tokens
{
EOF = 0, maxParseToken = int.MaxValue
// must have at least these two, values are almost arbitrary
}
public abstract class ScanBase
{
public abstract int yylex();
protected virtual bool yywrap() { return true; }
#if BABEL
protected abstract int CurrentSc { get; set; }
// EolState is the 32-bit of state data persisted at
// the end of each line for Visual Studio colorization.
// The default is to return CurrentSc. You must override
// this if you want more complicated behavior.
public virtual int EolState {
get { return CurrentSc; }
set { CurrentSc = value; }
}
}
public interface IColorScan
{
void SetSource(string source, int offset);
int GetNext(ref int state, out int start, out int end);
#endif // BABEL
}
#endif // STANDALONE
public abstract class ScanBuff
{
public const int EOF = -1;
public abstract int Pos { get; set; }
public abstract int Read();
public abstract int Peek();
public abstract int ReadPos { get; }
public abstract string GetString(int b, int e);
public const int UnicodeReplacementChar = 0xFFFD;
}
// If the compiler can't find ScanBase maybe you need to run
// GPPG with the /gplex option, or GPLEX with /noparser
#if BABEL
public sealed partial class Scanner : ScanBase, IColorScan
{
public ScanBuff buffer;
int currentScOrd = 0; // start condition ordinal
protected override int CurrentSc
{
// The current start state is a property
// to try to avoid the user error of setting
// scState but forgetting to update the FSA
// start state "currentStart"
//
get { return currentScOrd; } // i.e. return YY_START;
set { currentScOrd = value; // i.e. BEGIN(value);
currentStart = startState[value]; }
}
#else // BABEL
public sealed partial class Scanner : ScanBase
{
public ScanBuff buffer;
int currentScOrd = 0; // start condition ordinal
#endif // BABEL
private static int GetMaxParseToken() {
System.Reflection.FieldInfo f = typeof(Tokens).GetField("maxParseToken");
return (f == null ? int.MaxValue : (int)f.GetValue(null));
}
static int parserMax = GetMaxParseToken();
enum Result {accept, noMatch, contextFound};
##-->consts
#region user code
##-->codeIncl
#endregion user code
int state;
int currentStart = startState[0];
int chr; // last character read
int cNum = 0; // ordinal number of chr
int lNum = 0; // current line number
int lineStartNum; // ordinal number at start of line
//
// The following instance variables are used, among other
// things, for constructing the yylloc location objects.
//
int tokPos; // buffer position at start of token
int tokNum; // ordinal number of first character
int tokLen; // number of characters in token
int tokCol; // zero-based column number at start of token
int tokLin; // line number at start of token
int tokEPos; // buffer position at end of token
int tokECol; // column number at end of token
int tokELin; // line number at end of token
string tokTxt; // lazily constructed text of token
#if STACK
private Stack<int> scStack = new Stack<int>();
#endif // STACK
##-->tableDef
#if BACKUP
// ==============================================================
// == Nested class used for backup in automata which do backup ==
// ==============================================================
internal class Context // class used for automaton backup.
{
public int bPos;
public int cNum;
public int lNum; // Need this in case of backup over EOL.
public int state;
public int cChr;
}
#endif // BACKUP
// ==============================================================
// ==== Nested struct to support input switching in scanners ====
// ==============================================================
struct BufferContext {
internal ScanBuff buffSv;
internal int chrSv;
internal int cNumSv;
internal int lNumSv;
internal int startSv;
}
#region Buffer classes
// ==============================================================
// ===== Private methods to save and restore buffer contexts ====
// ==============================================================
/// <summary>
/// This method creates a buffer context record from
/// the current buffer object, together with some
/// scanner state values.
/// </summary>
BufferContext MkBuffCtx()
{
BufferContext rslt;
rslt.buffSv = this.buffer;
rslt.chrSv = this.chr;
rslt.cNumSv = this.cNum;
rslt.lNumSv = this.lNum;
rslt.startSv = this.lineStartNum;
return rslt;
}
/// <summary>
/// This method restores the buffer value and allied
/// scanner state from the given context record value.
/// </summary>
void RestoreBuffCtx(BufferContext value)
{
this.buffer = value.buffSv;
this.chr = value.chrSv;
this.cNum = value.cNumSv;
this.lNum = value.lNumSv;
this.lineStartNum = value.startSv;
}
// ==============================================================
// ===== Nested classes for various ScanBuff derived classes ====
// ==============================================================
// =============== Nested class for string input ================
// ==============================================================
/// <summary>
/// This class read characters from a single string as
/// required, for example, by Visual Studio language services
/// </summary>
public sealed class StringBuff : ScanBuff
{
string str; // input buffer
int bPos; // current position in buffer
int sLen;
public StringBuff(string str)
{
this.str = str;
this.sLen = str.Length;
}
private int Read16()
{
if (bPos < sLen) return str[bPos++];
#if BABEL
else if (bPos == sLen) { bPos++; return '\n'; } // one strike, see newline
#endif // BABEL
else { bPos++; return EOF; } // two strikes and you're out!
}
/// <summary>
/// Read returns UTF32 in this application.
/// </summary>
/// <returns>Unicode point as an int [0 .. 0xFFFFF]; -1 for EOF</returns>
public override int Read()
{
int utf16 = Read16();
if (utf16 < 0xD800 || utf16 > 0xDBFF)
return utf16;
else
{
int low16 = Read16();
if (low16 < 0xDC00 || low16 > 0xDFFF)
return UnicodeReplacementChar;
else
return (0x10000 + (utf16 & 0x3FF << 10) + (low16 & 0x3FF));
}
}
public override int ReadPos { get { return bPos - 1; } }
public override int Peek()
{
if (bPos < sLen) return str[bPos];
else return '\n';
}
public override string GetString(int beg, int end)
{
// "end" can be greater than sLen with the BABEL
// option set. Read returns a "virtual" EOL if
// an attempt is made to read past the end of the
// string buffer. Without the guard any attempt
// to fetch yytext for a token that includes the
// EOL will throw an index exception.
if (end > sLen) end = sLen;
if (end <= beg) return "";
else return str.Substring(beg, end - beg);
}
public override int Pos
{
get { return bPos; }
set { bPos = value; }
}
}
#if !NOFILES
// ====================== Nested class ==========================
// The LineBuff class contributed by Nigel Horspool,
// nigelh@cs.uvic.cs
// ==============================================================
public sealed class LineBuff : ScanBuff
{
IList<string> line; // list of source lines from a file
int numLines; // number of strings in line list
string curLine; // current line in that list
int cLine; // index of current line in the list
int curLen; // length of current line
int curLineStart; // position of line start in whole file
int curLineEnd; // position of line end in whole file
int maxPos; // max position ever visited in whole file
int cPos; // ordinal number of chr in source
// Constructed from a list of strings, one per source line.
// The lines have had trailing '\n' characters removed.
public LineBuff(IList<string> lineList)
{
line = lineList;
numLines = line.Count;
cPos = curLineStart = 0;
curLine = (numLines>0 ? line[0] : "");
maxPos = curLineEnd = curLen = curLine.Length;
cLine = 1;
}
public override int Read()
{
if (cPos < curLineEnd)
return curLine[cPos++ - curLineStart];
if (cPos++ == curLineEnd)
return '\n';
if (cLine >= numLines)
return EOF;
curLine = line[cLine];
curLen = curLine.Length;
curLineStart = curLineEnd + 1;
curLineEnd = curLineStart + curLen;
if (curLineEnd>maxPos)
maxPos = curLineEnd;
cLine++;
return curLen>0? curLine[0] : '\n';
}
public override int Peek()
{
return (cPos < curLineEnd)? curLine[cPos - curLineStart] : '\n';
}
// To speed up searches for the line containing a position
private int cachedPos = 0;
private int cachedIx = 0;
private int cachedLstart = 0;
// Given a position pos within the entire source, the results are
// ix -- the index of the containing line
// lstart -- the position of the first character on that line
private void findIndex( int pos, out int ix, out int lstart )
{
if (pos >= cachedPos) {
ix = cachedIx; lstart = cachedLstart;
} else {
ix = lstart = 0;
}
for( ; ; ) {
int len = line[ix].Length + 1;
if (pos < lstart+len) break;
lstart += len;
ix++;
}
cachedPos = pos;
cachedIx = ix;
cachedLstart = lstart;
}
public override string GetString(int beg, int end)
{
if (beg >= maxPos || end <= beg) return "";
int endIx, begIx, endLineStart, begLineStart;
findIndex(beg, out begIx, out begLineStart);
int begCol = beg - begLineStart;
findIndex(end, out endIx, out endLineStart);
int endCol = end - endLineStart;
string s = line[begIx];
if (begIx == endIx) {
// the usual case, substring all on one line
return (endCol <= s.Length)?
s.Substring(begCol, endCol-begCol)
: s.Substring(begCol) + "\n";
}
// the string spans multiple lines, yuk!
StringBuilder sb = new StringBuilder();
if (begCol < s.Length)
sb.Append(s.Substring(begCol));
for( ; ; ) {
sb.Append("\n");
s = line[++begIx];
if (begIx >= endIx) break;
sb.Append(s);
}
if (endCol <= s.Length) {
sb.Append(s.Substring(0, endCol));
} else {
sb.Append(s);
sb.Append("\n");
}
return sb.ToString();
}
public override int Pos
{
get { return cPos; }
set {
cPos = value;
findIndex(cPos, out cLine, out curLineStart);
curLine = line[cLine];
curLineEnd = curLineStart+curLine.Length;
}
}
public override int ReadPos { get { return cPos - 1; } }
}
// ==============================================================
// =========== Nested class StreamBuff : byte files =============
// ==============================================================
/// <summary>
/// This is the Buffer for byte files. It returns 8-bit characters.
/// </summary>
public class StreamBuff : ScanBuff
{
protected BufferedStream bStrm; // input buffer
protected int delta = 1; // number of bytes in chr, could be 0 for EOF.
public StreamBuff(Stream str) { this.bStrm = new BufferedStream(str); }
/// <summary>
/// Read returns subset [0 .. 0xFF] in this application.
/// </summary>
/// <returns>Unicode point as an int [0 .. 0xFF]; -1 for EOF</returns>
public override int Read() {
int ch0 = bStrm.ReadByte();
delta = (ch0 == EOF ? 0 : 1);
return ch0;
}
public override int ReadPos {
get { return (int)bStrm.Position - delta; }
}
public override int Peek()
{
int rslt = bStrm.ReadByte();
bStrm.Seek(-delta, SeekOrigin.Current);
return rslt;
}
public override string GetString(int beg, int end)
{
if (end - beg <= 0) return "";
long savePos = bStrm.Position;
char[] arr = new char[end - beg];
bStrm.Position = (long)beg;
for (int i = 0; i < (end - beg); i++)
arr[i] = (char)bStrm.ReadByte();
bStrm.Position = savePos;
return new String(arr);
}
// Pos is the position *after* reading chr!
public override int Pos
{
get { return (int)bStrm.Position; }
set { bStrm.Position = value; }
}
public static int GetCodePage(string command)
{
command = command.ToLower();
if (command.StartsWith("codepage:"))
command = command.Substring(9);
try
{
if (command.Equals("raw"))
return -1;
else if (command.Equals("guess"))
return -2;
else if (command.Equals("default"))
return 0;
else if (char.IsDigit(command[0]))
return int.Parse(command);
else
{
Encoding enc = Encoding.GetEncoding(command);
return enc.CodePage;
}
}
catch
{
Console.Error.WriteLine(
"Unknown codepage \"{0}\", using machine default", command);
return 0;
}
}
}
#if !BYTEMODE
// ==============================================================
// ===== Nested class CodePageBuff : for unicode text files =====
// ========= Byte files mapped by a specified CodePage ==========
// ==============================================================
public sealed class CodePageBuff : StreamBuff
{
char[] map = new char[256];
public CodePageBuff(Stream stream) : this(stream, Encoding.Default) { }
public CodePageBuff(Stream stream, Encoding enc) : base(stream)
{
bool done;
int bNum, cNum;
byte[] bArray = new byte[256];
if (!enc.IsSingleByte)
throw new NotImplementedException(
"Only UTF-8, UTF-16 and single-byte code pages allowed");
Decoder decoder = enc.GetDecoder();
for (int i = 0; i < 256; i++)
bArray[i] = (byte)i;
decoder.Convert(bArray, 0, 256, this.map, 0, 256, true, out bNum, out cNum, out done);
}
/// <summary>
/// Read returns subset unicode char corresponding to the
/// given character in the chosen codepage.
/// </summary>
/// <returns>Unicode point as an int; -1 for EOF</returns>
public override int Read() {
int ch0 = bStrm.ReadByte();
if (ch0 == EOF)
{
delta = 0; return -1;
}
else
{
delta = 1; return this.map[ch0];
}
}
public override string GetString(int beg, int end)
{
if (end - beg <= 0) return "";
long savePos = bStrm.Position;
int saveDelta = delta;
char[] arr = new char[end - beg];
bStrm.Position = (long)beg;
for (int i = 0; i < (end - beg); i++)
arr[i] = (char)Read();
bStrm.Position = savePos;
delta = saveDelta;
return new String(arr);
}
}
// ==============================================================
// ====== Nested class TextBuff : for unicode text files ========
// ==== This is the UTF8 class, UTF16 handled by subclasses =====
// ==============================================================
/// <summary>
/// This is the Buffer for UTF8 files.
/// It attempts to read the encoding preamble, which for
/// this encoding should be unicode point \uFEFF which is
/// encoded as EF BB BF
/// </summary>
public class TextBuff : ScanBuff
{
protected BufferedStream bStrm; // input buffer
protected int delta = 1; // length of chr, zero for EOF!
/// <summary>
/// TextBuff factory. Reads the file preamble
/// and returns a TextBuff, LittleEndTextBuff or
/// BigEndTextBuff according to the result.
/// </summary>
/// <param name="strm">The underlying stream</param>
/// <returns></returns>
public static ScanBuff NewTextBuff(Stream strm, int fallbackCodepage)
{
// First check if this is a UTF16 file
//
int b0 = strm.ReadByte();
int b1 = strm.ReadByte();
if (b0 == 0xfe && b1 == 0xff)
return new BigEndTextBuff(strm);
if (b0 == 0xff && b1 == 0xfe)
return new LittleEndTextBuff(strm);
int b2 = strm.ReadByte();
if (b0 == 0xef && b1 == 0xbb && b2 == 0xbf)
return new TextBuff(strm);
//
// There is no unicode preamble, so
// we go back to the bytefile default.
//
strm.Seek(0, SeekOrigin.Begin);
switch (fallbackCodepage)
{
case -2:
{
int guess = new Guesser.Scanner(strm).GuessCodepage();
strm.Seek(0, SeekOrigin.Begin);
if (guess == 0)
return new CodePageBuff(strm);
else if (guess == 65001)
return new TextBuff(strm);
else
return new StreamBuff(strm);
}
case -1:
return new StreamBuff(strm);
case 0:
return new CodePageBuff(strm); // Default codepage
case 65001:
return new TextBuff(strm);
case 1200:
return new LittleEndTextBuff(strm);
case 1201:
return new BigEndTextBuff(strm);
default:
return new CodePageBuff(strm, Encoding.GetEncoding(fallbackCodepage));
}
}
protected TextBuff(Stream str) {
this.bStrm = new BufferedStream(str);
}
/// <summary>
/// Read returns UTF32 in this application.
/// </summary>
/// <returns>Unicode point as an int [0 .. 0xFFFFF]; -1 for EOF</returns>
public override int Read()
{
int ch0 = bStrm.ReadByte();
int ch1, ch2, ch3,ch4, ch5;
int result = UnicodeReplacementChar;
if (ch0 < 0x7f)
{
delta = (ch0 == EOF ? 0 : 1);
result = ch0;
}
else if ((ch0 & 0xe0) == 0xc0)
{
delta = 2;
ch1 = bStrm.ReadByte();
if ((ch1 & 0xc0) == 0x80)
result = ((ch0 & 0x1f) << 6) + (ch1 & 0x3f);
}
else if ((ch0 & 0xf0) == 0xe0)
{
delta = 3;
ch1 = bStrm.ReadByte(); ch2 = bStrm.ReadByte();
if ((ch1 & 0xc0) == 0x80 && (ch2 & 0xc0) == 0x80)
result = ((ch0 & 0xf) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);
}
else if ((ch0 & 0xf8) == 0xf0)
{
delta = 4;
ch1 = bStrm.ReadByte(); ch2 = bStrm.ReadByte(); ch3 = bStrm.ReadByte();
if ((ch1 & 0xc0) == 0x80 && (ch2 & 0xc0) == 0x80 && (ch3 & 0xc0) == 0x80)
{
result = ((ch0 & 0x7) << 18) + ((ch1 & 0x3f) << 12) +
((ch2 & 0x3f) << 6) + (ch3 & 0x3f);
}
}
else if ((ch0 & 0xfc) == 0xf8)
{
delta = 5;
ch1 = bStrm.ReadByte(); ch2 = bStrm.ReadByte();
ch3 = bStrm.ReadByte(); ch4 = bStrm.ReadByte();
if ((ch1 & 0xc0) == 0x80 && (ch2 & 0xc0) == 0x80 && (ch3 & 0xc0) == 0x80 && (ch4 & 0xc0) == 0x80)
{
result = ((ch0 & 0x3) << 24) + ((ch1 & 0x3f) << 18) +
((ch2 & 0x3f) << 12) + ((ch3 & 0x3f) << 6) + (ch4 & 0x3f);
}
}
else if ((ch0 & 0xfe) == 0xfc)
{
delta = 6;
ch1 = bStrm.ReadByte(); ch2 = bStrm.ReadByte(); ch3 = bStrm.ReadByte();
ch4 = bStrm.ReadByte(); ch5 = bStrm.ReadByte();
if ((ch1 & 0xc0) == 0x80 && (ch2 & 0xc0) == 0x80 &&
(ch3 & 0xc0) == 0x80 && (ch4 & 0xc0) == 0x80 && (ch5 & 0xc0) == 0x80)
{
result = ((ch0 & 0x1) << 28) + ((ch1 & 0x3f) << 24) + ((ch2 & 0x3f) << 18) +
((ch3 & 0x3f) << 12) + ((ch4 & 0x3f) << 6) + (ch5 & 0x3f);
}
}
return result;
}
public sealed override int ReadPos
{
get { return (int)bStrm.Position - delta; }
}
public sealed override int Peek()
{
int rslt = Read();
bStrm.Seek(-delta, SeekOrigin.Current);
return rslt;
}
/// <summary>
/// Returns the string from the buffer between
/// the given file positions. This needs to be
/// done carefully, as the number of characters
/// is, in general, not equal to (end - beg).
/// </summary>
/// <param name="beg">Begin filepos</param>
/// <param name="end">End filepos</param>
/// <returns></returns>
public override string GetString(int beg, int end)
{
int i;
if (end - beg <= 0) return "";
long savePos = bStrm.Position;
int saveDelta = delta;
//
// The length of the array cannot be larger than
// end - beg, since if the characters expand by
// using surrogate pairs the UTF8 must have taken
// up at least four bytes for any such character.
//
char[] arr = new char[end - beg];
bStrm.Position = (long)beg;
// Normally each call of Read() returns a valid UTF16
// value which will take up one place in the string.
// However, if read returns a value > 0xFFFF then a
// *pair* of surrogate characters must be added.
for (i = 0; bStrm.Position < end; i++)
{
int value = Read();
if (value < 0xFFFF)
arr[i] = (char)value;
else
{
int temp = value - 0x10000;
arr[i++] = (char)(0xD800 + temp / 1024);
arr[i] = (char)(0xDC00 + temp % 1024);
}
}
bStrm.Position = savePos;
delta = saveDelta;
return new String(arr, 0, i);
}
// Pos is the position *after* reading chr!
public sealed override int Pos
{
get { return (int)bStrm.Position; }
set { bStrm.Position = value; }
}
}
// ==============================================================
// ====================== Nested class ==========================
// ==============================================================
/// <summary>
/// This is the Buffer for Big-endian UTF16 files.
/// </summary>
public sealed class BigEndTextBuff : TextBuff
{
internal BigEndTextBuff(Stream str) : base(str) { } //
private int Read16()
{
int ch0 = bStrm.ReadByte();
int ch1 = bStrm.ReadByte();
if (ch1 == EOF)
{
// An EOF in either byte counts as an EOF
delta = (ch0 == EOF ? 0 : 1);
return -1;
}
else
{
delta = 2;
return (ch0 << 8) + ch1;
}
}
/// <summary>
/// Read returns UTF32 in this application.
/// </summary>
/// <returns>Unicode point as an int [0 .. 0xFFFFF]; -1 for EOF</returns>
public override int Read()
{
int utf16 = Read16();
if (utf16 < 0xD800 || utf16 > 0xDBFF)
return utf16;
else
{
int low16 = Read16();
delta = 4;
if (low16 < 0xDC00 || low16 > 0xDFFF)
return UnicodeReplacementChar;
else
return (0x10000 + (utf16 & 0x3FF << 10) + (low16 & 0x3FF));
}
}
/// <summary>
/// Returns the string from the buffer between
/// the given file positions. This needs to be
/// done carefully, as the number of characters
/// is, in general, not equal to (end - beg).
/// </summary>
/// <param name="beg">Begin filepos</param>
/// <param name="end">End filepos</param>
/// <returns></returns>
public sealed override string GetString(int beg, int end)
{
int i;
if (end - beg <= 0) return "";
long savePos = bStrm.Position;
int saveDelta = delta;
char[] arr = new char[end - beg];
bStrm.Position = (long)beg;
// This stream will have utf16 valid for strings
for (i = 0; bStrm.Position < end; i++)
arr[i] = (char)Read();
bStrm.Position = savePos;
delta = saveDelta;
return new String(arr, 0, i);
}
}
// ==============================================================
// ====================== Nested class ==========================
// ==============================================================
/// <summary>
/// This is the Buffer for Little-endian UTF16 files.
/// </summary>
public sealed class LittleEndTextBuff : TextBuff
{
internal LittleEndTextBuff(Stream str) : base(str) { } // { this.bStrm = new BufferedStream(str); }
private int Read16()
{
int ch0 = bStrm.ReadByte();
int ch1 = bStrm.ReadByte();
if (ch1 == EOF)
{
// An EOF in either byte counts as an EOF
delta = (ch0 == EOF ? 0 : 1);
return -1;
}
else
{
delta = 2;
return (ch1 << 8) + ch0;
}
}
/// <summary>
/// Read returns UTF32 in this application.
/// </summary>
/// <returns>Unicode point as an int [0 .. 0xFFFFF]; -1 for EOF</returns>
public override int Read()
{
int utf16 = Read16();
if (utf16 < 0xD800 || utf16 > 0xDBFF)
return utf16;
else
{
int low16 = Read16();
delta = 4;
if (low16 < 0xDC00 || low16 > 0xDFFF)
return UnicodeReplacementChar;
else
return (0x10000 + (utf16 & 0x3FF << 10) + (low16 & 0x3FF));
}
}
/// <summary>
/// Returns the string from the buffer between
/// the given file positions. This needs to be
/// done carefully, as the number of characters
/// is, in general, not equal to (end - beg).
/// </summary>
/// <param name="beg">Begin filepos</param>
/// <param name="end">End filepos</param>
/// <returns></returns>
public sealed override string GetString(int beg, int end)
{
int i;
if (end - beg <= 0) return "";
long savePos = bStrm.Position;
int saveDelta = delta;
char[] arr = new char[end - beg];
bStrm.Position = (long)beg;
// This stream will have utf16 valid for strings
for (i = 0; bStrm.Position < end; i++)
arr[i] = (char)Read16();
bStrm.Position = savePos;
delta = saveDelta;
return new String(arr, 0, i);
}
}
#endif // !BYTEMODE
#endif // !NOFILES
#endregion Buffer classes
// =================== End Nested classes =======================
#if !NOFILES
public Scanner(Stream file) {
##-->bufferCtor
}
#endif // !NOFILES
public Scanner() { }
void GetChr()
{
if (chr == '\n') // This needs to be fixed for other conventions
{
lineStartNum = cNum + 1;
lNum++;
}
chr = buffer.Read();
cNum++;
}
void MarkToken()
{
tokPos = buffer.ReadPos;
tokNum = cNum;
tokLin = lNum;
tokCol = cNum - lineStartNum;
}
void MarkEnd()
{
tokTxt = null;
tokLen = cNum - tokNum;
tokEPos = buffer.ReadPos;
tokELin = lNum;
tokECol = cNum - lineStartNum;
}
// ==============================================================
// ===== Initialization of string-based input buffers ====
// ==============================================================
/// <summary>
/// Create and initialize a StringBuff buffer object for this scanner
/// </summary>
/// <param name="source">the input string</param>
/// <param name="offset">starting offset in the string</param>
public void SetSource(string source, int offset)
{
this.buffer = new StringBuff(source);
this.buffer.Pos = offset;
this.lNum = 0;
this.cNum = offset - 1;
this.chr = '\n'; // to initialize yyline, yycol and lineStart
GetChr();
}
#if !NOFILES
// ================ LineBuffer Initialization ===================
/// <summary>
/// Create and initialize a LineBuff buffer object for this scanner
/// </summary>
/// <param name="source">the list of input strings</param>
public void SetSource(IList<string> source)
{
this.buffer = new LineBuff(source);
this.chr = '\n'; // to initialize yyline, yycol and lineStart
this.lNum = 0;
this.cNum = -1;
GetChr();
}
// =============== StreamBuffer Initialization ==================
/// <summary>
/// Create and initialize a StreamBuff buffer object for this scanner.
/// StreamBuff is buffer for 8-bit byte files.
/// </summary>
/// <param name="source">the input byte stream</param>