2002-01-05 Mike Kestner <mkestner@speakeasy.net>

* generator/*.cs : Move into GtkSharp.Generation namespace.
	* generator/CodeGenerator.cs (Main): Add usage check. Add SymbolTable.
	* generator/EnumGen.cs (QualifiedName): New.
	(Generate): Add SymbolTable to signature.
	* generator/IGeneratable : Add QualifiedName prop and update Generate
	  signature.
	* generator/Parser.cs : Switch from plain Hashtable to SymbolTable.
	(Parse): Replaces the Types property and returns a SymbolTable.
	* generator/StructBase.cs : New base class to derive struct and object
	  types. Initial implementation of protected GenField method and ctor.
	* generator/StructGen.cs : New non-object struct type generatable.
	* generator/SymbolTable.cs : New. Manages complex types hash and a
	  simple types hash.  Will provide generic lookup interface.

svn path=/trunk/gtk-sharp/; revision=1855
This commit is contained in:
Mike Kestner 2002-01-05 12:45:55 +00:00
parent 30e653825c
commit 02fa6a31e6
8 changed files with 252 additions and 43 deletions

View File

@ -1,3 +1,19 @@
2002-01-05 Mike Kestner <mkestner@speakeasy.net>
* generator/*.cs : Move into GtkSharp.Generation namespace.
* generator/CodeGenerator.cs (Main): Add usage check. Add SymbolTable.
* generator/EnumGen.cs (QualifiedName): New.
(Generate): Add SymbolTable to signature.
* generator/IGeneratable : Add QualifiedName prop and update Generate
signature.
* generator/Parser.cs : Switch from plain Hashtable to SymbolTable.
(Parse): Replaces the Types property and returns a SymbolTable.
* generator/StructBase.cs : New base class to derive struct and object
types. Initial implementation of protected GenField method and ctor.
* generator/StructGen.cs : New non-object struct type generatable.
* generator/SymbolTable.cs : New. Manages complex types hash and a
simple types hash. Will provide generic lookup interface.
2002-01-04 Mike Kestner <mkestner@speakeasy.net>
* makefile : switch to the new generator.

View File

@ -1,10 +1,10 @@
// GtkSharp.CodeGenerator.cs - The main code generation engine.
// GtkSharp.Generation.CodeGenerator.cs - The main code generation engine.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001 Mike Kestner
namespace GtkSharp {
namespace GtkSharp.Generation {
using System;
using System.Collections;
@ -14,14 +14,19 @@ namespace GtkSharp {
public static int Main (string[] args)
{
Parser p = new Parser (args[0]);
Hashtable types = p.Types;
Console.WriteLine (types.Count);
if (args.Length != 1) {
Console.WriteLine ("Usage: codegen <filename>");
return 0;
}
IDictionaryEnumerator de = types.GetEnumerator();
Parser p = new Parser (args[0]);
SymbolTable table = p.Parse ();
Console.WriteLine (table.Count + " types parsed.");
IDictionaryEnumerator de = table.GetEnumerator();
while (de.MoveNext()) {
IGeneratable gen = (IGeneratable) de.Value;
gen.Generate ();
gen.Generate (table);
}
return 0;

View File

@ -1,10 +1,10 @@
// GtkSharp.EnumGen.cs - The Enumeration Generatable.
// GtkSharp.Generation.EnumGen.cs - The Enumeration Generatable.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001 Mike Kestner
namespace GtkSharp {
namespace GtkSharp.Generation {
using System;
using System.IO;
@ -35,6 +35,13 @@ namespace GtkSharp {
}
}
public String QualifiedName {
get
{
return ns + "." + elem.GetAttribute("cname");
}
}
public String MarshalType {
get
{
@ -47,7 +54,7 @@ namespace GtkSharp {
return "(int) " + var_name;
}
public void Generate ()
public void Generate (SymbolTable table)
{
String filename = "..\\" + ns.ToLower() + "\\generated\\" + Name + ".cs";
@ -58,7 +65,7 @@ namespace GtkSharp {
sw.WriteLine ("// <c> 2001 Mike Kestner");
sw.WriteLine ();
sw.WriteLine ("namespace " + ns + "{");
sw.WriteLine ("namespace " + ns + " {");
sw.WriteLine ();
if (elem.GetAttribute("type") == "flags") {

View File

@ -1,10 +1,10 @@
// GtkSharp.IGeneratable.cs - Interface to generate code for a type.
// GtkSharp.Generation.IGeneratable.cs - Interface to generate code for a type.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001 Mike Kestner
namespace GtkSharp {
namespace GtkSharp.Generation {
using System;
@ -16,8 +16,10 @@ namespace GtkSharp {
String Name {get;}
String QualifiedName {get;}
String CallByName (String var_name);
void Generate ();
void Generate (SymbolTable table);
}
}

View File

@ -1,10 +1,10 @@
// GtkSharp.Parser.cs - The XML Parsing engine.
// GtkSharp.Generation.Parser.cs - The XML Parsing engine.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001 Mike Kestner
namespace GtkSharp {
namespace GtkSharp.Generation {
using System;
using System.Collections;
@ -13,7 +13,7 @@ namespace GtkSharp {
public class Parser {
private XmlDocument doc;
private Hashtable types;
private SymbolTable table;
public Parser (String filename)
{
@ -30,33 +30,30 @@ namespace GtkSharp {
}
}
public Hashtable Types {
get
{
if (types != null) return types;
public SymbolTable Parse ()
{
if (table != null) return table;
XmlElement root = doc.DocumentElement;
XmlElement root = doc.DocumentElement;
if ((root == null) || !root.HasChildNodes) {
if ((root == null) || !root.HasChildNodes) {
Console.WriteLine ("No Namespaces found.");
return null;
}
types = new Hashtable ();
foreach (XmlNode ns in root.ChildNodes) {
if (ns.Name != "namespace") {
continue;
}
XmlElement elem = (XmlElement) ns;
ParseNamespace (elem);
}
return types;
}
table = new SymbolTable ();
foreach (XmlNode ns in root.ChildNodes) {
if (ns.Name != "namespace") {
continue;
}
XmlElement elem = (XmlElement) ns;
ParseNamespace (elem);
}
return table;
}
private void ParseNamespace (XmlElement ns)
@ -70,7 +67,7 @@ namespace GtkSharp {
}
XmlElement elem = (XmlElement) def;
switch (def.Name) {
case "alias":
@ -80,14 +77,14 @@ namespace GtkSharp {
break;
case "enum":
IGeneratable gen = new EnumGen (ns_name, elem);
types [gen.CName] = gen;
table.AddType (new EnumGen (ns_name, elem));
break;
case "object":
break;
case "struct":
table.AddType (new StructGen (ns_name, elem));
break;
default:
@ -95,8 +92,6 @@ namespace GtkSharp {
break;
}
}
}
}
}

37
generator/StructBase.cs Normal file
View File

@ -0,0 +1,37 @@
// GtkSharp.Generation.StructBase.cs - The Structure/Object Base Class.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001 Mike Kestner
namespace GtkSharp.Generation {
using System;
using System.IO;
using System.Xml;
public class StructBase {
protected String ns;
protected XmlElement elem;
public StructBase (String ns, XmlElement elem) {
this.ns = ns;
this.elem = elem;
}
protected void GenField (XmlElement field, SymbolTable table, StreamWriter sw)
{
String c_type = field.GetAttribute("type");
sw.Write ("\t\t" + table.GetCSType(c_type));
if (field.HasAttribute("array_len")) {
sw.Write ("[]");
}
sw.WriteLine (" " + field.GetAttribute("cname") + ";");
}
}
}

106
generator/StructGen.cs Normal file
View File

@ -0,0 +1,106 @@
// GtkSharp.Generation.StructGen.cs - The Structure Generatable.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001 Mike Kestner
namespace GtkSharp.Generation {
using System;
using System.IO;
using System.Xml;
public class StructGen : StructBase, IGeneratable {
public StructGen (String ns, XmlElement elem) : base (ns, elem) {}
public String Name {
get
{
return elem.GetAttribute("name");
}
}
public String QualifiedName {
get
{
return ns + "." + elem.GetAttribute("name");
}
}
public String CName {
get
{
return elem.GetAttribute("cname");
}
}
public String MarshalType {
get
{
return "IntPtr";
}
}
public String CallByName (String var_name)
{
return var_name;
}
public void Generate (SymbolTable table)
{
String filename = "..\\" + ns.ToLower() + "\\generated\\" + Name + ".cs";
FileStream stream = new FileStream (filename, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter (stream);
sw.WriteLine ("// Generated File. Do not modify.");
sw.WriteLine ("// <c> 2001 Mike Kestner");
sw.WriteLine ();
sw.WriteLine ("namespace " + ns + " {");
sw.WriteLine ();
sw.WriteLine ("\tusing System;");
sw.WriteLine ("\tusing System.Collections;");
sw.WriteLine ("\tusing System.Runtime.InteropServices;");
sw.WriteLine ();
sw.WriteLine ("\t[StructLayout(LayoutKind.Sequential)]");
sw.WriteLine ("\tpublic class " + Name + " {");
sw.WriteLine ();
foreach (XmlNode node in elem.ChildNodes) {
XmlElement member = (XmlElement) node;
switch (node.Name) {
case "field":
break;
case "callback":
break;
case "constructor":
break;
case "method":
break;
default:
Console.WriteLine ("Unexpected node");
break;
}
}
sw.WriteLine ("\t}");
sw.WriteLine ();
sw.WriteLine ("}");
sw.Flush();
sw.Close();
}
}
}

41
generator/SymbolTable.cs Normal file
View File

@ -0,0 +1,41 @@
// GtkSharp.Generation.SymbolTable.cs - The Symbol Table Class.
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// (c) 2001 Mike Kestner
namespace GtkSharp.Generation {
using System;
using System.Collections;
public class SymbolTable {
private Hashtable complex_types = new Hashtable ();
private Hashtable simple_types;
public void AddType (IGeneratable gen)
{
complex_types [gen.CName] = gen;
}
public int Count {
get
{
return complex_types.Count;
}
}
public IDictionaryEnumerator GetEnumerator ()
{
return complex_types.GetEnumerator();
}
public String GetCSType (String c_type)
{
return "";
}
}
}