25 using System.Collections.Generic;
27 using Babel.ParserGenerator;
31 namespace NShader.Lexer
52 public abstract int yylex();
53 protected abstract int CurrentSc {
get; set; }
58 public virtual int EolState {
get {
return CurrentSc; } set { CurrentSc = value; } }
63 void SetSource(
string source,
int offset);
64 int GetNext(ref
int state, out
int start, out
int end);
73 public const int EOF = -1;
74 public abstract int Pos {
get; set; }
75 public abstract int Read();
76 public abstract int Peek();
77 public abstract int ReadPos {
get; }
78 public abstract string GetString(
int b,
int e);
90 private static int GetMaxParseToken() {
91 System.Reflection.FieldInfo f = typeof(
Tokens).GetField(
"maxParseToken");
92 return (f == null ?
int.MaxValue : (
int)f.GetValue(null));
95 static int parserMax = GetMaxParseToken();
97 protected override int CurrentSc
104 get {
return scState; }
105 set { scState = value; currentStart = startState[value]; }
108 enum Result {accept, noMatch, contextFound};
110 const int maxAccept = 60;
111 const int initial = 61;
112 const int eofNum = 0;
113 const int goStart = -1;
114 const int INITIAL = 0;
115 const int COMMENT = 1;
149 int currentStart = initial;
169 private Stack<int> scStack =
new Stack<int>();
172 #region ScannerTables
174 public int min;
public int rng;
public int dflt;
176 public Table(
int m,
int x,
int d, sbyte[] n) {
177 min = m; rng = x; dflt = d; nxt = n;
181 static int[] startState = {61, 56, 0};
183 static int[] anchorState = {62, 56, 0};
192 static sbyte[] map0 =
new sbyte[126] {
193 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 0, 5, 5, 5, 2, 2,
194 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
195 5, 26, 17, 19, 2, 21, 22, 2, 31, 32, 3, 12, 30, 13, 14, 1,
196 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 4, 27, 24, 20, 25, 2,
197 2, 10, 10, 10, 10, 11, 16, 6, 15, 6, 6, 6, 6, 6, 6, 6,
198 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 33, 18, 34, 2, 6,
199 2, 10, 10, 10, 10, 11, 16, 6, 15, 6, 6, 6, 6, 6, 6, 6,
200 6, 6, 6, 6, 6, 6, 6, 6, 9, 6, 6, 28, 23, 29 };
204 if (chr < 126)
return map0[chr - 0];
205 else return (sbyte)2;
213 new Table(0, 0, 0, null);
215 new Table(0, 20, -1,
new sbyte[] {72, -1, -1, -1, -1, 72,
216 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 73});
218 new Table(20, 19, -1,
new sbyte[] {52, -1, -1, -1, -1, -1,
219 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 50, -1, 51});
221 new Table(0, 0, -1, null);
223 new Table(20, 1, -1,
new sbyte[] {49});
225 new Table(0, 17, -1,
new sbyte[] {74, -1, -1, -1, -1, 74,
226 48, -1, -1, 48, 48, 48, -1, -1, -1, 48, 48});
228 new Table(6, 11, -1,
new sbyte[] {6, 6, 6, 6, 6, 6,
231 new Table(7, 8, -1,
new sbyte[] {7, 7, -1, -1, 68, -1,
234 new Table(7, 8, -1,
new sbyte[] {7, 7, 67, -1, 68, -1,
237 new Table(20, 1, -1,
new sbyte[] {42});
239 new Table(20, 1, -1,
new sbyte[] {41});
241 new Table(7, 2, -1,
new sbyte[] {38, 38});
243 new Table(17, 2, 63,
new sbyte[] {37, 64});
245 new Table(6, 11, -1,
new sbyte[] {36, -1, -1, 36, 36, 36,
246 -1, -1, -1, 36, 36});
248 new Table(20, 1, -1,
new sbyte[] {35});
250 new Table(20, 1, -1,
new sbyte[] {34});
252 new Table(22, 1, -1,
new sbyte[] {33});
254 new Table(23, 1, -1,
new sbyte[] {32});
256 new Table(20, 1, -1,
new sbyte[] {31});
258 new Table(20, 1, -1,
new sbyte[] {30});
260 new Table(20, 1, -1,
new sbyte[] {29});
262 new Table(0, 0, -1, null);
264 new Table(0, 0, -1, null);
266 new Table(0, 0, -1, null);
268 new Table(0, 0, -1, null);
270 new Table(0, 0, -1, null);
272 new Table(0, 0, -1, null);
274 new Table(0, 0, -1, null);
276 new Table(0, 0, -1, null);
278 new Table(0, 0, -1, null);
280 new Table(0, 0, -1, null);
282 new Table(0, 0, -1, null);
284 new Table(0, 0, -1, null);
286 new Table(0, 0, -1, null);
288 new Table(0, 0, -1, null);
290 new Table(0, 0, -1, null);
292 new Table(6, 11, -1,
new sbyte[] {36, -1, -1, 36, 36, 36,
293 -1, -1, -1, 36, 36});
295 new Table(0, 0, -1, null);
297 new Table(7, 10, -1,
new sbyte[] {38, 38, -1, -1, 65, -1,
300 new Table(0, 0, -1, null);
302 new Table(7, 10, -1,
new sbyte[] {40, 40, -1, -1, -1, -1,
305 new Table(0, 0, -1, null);
307 new Table(0, 0, -1, null);
309 new Table(7, 10, -1,
new sbyte[] {38, 38, -1, -1, 69, -1,
312 new Table(0, 0, -1, null);
314 new Table(7, 10, -1,
new sbyte[] {45, 45, -1, -1, -1, -1,
317 new Table(7, 2, -1,
new sbyte[] {46, 46});
319 new Table(7, 10, -1,
new sbyte[] {47, 47, -1, 47, 47, -1,
322 new Table(6, 11, -1,
new sbyte[] {48, 48, 48, 48, 48, 48,
323 -1, -1, -1, 48, 48});
325 new Table(0, 0, -1, null);
327 new Table(0, 1, 50,
new sbyte[] {-1});
329 new Table(0, 4, 51,
new sbyte[] {-1, 51, 51, 53});
331 new Table(0, 0, -1, null);
333 new Table(1, 3, -1,
new sbyte[] {54, -1, 53});
335 new Table(0, 0, -1, null);
337 new Table(0, 0, -1, null);
339 new Table(0, 4, 58,
new sbyte[] {57, 58, 58, 59});
341 new Table(0, 0, -1, null);
343 new Table(0, 4, 58,
new sbyte[] {-1, 58, 58, 59});
345 new Table(1, 3, -1,
new sbyte[] {60, -1, 59});
347 new Table(0, 0, -1, null);
349 new Table(12, 32, 6,
new sbyte[] {9, 10, 11, 6, 6, 12,
350 3, 3, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
351 28, 55, 2, 3, 4, 5, 55, 6, 7, 8});
353 new Table(12, 32, 6,
new sbyte[] {9, 10, 11, 6, 6, 12,
354 3, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
355 28, 1, 2, 3, 4, 5, 1, 6, 7, 8});
357 new Table(17, 2, 63,
new sbyte[] {37, 64});
359 new Table(0, 1, 63,
new sbyte[] {-1});
361 new Table(7, 7, -1,
new sbyte[] {40, 40, -1, -1, -1, 66,
364 new Table(7, 2, -1,
new sbyte[] {40, 40});
366 new Table(7, 10, -1,
new sbyte[] {47, 47, -1, 47, 47, -1,
369 new Table(7, 7, -1,
new sbyte[] {46, 46, -1, -1, -1, 71,
372 new Table(7, 7, -1,
new sbyte[] {45, 45, -1, -1, -1, 70,
375 new Table(7, 2, -1,
new sbyte[] {45, 45});
377 new Table(7, 2, -1,
new sbyte[] {46, 46});
379 new Table(0, 20, -1,
new sbyte[] {72, -1, -1, -1, -1, 72,
380 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 73});
382 new Table(6, 11, -1,
new sbyte[] {36, -1, -1, 36, 36, 36,
383 -1, -1, -1, 36, 36});
385 new Table(0, 17, -1,
new sbyte[] {74, -1, -1, -1, -1, 74,
386 48, -1, -1, 48, 48, 48, -1, -1, -1, 48, 48});
389 int NextState(
int qStat) {
390 if (chr == ScanBuff.EOF)
391 return (qStat <= maxAccept && qStat != currentStart ? currentStart : eofNum);
394 int idx = Map(chr) - NxS[qStat].min;
395 if (idx < 0) idx += 35;
396 if ((uint)idx >= (uint)NxS[qStat].rng) rslt = NxS[qStat].dflt;
397 else rslt = NxS[qStat].nxt[idx];
398 return (rslt == goStart ? currentStart : rslt);
403 if (chr == ScanBuff.EOF)
404 return (state <= maxAccept && state != currentStart ? currentStart : eofNum);
407 int idx = Map(chr) - NxS[state].min;
408 if (idx < 0) idx += 35;
409 if ((uint)idx >= (uint)NxS[state].rng) rslt = NxS[state].dflt;
410 else rslt = NxS[state].nxt[idx];
411 return (rslt == goStart ? currentStart : rslt);
420 internal class Context
441 this.sLen = str.Length;
446 if (bPos < sLen)
return str[bPos++];
447 else if (bPos == sLen) { bPos++;
return '\n'; }
451 public override int ReadPos {
get {
return bPos - 1; } }
455 if (bPos < sLen)
return str[bPos];
467 if (end > sLen) end = sLen;
468 if (end <= beg)
return "";
469 else return str.Substring(beg, end - beg);
472 public override int Pos
475 set { bPos = value; }
483 BufferedStream bStrm;
489 return bStrm.ReadByte();
492 public override int ReadPos {
493 get {
return (
int)bStrm.Position - delta; }
498 int rslt = bStrm.ReadByte();
499 bStrm.Seek(-delta, SeekOrigin.Current);
505 if (end - beg <= 0)
return "";
506 long savePos = bStrm.Position;
507 char[] arr =
new char[end - beg];
508 bStrm.Position = (long)beg;
509 for (
int i = 0; i < (end - beg); i++)
510 arr[i] = (
char)bStrm.ReadByte();
511 bStrm.Position = savePos;
512 return new String(arr);
516 public override int Pos
518 get {
return (
int)bStrm.Position; }
519 set { bStrm.Position = value; }
534 protected int delta = 1;
537 {
return new Exception(String.Format(
"BadUTF8 Character")); }
550 int b0 = strm.ReadByte();
551 int b1 = strm.ReadByte();
553 if (b0 == 0xfe && b1 == 0xff)
555 if (b0 == 0xff && b1 == 0xfe)
558 int b2 = strm.ReadByte();
559 if (b0 == 0xef && b1 == 0xbb && b2 == 0xbf)
565 strm.Seek(0, SeekOrigin.Begin);
570 this.bStrm =
new BufferedStream(str);
575 int ch0 = bStrm.ReadByte();
580 delta = (ch0 ==
EOF ? 0 : 1);
583 else if ((ch0 & 0xe0) == 0xc0)
586 ch1 = bStrm.ReadByte();
587 if ((ch1 & 0xc0) == 0x80)
588 return ((ch0 & 0x1f) << 6) + (ch1 & 0x3f);
592 else if ((ch0 & 0xf0) == 0xe0)
595 ch1 = bStrm.ReadByte();
596 ch2 = bStrm.ReadByte();
597 if ((ch1 & ch2 & 0xc0) == 0x80)
598 return ((ch0 & 0xf) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);
606 public sealed
override int ReadPos
608 get {
return (
int)bStrm.Position - delta; }
611 public sealed
override int Peek()
614 bStrm.Seek(-delta, SeekOrigin.Current);
627 public sealed
override string GetString(
int beg,
int end)
630 if (end - beg <= 0)
return "";
631 long savePos = bStrm.Position;
632 char[] arr =
new char[end - beg];
633 bStrm.Position = (long)beg;
634 for (i = 0; bStrm.Position < end; i++)
635 arr[i] = (
char)Read();
636 bStrm.Position = savePos;
637 return new String(arr, 0, i);
641 public sealed
override int Pos
643 get {
return (
int)bStrm.Position; }
644 set { bStrm.Position = value; }
658 int ch0 = bStrm.ReadByte();
659 int ch1 = bStrm.ReadByte();
660 return (ch0 << 8) + ch1;
674 int ch0 = bStrm.ReadByte();
675 int ch1 = bStrm.ReadByte();
676 return (ch1 << 8) + ch0;
683 buffer = TextBuff.NewTextBuff(file);
695 lineStartNum = cNum + 1;
704 tokPos = buffer.ReadPos;
707 tokCol = cNum - lineStartNum;
713 tokLen = cNum - tokNum;
714 tokEPos = buffer.ReadPos;
716 tokECol = cNum - lineStartNum;
724 this.buffer.Pos = offset;
725 this.cNum = offset - 1;
730 public int GetNext(ref
int state, out
int start, out
int end)
750 do { next = Scan(); }
while (next >= parserMax);
754 int yyleng {
get {
return tokLen; } }
755 int yypos {
get {
return tokPos; } }
756 int yyline {
get {
return tokLin; } }
757 int yycol {
get {
return tokCol; } }
764 tokTxt = buffer.GetString(tokPos, tokEPos);
772 for (
int i = 0; i <= n; i++) GetChr();
777 set { this.handler = value; }}
781 internal int YY_START {
782 get {
return CurrentSc; }
783 set { CurrentSc = value; }
794 bool inAccept =
false;
795 Result rslt = Result.noMatch;
798 if (lineStartNum == cNum && NextState(anchorState[CurrentSc]) != currentStart)
799 state = anchorState[CurrentSc];
801 state = currentStart;
802 while (NextState() == state) {
804 if (lineStartNum == cNum) {
805 int anchor = anchorState[CurrentSc];
806 if (NextState(anchor) != state) {
813 #else // !LEFTANCHORS
814 state = currentStart;
815 while (NextState() == state)
817 #endif // LEFTANCHORS
820 while ((next = NextState()) != currentStart)
821 if (inAccept && next > maxAccept)
823 Context ctx =
new Context();
824 rslt = Recurse2(ctx, next);
825 if (rslt == Result.noMatch) RestoreStateAndPos(ctx);
833 if (state <= maxAccept) inAccept =
true;
837 if (lineStartNum == cNum) {
838 int anchor = anchorState[CurrentSc];
839 if (NextState(anchor) != currentStart)
843 state = currentStart;
844 while (NextState() == state) {
846 if (lineStartNum == cNum) {
847 anchor = anchorState[CurrentSc];
848 if (NextState(anchor) != state) {
855 #else // !LEFTANCHORS
856 state = currentStart;
857 while (NextState() == state)
859 #endif // LEFTANCHORS
862 while ((next = NextState()) != currentStart)
868 if (state > maxAccept)
869 state = currentStart;
874 #pragma warning disable 162
878 return (
int)Tokens.EOF;
884 return (
int)ShaderToken.OPERATOR;
891 return (
int)ShaderToken.UNDEFINED;
894 return (
int)ShaderToken.OPERATOR;
897 return (
int)ShaderTokenProvider.GetTokenFromIdentifier(yytext);
901 return (
int)ShaderToken.NUMBER;
904 return (
int)ShaderToken.OPERATOR;
907 return (
int)ShaderToken.OPERATOR;
910 return (
int)ShaderToken.OPERATOR;
913 return (
int)ShaderToken.OPERATOR;
916 return (
int)ShaderToken.OPERATOR;
919 return (
int)ShaderToken.OPERATOR;
922 return (
int)ShaderToken.OPERATOR;
925 return (
int)ShaderToken.OPERATOR;
928 return (
int)ShaderToken.OPERATOR;
931 return (
int)ShaderToken.DELIMITER;
934 return (
int)ShaderToken.LEFT_BRACKET;
937 return (
int)ShaderToken.RIGHT_BRACKET;
940 return (
int)ShaderToken.DELIMITER;
943 return (
int)ShaderToken.LEFT_PARENTHESIS;
946 return (
int)ShaderToken.RIGHT_PARENTHESIS;
949 return (
int)ShaderToken.LEFT_SQUARE_BRACKET;
952 return (
int)ShaderToken.RIGHT_SQUARE_BRACKET;
955 return (
int)ShaderToken.OPERATOR;
958 return (
int)ShaderToken.OPERATOR;
961 return (
int)ShaderToken.OPERATOR;
964 return (
int)ShaderToken.OPERATOR;
967 return (
int)ShaderToken.OPERATOR;
970 return (
int)ShaderToken.OPERATOR;
973 return (
int)ShaderToken.OPERATOR;
976 return (
int)ShaderToken.PREPROCESSOR;
979 return (
int)ShaderToken.STRING_LITERAL;
984 return (
int)ShaderToken.FLOAT;
987 return (
int)ShaderToken.OPERATOR;
990 return (
int)ShaderToken.OPERATOR;
995 return (
int)ShaderToken.FLOAT;
998 return (
int)ShaderToken.FLOAT;
1001 return (
int)ShaderToken.NUMBER;
1004 return (
int)ShaderTokenProvider.GetTokenFromSemantics(yytext);
1007 return (
int)ShaderToken.OPERATOR;
1010 return (
int)ShaderToken.COMMENT_LINE;
1014 BEGIN(COMMENT);
return (
int)ShaderToken.COMMENT;
1017 return (
int)ShaderToken.OPERATOR;
1020 return (
int)ShaderToken.COMMENT;
1026 return (
int)ShaderToken.COMMENT;
1029 BEGIN(INITIAL);
return (
int)ShaderToken.COMMENT;
1034 #pragma warning restore 162
1041 Result Recurse2(Context ctx,
int next)
1048 SaveStateAndPos(ctx);
1050 if (state == eofNum)
return Result.accept;
1054 while ((next = NextState()) != currentStart)
1056 if (inAccept && next > maxAccept)
1057 SaveStateAndPos(ctx);
1059 if (state == eofNum)
return Result.accept;
1061 inAccept = (state <= maxAccept);
1063 if (inAccept)
return Result.accept;
else return Result.noMatch;
1066 void SaveStateAndPos(Context ctx)
1068 ctx.bPos = buffer.Pos;
1074 void RestoreStateAndPos(Context ctx)
1076 buffer.Pos = ctx.bPos;
1082 void RestorePos(Context ctx) { buffer.Pos = ctx.bPos; cNum = ctx.cNum; }
1087 internal void BEGIN(
int next)
1088 { CurrentSc = next; }
1091 internal void yy_clear_stack() { scStack.Clear(); }
1092 internal int yy_top_state() {
return scStack.Peek(); }
1094 internal void yy_push_state(
int state)
1096 scStack.Push(CurrentSc);
1100 internal void yy_pop_state()
1103 if (scStack.Count > 0) {
1104 int newSc = scStack.Pop();
1110 internal void ECHO() { Console.Out.Write(yytext); }
static TextBuff NewTextBuff(Stream strm)
TextBuff factory. Reads the file preamble and returns a TextBuff, LittleEndTextBuff or BigEndTextBuff...
This is the Buffer for Little-endian UTF16 files.
sealed override string GetString(int beg, int end)
Returns the string from the buffer between the given file positions. This needs to be done carefully...
void SetSource(string source, int offset)
int GetNext(ref int state, out int start, out int end)
override string GetString(int beg, int end)
override string GetString(int beg, int end)
Tokens
Summary Canonical example of MPLEX automaton
This is the Buffer for UTF8 files. It attempts to read the encoding preamble, which for this encoding...
This is the Buffer for Big-endian UTF16 files.
sealed override int Peek()