Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL/3.4/GPDefLex.cs @ 9430

Last change on this file since 9430 was 9430, checked in by gkronber, 11 years ago

initial import of GPDL parser plugin

File size: 13.4 KB
Line 
1// GPDefLex.cs                                              HDO, 2006-08-28
2// -------------
3// Lexical analyzer (finite-state machine interpreter).
4// Generated by Coco-2 (SG).
5//=====================================|========================================
6
7#undef TEST_Lex
8
9using System;
10using System.Collections;
11using System.Collections.Specialized;
12using System.IO;
13using System.Text;
14
15public class GPDefLex {
16
17  public const String MODULENAME = "GPDefLex";
18  public const int    EOFSPIX    = 1;
19
20  public static TextReader src;
21
22  // --- token information ---
23  public static int    token;               // current token
24  public static int    tokenLine, tokenCol; // position of current token
25  public static String tokenStr;            // token string recognized
26
27  // --- current char. info, for "power users" only ---
28  public static char   curCh;               // current input character
29  public static int    curLine, curCol;     // position of curCh
30
31
32  public static void GPDefLexMethod(Utils.ModuleAction action, out String moduleName) {
33  //-----------------------------------|----------------------------------------
34    moduleName = MODULENAME;
35    switch (action) {
36      case Utils.ModuleAction.getModuleName:
37        return;
38      case Utils.ModuleAction.initModule:
39        caseSensitive = true;
40        lt            = new LexicalTable();
41        tokenStrArr   = new char[256];
42        kwHt          = CreateHashtable();
43        nHt           = CreateHashtable();
44        nl            = new ArrayList();
45        break;
46      case Utils.ModuleAction.resetModule:
47        kwHt.Clear();
48        nHt.Clear();
49        nl.Clear();
50        break;
51      case Utils.ModuleAction.cleanupModule:
52        lt            = null;
53        tokenStrArr   = null;
54        kwHt          = null;
55        nHt           = null;
56        nl            = null;
57        break;
58    } // switch
59  } // GPDefLexMethod
60
61  private static  Hashtable CreateHashtable() {
62    if (caseSensitive)
63      return new Hashtable();
64    else
65      return CollectionsUtil.CreateCaseInsensitiveHashtable();
66  } // CreateHashtable
67
68
69  public static void InitLex() {
70  //-----------------------------------|----------------------------------------
71    // --- initialize keyword hash table  ---
72    kwHt.Clear();
73    EnterKeyword(  1, "PROBLEM");
74    EnterKeyword(  2, "END");
75    EnterKeyword(  3, "EPS");
76    EnterKeyword(  4, "LOCAL");
77    EnterKeyword(  5, "NONTERMINALS");
78    EnterKeyword(  6, "RULES");
79    EnterKeyword(  7, "SEM");
80    EnterKeyword(  8, "MAXIMIZE");
81    EnterKeyword(  9, "MINIMIZE");
82    EnterKeyword( 10, "TERMINALS");
83    EnterKeyword( 11, "CONSTRAINTS");
84    EnterKeyword( 12, "INIT");
85    EnterKeyword( 13, "MUTATE");
86    EnterKeyword( 14, "CODE");
87    EnterKeyword( 15, "IN");
88    EnterKeyword( 16, "SET");
89    EnterKeyword( 17, "RANGE");
90    // --- initialize name data structures  ---
91    nHt.Clear();
92    nl.Clear();
93    nl.Add("");      // so spix = 0 is the empty string
94    nl.Add("!EOF!"); // so EOFSPIX = 1
95    // --- (re)set global data ---
96    curLineStr  = "";
97    curCh       = ' ';
98    curLine     = 0;
99    curCol      = 2;
100    tokenStr    = null;
101    token       = 0;
102    tokenCol    = 0;
103    tokenLine   = 0;
104    tokenStrLen = 0;
105    pendingEOLs = 0;
106    apxLen      = pendingEOLs; // to prevent warning
107  } // InitLex
108
109  private static void EnterKeyword(int token, String keyword) {
110    kwHt.Add(keyword, token);
111  } // EnterKeyword
112
113
114  // *** start of global LEX declarations from ATG ***
115 
116  // *** end of global LEX declarations from ATG ***
117
118
119  public static bool caseSensitive;
120
121  private class LexicalTable {
122    public int   header = 4;
123    public int[] startTab = {
124          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
125          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
126          0,  0,  0,  0,  0,  0,  0,  0,  4,  5,  0,  0,  0,  0, 15,  0,
127          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 12,  2,  0,  0,
128          0, 14, 14, 14, 14, 14, 11, 14, 11, 14, 11, 11, 14, 14, 14, 14,
129         14, 11, 14, 14, 14, 14, 11, 11, 14, 11, 14,  6,  0,  7,  0,  0,
130          0, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
131         11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,  8,  3,  9,  0,  0,
132          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
133          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
134          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
135          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
136          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
137          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
138          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
139          0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
140      };
141    public Sets.Set256 ignoredChars = new Sets.Set256(
142         0x0600, 0x0000, 0x0001
143      );
144    public Sets.Set256 commentStart = new Sets.Set256(
145         0x0000, 0x0000, 0x8000
146      );
147    public Sets.Set256[] cls = {
148        new Sets.Set256( 0x0000, 0x0000, 0x0000, 0x0000, 0xfffe, 0x07ff, 0xfffe, 0x07ff),
149        new Sets.Set256( 0x0000, 0x0000, 0x0000, 0x03ff),
150        new Sets.Set256( 0x0000, 0x0000, 0x0000, 0x0000, 0xf2be, 0x053d),
151        new Sets.Set256( 0x0000, 0x0000, 0x0000, 0x0000, 0x0d40, 0x02c2, 0xfffe, 0x07ff)
152      };
153  } // LexicalTable
154
155  private static LexicalTable lt;
156  private static Hashtable    kwHt;  // hash table for keywords: string -> token
157  private static Hashtable    nHt;   // hash table for names: string -> null
158  private static ArrayList    nl;    // name list  for names, index is spix
159
160  private static String curLineStr;  // current source line
161  private static char[] tokenStrArr; // token string in an array
162  private static int    tokenStrLen; // length of token string in tokenStrArr
163  private static int    state;       // current automaton state
164  private static int    apxLen;      // length of appendix in token string
165  private static int    pendingEOLs; // nr of EOLs found in comment
166
167  // --- to save and restore scanner state ---
168  private static char   savedCh;
169  private static int    savedCol, savedLine;
170
171  private static void SaveScannerState() {
172    savedCh      = curCh;
173    savedCol     = curCol;
174    savedLine    = curLine;
175  } // SaveScannerState
176
177  private static void RestoreScannerState() {
178    tokenStrLen -= apxLen;
179    apxLen       = 0;
180    curCh        = savedCh;
181    curCol       = savedCol;
182    curLine      = savedLine;
183  } // RestoreScannerState
184
185
186  public static void NextCh() {
187  //-----------------------------------|----------------------------------------
188    for (;;) {
189     
190      if (curCol < curLineStr.Length) {         // within line
191        curCol++;
192        curCh = curLineStr[curCol - 1];
193        return;
194      } else if (curCol == curLineStr.Length) { // end of line
195        curCol++;
196        curCh = Utils.LF; // to separate lines
197        return;
198      } else { // curCol > curLineStr.Length
199        curLineStr = src.ReadLine();
200        curLine++;
201        curCol = 0;
202        if (curLineStr == null) {               // end of file
203          curLineStr = "";
204          curCh = Utils.EF;
205          return;
206        } // if
207      } // else
208    } // for
209  } // NextCh
210
211
212  private static void CommentErr() {
213    Errors.LexError(curLine, curCol, "end of file in comment");
214  } // CommentErr
215
216  private static bool Comment() {
217   
218    int level = 1;
219   
220    SaveScannerState();
221    // --- from '/' '*' to '*' '/' nested ---
222    NextCh();
223    if (curCh == '*') {
224      NextCh();
225      for (;;)
226        switch (curCh) {
227          case '*':
228            NextCh();
229            if (curCh == '/') {
230              NextCh();
231              level--;
232              if (level == 0) {
233                return true;
234              } // if
235            } // if
236            break;
237          case '/':
238            NextCh();
239            if (curCh == '*') {
240              level++;
241              NextCh();
242            }
243            break;
244          case Utils.EF:
245            CommentErr();
246            return true;
247          default:
248            NextCh();
249            break;
250        } // switch
251    } else
252      RestoreScannerState();
253    return false;
254  } // Comment
255
256  private static void HandleLexErr() {
257    Errors.LexError(curLine, curCol,
258                    "invalid character '{0}' (hex {1:X})", curCh, (int)curCh);
259    if (apxLen > 0)
260      RestoreScannerState();
261  } // HandleLexErr
262
263
264  public static void GetToken() {
265  //-----------------------------------|----------------------------------------
266    token = -1;
267    tokenStr = null;
268    do {
269      // --- skip ignored chars and comments ---
270      for (;;) {
271        while (Sets.member(curCh, lt.ignoredChars))
272          NextCh();
273        if (!Sets.member(curCh, lt.commentStart))
274          break;
275        if (!Comment())
276          break;
277      } // for
278      // --- scan for next token ---
279      tokenLine   = curLine;
280      tokenCol    = curCol;
281      tokenStrLen = 0;
282      apxLen      = 0;
283      state = (curCh == Utils.EF) ? 1 : lt.startTab[curCh];
284      for (;;) {
285        tokenStrArr[tokenStrLen++]= curCh;
286        NextCh();
287        switch (state) {
288          case 0:
289            Errors.LexError(tokenLine, tokenCol, "invalid token start");
290            break;
291          case 1:
292            token = 0;
293            tokenStrLen = 0;
294            break;                      // EOF recognized
295          case 2:
296            token = 18;
297            break;                      // '=' recognized
298          case 3:
299            token = 19;
300            break;                      // '|' recognized
301          case 4:
302            token = 21;
303            break;                      // '(' recognized
304          case 5:
305            token = 22;
306            break;                      // ')' recognized
307          case 6:
308            token = 23;
309            break;                      // '[' recognized
310          case 7:
311            token = 24;
312            break;                      // ']' recognized
313          case 8:
314            token = 25;
315            break;                      // '{' recognized
316          case 9:
317            token = 26;
318            break;                      // '}' recognized
319          case 10:
320            token = 27;
321            break;                      // '..' recognized
322          case 11:
323            if (Sets.member(curCh, lt.cls[0])) {
324              state = 11;
325              continue;
326            } // if
327            else if (Sets.member(curCh, lt.cls[1])) {
328              state = 11;
329              continue;
330            } // if
331            else {
332              token = KeywordCheck();
333              if (token >= 0)
334                break;                  // keyword recognized
335              token = 28;
336              break;                    // ident recognized
337            } // else
338          case 12:
339            if (curCh == '<') {
340              state = 13;
341              continue;
342            } // if
343            else {
344              HandleLexErr();
345              break;
346            } // else
347          case 13:
348            token = 29;
349            break;                      // source recognized
350          case 14:
351            if (Sets.member(curCh, lt.cls[1])) {
352              state = 11;
353              continue;
354            } // if
355            else if (Sets.member(curCh, lt.cls[2])) {
356              state = 14;
357              continue;
358            } // if
359            else if (Sets.member(curCh, lt.cls[3])) {
360              state = 11;
361              continue;
362            } // if
363            else {
364              token = KeywordCheck();
365              if (token >= 0)
366                break;                  // keyword recognized
367              token = 28;
368              break;                    // ident recognized
369            } // else
370          case 15:
371            if (curCh == '.') {
372              state = 10;
373              continue;
374            } // if
375            else {
376              token = 20;
377              break;                    // '.' recognized
378            } // else
379        } // switch
380        break;
381      } // for
382    } while (token < 0);
383    if (tokenStr == null)
384      tokenStr = new String(tokenStrArr, 0, tokenStrLen);
385  } // GetToken
386
387  private static int KeywordCheck() {
388    tokenStr = new String(tokenStrArr, 0, tokenStrLen - apxLen);
389    Object token = kwHt[tokenStr];
390    return (token == null) ? -1 : (int)token;
391  } // KeywordCheck
392
393
394  public static int Hash(String s) {
395  //-----------------------------------|----------------------------------------
396    Object spix = nHt[s];
397    if (spix == null) {
398      if (caseSensitive)
399        nl.Add(s);
400      else
401        nl.Add(s.ToUpper());
402      spix   = nl.Count - 1;
403      nHt[s] = spix;
404    } // if
405    return (int)spix;
406  } // Hash
407 
408 
409  public static String HashedStr(int spix) {
410  //-----------------------------------|----------------------------------------
411    return (String)nl[spix];
412  } // HashedStr
413
414
415  public static void GETidentAttr(out string identStr) {
416    identStr = tokenStr;
417  } // GETidentAttr
418 
419  public static void GETsourceAttr()  {
420  } // GETsourceAttr
421
422} // GPDefLex
423
424// End of GPDefLex.cs
425//=====================================|========================================
Note: See TracBrowser for help on using the repository browser.