PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/adinetz/cbc/cbc/Common/CompileArgs.cs

#
C# | 252 lines | 148 code | 22 blank | 82 comment | 38 complexity | c023618434dc8c0cfb95e594abed3129 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. // parse the command line
  65. try {
  66. // flags to control re-specification
  67. bool targetSpecified = false, modeSpecified = false;
  68. for(int i = 0; i < args.Length; i++) {
  69. string arg = args[i];
  70. if(arg[0] != '-')
  71. Report.error(ErrorCode.InvalidCommandLine, null, true);
  72. switch(arg) {
  73. case "-cb":
  74. ca.useCbRuntime = true;
  75. break;
  76. case "-wd":
  77. ca.waitDebugAttach = true;
  78. break;
  79. case "-wr":
  80. ca.waitResult = true;
  81. break;
  82. case "-o":
  83. if(null != ca.outName)
  84. Report.error(ErrorCode.InvalidCommandLine, null, true);
  85. ca.outName = args[++i];
  86. break;
  87. case "-h":
  88. case "--h":
  89. case "--help":
  90. case "-help":
  91. case "-?":
  92. ca.help = true;
  93. break;
  94. case "-t":
  95. if(targetSpecified)
  96. Report.error(ErrorCode.InvalidCommandLine, null, true);
  97. targetSpecified = true;
  98. if(args[i + 1] == "exe")
  99. ca.target = Target.Exe;
  100. else if(args[i + 1] == "dll")
  101. ca.target = Target.Dll;
  102. else
  103. Report.error(ErrorCode.InvalidCommandLine, null, true);
  104. i++;
  105. break;
  106. case "-r":
  107. if(null != ca.reportName)
  108. Report.error(ErrorCode.InvalidCommandLine, null, true);
  109. ca.reportName = args[++i];
  110. break;
  111. case "-m":
  112. if(modeSpecified)
  113. Report.error(ErrorCode.InvalidCommandLine, null, true);
  114. modeSpecified = true;
  115. switch(args[i + 1]) {
  116. case "token":
  117. ca.compMode = CompileMode.Token; break;
  118. case "parse":
  119. ca.compMode = CompileMode.Parse; break;
  120. case "check":
  121. ca.compMode = CompileMode.Check; break;
  122. case "gen":
  123. ca.compMode = CompileMode.Gen; break;
  124. default:
  125. Report.error(ErrorCode.InvalidCommandLine, null, true);
  126. break;
  127. } // end of switch()
  128. i++;
  129. break;
  130. case "-l":
  131. // get the list of assemblies
  132. if(null != ca.assNames)
  133. Report.error(ErrorCode.InvalidCommandLine, null, true);
  134. List<string> assNames = new List<string>();
  135. for(i++; i < args.Length && args[i][0] != '-'; i++)
  136. assNames.Add(args[i]);
  137. if(assNames.Count == 0)
  138. Report.error(ErrorCode.InvalidCommandLine, null, true);
  139. ca.assNames = assNames.ToArray();
  140. if(i != arg.Length) i--;
  141. break;
  142. case "-s":
  143. // get the list of source files
  144. if(null != ca.sourceNames)
  145. Report.error(ErrorCode.InvalidCommandLine, null, true);
  146. List<string> srcNames = new List<string>();
  147. for(i++; i < args.Length && args[i][0] != '-'; i++)
  148. srcNames.Add(args[i]);
  149. if(srcNames.Count == 0)
  150. Report.error(ErrorCode.InvalidCommandLine, null, true);
  151. ca.sourceNames = srcNames.ToArray();
  152. if(i != arg.Length) i--;
  153. break;
  154. } // end of switch()
  155. } // end of for()
  156. } catch(IndexOutOfRangeException) {
  157. Report.error(ErrorCode.InvalidCommandLine, null, true);
  158. } // end of try
  159. // check whether all necessary properties have been defined
  160. if(!ca.help) {
  161. if(null == ca.sourceNames)
  162. Report.error(ErrorCode.NoSourceFiles, null, true);
  163. if(null == ca.outName)
  164. Report.error(ErrorCode.NoTargetFile, null, true);
  165. } // end of if()
  166. if(ca.help)
  167. printUsage();
  168. return ca;
  169. } // end of fromCmdLine()
  170. /// <summary>
  171. /// Prints compiler usage text
  172. /// </summary>
  173. public static void printUsage() {
  174. Console.WriteLine("Usage: cbc [options]");
  175. Console.WriteLine("Possible options include:");
  176. Console.WriteLine("-o <filename>: specifies the output file");
  177. Console.WriteLine("-h, --h, --help, -help, -?, <empty>: " +
  178. "outputs this message");
  179. Console.WriteLine("-t exe|dll: specifies target file type");
  180. Console.WriteLine("-r <filename>: specifies the output text file");
  181. Console.WriteLine("-m token|parse|check|gen: specifies " +
  182. "compilation level");
  183. Console.WriteLine("-l <assembly1> ... <assemblyn>: " +
  184. "references assemblies");
  185. Console.WriteLine("-s <file1> ... <filen>: " +
  186. "adds source files to compile");
  187. } // end of printUsage()
  188. /// <summary>
  189. /// The paths to the source files to compile
  190. /// </summary>
  191. public string[] sourceNames;
  192. /// <summary>
  193. /// The name of the output file
  194. /// </summary>
  195. public string outName;
  196. /// <summary>
  197. /// Indicates whether the help message should be printed
  198. /// </summary>
  199. public bool help = false;
  200. /// <summary>
  201. /// The default target
  202. /// </summary>
  203. public Target target = Target.Exe;
  204. /// <summary>
  205. /// The report name
  206. /// </summary>
  207. public string reportName = null;
  208. /// <summary>
  209. /// The compilation mode
  210. /// </summary>
  211. public CompileMode compMode = CompileMode.Gen;
  212. /// <summary>
  213. /// The names of the assemblies referenced
  214. /// </summary>
  215. public string[] assNames;
  216. /// <summary>
  217. /// Whether to wait for debug attach at the beginning of the compilation
  218. /// </summary>
  219. public bool waitDebugAttach = false;
  220. /// <summary>
  221. /// Whether to wait at the end of the compilation
  222. /// </summary>
  223. public bool waitResult = false;
  224. /// <summary>
  225. /// Whether to initialize C$ runtime. Without this option, the exe program
  226. /// using C$ runtime will not work
  227. /// </summary>
  228. public bool useCbRuntime = false;
  229. } // end of CompileArgs
  230. } // end of namespace CBucks.Compiler