- Added GLParser for OpenGL .spec files. Only supports enums right now.
- Modified ESCLParser and GLParser to inherit from Parser.
This commit is contained in:
parent
93743f913d
commit
edd686d606
4 changed files with 122 additions and 17 deletions
|
@ -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?<?<?\s?-?\w*\)?$", RegexOptions.Compiled);
|
||||
|
||||
public ESCLParser()
|
||||
{
|
||||
}
|
||||
|
||||
public string Prefix {get; set;}
|
||||
public string Version {get; set;}
|
||||
|
||||
public IEnumerable<XElement> Parse(string filename)
|
||||
{
|
||||
return Parse(File.ReadAllLines(filename));
|
||||
}
|
||||
|
||||
public IEnumerable<XElement> Parse(string[] lines)
|
||||
public override IEnumerable<XElement> Parse(string[] lines)
|
||||
{
|
||||
char[] splitters = new char[] { ' ', '\t', ',', '(', ')', ';', '\n', '\r' };
|
||||
|
||||
|
|
106
Source/Converter/GLParser.cs
Normal file
106
Source/Converter/GLParser.cs
Normal file
|
@ -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<XElement> 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:"));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -156,6 +156,7 @@
|
|||
<Compile Include="Properties\AssemblyInfo.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="GLParser.cs" />
|
||||
<None Include="..\..\OpenTK.snk">
|
||||
<Link>OpenTK.snk</Link>
|
||||
</None>
|
||||
|
|
|
@ -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<XElement> Parse(string[] lines);
|
||||
|
||||
public IEnumerable<XElement> Parse(string path)
|
||||
{
|
||||
return Parse(File.ReadAllLines(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue