From edd686d606a5a49b70e7195be3e033cce233157f Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sun, 10 Oct 2010 20:21:32 +0000 Subject: [PATCH] - Added GLParser for OpenGL .spec files. Only supports enums right now. - Modified ESCLParser and GLParser to inherit from Parser. --- Source/Converter/ESCLParser.cs | 16 +--- Source/Converter/GLParser.cs | 106 ++++++++++++++++++++++ Source/Converter/Generator.Convert.csproj | 1 + Source/Converter/Parser.cs | 16 +++- 4 files changed, 122 insertions(+), 17 deletions(-) create mode 100644 Source/Converter/GLParser.cs diff --git a/Source/Converter/ESCLParser.cs b/Source/Converter/ESCLParser.cs index c8a8adec..395d391a 100644 --- a/Source/Converter/ESCLParser.cs +++ b/Source/Converter/ESCLParser.cs @@ -36,25 +36,13 @@ namespace CHeaderToXML // Todo: Fails to parse ES extension headers, which mix enum and function definitions. // Parses ES and CL header files. - sealed class ESCLParser + sealed class ESCLParser : Parser { Regex extensions = new Regex("(ARB|EXT|AMD|NV|OES|QCOM)", RegexOptions.RightToLeft | RegexOptions.Compiled); Regex array_size = new Regex(@"\[.+\]", RegexOptions.RightToLeft | RegexOptions.Compiled); Regex EnumToken = new Regex(@"^#define \w+\s+\(?-?\w+\s? Parse(string filename) - { - return Parse(File.ReadAllLines(filename)); - } - - public IEnumerable Parse(string[] lines) + public override IEnumerable Parse(string[] lines) { char[] splitters = new char[] { ' ', '\t', ',', '(', ')', ';', '\n', '\r' }; diff --git a/Source/Converter/GLParser.cs b/Source/Converter/GLParser.cs new file mode 100644 index 00000000..c14ebf8d --- /dev/null +++ b/Source/Converter/GLParser.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; + +namespace CHeaderToXML +{ + class GLParser : Parser + { + static readonly char[] splitters = new char[] { ' ', '\t', ',', '(', ')', ';', '\n', '\r' }; + + enum ParserModes { None, Enum, Func }; + ParserModes CurrentMode; + + public override IEnumerable Parse(string[] lines) + { + XElement current = null; + + foreach (string l in lines) + { + string line = l.Replace('\t', ' ').Trim(); + if (!IsValid(line)) + continue; + + string[] words = SplitWords(line); + if (line.Contains("enum:")) + { + // This is a new enum definition + if (current != null) + yield return current; + + current = new XElement("enum", + new XAttribute("name", words[0])); + + CurrentMode = ParserModes.Enum; + } + else if (words.First() == "function") + { + if (current != null) + yield return current; + + CurrentMode = ParserModes.Func; + throw new NotImplementedException(); + } + else if (current != null) + { + switch (CurrentMode) + { + case ParserModes.Enum: + if (words[0] == "use") + { + current.Add(new XElement("use", + new XAttribute("enum", words[1]), + new XAttribute("token", words[2]))); + } + else if (words[1] == "=") + { + current.Add(new XElement("token", + new XAttribute("name", words[0]), + new XAttribute("value", words[2]))); + } + else + { + // Typical cause is hand-editing the specs and forgetting to add an '=' sign. + throw new InvalidOperationException(String.Format( + "[Error] Invalid constant definition: \"{0}\"", line)); + } + break; + + case ParserModes.Func: + throw new NotImplementedException(); + } + } + } + } + + string[] SplitWords(string line) + { + return line.Split(splitters, StringSplitOptions.RemoveEmptyEntries); + } + + bool IsValid(string line) + { + return !(String.IsNullOrEmpty(line) || + line.StartsWith("#") || // Disregard comments. + line.StartsWith("passthru") || // Disregard passthru statements. + line.StartsWith("required-props:") || + line.StartsWith("param:") || + line.StartsWith("dlflags:") || + line.StartsWith("glxflags:") || + line.StartsWith("vectorequiv:") || + //line.StartsWith("category:") || + line.StartsWith("version:") || + line.StartsWith("glxsingle:") || + line.StartsWith("glxropcode:") || + line.StartsWith("glxvendorpriv:") || + line.StartsWith("glsflags:") || + line.StartsWith("glsopcode:") || + line.StartsWith("glsalias:") || + line.StartsWith("wglflags:") || + line.StartsWith("extension:") || + line.StartsWith("alias:") || + line.StartsWith("offset:")); + } + } +} diff --git a/Source/Converter/Generator.Convert.csproj b/Source/Converter/Generator.Convert.csproj index dc64a57c..76a4bda5 100644 --- a/Source/Converter/Generator.Convert.csproj +++ b/Source/Converter/Generator.Convert.csproj @@ -156,6 +156,7 @@ Code + OpenTK.snk diff --git a/Source/Converter/Parser.cs b/Source/Converter/Parser.cs index cec1ac1e..4e8f336a 100644 --- a/Source/Converter/Parser.cs +++ b/Source/Converter/Parser.cs @@ -21,17 +21,27 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +using System.Collections.Generic; +using System.IO; using System.Xml.Linq; namespace CHeaderToXML { // The base class for a parser. - abstract class Parser : XDocument + abstract class Parser { // Defines a prefix that should be removed from methods and tokens in the XML files, e.g. "gl", "cl", etc. - protected string Prefix { get; set; } + public string Prefix { get; set; } + + // Defines the version of the spec files (optional). + public string Version { get; set; } // Implements the parsing logic for a specific input file. - protected abstract XDocument Parse(string[] lines); + public abstract IEnumerable Parse(string[] lines); + + public IEnumerable Parse(string path) + { + return Parse(File.ReadAllLines(path)); + } } }