Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ShaderMPlexer.cs
Go to the documentation of this file.
1 //
2 // This CSharp output file generated by Managed Package LEX
3 // Version: 0.6.0 (1-August-2007)
4 // Machine: HOME-PC
5 // DateTime: 28/01/2011 17:34:25
6 // UserName: Home
7 // MPLEX input file <ShaderMPLexer.lex>
8 // MPLEX frame file <C:\Program Files (x86)\Microsoft Visual Studio 2008 SDK\VisualStudioIntegration\Tools\bin\mplex.frame>
9 //
10 // Option settings: unicode, verbose, noparser, minimize, classes, compressmap, compressnext
11 //
12 
13 #define BACKUP
14 #define LEFTANCHORS
15 #define STANDALONE
16 //
17 // mplex.frame
18 // Version 0.6.1 of 1 August 2007
19 // Left and Right Anchored state support.
20 // Start condition stack. Two generic params.
21 // Using fixed length context handling for right anchors
22 //
23 using System;
24 using System.IO;
25 using System.Collections.Generic;
26 #if !STANDALONE
27 using Babel.ParserGenerator;
28 #endif // STANDALONE
29 
30 
31 namespace NShader.Lexer
32 {
33  /// <summary>
34  /// Summary Canonical example of MPLEX automaton
35  /// </summary>
36 
37 #if STANDALONE
38  //
39  // These are the dummy declarations for stand-alone MPLEX applications
40  // normally these declarations would come from the parser.
41  // If you declare /noparser, or %option noparser then you get this.
42  //
43 
44  public enum Tokens
45  {
46  EOF = 0, maxParseToken = int.MaxValue
47  // must have at least these two, values are almost arbitrary
48  }
49 
50  public abstract class ScanBase
51  {
52  public abstract int yylex();
53  protected abstract int CurrentSc { get; set; }
54  //
55  // Override this virtual EolState property if the scanner state is more
56  // complicated then a simple copy of the current start state ordinal
57  //
58  public virtual int EolState { get { return CurrentSc; } set { CurrentSc = value; } }
59  }
60 
61  public interface IColorScan
62  {
63  void SetSource(string source, int offset);
64  int GetNext(ref int state, out int start, out int end);
65  }
66 
67 
68 
69 #endif // STANDALONE
70 
71  public abstract class ScanBuff
72  {
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);
79  }
80 
81  // If the compiler can't find ScanBase maybe you need
82  // to run mppg with /mplex, or run mplex with /noparser
83  public sealed class Scanner : ScanBase, IColorScan
84  {
85 
86  public ScanBuff buffer;
87  private IErrorHandler handler;
88  int scState;
89 
90  private static int GetMaxParseToken() {
91  System.Reflection.FieldInfo f = typeof(Tokens).GetField("maxParseToken");
92  return (f == null ? int.MaxValue : (int)f.GetValue(null));
93  }
94 
95  static int parserMax = GetMaxParseToken();
96 
97  protected override int CurrentSc
98  {
99  // The current start state is a property
100  // to try to avoid the user error of setting
101  // scState but forgetting to update the FSA
102  // start state "currentStart"
103  //
104  get { return scState; }
105  set { scState = value; currentStart = startState[value]; }
106  }
107 
108  enum Result {accept, noMatch, contextFound};
109 
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;
116 
117 /*
118 // ShaderMPLexer.lex.
119 // Lexical description for MPLex. This file is inspired from http://svn.assembla.com/svn/ppjlab/trunk/scanner.lex
120 // ---------------------------------------------------------------------
121 //
122 // Copyright (c) 2009 Alexandre Mutel and Microsoft Corporation.
123 // All rights reserved.
124 //
125 // This code module is part of NShader, a plugin for visual studio
126 // to provide syntax highlighting for shader languages (hlsl, glsl, cg)
127 //
128 // ------------------------------------------------------------------
129 //
130 // This code is licensed under the Microsoft Public License.
131 // See the file License.txt for the license details.
132 // More info on: http://nshader.codeplex.com
133 //
134 // ------------------------------------------------------------------
135 */
136 /**********************************************************************************/
137 /********************************User Defined Code*********************************/
138 /**********************************************************************************/
139 public IShaderTokenProvider ShaderTokenProvider = null; // Token provider
140 /**********************************************************************************/
141 /**********Start Condition Declarations and Lexical Category Definitions***********/
142 /**********************************************************************************/
143 /**********************************************************************************/
144 /**********************************************************************************/
145 /********************************The Rules Section*********************************/
146 /**********************************************************************************/
147 /**********************************************************************************/
148  int state;
149  int currentStart = initial;
150  int chr; // last character read
151  int cNum; // ordinal number of chr
152  int lNum = 0; // current line number
153  int lineStartNum; // cNum at start of line
154 
155  //
156  // The following instance variables are useful, among other
157  // things, for constructing the yylloc location objects.
158  //
159  int tokPos; // buffer position at start of token
160  int tokNum; // ordinal number of first character
161  int tokLen; // number of character in token
162  int tokCol; // zero-based column number at start of token
163  int tokLin; // line number at start of token
164  int tokEPos; // buffer position at end of token
165  int tokECol; // column number at end of token
166  int tokELin; // line number at end of token
167  string tokTxt; // lazily constructed text of token
168 #if STACK
169  private Stack<int> scStack = new Stack<int>();
170 #endif // STACK
171 
172 #region ScannerTables
173  struct Table {
174  public int min; public int rng; public int dflt;
175  public sbyte[] nxt;
176  public Table(int m, int x, int d, sbyte[] n) {
177  min = m; rng = x; dflt = d; nxt = n;
178  }
179  };
180 
181  static int[] startState = {61, 56, 0};
182 
183  static int[] anchorState = {62, 56, 0};
184 
185 #region CharacterMap
186  //
187  // There are 35 equivalence classes
188  // There are 2 character sequence regions
189  // There are 1 tables, 126 entries
190  // There are 1 runs, 0 singletons
191  //
192  static sbyte[] map0 = new sbyte[126] {
193 /* \0 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 0, 5, 5, 5, 2, 2,
194 /* \020 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
195 /* \040 */ 5, 26, 17, 19, 2, 21, 22, 2, 31, 32, 3, 12, 30, 13, 14, 1,
196 /* 0 */ 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 /* P */ 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 /* p */ 6, 6, 6, 6, 6, 6, 6, 6, 9, 6, 6, 28, 23, 29 };
201 
202  sbyte Map(int chr)
203  { // '\0' <= chr <= '\uFFFF'
204  if (chr < 126) return map0[chr - 0];
205  else return (sbyte)2;
206  }
207 #endregion
208 
209  static Table[] NxS = new Table[75];
210 
211  static Scanner() {
212  NxS[0] = // Shortest string ""
213  new Table(0, 0, 0, null);
214  NxS[1] = // Shortest string "^\t"
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});
217  NxS[2] = // Shortest string "/"
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});
220  NxS[3] = // Shortest string "\0"
221  new Table(0, 0, -1, null);
222  NxS[4] = // Shortest string "*"
223  new Table(20, 1, -1, new sbyte[] {49});
224  NxS[5] = // Shortest string ":"
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});
227  NxS[6] = // Shortest string "G"
228  new Table(6, 11, -1, new sbyte[] {6, 6, 6, 6, 6, 6,
229  -1, -1, -1, 6, 6});
230  NxS[7] = // Shortest string "1"
231  new Table(7, 8, -1, new sbyte[] {7, 7, -1, -1, 68, -1,
232  -1, 43});
233  NxS[8] = // Shortest string "0"
234  new Table(7, 8, -1, new sbyte[] {7, 7, 67, -1, 68, -1,
235  -1, 43});
236  NxS[9] = // Shortest string "+"
237  new Table(20, 1, -1, new sbyte[] {42});
238  NxS[10] = // Shortest string "-"
239  new Table(20, 1, -1, new sbyte[] {41});
240  NxS[11] = // Shortest string "."
241  new Table(7, 2, -1, new sbyte[] {38, 38});
242  NxS[12] = // Shortest string """
243  new Table(17, 2, 63, new sbyte[] {37, 64});
244  NxS[13] = // Shortest string "^#"
245  new Table(6, 11, -1, new sbyte[] {36, -1, -1, 36, 36, 36,
246  -1, -1, -1, 36, 36});
247  NxS[14] = // Shortest string "="
248  new Table(20, 1, -1, new sbyte[] {35});
249  NxS[15] = // Shortest string "%"
250  new Table(20, 1, -1, new sbyte[] {34});
251  NxS[16] = // Shortest string "&"
252  new Table(22, 1, -1, new sbyte[] {33});
253  NxS[17] = // Shortest string "|"
254  new Table(23, 1, -1, new sbyte[] {32});
255  NxS[18] = // Shortest string "<"
256  new Table(20, 1, -1, new sbyte[] {31});
257  NxS[19] = // Shortest string ">"
258  new Table(20, 1, -1, new sbyte[] {30});
259  NxS[20] = // Shortest string "!"
260  new Table(20, 1, -1, new sbyte[] {29});
261  NxS[21] = // Shortest string ";"
262  new Table(0, 0, -1, null);
263  NxS[22] = // Shortest string "{"
264  new Table(0, 0, -1, null);
265  NxS[23] = // Shortest string "}"
266  new Table(0, 0, -1, null);
267  NxS[24] = // Shortest string ","
268  new Table(0, 0, -1, null);
269  NxS[25] = // Shortest string "("
270  new Table(0, 0, -1, null);
271  NxS[26] = // Shortest string ")"
272  new Table(0, 0, -1, null);
273  NxS[27] = // Shortest string "["
274  new Table(0, 0, -1, null);
275  NxS[28] = // Shortest string "]"
276  new Table(0, 0, -1, null);
277  NxS[29] = // Shortest string "!="
278  new Table(0, 0, -1, null);
279  NxS[30] = // Shortest string ">="
280  new Table(0, 0, -1, null);
281  NxS[31] = // Shortest string "<="
282  new Table(0, 0, -1, null);
283  NxS[32] = // Shortest string "||"
284  new Table(0, 0, -1, null);
285  NxS[33] = // Shortest string "&&"
286  new Table(0, 0, -1, null);
287  NxS[34] = // Shortest string "%="
288  new Table(0, 0, -1, null);
289  NxS[35] = // Shortest string "=="
290  new Table(0, 0, -1, null);
291  NxS[36] = // Shortest string "^#G"
292  new Table(6, 11, -1, new sbyte[] {36, -1, -1, 36, 36, 36,
293  -1, -1, -1, 36, 36});
294  NxS[37] = // Shortest string """"
295  new Table(0, 0, -1, null);
296  NxS[38] = // Shortest string ".1"
297  new Table(7, 10, -1, new sbyte[] {38, 38, -1, -1, 65, -1,
298  -1, -1, 39, 39});
299  NxS[39] = // Shortest string ".1H"
300  new Table(0, 0, -1, null);
301  NxS[40] = // Shortest string ".1E1"
302  new Table(7, 10, -1, new sbyte[] {40, 40, -1, -1, -1, -1,
303  -1, -1, 39, 39});
304  NxS[41] = // Shortest string "-="
305  new Table(0, 0, -1, null);
306  NxS[42] = // Shortest string "+="
307  new Table(0, 0, -1, null);
308  NxS[43] = // Shortest string "0."
309  new Table(7, 10, -1, new sbyte[] {38, 38, -1, -1, 69, -1,
310  -1, -1, 44, 44});
311  NxS[44] = // Shortest string "0.H"
312  new Table(0, 0, -1, null);
313  NxS[45] = // Shortest string "0.E1"
314  new Table(7, 10, -1, new sbyte[] {45, 45, -1, -1, -1, -1,
315  -1, -1, 44, 44});
316  NxS[46] = // Shortest string "0E1"
317  new Table(7, 2, -1, new sbyte[] {46, 46});
318  NxS[47] = // Shortest string "0x1"
319  new Table(7, 10, -1, new sbyte[] {47, 47, -1, 47, 47, -1,
320  -1, -1, -1, 47});
321  NxS[48] = // Shortest string ":G"
322  new Table(6, 11, -1, new sbyte[] {48, 48, 48, 48, 48, 48,
323  -1, -1, -1, 48, 48});
324  NxS[49] = // Shortest string "*="
325  new Table(0, 0, -1, null);
326  NxS[50] = // Shortest string "//"
327  new Table(0, 1, 50, new sbyte[] {-1});
328  NxS[51] = // Shortest string "/*"
329  new Table(0, 4, 51, new sbyte[] {-1, 51, 51, 53});
330  NxS[52] = // Shortest string "/="
331  new Table(0, 0, -1, null);
332  NxS[53] = // Shortest string "/**"
333  new Table(1, 3, -1, new sbyte[] {54, -1, 53});
334  NxS[54] = // Shortest string "/**/"
335  new Table(0, 0, -1, null);
336  NxS[55] = // Shortest string "\t"
337  new Table(0, 0, -1, null);
338  NxS[56] = // Shortest string ""
339  new Table(0, 4, 58, new sbyte[] {57, 58, 58, 59});
340  NxS[57] = // Shortest string ""
341  new Table(0, 0, -1, null);
342  NxS[58] = // Shortest string "/"
343  new Table(0, 4, 58, new sbyte[] {-1, 58, 58, 59});
344  NxS[59] = // Shortest string "*"
345  new Table(1, 3, -1, new sbyte[] {60, -1, 59});
346  NxS[60] = // Shortest string "*/"
347  new Table(0, 0, -1, null);
348  NxS[61] = // Shortest string ""
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});
352  NxS[62] = // Shortest string "^"
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});
356  NxS[63] = // Shortest string ""/"
357  new Table(17, 2, 63, new sbyte[] {37, 64});
358  NxS[64] = // Shortest string ""\"
359  new Table(0, 1, 63, new sbyte[] {-1});
360  NxS[65] = // Shortest string ".1E"
361  new Table(7, 7, -1, new sbyte[] {40, 40, -1, -1, -1, 66,
362  66});
363  NxS[66] = // Shortest string ".1E+"
364  new Table(7, 2, -1, new sbyte[] {40, 40});
365  NxS[67] = // Shortest string "0x"
366  new Table(7, 10, -1, new sbyte[] {47, 47, -1, 47, 47, -1,
367  -1, -1, -1, 47});
368  NxS[68] = // Shortest string "0E"
369  new Table(7, 7, -1, new sbyte[] {46, 46, -1, -1, -1, 71,
370  71});
371  NxS[69] = // Shortest string "0.E"
372  new Table(7, 7, -1, new sbyte[] {45, 45, -1, -1, -1, 70,
373  70});
374  NxS[70] = // Shortest string "0.E+"
375  new Table(7, 2, -1, new sbyte[] {45, 45});
376  NxS[71] = // Shortest string "0E+"
377  new Table(7, 2, -1, new sbyte[] {46, 46});
378  NxS[72] = // Shortest string "^\t\t"
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});
381  NxS[73] = // Shortest string "^\t#"
382  new Table(6, 11, -1, new sbyte[] {36, -1, -1, 36, 36, 36,
383  -1, -1, -1, 36, 36});
384  NxS[74] = // Shortest string ":\t"
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});
387  }
388 
389 int NextState(int qStat) {
390  if (chr == ScanBuff.EOF)
391  return (qStat <= maxAccept && qStat != currentStart ? currentStart : eofNum);
392  else {
393  int rslt;
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);
399  }
400 }
401 
402 int NextState() {
403  if (chr == ScanBuff.EOF)
404  return (state <= maxAccept && state != currentStart ? currentStart : eofNum);
405  else {
406  int rslt;
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);
412  }
413 }
414 #endregion
415 
416 
417 #if BACKUP
418  // ====================== Nested class ==========================
419 
420  internal class Context // class used for automaton backup.
421  {
422  public int bPos;
423  public int cNum;
424  public int state;
425  public int cChr;
426  }
427 #endif // BACKUP
428 
429 
430  // ====================== Nested class ==========================
431 
432  public sealed class StringBuff : ScanBuff
433  {
434  string str; // input buffer
435  int bPos; // current position in buffer
436  int sLen;
437 
438  public StringBuff(string str)
439  {
440  this.str = str;
441  this.sLen = str.Length;
442  }
443 
444  public override int Read()
445  {
446  if (bPos < sLen) return str[bPos++];
447  else if (bPos == sLen) { bPos++; return '\n'; } // one strike, see newline
448  else return EOF; // two strikes and you're out!
449  }
450 
451  public override int ReadPos { get { return bPos - 1; } }
452 
453  public override int Peek()
454  {
455  if (bPos < sLen) return str[bPos];
456  else return '\n';
457  }
458 
459  public override string GetString(int beg, int end)
460  {
461  // "end" can be greater than sLen with the BABEL
462  // option set. Read returns a "virtual" EOL if
463  // an attempt is made to read past the end of the
464  // string buffer. Without the guard any attempt
465  // to fetch yytext for a token that includes the
466  // EOL will throw an index exception.
467  if (end > sLen) end = sLen;
468  if (end <= beg) return "";
469  else return str.Substring(beg, end - beg);
470  }
471 
472  public override int Pos
473  {
474  get { return bPos; }
475  set { bPos = value; }
476  }
477  }
478 
479  // ====================== Nested class ==========================
480 
481  public sealed class StreamBuff : ScanBuff
482  {
483  BufferedStream bStrm; // input buffer
484  int delta = 1;
485 
486  public StreamBuff(Stream str) { this.bStrm = new BufferedStream(str); }
487 
488  public override int Read() {
489  return bStrm.ReadByte();
490  }
491 
492  public override int ReadPos {
493  get { return (int)bStrm.Position - delta; }
494  }
495 
496  public override int Peek()
497  {
498  int rslt = bStrm.ReadByte();
499  bStrm.Seek(-delta, SeekOrigin.Current);
500  return rslt;
501  }
502 
503  public override string GetString(int beg, int end)
504  {
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);
513  }
514 
515  // Pos is the position *after* reading chr!
516  public override int Pos
517  {
518  get { return (int)bStrm.Position; }
519  set { bStrm.Position = value; }
520  }
521  }
522 
523  // ====================== Nested class ==========================
524 
525  /// <summary>
526  /// This is the Buffer for UTF8 files.
527  /// It attempts to read the encoding preamble, which for
528  /// this encoding should be unicode point \uFEFF which is
529  /// encoded as EF BB BF
530  /// </summary>
531  public class TextBuff : ScanBuff
532  {
533  protected BufferedStream bStrm; // input buffer
534  protected int delta = 1;
535 
536  private Exception BadUTF8()
537  { return new Exception(String.Format("BadUTF8 Character")); }
538 
539  /// <summary>
540  /// TextBuff factory. Reads the file preamble
541  /// and returns a TextBuff, LittleEndTextBuff or
542  /// BigEndTextBuff according to the result.
543  /// </summary>
544  /// <param name="strm">The underlying stream</param>
545  /// <returns></returns>
546  public static TextBuff NewTextBuff(Stream strm)
547  {
548  // First check if this is a UTF16 file
549  //
550  int b0 = strm.ReadByte();
551  int b1 = strm.ReadByte();
552 
553  if (b0 == 0xfe && b1 == 0xff)
554  return new BigEndTextBuff(strm);
555  if (b0 == 0xff && b1 == 0xfe)
556  return new LittleEndTextBuff(strm);
557 
558  int b2 = strm.ReadByte();
559  if (b0 == 0xef && b1 == 0xbb && b2 == 0xbf)
560  return new TextBuff(strm);
561  //
562  // There is no unicode preamble, so we
563  // must go back to the UTF8 default.
564  //
565  strm.Seek(0, SeekOrigin.Begin);
566  return new TextBuff(strm);
567  }
568 
569  protected TextBuff(Stream str) {
570  this.bStrm = new BufferedStream(str);
571  }
572 
573  public override int Read()
574  {
575  int ch0 = bStrm.ReadByte();
576  int ch1;
577  int ch2;
578  if (ch0 < 0x7f)
579  {
580  delta = (ch0 == EOF ? 0 : 1);
581  return ch0;
582  }
583  else if ((ch0 & 0xe0) == 0xc0)
584  {
585  delta = 2;
586  ch1 = bStrm.ReadByte();
587  if ((ch1 & 0xc0) == 0x80)
588  return ((ch0 & 0x1f) << 6) + (ch1 & 0x3f);
589  else
590  throw BadUTF8();
591  }
592  else if ((ch0 & 0xf0) == 0xe0)
593  {
594  delta = 3;
595  ch1 = bStrm.ReadByte();
596  ch2 = bStrm.ReadByte();
597  if ((ch1 & ch2 & 0xc0) == 0x80)
598  return ((ch0 & 0xf) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);
599  else
600  throw BadUTF8();
601  }
602  else
603  throw BadUTF8();
604  }
605 
606  public sealed override int ReadPos
607  {
608  get { return (int)bStrm.Position - delta; }
609  }
610 
611  public sealed override int Peek()
612  {
613  int rslt = Read();
614  bStrm.Seek(-delta, SeekOrigin.Current);
615  return rslt;
616  }
617 
618  /// <summary>
619  /// Returns the string from the buffer between
620  /// the given file positions. This needs to be
621  /// done carefully, as the number of characters
622  /// is, in general, not equal to (end - beg).
623  /// </summary>
624  /// <param name="beg">Begin filepos</param>
625  /// <param name="end">End filepos</param>
626  /// <returns></returns>
627  public sealed override string GetString(int beg, int end)
628  {
629  int i;
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);
638  }
639 
640  // Pos is the position *after* reading chr!
641  public sealed override int Pos
642  {
643  get { return (int)bStrm.Position; }
644  set { bStrm.Position = value; }
645  }
646  }
647 
648  // ====================== Nested class ==========================
649  /// <summary>
650  /// This is the Buffer for Big-endian UTF16 files.
651  /// </summary>
652  public sealed class BigEndTextBuff : TextBuff
653  {
654  internal BigEndTextBuff(Stream str) : base(str) { } //
655 
656  public override int Read()
657  {
658  int ch0 = bStrm.ReadByte();
659  int ch1 = bStrm.ReadByte();
660  return (ch0 << 8) + ch1;
661  }
662  }
663 
664  // ====================== Nested class ==========================
665  /// <summary>
666  /// This is the Buffer for Little-endian UTF16 files.
667  /// </summary>
668  public sealed class LittleEndTextBuff : TextBuff
669  {
670  internal LittleEndTextBuff(Stream str) : base(str) { } // { this.bStrm = new BufferedStream(str); }
671 
672  public override int Read()
673  {
674  int ch0 = bStrm.ReadByte();
675  int ch1 = bStrm.ReadByte();
676  return (ch1 << 8) + ch0;
677  }
678  }
679 
680  // =================== End Nested classes =======================
681 
682  public Scanner(Stream file) {
683  buffer = TextBuff.NewTextBuff(file); // selected by /unicode option
684  this.cNum = -1;
685  this.chr = '\n'; // to initialize yyline, yycol and lineStart
686  GetChr();
687  }
688 
689  public Scanner() { }
690 
691  void GetChr()
692  {
693  if (chr == '\n')
694  {
695  lineStartNum = cNum + 1;
696  lNum++;
697  }
698  chr = buffer.Read();
699  cNum++;
700  }
701 
702  void MarkToken()
703  {
704  tokPos = buffer.ReadPos;
705  tokNum = cNum;
706  tokLin = lNum;
707  tokCol = cNum - lineStartNum;
708  }
709 
710  void MarkEnd()
711  {
712  tokTxt = null;
713  tokLen = cNum - tokNum;
714  tokEPos = buffer.ReadPos;
715  tokELin = lNum;
716  tokECol = cNum - lineStartNum;
717  }
718 
719  // ================ StringBuffer Initialization ===================
720 
721  public void SetSource(string source, int offset)
722  {
723  this.buffer = new StringBuff(source);
724  this.buffer.Pos = offset;
725  this.cNum = offset - 1;
726  this.chr = '\n'; // to initialize yyline, yycol and lineStart
727  GetChr();
728  }
729 
730  public int GetNext(ref int state, out int start, out int end)
731  {
732  Tokens next;
733  EolState = state;
734  next = (Tokens)Scan();
735  state = EolState;
736  start = tokPos;
737  end = tokEPos - 1; // end is the index of last char.
738  return (int)next;
739  }
740 
741  // ======== IScanner<> Implementation =========
742 
743  public override int yylex()
744  {
745  // parserMax is set by reflecting on the Tokens
746  // enumeration. If maxParseTokeen is defined
747  // that is used, otherwise int.MaxValue is used.
748  //
749  int next;
750  do { next = Scan(); } while (next >= parserMax);
751  return next;
752  }
753 
754  int yyleng { get { return tokLen; } }
755  int yypos { get { return tokPos; } }
756  int yyline { get { return tokLin; } }
757  int yycol { get { return tokCol; } }
758 
759  public string yytext
760  {
761  get
762  {
763  if (tokTxt == null)
764  tokTxt = buffer.GetString(tokPos, tokEPos);
765  return tokTxt;
766  }
767  }
768 
769  void yyless(int n) {
770  buffer.Pos = tokPos;
771  cNum = tokNum;
772  for (int i = 0; i <= n; i++) GetChr();
773  MarkEnd();
774  }
775 
776  public IErrorHandler Handler { get { return this.handler; }
777  set { this.handler = value; }}
778 
779  // ============ methods available in actions ==============
780 
781  internal int YY_START {
782  get { return CurrentSc; }
783  set { CurrentSc = value; }
784  }
785 
786  // ============== The main tokenizer code =================
787 
788  int Scan()
789  {
790  for (; ; )
791  {
792  int next; // next state to enter
793 #if BACKUP
794  bool inAccept = false; // inAccept ==> current state is an accept state
795  Result rslt = Result.noMatch;
796  // skip "idle" transitions
797 #if LEFTANCHORS
798  if (lineStartNum == cNum && NextState(anchorState[CurrentSc]) != currentStart)
799  state = anchorState[CurrentSc];
800  else {
801  state = currentStart;
802  while (NextState() == state) {
803  GetChr();
804  if (lineStartNum == cNum) {
805  int anchor = anchorState[CurrentSc];
806  if (NextState(anchor) != state) {
807  state = anchor;
808  break;
809  }
810  }
811  }
812  }
813 #else // !LEFTANCHORS
814  state = currentStart;
815  while (NextState() == state)
816  GetChr(); // skip "idle" transitions
817 #endif // LEFTANCHORS
818  MarkToken();
819 
820  while ((next = NextState()) != currentStart)
821  if (inAccept && next > maxAccept) // need to prepare backup data
822  {
823  Context ctx = new Context();
824  rslt = Recurse2(ctx, next);
825  if (rslt == Result.noMatch) RestoreStateAndPos(ctx);
826  // else if (rslt == Result.contextFound) RestorePos(ctx);
827  break;
828  }
829  else
830  {
831  state = next;
832  GetChr();
833  if (state <= maxAccept) inAccept = true;
834  }
835 #else // !BACKUP
836 #if LEFTANCHORS
837  if (lineStartNum == cNum) {
838  int anchor = anchorState[CurrentSc];
839  if (NextState(anchor) != currentStart)
840  state = anchor;
841  }
842  else {
843  state = currentStart;
844  while (NextState() == state) {
845  GetChr();
846  if (lineStartNum == cNum) {
847  anchor = anchorState[CurrentSc];
848  if (NextState(anchor) != state) {
849  state = anchor;
850  break;
851  }
852  }
853  }
854  }
855 #else // !LEFTANCHORS
856  state = currentStart;
857  while (NextState() == state)
858  GetChr(); // skip "idle" transitions
859 #endif // LEFTANCHORS
860  MarkToken();
861  // common code
862  while ((next = NextState()) != currentStart)
863  {
864  state = next;
865  GetChr();
866  }
867 #endif // BACKUP
868  if (state > maxAccept)
869  state = currentStart;
870  else
871  {
872  MarkEnd();
873 #region ActionSwitch
874 #pragma warning disable 162
875  switch (state)
876  {
877  case eofNum:
878  return (int)Tokens.EOF;
879  case 1:
880  case 55:
881 /* Ignore */
882  break;
883  case 2:
884 return (int)ShaderToken.OPERATOR;
885  break;
886  case 3:
887  case 5:
888  case 12:
889  case 13:
890  case 17:
891 return (int)ShaderToken.UNDEFINED;
892  break;
893  case 4:
894 return (int)ShaderToken.OPERATOR;
895  break;
896  case 6:
897 return (int)ShaderTokenProvider.GetTokenFromIdentifier(yytext);
898  break;
899  case 7:
900  case 8:
901 return (int)ShaderToken.NUMBER;
902  break;
903  case 9:
904 return (int)ShaderToken.OPERATOR;
905  break;
906  case 10:
907 return (int)ShaderToken.OPERATOR;
908  break;
909  case 11:
910 return (int)ShaderToken.OPERATOR;
911  break;
912  case 14:
913 return (int)ShaderToken.OPERATOR;
914  break;
915  case 15:
916 return (int)ShaderToken.OPERATOR;
917  break;
918  case 16:
919 return (int)ShaderToken.OPERATOR;
920  break;
921  case 18:
922 return (int)ShaderToken.OPERATOR;
923  break;
924  case 19:
925 return (int)ShaderToken.OPERATOR;
926  break;
927  case 20:
928 return (int)ShaderToken.OPERATOR;
929  break;
930  case 21:
931 return (int)ShaderToken.DELIMITER;
932  break;
933  case 22:
934 return (int)ShaderToken.LEFT_BRACKET;
935  break;
936  case 23:
937 return (int)ShaderToken.RIGHT_BRACKET;
938  break;
939  case 24:
940 return (int)ShaderToken.DELIMITER;
941  break;
942  case 25:
943 return (int)ShaderToken.LEFT_PARENTHESIS;
944  break;
945  case 26:
946 return (int)ShaderToken.RIGHT_PARENTHESIS;
947  break;
948  case 27:
949 return (int)ShaderToken.LEFT_SQUARE_BRACKET;
950  break;
951  case 28:
952 return (int)ShaderToken.RIGHT_SQUARE_BRACKET;
953  break;
954  case 29:
955 return (int)ShaderToken.OPERATOR;
956  break;
957  case 30:
958 return (int)ShaderToken.OPERATOR;
959  break;
960  case 31:
961 return (int)ShaderToken.OPERATOR;
962  break;
963  case 32:
964 return (int)ShaderToken.OPERATOR;
965  break;
966  case 33:
967 return (int)ShaderToken.OPERATOR;
968  break;
969  case 34:
970 return (int)ShaderToken.OPERATOR;
971  break;
972  case 35:
973 return (int)ShaderToken.OPERATOR;
974  break;
975  case 36:
976 return (int)ShaderToken.PREPROCESSOR;
977  break;
978  case 37:
979 return (int)ShaderToken.STRING_LITERAL;
980  break;
981  case 38:
982  case 39:
983  case 40:
984 return (int)ShaderToken.FLOAT;
985  break;
986  case 41:
987 return (int)ShaderToken.OPERATOR;
988  break;
989  case 42:
990 return (int)ShaderToken.OPERATOR;
991  break;
992  case 43:
993  case 44:
994  case 45:
995 return (int)ShaderToken.FLOAT;
996  break;
997  case 46:
998 return (int)ShaderToken.FLOAT;
999  break;
1000  case 47:
1001 return (int)ShaderToken.NUMBER;
1002  break;
1003  case 48:
1004 return (int)ShaderTokenProvider.GetTokenFromSemantics(yytext);
1005  break;
1006  case 49:
1007 return (int)ShaderToken.OPERATOR;
1008  break;
1009  case 50:
1010 return (int)ShaderToken.COMMENT_LINE;
1011  break;
1012  case 51:
1013  case 53:
1014 BEGIN(COMMENT); return (int)ShaderToken.COMMENT;
1015  break;
1016  case 52:
1017 return (int)ShaderToken.OPERATOR;
1018  break;
1019  case 54:
1020 return (int)ShaderToken.COMMENT;
1021  break;
1022  case 56:
1023  case 57:
1024  case 58:
1025  case 59:
1026 return (int)ShaderToken.COMMENT;
1027  break;
1028  case 60:
1029 BEGIN(INITIAL); return (int)ShaderToken.COMMENT;
1030  break;
1031  default:
1032  break;
1033  }
1034 #pragma warning restore 162
1035 #endregion
1036  }
1037  }
1038  }
1039 
1040 #if BACKUP
1041  Result Recurse2(Context ctx, int next)
1042  {
1043  // Assert: at entry "state" is an accept state AND
1044  // NextState(state, chr) != currentStart AND
1045  // NextState(state, chr) is not an accept state.
1046  //
1047  bool inAccept;
1048  SaveStateAndPos(ctx);
1049  state = next;
1050  if (state == eofNum) return Result.accept;
1051  GetChr();
1052  inAccept = false;
1053 
1054  while ((next = NextState()) != currentStart)
1055  {
1056  if (inAccept && next > maxAccept) // need to prepare backup data
1057  SaveStateAndPos(ctx);
1058  state = next;
1059  if (state == eofNum) return Result.accept;
1060  GetChr();
1061  inAccept = (state <= maxAccept);
1062  }
1063  if (inAccept) return Result.accept; else return Result.noMatch;
1064  }
1065 
1066  void SaveStateAndPos(Context ctx)
1067  {
1068  ctx.bPos = buffer.Pos;
1069  ctx.cNum = cNum;
1070  ctx.state = state;
1071  ctx.cChr = chr;
1072  }
1073 
1074  void RestoreStateAndPos(Context ctx)
1075  {
1076  buffer.Pos = ctx.bPos;
1077  cNum = ctx.cNum;
1078  state = ctx.state;
1079  chr = ctx.cChr;
1080  }
1081 
1082  void RestorePos(Context ctx) { buffer.Pos = ctx.bPos; cNum = ctx.cNum; }
1083 #endif // BACKUP
1084 
1085  // ============= End of the tokenizer code ================
1086 
1087  internal void BEGIN(int next)
1088  { CurrentSc = next; }
1089 
1090 #if STACK
1091  internal void yy_clear_stack() { scStack.Clear(); }
1092  internal int yy_top_state() { return scStack.Peek(); }
1093 
1094  internal void yy_push_state(int state)
1095  {
1096  scStack.Push(CurrentSc);
1097  CurrentSc = state;
1098  }
1099 
1100  internal void yy_pop_state()
1101  {
1102  // Protect against input errors that pop too far ...
1103  if (scStack.Count > 0) {
1104  int newSc = scStack.Pop();
1105  CurrentSc = newSc;
1106  } // Otherwise leave stack unchanged.
1107  }
1108  #endif // STACK
1109 
1110  internal void ECHO() { Console.Out.Write(yytext); }
1111 
1112  } // end class Scanner
1113 } // end namespace
static TextBuff NewTextBuff(Stream strm)
TextBuff factory. Reads the file preamble and returns a TextBuff, LittleEndTextBuff or BigEndTextBuff...
function b
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)
override int yylex()
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()