GtkSharp/generator/Parser.cs
Mike Kestner b6114fef1e 2003-05-18 Mike Kestner <mkestner@speakeasy.net>
* generator/CallbackGen.cs : use non-static symtab, kill doc comments
	* generator/ClassBase.cs : use non-static symtab
	* generator/CodeGenerator.cs : use non-static symtab
	* generator/EnumGen.cs : kill doc comments, don't gen using System here
	* generator/GenBase.cs : gen using System here for all types
	* generator/InterfaceGen.cs : don't gen using System here.
	* generator/Method.cs : use non-static symtab
	* generator/ObjectGen.cs : kill doc comments, use non-static symtab
	* generator/OpaqueGen.cs : don't gen using System here.
	* generator/Parameters.cs : use non static symtab.
	* generator/Parser.cs : use non static symtab. add SimpleGen's and
	ManualGen's
	* generator/Property.cs : use non static symtab
	* generator/SignalHandler.cs : use non static symtab
	* generator/StructBase.cs : use non static symtab
	* generator/SymbolTable.cs : major refactoring. now uses SimpleGen and
	ManualGen IGeneratables to simplify the method and prop code.  Is now
	instance based with a static prop to get the singleton instance, so that
	a this indexer can be provided to access the IGeneratables nicely. Gearing
	up to remove even more code from here by accessing IGeneratables directly.

svn path=/trunk/gtk-sharp/; revision=14687
2003-05-19 02:45:17 +00:00

140 lines
3 KiB
C#

// GtkSharp.Generation.Parser.cs - The XML Parsing engine.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001 Mike Kestner
namespace GtkSharp.Generation {
using System;
using System.Collections;
using System.IO;
using System.Xml;
public class Parser {
private XmlDocument doc;
public Parser (String filename)
{
doc = new XmlDocument ();
try {
Stream stream = File.OpenRead (filename);
doc.Load (stream);
stream.Close ();
} catch (XmlException e) {
Console.WriteLine ("Invalid XML file.");
Console.WriteLine (e.ToString());
}
}
public void Parse (bool generate)
{
XmlElement root = doc.DocumentElement;
if ((root == null) || !root.HasChildNodes) {
Console.WriteLine ("No Namespaces found.");
return;
}
foreach (XmlNode ns in root.ChildNodes) {
XmlElement elem = ns as XmlElement;
if (elem == null)
continue;
if (ns.Name == "namespace")
ParseNamespace (elem, generate);
else if (ns.Name == "symbol")
ParseSymbol (elem);
}
}
private void ParseNamespace (XmlElement ns, bool generate)
{
String ns_name = ns.GetAttribute ("name");
foreach (XmlNode def in ns.ChildNodes) {
if (def.NodeType != XmlNodeType.Element) {
continue;
}
XmlElement elem = (XmlElement) def;
IGeneratable igen = null;
if (elem.HasAttribute("hidden"))
continue;
switch (def.Name) {
case "alias":
string aname = elem.GetAttribute("cname");
string atype = elem.GetAttribute("type");
if ((aname == "") || (atype == ""))
continue;
SymbolTable.Table.AddAlias (aname, atype);
break;
case "boxed":
if (elem.HasAttribute ("opaque"))
igen = new OpaqueGen (ns, elem);
else
igen = new BoxedGen (ns, elem);
break;
case "callback":
igen = new CallbackGen (ns, elem);
break;
case "enum":
igen = new EnumGen (ns, elem);
break;
case "interface":
igen = new InterfaceGen (ns, elem);
break;
case "object":
igen = new ObjectGen (ns, elem);
break;
case "struct":
if (elem.HasAttribute ("opaque"))
igen = new OpaqueGen (ns, elem);
else
igen = new StructGen (ns, elem);
break;
default:
Console.WriteLine ("Unexpected node named " + def.Name);
break;
}
if (igen != null) {
igen.DoGenerate = generate;
SymbolTable.Table.AddType (igen);
}
}
}
private void ParseSymbol (XmlElement symbol)
{
string type = symbol.GetAttribute ("type");
string cname = symbol.GetAttribute ("cname");
string name = symbol.GetAttribute ("name");
if (type == "simple")
SymbolTable.Table.AddType (new SimpleGen (cname, name));
else if (type == "manual")
SymbolTable.Table.AddType (new ManualGen (cname, name));
else
Console.WriteLine ("Unexpected symbol type " + type);
}
}
}