tools/incparser/Program.cs
author Scott Ehlert <ds@alliedmods.net>
Sat Apr 23 21:53:53 2011 -0500 (2011-04-23)
changeset 3261 e4359292e59a
parent 2019 9f473114a511
permissions -rw-r--r--
Added detection for clang compiler and fixed various warnings triggered by it (bug 4878, r=dvander).
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.IO;
     5 
     6 namespace incparser
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             Environment.Exit(SubMain(args));
    13         }
    14 
    15         static int SubMain(string[] args)
    16         {
    17             string directory = ".";
    18             string template = "template.txt";
    19             string outputfile = "output.txt";
    20             string file = null;
    21 
    22             if (args.Length == 0 || (args.Length == 1 && args[0] == "-h"))
    23             {
    24                 PrintHelp();
    25                 return 0;
    26             }
    27 
    28             for (int i=0; i<args.Length-1; i++)
    29             {
    30                 if (args[i] == "-d")
    31                 {
    32                     directory = args[i + 1];
    33                 }
    34 
    35                 if (args[i] == "-t")
    36                 {
    37                     template = args[i + 1];
    38                 }
    39 
    40                 if (args[i] == "-o")
    41                 {
    42                     outputfile = args[i + 1];
    43                 }
    44 
    45                 if (args[i] == "-f")
    46                 {
    47                     file = args[i + 1];
    48                 }
    49 
    50                 if (args[i] == "-h")
    51                 {
    52                     if (args[i + 1] == "template")
    53                     {
    54                         PrintTemplateHelp();
    55                         return 0;
    56                     }
    57                     PrintHelp();
    58                     return 0;
    59                 }
    60             }
    61 
    62             IncParser inc = null;
    63 
    64             if (file == null)
    65             {
    66                 DirectoryInfo di = new DirectoryInfo(directory);
    67                 FileInfo[] rgFiles = di.GetFiles("*.inc");
    68 
    69                 ParseWriter pwr = new ParseWriter();
    70 
    71                 foreach (FileInfo fi in rgFiles)
    72                 {
    73                     pwr.Reset();
    74 
    75                     Console.Write("Parsing file: " + fi.ToString() + "... ");
    76                   
    77                     try
    78                     {
    79                       inc = new IncParser(fi.FullName);
    80                     }
    81                     catch (ParseException e)
    82                     {
    83                         Console.WriteLine("Initial browsing failed: " + e.Message);
    84                         continue;
    85                     }
    86                     catch (System.Exception e)
    87                     {
    88                         Console.WriteLine("Failed to read file: " + e.Message);
    89                         continue;
    90                     }
    91 
    92                     try
    93                     {
    94                         inc.Parse(pwr);
    95                     }
    96                     catch (System.Exception e)
    97                     {
    98                         Console.WriteLine("Error parsing file (line " + inc.GetLineNumber() + "): " + e.Message);
    99                         continue;
   100                     }
   101 
   102                     if (pwr.Level != 0)
   103                     {
   104                         Console.WriteLine("Fatal parse error detected; unable to complete output.");
   105                         continue;
   106                     }
   107 
   108                     Console.WriteLine("Complete!");
   109                  }
   110 
   111                  pwr.WriteFiles(template, outputfile);
   112 
   113                  Console.WriteLine("Parsing Complete!");
   114 
   115                  return 0;
   116             }
   117       
   118             try
   119             {
   120                 inc = new IncParser(file);
   121             }
   122             catch (ParseException e)
   123             {
   124                 Console.WriteLine("Initial browsing failed: " + e.Message);
   125                 return 1;
   126             }
   127             catch (System.Exception e)
   128             {
   129                 Console.WriteLine("Failed to read file: " + e.Message);
   130                 return 1;
   131             }
   132 
   133             ParseWriter pw = new ParseWriter();
   134 
   135             try
   136             {
   137                 inc.Parse(pw);
   138             }
   139             catch (System.Exception e)
   140             {
   141                 Console.WriteLine("Error parsing file (line " + inc.GetLineNumber() + "): " + e.Message);
   142                 return 1;
   143             }
   144 
   145             if (pw.Level != 0)
   146             {
   147                 Console.WriteLine("Fatal parse error detected; unable to complete output.");
   148                 return 1;
   149             }
   150 
   151             Console.Write(pw.Contents);
   152             Console.Write("\n");
   153 
   154             return 0;
   155         }
   156 
   157         static void PrintHelp()
   158         {
   159             Console.WriteLine("SourcePawn include file parser by BAILOPAN (edited by pRED*)");
   160             Console.Write("\n");
   161             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)");
   162             Console.Write("\n");
   163             Console.WriteLine("Parameters:");
   164             Console.Write("\n");
   165             Console.WriteLine("-f <filename> - Specify an input file to be used");
   166             Console.WriteLine("-d <path> - Specify a directory to parse (only *.inc files are used)");
   167             Console.WriteLine("-t <filename> - Specify a template file to be used");
   168             Console.WriteLine("-o <filename> - Specify an output file to be used");
   169             Console.WriteLine("-h - Display this help");
   170             Console.WriteLine("-h template - Displays help about templates");
   171         }
   172 
   173         static void PrintTemplateHelp()
   174         {
   175             Console.WriteLine("Template File Help:");
   176             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");
   177             Console.Write("\n");
   178             Console.WriteLine("Variables:");
   179             Console.Write("\n");
   180             Console.WriteLine("$defines $enums $enumtypes $forwards $natives $stocks $funcenums $functags $structs");
   181         }
   182     }
   183 }