diff --git a/tools/incparser/IncParser.cs b/tools/incparser/IncParser.cs index f417b8a3..94010d04 100644 --- a/tools/incparser/IncParser.cs +++ b/tools/incparser/IncParser.cs @@ -133,7 +133,7 @@ namespace incparser Initialize(); } - public void Parse(ParseWriter w) + public void Parse(ParseWriter w) { w.BeginSection(System.IO.Path.GetFileName(FileName)); LexToken tok = LexToken.TOKEN_NONE; @@ -198,7 +198,12 @@ namespace incparser } default: { - throw new ParseException("Unrecognized token: " + tok); + if (_LexString == "static" || _LexString == "new") + { + PARSE_Public(w); + break; + } + throw new ParseException("Unrecognized token: " + tok + " " + _LexString); } } } @@ -207,16 +212,29 @@ namespace incparser private void PARSE_Public(ParseWriter w) { + MatchToken(LexToken.TOKEN_CONST); + /* Eat up an optional tag */ MatchToken(LexToken.TOKEN_LABEL); - + /* Eat up a name */ NeedToken(LexToken.TOKEN_SYMBOL); + while (MatchChar('[')) + { + DiscardUntilChar(']'); + NeedChar(']'); + } + if (MatchChar('=')) { ignore_block(); } + else if (MatchChar('(') && MatchChar(')')) + { + ignore_block(); + return; + } NeedChar(';'); } @@ -225,6 +243,7 @@ namespace incparser { /* For now, we completely ignore these (they're not written to the output) */ NeedToken(LexToken.TOKEN_SYMBOL); + w.structList.Add(LexString); NeedChar('{'); bool need_closebrace = true; @@ -294,6 +313,7 @@ namespace incparser /* Get the functag name */ NeedToken(LexToken.TOKEN_SYMBOL); w.WritePair("name", LexString); + w.funcenumList.Add(LexString); NeedChar('{'); @@ -359,6 +379,7 @@ namespace incparser /* Get the functag name */ NeedToken(LexToken.TOKEN_SYMBOL); w.WritePair("name", LexString); + w.functagList.Add(LexString); if (MatchToken(LexToken.TOKEN_LABEL)) { @@ -422,14 +443,17 @@ namespace incparser if (tok == LexToken.TOKEN_FORWARD) { w.BeginSection("forward"); + w.forwardList.Add(name); } else if (tok == LexToken.TOKEN_NATIVE) { w.BeginSection("native"); + w.nativeList.Add(name); } else if (tok == LexToken.TOKEN_STOCK) { w.BeginSection("stock"); + w.stockList.Add(name); } w.WritePair("name", name); @@ -571,9 +595,17 @@ namespace incparser w.WritePair("name", name); w.BeginSection("properties"); + w.enumList.Add(name); + NeedChar('{'); bool more_entries = true; bool need_closebrace = true; + + if (MatchToken(LexToken.TOKEN_DOCBLOCK)) + { + w.WritePair("doc", LexString); + } + do { if (MatchChar('}')) @@ -583,12 +615,14 @@ namespace incparser } NeedToken(LexToken.TOKEN_SYMBOL); name = LexString; + if (MatchChar('=')) { - DiscardUntilCharOrComment(',', true); + DiscardUntilCharOrComment(",\n}", true); } more_entries = MatchChar(','); w.WritePair("name", name); + w.enumTypeList.Add(name); if (MatchToken(LexToken.TOKEN_DOCBLOCK)) { w.WritePair("doc", LexString); @@ -637,6 +671,8 @@ namespace incparser } /* Write */ + w.defineList.Add(name); + w.BeginSection("define"); w.WritePair("name", name); w.WritePair("linetext", value); @@ -721,6 +757,37 @@ namespace incparser } } + /** + * Discards all data up to a certain character, ignoring + * the contents, unless specified to stop at a doc block comment. + */ + private void DiscardUntilCharOrComment(string s, bool stop_at_comment) + { + for (int i = 0; i < Contents.Length; i++) + { + if (Contents[i] == '\n') + { + LineNo++; + } + else if (s.Contains(Contents[i].ToString())) + { + if (i > 0) + { + Contents.Remove(0, i); + } + return; + } + else if (stop_at_comment && CheckString(i, "/**")) + { + if (i > 0) + { + Contents.Remove(0, i); + } + return; + } + } + } + /** * Finds the end of an expression */ @@ -938,7 +1005,7 @@ namespace incparser bool is_doc = false; bool is_newline = true; - for (int i=0; i= 3 diff --git a/tools/incparser/ParseWriter.cs b/tools/incparser/ParseWriter.cs index d8943978..fdddd1cd 100644 --- a/tools/incparser/ParseWriter.cs +++ b/tools/incparser/ParseWriter.cs @@ -1,6 +1,8 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Text; +using System.IO; namespace incparser { @@ -9,6 +11,16 @@ namespace incparser private int level; private StringBuilder data; + public ArrayList enumList = new ArrayList(); + public ArrayList defineList = new ArrayList(); + public ArrayList enumTypeList = new ArrayList(); + public ArrayList forwardList = new ArrayList(); + public ArrayList nativeList = new ArrayList(); + public ArrayList stockList = new ArrayList(); + public ArrayList funcenumList = new ArrayList(); + public ArrayList functagList = new ArrayList(); + public ArrayList structList = new ArrayList(); + public int Level { get @@ -25,6 +37,12 @@ namespace incparser } } + public void Reset() + { + level = 0; + data = new StringBuilder(); + } + public ParseWriter() { level = 0; @@ -52,6 +70,79 @@ namespace incparser WriteLine("}"); } + public void WriteFiles(string template, string outputfile) + { + StreamReader sr = null; + + try + { + sr = File.OpenText(template); + } + catch (Exception e) + { + Console.WriteLine("Failed to open template file: " + e.Message); + return; + } + + string contents = sr.ReadToEnd(); + + string replace = ToOutputString(defineList); + contents = contents.Replace("$defines", replace); + + replace = ToOutputString(enumList); + contents = contents.Replace("$enums", replace); + + replace = ToOutputString(enumTypeList); + contents = contents.Replace("$enumtypes", replace); + + replace = ToOutputString(forwardList); + contents = contents.Replace("$forwards", replace); + + replace = ToOutputString(nativeList); + contents = contents.Replace("$natives", replace); + + replace = ToOutputString(stockList); + contents = contents.Replace("$stocks", replace); + + replace = ToOutputString(funcenumList); + contents = contents.Replace("$funcenums", replace); + + replace = ToOutputString(functagList); + contents = contents.Replace("$functags", replace); + + replace = ToOutputString(structList); + contents = contents.Replace("$structs", replace); + + StreamWriter sw; + sw = File.CreateText(outputfile); + + sw.Write(contents); + + sr.Close(); + sw.Close(); + } + + private string ToOutputString(ArrayList a) + { + string defines = ""; + int count = 0; + + foreach (object o in a) + { + defines += o; + defines += " "; + count += o.ToString().Length; + + if (count > 180) + { + defines += "\r\n"; + count = 0; + } + } + + return defines; + } + private void WriteLine(string line) { Tabinate(); diff --git a/tools/incparser/Program.cs b/tools/incparser/Program.cs index 6e4e5293..0553d8ea 100644 --- a/tools/incparser/Program.cs +++ b/tools/incparser/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using System.IO; namespace incparser { @@ -13,16 +14,110 @@ namespace incparser static int SubMain(string[] args) { - if (args.Length < 1) + string directory = "."; + string template = "template.txt"; + string outputfile = "output.txt"; + string file = null; + + if (args.Length == 0 || (args.Length == 1 && args[0] == "-h")) { - Console.WriteLine("Usage: incparser "); - return 1; + PrintHelp(); + return 0; + } + + for (int i=0; i - Specify an input file to be used"); + Console.WriteLine("-d - Specify a directory to parse (only *.inc files are used)"); + Console.WriteLine("-t - Specify a template file to be used"); + Console.WriteLine("-o - Specify an output file to be used"); + Console.WriteLine("-h - Display this help"); + Console.WriteLine("-h template - Displays help about templates"); + } + + static void PrintTemplateHelp() + { + Console.WriteLine("Template File Help:"); + 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"); + Console.Write("\n"); + Console.WriteLine("Variables:"); + Console.Write("\n"); + Console.WriteLine("$defines $enums $enumtypes $forwards $natives $stocks $funcenums $functags $structs"); + } } } diff --git a/tools/incparser/incparser.csproj b/tools/incparser/incparser.csproj index fc14f352..88a8cc31 100644 --- a/tools/incparser/incparser.csproj +++ b/tools/incparser/incparser.csproj @@ -1,14 +1,20 @@ - + Debug AnyCPU - 8.0.50727 + 9.0.21022 2.0 {064EA9DC-51DC-4EE5-843B-0E3F7069635E} Exe Properties incparser incparser + + + + + 2.0 + v3.5 true diff --git a/tools/incparser/incparser.sln b/tools/incparser/incparser.sln index 1db21f97..570e60b1 100644 --- a/tools/incparser/incparser.sln +++ b/tools/incparser/incparser.sln @@ -1,6 +1,6 @@  -Microsoft Visual Studio Solution File, Format Version 9.00 -# Visual Studio 2005 +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual C# Express 2008 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "incparser", "incparser.csproj", "{064EA9DC-51DC-4EE5-843B-0E3F7069635E}" EndProject Global