//
using System;
+using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Collections;
// instance
public void CreateTopLevel(Type parent, string className)
{
+ debug();
if (className == null) {
className = "derived" + parent.Name;
}
// pushes a reference to the new current type
public void CreateObject(Type type, string varName)
{
+ debug();
bool isDefaultName;
if (varName == null) {
isDefaultName = true;
// pushes a reference to the property
public void CreateProperty(PropertyInfo property)
{
+ debug();
CodePropertyReferenceExpression prop = new CodePropertyReferenceExpression(
(CodeExpression)objects[objects.Count - 1],
property.Name);
// pushes a reference to the event
public void CreateEvent(EventInfo evt)
{
+ debug();
CodeEventReferenceExpression expr = new CodeEventReferenceExpression(
(CodeExpression)objects[objects.Count - 1],
evt.Name);
// property
public void CreateDependencyProperty(Type attachedTo, string propertyName, Type propertyType)
{
+ debug();
string varName = "temp";
varName += tempIndex;
tempIndex += 1;
// pops 2 items: the name of the property, and the object to attach to
public void EndDependencyProperty()
{
- objects.RemoveAt(objects.Count - 1);
+ debug();
+ objects.RemoveAt(objects.Count - 1); // pop the variable name - we don't need it since it's already
+ // baked into the call
CodeExpression call = (CodeExpression)(objects[objects.Count - 1]);
objects.RemoveAt(objects.Count - 1);
constructor.Statements.Add(call);
// top of stack must be an object reference
public void CreateElementText(string text)
{
+ debug();
CodeVariableReferenceExpression var = (CodeVariableReferenceExpression)objects[objects.Count - 1];
CodeMethodInvokeExpression call = new CodeMethodInvokeExpression(
var,
// top of stack is reference to an event
public void CreateEventDelegate(string functionName, Type eventDelegateType)
{
+ debug();
CodeExpression expr = new CodeObjectCreateExpression(
eventDelegateType,
new CodeMethodReferenceExpression(
// top of stack is reference to a property
public void CreatePropertyDelegate(string functionName, Type propertyType)
{
+ debug();
CodeExpression expr = new CodeObjectCreateExpression(
propertyType,
new CodeMethodReferenceExpression(
// top of stack is reference to a property
public void CreatePropertyText(string text, Type propertyType)
{
+ debug();
CreateDependencyPropertyText(text, propertyType);
}
// top of stack is reference to an attached property
public void CreateDependencyPropertyText(string text, Type propertyType)
{
+ debug();
CodeExpression expr = new CodePrimitiveExpression(text);
if (propertyType != typeof(string)) {
expr = new CodeCastExpression(
public void CreatePropertyObject(Type type, string varName)
{
+ debug();
bool isDefaultName;
if (varName == null) {
isDefaultName = true;
}
- public void EndPropertyObject(Type sourceType)
+ public void EndPropertyObject(Type destType)
{
+ debug();
CodeExpression varRef = (CodeExpression)objects[objects.Count - 1];
objects.RemoveAt(objects.Count - 1);
- Type destType = (Type)objects[objects.Count - 1];
+ Type sourceType = (Type)objects[objects.Count - 1];
objects.RemoveAt(objects.Count - 1);
+ Debug.WriteLine(destType + "->" + sourceType);
+
CodeExpression expr;
if (destType == sourceType)
(CodeExpression)objects[objects.Count - 1],
expr);
constructor.Statements.Add(assignment);
-
}
public void EndObject()
{
+ debug();
objects.RemoveAt(objects.Count - 1);
}
public void EndProperty()
{
+ debug();
objects.RemoveAt(objects.Count - 1);
}
public void EndEvent()
{
+ debug();
objects.RemoveAt(objects.Count - 1);
}
public void Finish()
{
+ debug();
generator.GenerateCodeFromCompileUnit(code, writer, null);
writer.Close();
}
public void CreateCode(string code)
{
+ debug();
type.Members.Add(new CodeSnippetTypeMember(code));
}
+
+ private void debug()
+ {
+ Debug.WriteLine(new System.Diagnostics.StackTrace());
+ }
}
}
using System;
using System.Collections;
+using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Reflection;
private enum CurrentType { Object,
Property,
+ PropertyObject,
DependencyProperty,
Code }
public void Parse()
{
while (reader.Read()) {
+ Debug.WriteLine("NOW PARSING: " + reader.NodeType + "; " + reader.Name + "; " + reader.Value);
if (begun && currentState == null && reader.NodeType != XmlNodeType.Whitespace)
throw new Exception("Too far: " + reader.NodeType + ", " + reader.Name);
if (currentState != null && currentState.type == CurrentType.Code)
void parseCodeElement()
{
- oldStates.Add(currentState);
- currentState = new ParserState();
- currentState.type = CurrentType.Code;
- currentState.obj = "";
+ push(CurrentType.Code, "");
}
void parseText()
{
- if (currentState.type == CurrentType.Object) {
+ if (currentState.type == CurrentType.Object || currentState.type == CurrentType.PropertyObject) {
writer.CreateElementText(reader.Value);
} else if (currentState.type == CurrentType.DependencyProperty) {
DependencyProperty dp = (DependencyProperty)currentState.obj;
// TODO: exception
}
- oldStates.Add(currentState);
- currentState = new ParserState();
- currentState.type = CurrentType.Property;
- currentState.obj = prop;
+ push(CurrentType.Property, prop);
writer.CreateProperty(prop);
Type typeAttachedTo = findTypeToAttachTo(attachedTo, propertyName);
DependencyProperty dp = getDependencyProperty(typeAttachedTo, propertyName);
- oldStates.Add(currentState);
- currentState = new ParserState();
- currentState.obj = dp;
- currentState.type = CurrentType.DependencyProperty;
+ push(CurrentType.DependencyProperty, dp);
writer.CreateDependencyProperty(typeAttachedTo, propertyName, dp.PropertyType);
}
if (isEmpty) {
- writer.EndObject();
+ if (currentState.type == CurrentType.Object) {
+ writer.EndObject();
+ } else if (currentState.type == CurrentType.PropertyObject) {
+ ParserState state = (ParserState)oldStates[oldStates.Count - 1];
+ writer.EndPropertyObject(((PropertyInfo)state.obj).PropertyType);
+ }
pop();
}
}
void addChild(Type type, string objectName)
{
writer.CreateObject(type, objectName);
- oldStates.Add(currentState);
- currentState = new ParserState();
- currentState.type = CurrentType.Object;
- currentState.obj = type;
+ push(CurrentType.Object, type);
}
void addPropertyChild(Type type, string objectName)
{
-// writer.CreatePropertyObject(type, objectName);
- writer.CreatePropertyObject(((PropertyInfo)currentState.obj).PropertyType, objectName);
+ writer.CreatePropertyObject(type, objectName);
- oldStates.Add(currentState);
- currentState = new ParserState();
- currentState.type = CurrentType.Object;
- currentState.obj = type;
+ push(CurrentType.PropertyObject, type);
}
{
Type typeAttachedTo = null;
foreach (ParserState state in oldStates) {
- if (state.type == CurrentType.Object &&
+ if ((state.type == CurrentType.Object ||
+ state.type == CurrentType.PropertyObject) &&
((Type)state.obj).Name == attachedTo) {
typeAttachedTo = (Type)state.obj;
break;
void parseEndElement()
{
- if (currentState.type == CurrentType.Code) {
+ Debug.WriteLine("IN ENDELEMENT, SWITCHING ON " + currentState.type);
+ switch (currentState.type) {
+ case CurrentType.Code:
writer.CreateCode((string)currentState.obj);
- } else if (currentState.type == CurrentType.Object) {
- ParserState prev = null;
- if (oldStates.Count > 1)
- prev = (ParserState)oldStates[oldStates.Count - 1];
-
- if (prev != null && prev.type == CurrentType.Property)
- writer.EndPropertyObject((Type)currentState.obj);
- else
- writer.EndObject();
- } else if (currentState.type == CurrentType.Property) {
+ break;
+ case CurrentType.Object:
+ writer.EndObject();
+ break;
+ case CurrentType.PropertyObject:
+ writer.EndPropertyObject((Type)currentState.obj);
+ break;
+ case CurrentType.Property:
writer.EndProperty();
- } else if (currentState.type == CurrentType.DependencyProperty) {
+ break;
+ case CurrentType.DependencyProperty:
writer.EndDependencyProperty();
+ break;
}
pop();
}
void pop()
{
+ Debug.WriteLine("POPPING: " + currentState.type);
if (oldStates.Count == 0) {
currentState = null;
writer.Finish();
currentState = (ParserState)oldStates[lastIndex];
oldStates.RemoveAt(lastIndex);
}
-
+ void push(CurrentType type, Object obj)
+ {
+ Debug.WriteLine("PUSHING: " + type);
+ oldStates.Add(currentState);
+ currentState = new ParserState();
+ currentState.type = type;
+ currentState.obj = obj;
+ }
}
}