PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/adinetz02/cbc/cbc/Common/CompileArgs.cs

#
C# | 268 lines | 159 code | 23 blank | 86 comment | 38 complexity | 61f569a1759c9e11024826865d462fa7 MD5 | raw file
Possible License(s): AGPL-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace CBucks.Compiler {
  5. // TODO: add correct & comprehensive handling for all command
  6. // line errors
  7. /// <summary>
  8. /// The compilation target type
  9. /// </summary>
  10. public enum Target {
  11. /// <summary>
  12. /// No target type
  13. /// </summary>
  14. None,
  15. /// <summary>
  16. /// Executable file
  17. /// </summary>
  18. Exe,
  19. /// <summary>
  20. /// Dynamic Library
  21. /// </summary>
  22. Dll
  23. } // end of enum Target
  24. /// <summary>
  25. /// The compilation mode enumeration
  26. /// </summary>
  27. public enum CompileMode {
  28. /// <summary>
  29. /// No compilation
  30. /// </summary>
  31. None,
  32. /// <summary>
  33. /// The tokenize-only compilation mode
  34. /// </summary>
  35. Token,
  36. /// <summary>
  37. /// The parse-only compilation mode
  38. /// </summary>
  39. Parse,
  40. /// <summary>
  41. /// The parse & check compilation mode (no code generation)
  42. /// </summary>
  43. Check,
  44. /// <summary>
  45. /// Code generation mode. This is the default value.
  46. /// </summary>
  47. Gen
  48. } // end of CompileMode
  49. /// <summary>
  50. /// Represents arguments of the compilation
  51. /// </summary>
  52. public sealed class CompileArgs {
  53. /// <summary>
  54. /// Creates a new CompileArgs instance
  55. /// </summary>
  56. public CompileArgs() { }
  57. /// <summary>
  58. /// Creates a list of compile arguments from the command line
  59. /// </summary>
  60. /// <param name="args"> The arguments of the command line </param>
  61. /// <returns> The compilation arguments </returns>
  62. public static CompileArgs fromCmdLine(string[] args) {
  63. CompileArgs ca = new CompileArgs();
  64. ca.compMode = CompileMode.Gen;
  65. ca.target = Target.Exe;
  66. // parse the command line
  67. try {
  68. // flags to control re-specification
  69. bool targetSpecified = false, modeSpecified = false;
  70. for(int i = 0; i < args.Length; i++) {
  71. string arg = args[i];
  72. if(arg[0] != '-')
  73. Report.error(ErrorCode.InvalidCommandLine, null, true);
  74. switch(arg) {
  75. case "-cb":
  76. ca.useCbRuntime = true;
  77. break;
  78. case "-wd":
  79. ca.waitDebugAttach = true;
  80. break;
  81. case "-ce":
  82. ca.catchOuterExceptions = false;
  83. break;
  84. case "-wr":
  85. ca.waitResult = true;
  86. break;
  87. case "-o":
  88. if(null != ca.outName)
  89. Report.error(ErrorCode.InvalidCommandLine, null, true);
  90. ca.outName = args[++i];
  91. break;
  92. case "-h":
  93. case "--h":
  94. case "--help":
  95. case "-help":
  96. case "-?":
  97. ca.help = true;
  98. break;
  99. case "-t":
  100. if(targetSpecified)
  101. Report.error(ErrorCode.InvalidCommandLine, null, true);
  102. targetSpecified = true;
  103. if(args[i + 1] == "exe")
  104. ca.target = Target.Exe;
  105. else if(args[i + 1] == "dll")
  106. ca.target = Target.Dll;
  107. else
  108. Report.error(ErrorCode.InvalidCommandLine, null, true);
  109. i++;
  110. break;
  111. case "-r":
  112. if(null != ca.reportName)
  113. Report.error(ErrorCode.InvalidCommandLine, null, true);
  114. ca.reportName = args[++i];
  115. break;
  116. case "-m":
  117. if(modeSpecified)
  118. Report.error(ErrorCode.InvalidCommandLine, null, true);
  119. modeSpecified = true;
  120. switch(args[i + 1]) {
  121. case "token":
  122. ca.compMode = CompileMode.Token; break;
  123. case "parse":
  124. ca.compMode = CompileMode.Parse; break;
  125. case "check":
  126. ca.compMode = CompileMode.Check; break;
  127. case "gen":
  128. ca.compMode = CompileMode.Gen; break;
  129. default:
  130. Report.error(ErrorCode.InvalidCommandLine, null, true);
  131. break;
  132. } // end of switch()
  133. i++;
  134. break;
  135. case "-l":
  136. // get the list of assemblies
  137. if(null != ca.assNames)
  138. Report.error(ErrorCode.InvalidCommandLine, null, true);
  139. List<string> assNames = new List<string>();
  140. for(i++; i < args.Length && args[i][0] != '-'; i++)
  141. assNames.Add(args[i]);
  142. if(assNames.Count == 0)
  143. Report.error(ErrorCode.InvalidCommandLine, null, true);
  144. ca.assNames = assNames.ToArray();
  145. if(i != args.Length) i--;
  146. break;
  147. case "-s":
  148. // get the list of source files
  149. if(null != ca.sourceNames)
  150. Report.error(ErrorCode.InvalidCommandLine, null, true);
  151. List<string> srcNames = new List<string>();
  152. for(i++; i < args.Length && args[i][0] != '-'; i++)
  153. srcNames.Add(args[i]);
  154. if(srcNames.Count == 0)
  155. Report.error(ErrorCode.InvalidCommandLine, null, true);
  156. ca.sourceNames = srcNames.ToArray();
  157. if(i != args.Length) i--;
  158. break;
  159. } // end of switch()
  160. } // end of for()
  161. } catch(IndexOutOfRangeException) {
  162. Report.error(ErrorCode.InvalidCommandLine, null, true);
  163. } // end of try
  164. // check whether all necessary properties have been defined
  165. if(!ca.help) {
  166. if(null == ca.sourceNames)
  167. Report.error(ErrorCode.NoSourceFiles, null, true);
  168. if(null == ca.outName)
  169. Report.error(ErrorCode.NoTargetFile, null, true);
  170. } // end of if()
  171. if(ca.help)
  172. printUsage();
  173. return ca;
  174. } // end of fromCmdLine()
  175. /// <summary>
  176. /// Prints compiler usage text
  177. /// </summary>
  178. public static void printUsage() {
  179. Console.WriteLine("Usage: cbc [options]");
  180. Console.WriteLine("Possible options include:");
  181. Console.WriteLine("-o <filename>: specifies the output file");
  182. Console.WriteLine("-h, --h, --help, -help, -?, <empty>: " +
  183. "outputs this message");
  184. Console.WriteLine("-t exe|dll: specifies target file type");
  185. Console.WriteLine("-r <filename>: specifies the output text file");
  186. Console.WriteLine("-m token|parse|check|gen: specifies " +
  187. "compilation level");
  188. Console.WriteLine("-l <assembly1> ... <assemblyn>: " +
  189. "references assemblies");
  190. Console.WriteLine("-s <file1> ... <filen>: " +
  191. "adds source files to compile");
  192. Console.WriteLine("-wd: waits for the user to press <ENTER> at the " +
  193. "start of the program; useful for compiler debugging");
  194. Console.WriteLine("-wr: waits for the user to press <ENTER> at the " +
  195. "end of the program; useful for compiler debugging");
  196. Console.WriteLine("-ce: does not catch exceptions at the top level");
  197. } // end of printUsage()
  198. /// <summary>
  199. /// The paths to the source files to compile
  200. /// </summary>
  201. public string[] sourceNames;
  202. /// <summary>
  203. /// The name of the output file
  204. /// </summary>
  205. public string outName;
  206. /// <summary>
  207. /// Indicates whether the help message should be printed
  208. /// </summary>
  209. public bool help = false;
  210. /// <summary>
  211. /// The default target
  212. /// </summary>
  213. public Target target = Target.Exe;
  214. /// <summary>
  215. /// The report name
  216. /// </summary>
  217. public string reportName = null;
  218. /// <summary>
  219. /// The compilation mode
  220. /// </summary>
  221. public CompileMode compMode = CompileMode.Gen;
  222. /// <summary>
  223. /// The names of the assemblies referenced
  224. /// </summary>
  225. public string[] assNames;
  226. /// <summary>
  227. /// Whether to wait for debug attach at the beginning of the compilation
  228. /// </summary>
  229. public bool waitDebugAttach = false;
  230. /// <summary>
  231. /// Whether to wait at the end of the compilation
  232. /// </summary>
  233. public bool waitResult = false;
  234. /// <summary>
  235. /// Whether to initialize C$ runtime. Without this option, the exe program
  236. /// using C$ runtime will not work
  237. /// </summary>
  238. public bool useCbRuntime = false;
  239. /// <summary>
  240. /// Indicates whether to catch outer exceptions; turning the option off
  241. /// helps with debugging the compiler
  242. /// </summary>
  243. public bool catchOuterExceptions = true;
  244. } // end of CompileArgs
  245. } // end of namespace CBucks.Compiler