Incparser upgrades.
authorMatt Woodrow <pred@alliedmods.net>
Mon Apr 21 21:41:38 2008 +0000 (2008-04-21)
changeset 20199f473114a511
parent 2018 9466b2246fd3
child 2020 87ea1cf0fe76
Incparser upgrades.
tools/incparser/IncParser.cs
tools/incparser/ParseWriter.cs
tools/incparser/Program.cs
tools/incparser/incparser.csproj
tools/incparser/incparser.sln
     1.1 --- a/tools/incparser/IncParser.cs	Sat Apr 19 10:35:24 2008 +0000
     1.2 +++ b/tools/incparser/IncParser.cs	Mon Apr 21 21:41:38 2008 +0000
     1.3 @@ -133,7 +133,7 @@
     1.4              Initialize();
     1.5          }
     1.6  
     1.7 -        public void Parse(ParseWriter w)
     1.8 +         public void Parse(ParseWriter w)
     1.9          {
    1.10              w.BeginSection(System.IO.Path.GetFileName(FileName));
    1.11              LexToken tok = LexToken.TOKEN_NONE;
    1.12 @@ -198,7 +198,12 @@
    1.13                          }
    1.14                      default:
    1.15                          {
    1.16 -                            throw new ParseException("Unrecognized token: " + tok);
    1.17 +                            if (_LexString == "static" || _LexString == "new")
    1.18 +                            {
    1.19 +                                PARSE_Public(w);
    1.20 +                                break;
    1.21 +                            }
    1.22 +                            throw new ParseException("Unrecognized token: " + tok + " " + _LexString);
    1.23                          }
    1.24                  }
    1.25              }
    1.26 @@ -207,16 +212,29 @@
    1.27  
    1.28          private void PARSE_Public(ParseWriter w)
    1.29          {
    1.30 +            MatchToken(LexToken.TOKEN_CONST);
    1.31 +
    1.32              /* Eat up an optional tag */
    1.33              MatchToken(LexToken.TOKEN_LABEL);
    1.34 -            
    1.35 +
    1.36              /* Eat up a name */
    1.37              NeedToken(LexToken.TOKEN_SYMBOL);
    1.38  
    1.39 +            while (MatchChar('['))
    1.40 +            {
    1.41 +                DiscardUntilChar(']');
    1.42 +                NeedChar(']');
    1.43 +            }
    1.44 +
    1.45              if (MatchChar('='))
    1.46              {
    1.47                  ignore_block();
    1.48              }
    1.49 +            else if (MatchChar('(') && MatchChar(')'))
    1.50 +            {
    1.51 +                ignore_block();
    1.52 +                return;
    1.53 +            }
    1.54  
    1.55              NeedChar(';');
    1.56          }
    1.57 @@ -225,6 +243,7 @@
    1.58          {
    1.59              /* For now, we completely ignore these (they're not written to the output) */
    1.60              NeedToken(LexToken.TOKEN_SYMBOL);
    1.61 +            w.structList.Add(LexString);
    1.62              NeedChar('{');
    1.63  
    1.64              bool need_closebrace = true;
    1.65 @@ -294,6 +313,7 @@
    1.66              /* Get the functag name */
    1.67              NeedToken(LexToken.TOKEN_SYMBOL);
    1.68              w.WritePair("name", LexString);
    1.69 +            w.funcenumList.Add(LexString);
    1.70  
    1.71              NeedChar('{');
    1.72  
    1.73 @@ -359,6 +379,7 @@
    1.74              /* Get the functag name */
    1.75              NeedToken(LexToken.TOKEN_SYMBOL);
    1.76              w.WritePair("name", LexString);
    1.77 +            w.functagList.Add(LexString);
    1.78  
    1.79              if (MatchToken(LexToken.TOKEN_LABEL))
    1.80              {
    1.81 @@ -422,14 +443,17 @@
    1.82              if (tok == LexToken.TOKEN_FORWARD)
    1.83              {
    1.84                  w.BeginSection("forward");
    1.85 +                w.forwardList.Add(name);
    1.86              }
    1.87              else if (tok == LexToken.TOKEN_NATIVE)
    1.88              {
    1.89                  w.BeginSection("native");
    1.90 +                w.nativeList.Add(name);
    1.91              }
    1.92              else if (tok == LexToken.TOKEN_STOCK)
    1.93              {
    1.94                  w.BeginSection("stock");
    1.95 +                w.stockList.Add(name);
    1.96              }
    1.97  
    1.98              w.WritePair("name", name);
    1.99 @@ -571,9 +595,17 @@
   1.100              w.WritePair("name", name);
   1.101              w.BeginSection("properties");
   1.102  
   1.103 +            w.enumList.Add(name);
   1.104 +
   1.105              NeedChar('{');
   1.106              bool more_entries = true;
   1.107              bool need_closebrace = true;
   1.108 +
   1.109 +            if (MatchToken(LexToken.TOKEN_DOCBLOCK))
   1.110 +            {
   1.111 +              w.WritePair("doc", LexString);
   1.112 +            }
   1.113 +
   1.114              do
   1.115              {
   1.116                  if (MatchChar('}'))
   1.117 @@ -583,12 +615,14 @@
   1.118                  }
   1.119                  NeedToken(LexToken.TOKEN_SYMBOL);
   1.120                  name = LexString;
   1.121 + 
   1.122                  if (MatchChar('='))
   1.123                  {
   1.124 -                    DiscardUntilCharOrComment(',', true);
   1.125 +                    DiscardUntilCharOrComment(",\n}", true);
   1.126                  }
   1.127                  more_entries = MatchChar(',');
   1.128                  w.WritePair("name", name);
   1.129 +                w.enumTypeList.Add(name);
   1.130                  if (MatchToken(LexToken.TOKEN_DOCBLOCK))
   1.131                  {
   1.132                      w.WritePair("doc", LexString);
   1.133 @@ -637,6 +671,8 @@
   1.134              }
   1.135  
   1.136              /* Write */
   1.137 +            w.defineList.Add(name);
   1.138 +
   1.139              w.BeginSection("define");
   1.140              w.WritePair("name", name);
   1.141              w.WritePair("linetext", value);
   1.142 @@ -721,6 +757,37 @@
   1.143              }
   1.144          }
   1.145  
   1.146 +       /**
   1.147 +        * Discards all data up to a certain character, ignoring
   1.148 +        * the contents, unless specified to stop at a doc block comment.
   1.149 +        */
   1.150 +        private void DiscardUntilCharOrComment(string s, bool stop_at_comment)
   1.151 +        {
   1.152 +          for (int i = 0; i < Contents.Length; i++)
   1.153 +          {
   1.154 +            if (Contents[i] == '\n')
   1.155 +            {
   1.156 +              LineNo++;
   1.157 +            }
   1.158 +            else if (s.Contains(Contents[i].ToString()))
   1.159 +            {
   1.160 +              if (i > 0)
   1.161 +              {
   1.162 +                Contents.Remove(0, i);
   1.163 +              }
   1.164 +              return;
   1.165 +            }
   1.166 +            else if (stop_at_comment && CheckString(i, "/**"))
   1.167 +            {
   1.168 +              if (i > 0)
   1.169 +              {
   1.170 +                Contents.Remove(0, i);
   1.171 +              }
   1.172 +              return;
   1.173 +            }
   1.174 +          }
   1.175 +        }
   1.176 +
   1.177          /**
   1.178           * Finds the end of an expression
   1.179           */
   1.180 @@ -938,7 +1005,7 @@
   1.181              bool is_doc = false;
   1.182              bool is_newline = true;
   1.183  
   1.184 -            for (int i=0; i<Contents.Length-1; i++)
   1.185 +            for (int i=0; i<Contents.Length; i++)
   1.186              {
   1.187                  if (Contents[i] == '\n')
   1.188                  {
   1.189 @@ -953,6 +1020,7 @@
   1.190                              in_comment = true;
   1.191                              is_ml = false;
   1.192                              is_doc = false;
   1.193 +                            is_newline = false;
   1.194                          } else if (Contents[i+1] == '*') {
   1.195                              /* Detect whether this is a doc comment or not */
   1.196                              if (Contents.Length - i >= 3
     2.1 --- a/tools/incparser/ParseWriter.cs	Sat Apr 19 10:35:24 2008 +0000
     2.2 +++ b/tools/incparser/ParseWriter.cs	Mon Apr 21 21:41:38 2008 +0000
     2.3 @@ -1,6 +1,8 @@
     2.4  using System;
     2.5 +using System.Collections;
     2.6  using System.Collections.Generic;
     2.7  using System.Text;
     2.8 +using System.IO;
     2.9  
    2.10  namespace incparser
    2.11  {
    2.12 @@ -9,6 +11,16 @@
    2.13          private int level;
    2.14          private StringBuilder data;
    2.15  
    2.16 +        public ArrayList enumList = new ArrayList();
    2.17 +        public ArrayList defineList = new ArrayList();
    2.18 +        public ArrayList enumTypeList = new ArrayList();
    2.19 +        public ArrayList forwardList = new ArrayList();
    2.20 +        public ArrayList nativeList = new ArrayList();
    2.21 +        public ArrayList stockList = new ArrayList();
    2.22 +        public ArrayList funcenumList = new ArrayList();
    2.23 +        public ArrayList functagList = new ArrayList();
    2.24 +        public ArrayList structList = new ArrayList();
    2.25 +
    2.26          public int Level
    2.27          {
    2.28              get
    2.29 @@ -25,6 +37,12 @@
    2.30              }
    2.31          }
    2.32  
    2.33 +        public void Reset()
    2.34 +        {
    2.35 +            level = 0;
    2.36 +            data = new StringBuilder();
    2.37 +        }
    2.38 +
    2.39          public ParseWriter()
    2.40          {
    2.41              level = 0;
    2.42 @@ -52,6 +70,79 @@
    2.43              WriteLine("}");
    2.44          }
    2.45  
    2.46 +        public void WriteFiles(string template, string outputfile)
    2.47 +        {
    2.48 +            StreamReader sr = null;
    2.49 +
    2.50 +            try
    2.51 +            {
    2.52 +                sr = File.OpenText(template);
    2.53 +            }
    2.54 +            catch (Exception e)
    2.55 +            {
    2.56 +                Console.WriteLine("Failed to open template file: " + e.Message);
    2.57 +                return;
    2.58 +            }
    2.59 +
    2.60 +            string contents = sr.ReadToEnd();
    2.61 +
    2.62 +            string replace = ToOutputString(defineList);
    2.63 +            contents = contents.Replace("$defines", replace);
    2.64 +
    2.65 +            replace = ToOutputString(enumList);
    2.66 +            contents = contents.Replace("$enums", replace);
    2.67 +
    2.68 +            replace = ToOutputString(enumTypeList);
    2.69 +            contents = contents.Replace("$enumtypes", replace);
    2.70 +
    2.71 +            replace = ToOutputString(forwardList);
    2.72 +            contents = contents.Replace("$forwards", replace);
    2.73 +
    2.74 +            replace = ToOutputString(nativeList);
    2.75 +            contents = contents.Replace("$natives", replace);
    2.76 +
    2.77 +            replace = ToOutputString(stockList);
    2.78 +            contents = contents.Replace("$stocks", replace);
    2.79 +
    2.80 +            replace = ToOutputString(funcenumList);
    2.81 +            contents = contents.Replace("$funcenums", replace);
    2.82 +
    2.83 +            replace = ToOutputString(functagList);
    2.84 +            contents = contents.Replace("$functags", replace);
    2.85 +
    2.86 +            replace = ToOutputString(structList);
    2.87 +            contents = contents.Replace("$structs", replace);
    2.88 +
    2.89 +            StreamWriter sw;
    2.90 +            sw = File.CreateText(outputfile);
    2.91 +
    2.92 +            sw.Write(contents);
    2.93 +
    2.94 +            sr.Close();
    2.95 +            sw.Close();
    2.96 +        }
    2.97 +
    2.98 +        private string ToOutputString(ArrayList a)
    2.99 +        {
   2.100 +            string defines = "";
   2.101 +            int count = 0;
   2.102 +
   2.103 +            foreach (object o in a)
   2.104 +            {
   2.105 +              defines += o;
   2.106 +              defines += " ";
   2.107 +              count += o.ToString().Length;
   2.108 +
   2.109 +              if (count > 180)
   2.110 +              {
   2.111 +                defines += "\r\n";
   2.112 +                count = 0;
   2.113 +              }
   2.114 +            }
   2.115 +
   2.116 +            return defines;
   2.117 +        }
   2.118 +
   2.119          private void WriteLine(string line)
   2.120          {
   2.121              Tabinate();
     3.1 --- a/tools/incparser/Program.cs	Sat Apr 19 10:35:24 2008 +0000
     3.2 +++ b/tools/incparser/Program.cs	Mon Apr 21 21:41:38 2008 +0000
     3.3 @@ -1,6 +1,7 @@
     3.4  using System;
     3.5  using System.Collections.Generic;
     3.6  using System.Text;
     3.7 +using System.IO;
     3.8  
     3.9  namespace incparser
    3.10  {
    3.11 @@ -13,16 +14,110 @@
    3.12  
    3.13          static int SubMain(string[] args)
    3.14          {
    3.15 -            if (args.Length < 1)
    3.16 +            string directory = ".";
    3.17 +            string template = "template.txt";
    3.18 +            string outputfile = "output.txt";
    3.19 +            string file = null;
    3.20 +
    3.21 +            if (args.Length == 0 || (args.Length == 1 && args[0] == "-h"))
    3.22 +            {
    3.23 +                PrintHelp();
    3.24 +                return 0;
    3.25 +            }
    3.26 +
    3.27 +            for (int i=0; i<args.Length-1; i++)
    3.28              {
    3.29 -                Console.WriteLine("Usage: incparser <infile>");
    3.30 -                return 1;
    3.31 +                if (args[i] == "-d")
    3.32 +                {
    3.33 +                    directory = args[i + 1];
    3.34 +                }
    3.35 +
    3.36 +                if (args[i] == "-t")
    3.37 +                {
    3.38 +                    template = args[i + 1];
    3.39 +                }
    3.40 +
    3.41 +                if (args[i] == "-o")
    3.42 +                {
    3.43 +                    outputfile = args[i + 1];
    3.44 +                }
    3.45 +
    3.46 +                if (args[i] == "-f")
    3.47 +                {
    3.48 +                    file = args[i + 1];
    3.49 +                }
    3.50 +
    3.51 +                if (args[i] == "-h")
    3.52 +                {
    3.53 +                    if (args[i + 1] == "template")
    3.54 +                    {
    3.55 +                        PrintTemplateHelp();
    3.56 +                        return 0;
    3.57 +                    }
    3.58 +                    PrintHelp();
    3.59 +                    return 0;
    3.60 +                }
    3.61              }
    3.62  
    3.63              IncParser inc = null;
    3.64 +
    3.65 +            if (file == null)
    3.66 +            {
    3.67 +                DirectoryInfo di = new DirectoryInfo(directory);
    3.68 +                FileInfo[] rgFiles = di.GetFiles("*.inc");
    3.69 +
    3.70 +                ParseWriter pwr = new ParseWriter();
    3.71 +
    3.72 +                foreach (FileInfo fi in rgFiles)
    3.73 +                {
    3.74 +                    pwr.Reset();
    3.75 +
    3.76 +                    Console.Write("Parsing file: " + fi.ToString() + "... ");
    3.77 +                  
    3.78 +                    try
    3.79 +                    {
    3.80 +                      inc = new IncParser(fi.FullName);
    3.81 +                    }
    3.82 +                    catch (ParseException e)
    3.83 +                    {
    3.84 +                        Console.WriteLine("Initial browsing failed: " + e.Message);
    3.85 +                        continue;
    3.86 +                    }
    3.87 +                    catch (System.Exception e)
    3.88 +                    {
    3.89 +                        Console.WriteLine("Failed to read file: " + e.Message);
    3.90 +                        continue;
    3.91 +                    }
    3.92 +
    3.93 +                    try
    3.94 +                    {
    3.95 +                        inc.Parse(pwr);
    3.96 +                    }
    3.97 +                    catch (System.Exception e)
    3.98 +                    {
    3.99 +                        Console.WriteLine("Error parsing file (line " + inc.GetLineNumber() + "): " + e.Message);
   3.100 +                        continue;
   3.101 +                    }
   3.102 +
   3.103 +                    if (pwr.Level != 0)
   3.104 +                    {
   3.105 +                        Console.WriteLine("Fatal parse error detected; unable to complete output.");
   3.106 +                        continue;
   3.107 +                    }
   3.108 +
   3.109 +                    Console.WriteLine("Complete!");
   3.110 +                 }
   3.111 +
   3.112 +                 pwr.WriteFiles(template, outputfile);
   3.113 +
   3.114 +                 Console.WriteLine("Parsing Complete!");
   3.115 +
   3.116 +                 return 0;
   3.117 +            }
   3.118 +      
   3.119              try
   3.120              {
   3.121 -                inc = new IncParser(args[0]);
   3.122 +                inc = new IncParser(file);
   3.123              }
   3.124              catch (ParseException e)
   3.125              {
   3.126 @@ -58,5 +153,31 @@
   3.127  
   3.128              return 0;
   3.129          }
   3.130 +
   3.131 +        static void PrintHelp()
   3.132 +        {
   3.133 +            Console.WriteLine("SourcePawn include file parser by BAILOPAN (edited by pRED*)");
   3.134 +            Console.Write("\n");
   3.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)");
   3.136 +            Console.Write("\n");
   3.137 +            Console.WriteLine("Parameters:");
   3.138 +            Console.Write("\n");
   3.139 +            Console.WriteLine("-f <filename> - Specify an input file to be used");
   3.140 +            Console.WriteLine("-d <path> - Specify a directory to parse (only *.inc files are used)");
   3.141 +            Console.WriteLine("-t <filename> - Specify a template file to be used");
   3.142 +            Console.WriteLine("-o <filename> - Specify an output file to be used");
   3.143 +            Console.WriteLine("-h - Display this help");
   3.144 +            Console.WriteLine("-h template - Displays help about templates");
   3.145 +        }
   3.146 +
   3.147 +        static void PrintTemplateHelp()
   3.148 +        {
   3.149 +            Console.WriteLine("Template File Help:");
   3.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");
   3.151 +            Console.Write("\n");
   3.152 +            Console.WriteLine("Variables:");
   3.153 +            Console.Write("\n");
   3.154 +            Console.WriteLine("$defines $enums $enumtypes $forwards $natives $stocks $funcenums $functags $structs");
   3.155 +        }
   3.156      }
   3.157  }
     4.1 --- a/tools/incparser/incparser.csproj	Sat Apr 19 10:35:24 2008 +0000
     4.2 +++ b/tools/incparser/incparser.csproj	Mon Apr 21 21:41:38 2008 +0000
     4.3 @@ -1,14 +1,20 @@
     4.4 -<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     4.5 +<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
     4.6    <PropertyGroup>
     4.7      <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     4.8      <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
     4.9 -    <ProductVersion>8.0.50727</ProductVersion>
    4.10 +    <ProductVersion>9.0.21022</ProductVersion>
    4.11      <SchemaVersion>2.0</SchemaVersion>
    4.12      <ProjectGuid>{064EA9DC-51DC-4EE5-843B-0E3F7069635E}</ProjectGuid>
    4.13      <OutputType>Exe</OutputType>
    4.14      <AppDesignerFolder>Properties</AppDesignerFolder>
    4.15      <RootNamespace>incparser</RootNamespace>
    4.16      <AssemblyName>incparser</AssemblyName>
    4.17 +    <FileUpgradeFlags>
    4.18 +    </FileUpgradeFlags>
    4.19 +    <UpgradeBackupLocation>
    4.20 +    </UpgradeBackupLocation>
    4.21 +    <OldToolsVersion>2.0</OldToolsVersion>
    4.22 +    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    4.23    </PropertyGroup>
    4.24    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    4.25      <DebugSymbols>true</DebugSymbols>
     5.1 --- a/tools/incparser/incparser.sln	Sat Apr 19 10:35:24 2008 +0000
     5.2 +++ b/tools/incparser/incparser.sln	Mon Apr 21 21:41:38 2008 +0000
     5.3 @@ -1,6 +1,6 @@
     5.4  
     5.5 -Microsoft Visual Studio Solution File, Format Version 9.00
     5.6 -# Visual Studio 2005
     5.7 +Microsoft Visual Studio Solution File, Format Version 10.00
     5.8 +# Visual C# Express 2008
     5.9  Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "incparser", "incparser.csproj", "{064EA9DC-51DC-4EE5-843B-0E3F7069635E}"
    5.10  EndProject
    5.11  Global