[9430] | 1 | // Errors.cs HDO, 2006-08-28
|
---|
| 2 | // ---------
|
---|
| 3 | // Error handling, storage of error messages and listing generation.
|
---|
| 4 | //=====================================|========================================
|
---|
| 5 |
|
---|
| 6 | #undef TEST_ERRORS
|
---|
| 7 |
|
---|
| 8 | using System;
|
---|
| 9 | using System.IO;
|
---|
| 10 |
|
---|
| 11 | public class Errors {
|
---|
| 12 |
|
---|
| 13 | public const String MODULENAME = "Errors";
|
---|
| 14 |
|
---|
| 15 | public static void ErrorsMethod(Utils.ModuleAction action, out String moduleName) {
|
---|
| 16 | //-----------------------------------|----------------------------------------
|
---|
| 17 | moduleName = MODULENAME;
|
---|
| 18 | switch (action) {
|
---|
| 19 | case Utils.ModuleAction.getModuleName:
|
---|
| 20 | return;
|
---|
| 21 | case Utils.ModuleAction.initModule:
|
---|
| 22 | aps = new AbortMethod[MAXABORTPROCS];
|
---|
| 23 | sp = 0;
|
---|
| 24 | PushAbortMethod(DefaultAbort);
|
---|
| 25 | stopParsing = null;
|
---|
| 26 | eowCnt = new int[4];
|
---|
| 27 | curEoW = new ErrorWarn[4];
|
---|
| 28 | break;
|
---|
| 29 | case Utils.ModuleAction.resetModule:
|
---|
| 30 | break;
|
---|
| 31 | case Utils.ModuleAction.cleanupModule:
|
---|
| 32 | eowl = null;
|
---|
| 33 | eowCnt = null;
|
---|
| 34 | curEoW = null;
|
---|
| 35 | return;
|
---|
| 36 | } // switch
|
---|
| 37 | // --- for initModule and resetModule ---
|
---|
| 38 | eowl = null;
|
---|
| 39 | numOfErrs = 0;
|
---|
| 40 | numOfWarns = 0;
|
---|
| 41 | for (int i = 0; i < 4; i++)
|
---|
| 42 | eowCnt[i] = 0;
|
---|
| 43 | } // ErrorsMethod
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | public const int MAXABORTPROCS = 3;
|
---|
| 47 | public const int MAXNUMOFERRS = 30; // sum of lex., syn. and sem. errors
|
---|
| 48 | public const int MAXNUMOFWARNS = 50;
|
---|
| 49 |
|
---|
| 50 | public delegate void StopParsingMethod();
|
---|
| 51 |
|
---|
| 52 | public delegate void AbortMethod(String abortKind, String moduleName,
|
---|
| 53 | String methodName, String descr);
|
---|
| 54 |
|
---|
| 55 | enum EoWKind {
|
---|
| 56 | lexErr,
|
---|
| 57 | synErr,
|
---|
| 58 | semErr,
|
---|
| 59 | warn
|
---|
| 60 | } // EoWKind
|
---|
| 61 |
|
---|
| 62 | public class ErrorWarnInfo {
|
---|
| 63 | public String msg;
|
---|
| 64 | public int line, col;
|
---|
| 65 | } // ErrorWarnInfo
|
---|
| 66 |
|
---|
| 67 | private class ErrorWarn {
|
---|
| 68 | public EoWKind kind;
|
---|
| 69 | public ErrorWarnInfo info;
|
---|
| 70 | public ErrorWarn next;
|
---|
| 71 | } // ErrorWarn
|
---|
| 72 |
|
---|
| 73 | private static AbortMethod[] aps; // abort method stack
|
---|
| 74 | private static int sp; // stack pointer for aps
|
---|
| 75 | private static StopParsingMethod stopParsing;
|
---|
| 76 |
|
---|
| 77 | // --- errors and/or warnings handling ---
|
---|
| 78 |
|
---|
| 79 | private static ErrorWarn eowl; // sorted list of errors or warnings
|
---|
| 80 | private static int numOfErrs, numOfWarns;
|
---|
| 81 | private static int[] eowCnt;
|
---|
| 82 |
|
---|
| 83 | private static void DefaultAbort(String abortKind, String moduleName,
|
---|
| 84 | String funcName, String descr) {
|
---|
| 85 | Console.WriteLine();
|
---|
| 86 | Console.WriteLine("*** {0} in module {1} function {2}",
|
---|
| 87 | abortKind, moduleName, funcName);
|
---|
| 88 | Console.WriteLine("*** {0}", descr);
|
---|
| 89 | Utils.Modules(Utils.ModuleAction.cleanupModule);
|
---|
| 90 | Environment.Exit(Utils.EXIT_FAILURE);
|
---|
| 91 | } // DefaultAbort
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | // --- install stop parsing and abort functions ---
|
---|
| 95 |
|
---|
| 96 | public static void InstallStopParsingMethod(StopParsingMethod spp) {
|
---|
| 97 | //-----------------------------------|----------------------------------------
|
---|
| 98 | stopParsing = spp;
|
---|
| 99 | } // InstallStopParsingMethod
|
---|
| 100 |
|
---|
| 101 | public static void PushAbortMethod(AbortMethod ap) {
|
---|
| 102 | //-----------------------------------|----------------------------------------
|
---|
| 103 | if (sp == MAXABORTPROCS)
|
---|
| 104 | Restriction(MODULENAME, "PushAbortFunc", "too many abort functions");
|
---|
| 105 | aps[sp++] = ap;
|
---|
| 106 | } // PushAbortMethod
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | // --- report error or restriction and call abort functions ---
|
---|
| 110 |
|
---|
| 111 | public static void CompilerError(String moduleName, String funcName,
|
---|
| 112 | String fmt, params Object[] p) {
|
---|
| 113 | //-----------------------------------|----------------------------------------
|
---|
| 114 | TextWriter w = new StringWriter();
|
---|
| 115 | w.WriteLine(fmt, p);
|
---|
| 116 | String msg = w.ToString();
|
---|
| 117 | for (int i = sp - 1; i >= 0; i--)
|
---|
| 118 | aps[i]("compiler error", moduleName, funcName, msg); // should not return*
|
---|
| 119 | DefaultAbort("compiler error", moduleName, funcName, msg);
|
---|
| 120 | } // CompilerError
|
---|
| 121 |
|
---|
| 122 | public static void Restriction(String moduleName, String funcName,
|
---|
| 123 | String fmt, params Object[] p) {
|
---|
| 124 | //-----------------------------------|----------------------------------------
|
---|
| 125 | TextWriter w = new StringWriter();
|
---|
| 126 | w.WriteLine(fmt, p);
|
---|
| 127 | String msg = w.ToString();
|
---|
| 128 | for (int i = sp - 1; i >= 0; i--)
|
---|
| 129 | aps[i]("restriction", moduleName, funcName, msg); // should not return
|
---|
| 130 | DefaultAbort("restriction", moduleName, funcName, msg);
|
---|
| 131 | } // Restriction
|
---|
| 132 |
|
---|
| 133 | public static void CallStopParsing() {
|
---|
| 134 | //-----------------------------------|----------------------------------------
|
---|
| 135 | if (stopParsing != null)
|
---|
| 136 | stopParsing();
|
---|
| 137 | } // CallStopParsing
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | // --- storage of errors found on compilation ---
|
---|
| 141 |
|
---|
| 142 | private static void EnterMessage(EoWKind knd, int line, int col, String msg) {
|
---|
| 143 | ErrorWarn eow, prveow, nxteow;
|
---|
| 144 | eowCnt[(int)knd]++;
|
---|
| 145 | eow = new ErrorWarn();
|
---|
| 146 | eow.kind = knd;
|
---|
| 147 | eow.info = new ErrorWarnInfo();
|
---|
| 148 | eow.info.line = line;
|
---|
| 149 | eow.info.col = col;
|
---|
| 150 | eow.info.msg = msg;
|
---|
| 151 | prveow = null;
|
---|
| 152 | nxteow = eowl;
|
---|
| 153 | while (nxteow != null && nxteow.info.line <= line) {
|
---|
| 154 | prveow = nxteow;
|
---|
| 155 | nxteow = nxteow.next;
|
---|
| 156 | } // while
|
---|
| 157 | while (nxteow != null && nxteow.info.line == line && nxteow.info.col <= col) {
|
---|
| 158 | prveow = nxteow;
|
---|
| 159 | nxteow = nxteow.next;
|
---|
| 160 | } // while
|
---|
| 161 | if (prveow == null)
|
---|
| 162 | eowl = eow;
|
---|
| 163 | else
|
---|
| 164 | prveow.next = eow;
|
---|
| 165 | eow.next = nxteow;
|
---|
| 166 | } // EnterMessage
|
---|
| 167 |
|
---|
| 168 | private static void CheckForLimits(EoWKind knd, int line, int col) {
|
---|
| 169 | if (knd == EoWKind.lexErr || knd == EoWKind.synErr || knd == EoWKind.semErr) {
|
---|
| 170 | numOfErrs++;
|
---|
| 171 | if (numOfErrs == MAXNUMOFERRS) {
|
---|
| 172 | Warning(line, col, "too many errors, parsing stopped");
|
---|
| 173 | CallStopParsing();
|
---|
| 174 | } // if
|
---|
| 175 | } else { // knd == EoWKind.warn
|
---|
| 176 | numOfWarns++;
|
---|
| 177 | if (numOfWarns == MAXNUMOFWARNS) {
|
---|
| 178 | Warning(line, col, "too many warnings, parsing stopped");
|
---|
| 179 | CallStopParsing();
|
---|
| 180 | } // if
|
---|
| 181 | } // else
|
---|
| 182 | } // CheckForLimits
|
---|
| 183 |
|
---|
| 184 |
|
---|
| 185 | public static void LexError(int line, int col, String fmt, params Object[] p) {
|
---|
| 186 | //-----------------------------------|----------------------------------------
|
---|
| 187 | if (numOfErrs == MAXNUMOFERRS)
|
---|
| 188 | return;
|
---|
| 189 | TextWriter w = new StringWriter();
|
---|
| 190 | w.WriteLine(fmt, p);
|
---|
| 191 | String msg = w.ToString();
|
---|
| 192 | EnterMessage(EoWKind.lexErr, line, col, msg);
|
---|
| 193 | CheckForLimits(EoWKind.lexErr, line, col);
|
---|
| 194 | } // SynError
|
---|
| 195 |
|
---|
| 196 | public static void SynError(int line, int col, String fmt, params Object[] p) {
|
---|
| 197 | //-----------------------------------|----------------------------------------
|
---|
| 198 | if (numOfErrs == MAXNUMOFERRS)
|
---|
| 199 | return;
|
---|
| 200 | TextWriter w = new StringWriter();
|
---|
| 201 | w.WriteLine(fmt, p);
|
---|
| 202 | String msg = w.ToString();
|
---|
| 203 | EnterMessage(EoWKind.synErr, line, col, msg);
|
---|
| 204 | CheckForLimits(EoWKind.synErr, line, col);
|
---|
| 205 | } // SynError
|
---|
| 206 |
|
---|
| 207 | public static void SemError(int line, int col, String fmt, params Object[] p) {
|
---|
| 208 | //-----------------------------------|----------------------------------------
|
---|
| 209 | if (numOfErrs == MAXNUMOFERRS)
|
---|
| 210 | return;
|
---|
| 211 | TextWriter w = new StringWriter();
|
---|
| 212 | w.WriteLine(fmt, p);
|
---|
| 213 | String msg = w.ToString();
|
---|
| 214 | EnterMessage(EoWKind.semErr, line, col, msg);
|
---|
| 215 | CheckForLimits(EoWKind.semErr, line, col);
|
---|
| 216 | } // SemError
|
---|
| 217 |
|
---|
| 218 | public static void Warning(int line, int col, String fmt, params Object[] p) {
|
---|
| 219 | //-----------------------------------|----------------------------------------
|
---|
| 220 | if (numOfWarns == MAXNUMOFWARNS)
|
---|
| 221 | return;
|
---|
| 222 | TextWriter w = new StringWriter();
|
---|
| 223 | w.WriteLine(fmt, p);
|
---|
| 224 | String msg = w.ToString();
|
---|
| 225 | EnterMessage(EoWKind.warn, line, col, msg);
|
---|
| 226 | CheckForLimits(EoWKind.warn, line, col);
|
---|
| 227 | } // Warning
|
---|
| 228 |
|
---|
| 229 |
|
---|
| 230 | // --- retrieval of stored source errors and warnings ---
|
---|
| 231 |
|
---|
| 232 | public static int NumOfErrors() {
|
---|
| 233 | //-----------------------------------|----------------------------------------
|
---|
| 234 | return numOfErrs;
|
---|
| 235 | } // NumOfErrors
|
---|
| 236 |
|
---|
| 237 | public static int NumOfLexErrors() {
|
---|
| 238 | //-----------------------------------|----------------------------------------
|
---|
| 239 | return eowCnt[(int)EoWKind.lexErr];
|
---|
| 240 | } // NumOfLexErrors
|
---|
| 241 |
|
---|
| 242 | public static int NumOfSynErrors() {
|
---|
| 243 | //-----------------------------------|----------------------------------------
|
---|
| 244 | return eowCnt[(int)EoWKind.synErr];
|
---|
| 245 | } // NumOfSynErrors
|
---|
| 246 |
|
---|
| 247 | public static int NumOfSemErrors() {
|
---|
| 248 | //-----------------------------------|----------------------------------------
|
---|
| 249 | return eowCnt[(int)EoWKind.semErr];
|
---|
| 250 | } // NumOfSemErrors
|
---|
| 251 |
|
---|
| 252 | public static int NumOfWarnings() {
|
---|
| 253 | //-----------------------------------|----------------------------------------
|
---|
| 254 | return eowCnt[(int)EoWKind.warn];
|
---|
| 255 | } // NumOfWarnings
|
---|
| 256 |
|
---|
| 257 |
|
---|
| 258 | private static ErrorWarn[] curEoW;
|
---|
| 259 |
|
---|
| 260 | private static ErrorWarnInfo GetMessage(bool first, EoWKind knd) {
|
---|
| 261 | ErrorWarn eow = null;
|
---|
| 262 | ErrorWarnInfo eowi = null;
|
---|
| 263 | if (first)
|
---|
| 264 | eow = eowl;
|
---|
| 265 | else
|
---|
| 266 | eow = curEoW[(int)knd].next;
|
---|
| 267 | while (eow != null && eow.kind != knd)
|
---|
| 268 | eow = eow.next;
|
---|
| 269 | curEoW[(int)knd] = eow;
|
---|
| 270 | if (eow == null) {
|
---|
| 271 | eowi = new ErrorWarnInfo();
|
---|
| 272 | eowi.msg = "";
|
---|
| 273 | eowi.line = 0;
|
---|
| 274 | eowi.col = 0;
|
---|
| 275 | } else
|
---|
| 276 | eowi = eow.info;
|
---|
| 277 | return eowi;
|
---|
| 278 | } // GetMessage
|
---|
| 279 |
|
---|
| 280 | public static ErrorWarnInfo GetLexError(bool first) {
|
---|
| 281 | //-----------------------------------|----------------------------------------
|
---|
| 282 | return GetMessage(first, EoWKind.lexErr);
|
---|
| 283 | } // ErrorWarnInfo
|
---|
| 284 |
|
---|
| 285 | public static ErrorWarnInfo GetSynError(bool first) {
|
---|
| 286 | //-----------------------------------|----------------------------------------
|
---|
| 287 | return GetMessage(first, EoWKind.synErr);
|
---|
| 288 | } // ErrorWarnInfo
|
---|
| 289 |
|
---|
| 290 | public static ErrorWarnInfo GetSemError(bool first) {
|
---|
| 291 | //-----------------------------------|----------------------------------------
|
---|
| 292 | return GetMessage(first, EoWKind.semErr);
|
---|
| 293 | } // ErrorWarnInfo
|
---|
| 294 |
|
---|
| 295 | public static ErrorWarnInfo GetWarning(bool first) {
|
---|
| 296 | //-----------------------------------|----------------------------------------
|
---|
| 297 | return GetMessage(first, EoWKind.warn);
|
---|
| 298 | } // ErrorWarnInfo
|
---|
| 299 |
|
---|
| 300 |
|
---|
| 301 | // --- listing generation ---
|
---|
| 302 |
|
---|
| 303 | private static void PutMsg(TextWriter lst, ErrorWarn eow) {
|
---|
| 304 | switch (eow.kind) {
|
---|
| 305 | case EoWKind.lexErr:
|
---|
| 306 | lst.Write("+LEX+");
|
---|
| 307 | break;
|
---|
| 308 | case EoWKind.synErr:
|
---|
| 309 | lst.Write("*SYN*");
|
---|
| 310 | break;
|
---|
| 311 | case EoWKind.semErr:
|
---|
| 312 | lst.Write("#SEM#");
|
---|
| 313 | break;
|
---|
| 314 | case EoWKind.warn:
|
---|
| 315 | lst.Write("!WRN!");
|
---|
| 316 | break;
|
---|
| 317 | } // switch
|
---|
| 318 | for (int i = 0; i < eow.info.col; i++)
|
---|
| 319 | lst.Write(" ");
|
---|
| 320 | lst.Write(" ^{0}", eow.info.msg);
|
---|
| 321 | } // PutMsg
|
---|
| 322 |
|
---|
| 323 | public enum ListingShape {
|
---|
| 324 | longListing,
|
---|
| 325 | shortListing
|
---|
| 326 | } // ListingShape
|
---|
| 327 |
|
---|
| 328 | public static void GenerateListing(TextReader src, TextWriter lst,
|
---|
| 329 | ListingShape listShape) {
|
---|
| 330 | //-----------------------------------|----------------------------------------
|
---|
| 331 | ErrorWarn eow = null;
|
---|
| 332 | int lnr, skip;
|
---|
| 333 | String srcLine;
|
---|
| 334 |
|
---|
| 335 | ((StreamReader)src).BaseStream.Seek(0, SeekOrigin.Begin);
|
---|
| 336 |
|
---|
| 337 | eow = eowl;
|
---|
| 338 | if (eow != null) {
|
---|
| 339 | while (eow != null && eow.info.line < 1) {
|
---|
| 340 | PutMsg(lst, eow);
|
---|
| 341 | eow = eow.next;
|
---|
| 342 | } // while
|
---|
| 343 | lst.WriteLine();
|
---|
| 344 | } // if
|
---|
| 345 | lnr = 1;
|
---|
| 346 | for (; ; ) {
|
---|
| 347 | if (listShape == ListingShape.shortListing) {
|
---|
| 348 | if (eow == null) {
|
---|
| 349 | lst.WriteLine("...");
|
---|
| 350 | break;
|
---|
| 351 | } // if
|
---|
| 352 | skip = eow.info.line - lnr;
|
---|
| 353 | if (skip > 0) {
|
---|
| 354 | lst.WriteLine("...");
|
---|
| 355 | lnr = eow.info.line;
|
---|
| 356 | while (skip-- > 0) {
|
---|
| 357 | srcLine = src.ReadLine();
|
---|
| 358 | if (srcLine == null)
|
---|
| 359 | break;
|
---|
| 360 | } // while
|
---|
| 361 | } // if
|
---|
| 362 | } // if
|
---|
| 363 | srcLine = src.ReadLine();
|
---|
| 364 | if (srcLine == null)
|
---|
| 365 | break;
|
---|
| 366 | lst.WriteLine("{0,5}| {1}", lnr, srcLine);
|
---|
| 367 | while (eow != null && eow.info.line == lnr) {
|
---|
| 368 | PutMsg(lst, eow);
|
---|
| 369 | eow = eow.next;
|
---|
| 370 | } // while
|
---|
| 371 | lnr++;
|
---|
| 372 | } // for
|
---|
| 373 | lst.WriteLine();
|
---|
| 374 | while (eow != null) {
|
---|
| 375 | PutMsg(lst, eow);
|
---|
| 376 | eow = eow.next;
|
---|
| 377 | } // while
|
---|
| 378 | lst.WriteLine();
|
---|
| 379 | lst.WriteLine("error(s) and warning(s):");
|
---|
| 380 | lst.WriteLine("-----------------------");
|
---|
| 381 | lst.WriteLine();
|
---|
| 382 | lst.WriteLine("{0,5} lexical error(s) ", NumOfLexErrors());
|
---|
| 383 | lst.WriteLine("{0,5} syntax error(s) ", NumOfSynErrors());
|
---|
| 384 | lst.WriteLine("{0,5} semantic error(s)", NumOfSemErrors());
|
---|
| 385 | lst.WriteLine("{0,5} warning(s) ", NumOfWarnings());
|
---|
| 386 | } // GenerateListing
|
---|
| 387 |
|
---|
| 388 |
|
---|
| 389 | #if TEST_ERRORS
|
---|
| 390 |
|
---|
| 391 | public static void Main(String[] args) {
|
---|
| 392 | Console.WriteLine("START: Errors");
|
---|
| 393 |
|
---|
| 394 | Console.WriteLine("installing ...");
|
---|
| 395 | Utils.InstallModule("Utils", new Utils.ModuleMethodDelegate(Utils.UtilsMethod));
|
---|
| 396 | Utils.InstallModule("Sets", new Utils.ModuleMethodDelegate(Sets.SetsMethod ));
|
---|
| 397 | Utils.InstallModule("Errors", new Utils.ModuleMethodDelegate(Errors.ErrorsMethod));
|
---|
| 398 | Console.WriteLine("initModule ...");
|
---|
| 399 | Utils.Modules(Utils.ModuleAction.initModule);
|
---|
| 400 |
|
---|
| 401 | LexError(1, 1, "LexError at {0}, {1}", 1, 1);
|
---|
| 402 | SynError(3, 10, "SynError at {0}, {1}", 2, 10);
|
---|
| 403 | SemError(5, 5, "SemError at {0}, {1}", 5, 5);
|
---|
| 404 | Warning(0, 0, "Warning at {0}, {1}", 0, 0);
|
---|
| 405 | Warning(9, 0, "Warning at {0}, {1}", 9, 0);
|
---|
| 406 |
|
---|
| 407 | String srcName;
|
---|
| 408 | Utils.GetInputFileName("source file name > ", out srcName);
|
---|
| 409 | FileStream srcFs = new FileStream(srcName, FileMode.Open);
|
---|
| 410 | StreamReader src = new StreamReader(srcFs);
|
---|
| 411 | FileStream lstFs = new FileStream(srcName + ".lst", FileMode.Create);
|
---|
| 412 | StreamWriter lst = new StreamWriter(lstFs);
|
---|
| 413 |
|
---|
| 414 | lst.WriteLine("START");
|
---|
| 415 | GenerateListing(src, lst, ListingShape.longListing);
|
---|
| 416 | lst.WriteLine("END");
|
---|
| 417 |
|
---|
| 418 | src.Close();
|
---|
| 419 | lst.Close();
|
---|
| 420 |
|
---|
| 421 | Console.WriteLine("resetModule ...");
|
---|
| 422 | Utils.Modules(Utils.ModuleAction.resetModule);
|
---|
| 423 |
|
---|
| 424 | Console.WriteLine("cleanupModule ...");
|
---|
| 425 | Utils.Modules(Utils.ModuleAction.cleanupModule);
|
---|
| 426 | Console.WriteLine("END");
|
---|
| 427 | // Console.WriteLine("type [CR] to continue ...");
|
---|
| 428 | // Console.ReadLine();
|
---|
| 429 | } // Main
|
---|
| 430 |
|
---|
| 431 | #endif
|
---|
| 432 |
|
---|
| 433 | } // Errors
|
---|
| 434 |
|
---|
| 435 | // End of Errors.cs
|
---|
| 436 | //=====================================|========================================
|
---|
| 437 |
|
---|