1 | // ==>ModuleName.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 |
|
---|
9 | using System;
|
---|
10 | using System.Collections;
|
---|
11 | using System.Collections.Specialized;
|
---|
12 | using System.IO;
|
---|
13 | using System.Text;
|
---|
14 |
|
---|
15 | public class ==>ModuleName {
|
---|
16 |
|
---|
17 | public const String MODULENAME = "==>ModuleName";
|
---|
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 ==>ModuleNameMethod(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 = ==>CaseSensitivity;
|
---|
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 | } // ==>ModuleNameMethod
|
---|
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 | ==>Initialisations
|
---|
74 | // --- initialize name data structures ---
|
---|
75 | nHt.Clear();
|
---|
76 | nl.Clear();
|
---|
77 | nl.Add(""); // so spix = 0 is the empty string
|
---|
78 | nl.Add("!EOF!"); // so EOFSPIX = 1
|
---|
79 | // --- (re)set global data ---
|
---|
80 | curLineStr = "";
|
---|
81 | curCh = ' ';
|
---|
82 | curLine = 0;
|
---|
83 | curCol = 2;
|
---|
84 | tokenStr = null;
|
---|
85 | token = 0;
|
---|
86 | tokenCol = 0;
|
---|
87 | tokenLine = 0;
|
---|
88 | tokenStrLen = 0;
|
---|
89 | pendingEOLs = 0;
|
---|
90 | apxLen = pendingEOLs; // to prevent warning
|
---|
91 | } // InitLex
|
---|
92 |
|
---|
93 | private static void EnterKeyword(int token, String keyword) {
|
---|
94 | kwHt.Add(keyword, token);
|
---|
95 | } // EnterKeyword
|
---|
96 |
|
---|
97 |
|
---|
98 | // *** start of global LEX declarations from ATG ***
|
---|
99 | ==>GlobalDeclarations
|
---|
100 | // *** end of global LEX declarations from ATG ***
|
---|
101 |
|
---|
102 |
|
---|
103 | public static bool caseSensitive;
|
---|
104 |
|
---|
105 | private class LexicalTable {
|
---|
106 | public int header = ==>header;
|
---|
107 | public int[] startTab = {
|
---|
108 | ==>startTab
|
---|
109 | };
|
---|
110 | public Sets.Set256 ignoredChars = new Sets.Set256(
|
---|
111 | ==>ignoredChars
|
---|
112 | );
|
---|
113 | public Sets.Set256 commentStart = new Sets.Set256(
|
---|
114 | ==>commentStart
|
---|
115 | );
|
---|
116 | public Sets.Set256[] cls = {
|
---|
117 | ==>cls
|
---|
118 | };
|
---|
119 | } // LexicalTable
|
---|
120 |
|
---|
121 | private static LexicalTable lt;
|
---|
122 | private static Hashtable kwHt; // hash table for keywords: string -> token
|
---|
123 | private static Hashtable nHt; // hash table for names: string -> null
|
---|
124 | private static ArrayList nl; // name list for names, index is spix
|
---|
125 |
|
---|
126 | private static String curLineStr; // current source line
|
---|
127 | private static char[] tokenStrArr; // token string in an array
|
---|
128 | private static int tokenStrLen; // length of token string in tokenStrArr
|
---|
129 | private static int state; // current automaton state
|
---|
130 | private static int apxLen; // length of appendix in token string
|
---|
131 | private static int pendingEOLs; // nr of EOLs found in comment
|
---|
132 |
|
---|
133 | // --- to save and restore scanner state ---
|
---|
134 | private static char savedCh;
|
---|
135 | private static int savedCol, savedLine;
|
---|
136 |
|
---|
137 | private static void SaveScannerState() {
|
---|
138 | savedCh = curCh;
|
---|
139 | savedCol = curCol;
|
---|
140 | savedLine = curLine;
|
---|
141 | } // SaveScannerState
|
---|
142 |
|
---|
143 | private static void RestoreScannerState() {
|
---|
144 | tokenStrLen -= apxLen;
|
---|
145 | apxLen = 0;
|
---|
146 | curCh = savedCh;
|
---|
147 | curCol = savedCol;
|
---|
148 | curLine = savedLine;
|
---|
149 | } // RestoreScannerState
|
---|
150 |
|
---|
151 |
|
---|
152 | public static void NextCh() {
|
---|
153 | //-----------------------------------|----------------------------------------
|
---|
154 | for (;;) {
|
---|
155 | ==>NextChMethodFront
|
---|
156 | if (curCol < curLineStr.Length) { // within line
|
---|
157 | curCol++;
|
---|
158 | curCh = curLineStr[curCol - 1];
|
---|
159 | return;
|
---|
160 | } else if (curCol == curLineStr.Length) { // end of line
|
---|
161 | curCol++;
|
---|
162 | curCh = Utils.LF; // to separate lines
|
---|
163 | return;
|
---|
164 | } else { // curCol > curLineStr.Length
|
---|
165 | curLineStr = src.ReadLine();
|
---|
166 | curLine++;
|
---|
167 | curCol = 0;
|
---|
168 | if (curLineStr == null) { // end of file
|
---|
169 | curLineStr = "";
|
---|
170 | curCh = Utils.EF;
|
---|
171 | return;
|
---|
172 | } // if
|
---|
173 | } // else
|
---|
174 | } // for
|
---|
175 | } // NextCh
|
---|
176 |
|
---|
177 |
|
---|
178 | private static void CommentErr() {
|
---|
179 | Errors.LexError(curLine, curCol, "end of file in comment");
|
---|
180 | } // CommentErr
|
---|
181 |
|
---|
182 | private static bool Comment() {
|
---|
183 | ==>CommentMethod
|
---|
184 | return false;
|
---|
185 | } // Comment
|
---|
186 |
|
---|
187 | private static void HandleLexErr() {
|
---|
188 | Errors.LexError(curLine, curCol,
|
---|
189 | "invalid character '{0}' (hex {1:X})", curCh, (int)curCh);
|
---|
190 | if (apxLen > 0)
|
---|
191 | RestoreScannerState();
|
---|
192 | } // HandleLexErr
|
---|
193 |
|
---|
194 |
|
---|
195 | public static void GetToken() {
|
---|
196 | //-----------------------------------|----------------------------------------
|
---|
197 | token = -1;
|
---|
198 | tokenStr = null;
|
---|
199 | do {
|
---|
200 | ==>GetTokenMethodFront
|
---|
201 | // --- scan for next token ---
|
---|
202 | tokenLine = curLine;
|
---|
203 | tokenCol = curCol;
|
---|
204 | tokenStrLen = 0;
|
---|
205 | apxLen = 0;
|
---|
206 | state = (curCh == Utils.EF) ? 1 : lt.startTab[curCh];
|
---|
207 | for (;;) {
|
---|
208 | tokenStrArr[tokenStrLen++]= curCh;
|
---|
209 | NextCh();
|
---|
210 | switch (state) {
|
---|
211 | case 0:
|
---|
212 | Errors.LexError(tokenLine, tokenCol, "invalid token start");
|
---|
213 | break;
|
---|
214 | case 1:
|
---|
215 | token = 0;
|
---|
216 | tokenStrLen = 0;
|
---|
217 | break; // EOF recognized
|
---|
218 | ==>GetTokenMethodCaseBody
|
---|
219 | } // switch
|
---|
220 | break;
|
---|
221 | } // for
|
---|
222 | } while (token < 0);
|
---|
223 | if (tokenStr == null)
|
---|
224 | tokenStr = new String(tokenStrArr, 0, tokenStrLen);
|
---|
225 | } // GetToken
|
---|
226 |
|
---|
227 | private static int KeywordCheck() {
|
---|
228 | tokenStr = new String(tokenStrArr, 0, tokenStrLen - apxLen);
|
---|
229 | Object token = kwHt[tokenStr];
|
---|
230 | return (token == null) ? -1 : (int)token;
|
---|
231 | } // KeywordCheck
|
---|
232 |
|
---|
233 |
|
---|
234 | public static int Hash(String s) {
|
---|
235 | //-----------------------------------|----------------------------------------
|
---|
236 | Object spix = nHt[s];
|
---|
237 | if (spix == null) {
|
---|
238 | if (caseSensitive)
|
---|
239 | nl.Add(s);
|
---|
240 | else
|
---|
241 | nl.Add(s.ToUpper());
|
---|
242 | spix = nl.Count - 1;
|
---|
243 | nHt[s] = spix;
|
---|
244 | } // if
|
---|
245 | return (int)spix;
|
---|
246 | } // Hash
|
---|
247 |
|
---|
248 |
|
---|
249 | public static String HashedStr(int spix) {
|
---|
250 | //-----------------------------------|----------------------------------------
|
---|
251 | return (String)nl[spix];
|
---|
252 | } // HashedStr
|
---|
253 |
|
---|
254 |
|
---|
255 | ==>TokenClass&PragmaMethodsImpls
|
---|
256 |
|
---|
257 | } // ==>ModuleName
|
---|
258 |
|
---|
259 | // End of ==>ModuleName.cs
|
---|
260 | //=====================================|========================================
|
---|