Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/SyntaxAnalyzer/GPDefSyntaxAnalyzer.cs @ 9683

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

initial import of GPDL parser plugin

File size: 6.0 KB
Line 
1// ATTENTION: Replace all occurences of GPDefSyntaxAnalyzer by the compiler name!
2
3// GPDefSyntaxAnalyzer.cs                                                        HDO, 2006-08-28
4// -----
5// Main program for compiler generated from GPDefSyntaxAnalyzer.atg with Coco-2.
6//=====================================|========================================
7
8using System;
9using System.IO;
10
11using Lex = GPDefSyntaxAnalyzerLex;
12using Syn = GPDefSyntaxAnalyzerSyn;
13
14public class GPDefSyntaxAnalyzer {
15
16  private static String NAME = "GPDefSyntaxAnalyzer";
17  private static bool interactiveMode;
18
19  private static void Abort(String abortKind, String moduleName,
20                            String methName,  String descr) {
21    Console.WriteLine();
22    Console.WriteLine("*** {0} in class {1} method {2}",
23                      abortKind, moduleName, methName);
24    Console.WriteLine("*** {0}", descr);
25    Console.WriteLine();
26    Console.WriteLine();
27    Console.WriteLine(NAME + " aborted");
28    Utils.Modules(Utils.ModuleAction.cleanupModule);
29    if (interactiveMode) {
30      Console.Write("type [CR] to quit");
31      string dummy = Console.ReadLine();
32    } // if
33    Environment.Exit(Utils.EXIT_FAILURE);
34  } // Abort
35
36  private static int CompileFile(String srcFileName) {
37    FileStream srcFs = null;
38    try {
39      srcFs = new FileStream(srcFileName, FileMode.Open);
40    } catch (Exception) {
41      Console.WriteLine("*** file \"{0}\" not found", srcFileName);
42      return Utils.EXIT_FAILURE;
43    } // try/catch
44    try {
45      Lex.src = new StreamReader(srcFs);
46      Console.WriteLine("parsing ...");
47      Syn.Parse();
48      Lex.src.Close();
49      Lex.src.Dispose();
50      Lex.src = null;
51      if (Errors.NumOfErrors() > 0) {
52        Console.WriteLine("listing ...");
53        String lstFileName = Path.ChangeExtension(srcFileName, ".lst");
54        FileStream lstFs = null;
55        try {
56          lstFs = new FileStream(lstFileName, FileMode.Create);
57        } catch (Exception) {
58          Utils.FatalError(NAME, "CompileFile", "file \"{0}\" not created", lstFileName);
59          return Utils.EXIT_FAILURE;
60        } // try/catch
61        StreamWriter lstWriter = null;
62        try {
63          lstWriter = new StreamWriter(lstFs);
64          StreamReader srcFsReader = null;
65          try { // open the source file a second time to generate the error listing
66            srcFsReader = new StreamReader(new FileStream(srcFileName, FileMode.Open));
67            lstWriter.WriteLine(NAME + " (file: \"{0}\")", srcFileName);
68            Errors.GenerateListing(srcFsReader, lstWriter, Errors.ListingShape.longListing);
69          } finally {
70            srcFsReader.Close();
71            srcFsReader.Dispose();
72          } // try/finally
73          Console.WriteLine("{0} error(s) detected", Errors.NumOfErrors());
74        } finally {
75          lstWriter.Close();
76          lstWriter.Dispose();
77        } // try/finally
78      } else
79        Console.WriteLine("no errors detected");
80    } finally {
81      if (Lex.src != null) {
82        Lex.src.Close();
83        Lex.src.Dispose();
84        Lex.src = null;
85      } // if
86      Utils.Modules(Utils.ModuleAction.resetModule);
87    } // try/finally to make sure srcFs and srcReader are closed
88    return Utils.EXIT_SUCCESS;
89  } // CompileFile
90
91
92  public static void Main(String[] args) {
93  //-----------------------------------|----------------------------------------
94    int result = 0;
95
96    // --- install modules ---
97    Utils.InstallModule("Utils",   new Utils.ModuleMethodDelegate(Utils.UtilsMethod ));
98    Utils.InstallModule("Sets",    new Utils.ModuleMethodDelegate(Sets.SetsMethod  ));
99    Utils.InstallModule("Errors",  new Utils.ModuleMethodDelegate(Errors.ErrorsMethod));
100
101    Utils.InstallModule("GPDefSyntaxAnalyzerLex",  new Utils.ModuleMethodDelegate(GPDefSyntaxAnalyzerLex.GPDefSyntaxAnalyzerLexMethod));
102    Utils.InstallModule("GPDefSyntaxAnalyzerSem",  new Utils.ModuleMethodDelegate(GPDefSyntaxAnalyzerSem.GPDefSyntaxAnalyzerSemMethod));
103    Utils.InstallModule("GPDefSyntaxAnalyzerSyn",  new Utils.ModuleMethodDelegate(GPDefSyntaxAnalyzerSyn.GPDefSyntaxAnalyzerSynMethod));
104
105    // --- initialize modules ---
106    Utils.Modules(Utils.ModuleAction.initModule);
107
108    Errors.PushAbortMethod(new Errors.AbortMethod(Abort));
109
110    Console.WriteLine("---------------------------");
111    Console.WriteLine(" {0} Compiler {1," + (5 - NAME.Length) +"} Version X  ", NAME, "");
112    Console.WriteLine(" Frontend gen. with Coco-2");
113    Console.WriteLine("---------------------------");
114    Console.WriteLine();
115
116    if (args.Length > 0) { // command line mode
117      interactiveMode = false;
118      Console.WriteLine();
119      int i = 0;
120      do {
121        Console.WriteLine("source file \"{0}\"", args[i]);
122        result = CompileFile(args[i]);
123        if (result != Utils.EXIT_SUCCESS)
124          Environment.Exit(result);
125        Console.WriteLine();
126        i++;
127      } while (i < args.Length);
128    } else { // args.Length == 0, interactive mode
129      interactiveMode = true;
130      for (;;) {
131        String srcFileName;
132        Utils.GetInputFileName("source file > ", out srcFileName);
133        if (srcFileName.Length > 0) {
134          result = CompileFile(srcFileName);
135          if (result != Utils.EXIT_SUCCESS)
136            Environment.Exit(result);
137        } // if
138        char answerCh;
139        do {
140          Console.WriteLine();
141          Console.Write("[c]ontinue or [q]uit > ");
142          answerCh = Char.ToUpper(Console.ReadKey().KeyChar);
143        } while (answerCh != 'C' && answerCh != 'Q');
144        if (answerCh == 'Q')
145          break;
146        else // answerCh == 'C'
147          Console.WriteLine();
148      } // for
149    } // else
150
151    Utils.Modules(Utils.ModuleAction.cleanupModule);
152    Environment.Exit(Utils.EXIT_SUCCESS);
153
154  } // Main
155
156} // GPDefSyntaxAnalyzer
157
158// End of GPDefSyntaxAnalyzer.cs
159//=====================================|========================================
Note: See TracBrowser for help on using the repository browser.