1.1 --- a/tools/incparser/Program.cs Sat Apr 19 10:35:24 2008 +0000
1.2 +++ b/tools/incparser/Program.cs Mon Apr 21 21:41:38 2008 +0000
1.3 @@ -1,6 +1,7 @@
1.4 using System;
1.5 using System.Collections.Generic;
1.6 using System.Text;
1.7 +using System.IO;
1.8
1.9 namespace incparser
1.10 {
1.11 @@ -13,16 +14,110 @@
1.12
1.13 static int SubMain(string[] args)
1.14 {
1.15 - if (args.Length < 1)
1.16 + string directory = ".";
1.17 + string template = "template.txt";
1.18 + string outputfile = "output.txt";
1.19 + string file = null;
1.20 +
1.21 + if (args.Length == 0 || (args.Length == 1 && args[0] == "-h"))
1.22 + {
1.23 + PrintHelp();
1.24 + return 0;
1.25 + }
1.26 +
1.27 + for (int i=0; i<args.Length-1; i++)
1.28 {
1.29 - Console.WriteLine("Usage: incparser <infile>");
1.30 - return 1;
1.31 + if (args[i] == "-d")
1.32 + {
1.33 + directory = args[i + 1];
1.34 + }
1.35 +
1.36 + if (args[i] == "-t")
1.37 + {
1.38 + template = args[i + 1];
1.39 + }
1.40 +
1.41 + if (args[i] == "-o")
1.42 + {
1.43 + outputfile = args[i + 1];
1.44 + }
1.45 +
1.46 + if (args[i] == "-f")
1.47 + {
1.48 + file = args[i + 1];
1.49 + }
1.50 +
1.51 + if (args[i] == "-h")
1.52 + {
1.53 + if (args[i + 1] == "template")
1.54 + {
1.55 + PrintTemplateHelp();
1.56 + return 0;
1.57 + }
1.58 + PrintHelp();
1.59 + return 0;
1.60 + }
1.61 }
1.62
1.63 IncParser inc = null;
1.64 +
1.65 + if (file == null)
1.66 + {
1.67 + DirectoryInfo di = new DirectoryInfo(directory);
1.68 + FileInfo[] rgFiles = di.GetFiles("*.inc");
1.69 +
1.70 + ParseWriter pwr = new ParseWriter();
1.71 +
1.72 + foreach (FileInfo fi in rgFiles)
1.73 + {
1.74 + pwr.Reset();
1.75 +
1.76 + Console.Write("Parsing file: " + fi.ToString() + "... ");
1.77 +
1.78 + try
1.79 + {
1.80 + inc = new IncParser(fi.FullName);
1.81 + }
1.82 + catch (ParseException e)
1.83 + {
1.84 + Console.WriteLine("Initial browsing failed: " + e.Message);
1.85 + continue;
1.86 + }
1.87 + catch (System.Exception e)
1.88 + {
1.89 + Console.WriteLine("Failed to read file: " + e.Message);
1.90 + continue;
1.91 + }
1.92 +
1.93 + try
1.94 + {
1.95 + inc.Parse(pwr);
1.96 + }
1.97 + catch (System.Exception e)
1.98 + {
1.99 + Console.WriteLine("Error parsing file (line " + inc.GetLineNumber() + "): " + e.Message);
1.100 + continue;
1.101 + }
1.102 +
1.103 + if (pwr.Level != 0)
1.104 + {
1.105 + Console.WriteLine("Fatal parse error detected; unable to complete output.");
1.106 + continue;
1.107 + }
1.108 +
1.109 + Console.WriteLine("Complete!");
1.110 + }
1.111 +
1.112 + pwr.WriteFiles(template, outputfile);
1.113 +
1.114 + Console.WriteLine("Parsing Complete!");
1.115 +
1.116 + return 0;
1.117 + }
1.118 +
1.119 try
1.120 {
1.121 - inc = new IncParser(args[0]);
1.122 + inc = new IncParser(file);
1.123 }
1.124 catch (ParseException e)
1.125 {
1.126 @@ -58,5 +153,31 @@
1.127
1.128 return 0;
1.129 }
1.130 +
1.131 + static void PrintHelp()
1.132 + {
1.133 + Console.WriteLine("SourcePawn include file parser by BAILOPAN (edited by pRED*)");
1.134 + Console.Write("\n");
1.135 + Console.WriteLine("This can parse a single file into SMC configuration format or an entire directory into a template file if -f is not specified (current directory is used if -d is not specified)");
1.136 + Console.Write("\n");
1.137 + Console.WriteLine("Parameters:");
1.138 + Console.Write("\n");
1.139 + Console.WriteLine("-f <filename> - Specify an input file to be used");
1.140 + Console.WriteLine("-d <path> - Specify a directory to parse (only *.inc files are used)");
1.141 + Console.WriteLine("-t <filename> - Specify a template file to be used");
1.142 + Console.WriteLine("-o <filename> - Specify an output file to be used");
1.143 + Console.WriteLine("-h - Display this help");
1.144 + Console.WriteLine("-h template - Displays help about templates");
1.145 + }
1.146 +
1.147 + static void PrintTemplateHelp()
1.148 + {
1.149 + Console.WriteLine("Template File Help:");
1.150 + Console.WriteLine("The inc parser can read a template file and replace variables with the outputs of it's parse and write into the output file");
1.151 + Console.Write("\n");
1.152 + Console.WriteLine("Variables:");
1.153 + Console.Write("\n");
1.154 + Console.WriteLine("$defines $enums $enumtypes $forwards $natives $stocks $funcenums $functags $structs");
1.155 + }
1.156 }
1.157 }