1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

Make DirectoryHT generic (DirectoryHT<TypeRepTemplate>)

This commit is contained in:
Kevin Glynn 2010-10-28 16:57:10 +02:00
parent 75d5812a1a
commit 05687e6b39
7 changed files with 225 additions and 241 deletions

View File

@ -14,7 +14,7 @@ namespace RusticiSoftware.Translator.CLR
public abstract class RepBase
{
protected static DirectoryHT TypeTemplateCache = new DirectoryHT();
protected static DirectoryHT<TypeRepTemplate> TypeTemplateCache = new DirectoryHT<TypeRepTemplate>();
//
protected const string TYPEVAR = "${TYPE}";
@ -288,12 +288,12 @@ namespace RusticiSoftware.Translator.CLR
{
private static Hashtable TypeRepCache = new Hashtable();
public static void Initialize(DirectoryHT e)
public static void Initialize(DirectoryHT<TypeRepTemplate> e)
{
TypeTemplateCache = e;
}
public static DirectoryHT TypeEnv
public static DirectoryHT<TypeRepTemplate> TypeEnv
{
get { return TypeTemplateCache; }
}
@ -472,11 +472,11 @@ namespace RusticiSoftware.Translator.CLR
private static TypeRepTemplate TemplateSearch(string typeName, ICollection pth)
{
TypeRepTemplate ret = null;
DirectoryHT ns;
DirectoryHT<TypeRepTemplate> ns;
foreach (string p in pth)
{
ns = (DirectoryHT)TypeTemplateCache.subDir(p);
ns = TypeTemplateCache.subDir(p);
ret = (ns == null ? null : ns[typeName] as TypeRepTemplate);
if (ret != null)
break;

View File

@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Xml;
using Antlr.Runtime.Tree;
using Antlr.Runtime;
@ -19,7 +18,7 @@ namespace RusticiSoftware.Translator.CSharp
class CS2J
{
private const string VERSION = "2009.1.1.x";
private static DirectoryHT AppEnv { get; set; }
private static DirectoryHT<TypeRepTemplate> AppEnv { get; set; }
private static CS2JSettings cfg = new CS2JSettings();
public delegate void FileProcessor(string fName);
@ -61,7 +60,7 @@ namespace RusticiSoftware.Translator.CSharp
long startTime = DateTime.Now.Ticks;
IList<string> remArgs = new List<string>();
XmlTextWriter enumXmlWriter = null;
AppEnv = new DirectoryHT(null);
AppEnv = new DirectoryHT<TypeRepTemplate>(null);
// Use a try/catch block for parser exceptions
try
@ -116,7 +115,7 @@ namespace RusticiSoftware.Translator.CSharp
if (cfg.DumpXmls)
{
// Get package name and convert to directory name
foreach (DictionaryEntry de in AppEnv)
foreach (KeyValuePair<string,TypeRepTemplate> de in AppEnv)
{
String xmlFName = Path.Combine(cfg.XmlDir,
((string)de.Key).Replace('.', Path.DirectorySeparatorChar) + ".xml");

View File

@ -86,7 +86,6 @@ namespace RusticiSoftware.Translator.CSharp
public override String GetErrorMessage(RecognitionException e, String[] tokenNames)
{
IList<string> stack = GetRuleInvocationStack(e, this.GetType().Name);
String msg = null;
StringBuilder sb = new StringBuilder();
sb.Append("\r\n");
foreach (object o in stack)

View File

@ -13,7 +13,7 @@ options {
ASTLabelType=CommonTree;
language=CSharp2;
superClass='RusticiSoftware.Translator.CSharp.CommonWalker';
output=AST;
//output=AST;
//backtrack=true;
}
@ -27,6 +27,7 @@ scope UseScope {
{
using System.Text;
using RusticiSoftware.Translator.CLR;
using RusticiSoftware.Translator.Utils;
}
@members
@ -37,6 +38,10 @@ scope UseScope {
// passing a whole load of returns through intermediate rules.
protected string Capture {get; set;}
// This is the environment that we are building, it maps fully qualified type names to their
// translations
protected DirectoryHT<TypeRepTemplate> AppEnv {get; set;}
}
/********************************************************************************************

View File

@ -15,7 +15,6 @@ tokens {
@lexer::header
{
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
}

View File

@ -1,21 +1,21 @@
using System;
using System.Collections;
using System.Text;
using System.Collections.Generic;
namespace RusticiSoftware.Translator.Utils
{
// Implements a hierarchy of directories.
public class DirectoryHT : IDictionary
public class DirectoryHT<TValue> : IDictionary<string, TValue>
{
private DirectoryHT _parent = null;
private DirectoryHT<TValue> _parent = null;
private Hashtable leaves = new Hashtable();
private Dictionary<string, TValue> leaves = new Dictionary<string, TValue>();
private Hashtable children = new Hashtable();
private Dictionary<string, DirectoryHT<TValue>> children = new Dictionary<string, DirectoryHT<TValue>>();
public DirectoryHT(DirectoryHT p)
public DirectoryHT(DirectoryHT<TValue> p)
{
_parent = p;
}
@ -25,267 +25,249 @@ namespace RusticiSoftware.Translator.Utils
{ }
public Hashtable Leaves
public Dictionary<string, TValue> Leaves
{
get { return leaves; }
}
// p is key to a sub directory
public DirectoryHT subDir(string p)
public DirectoryHT<TValue> subDir(string p)
{
string[] components = p.Split(new char[] { '.' }, 2);
if (components.Length == 1)
{
return (DirectoryHT) children[components[0]];
return children[components[0]];
}
else
{
DirectoryHT child = (DirectoryHT)children[components[0]];
DirectoryHT<TValue> child = children[components[0]];
return (child == null ? null : child.subDir(components[1]));
}
}
#region IDictionary Members
public void Add(object key, object value)
public bool ContainsKey(string key)
{
string[] components = key.Split(new char[] { '.' }, 2);
if (components.Length == 1)
{
return leaves.ContainsKey(components[0]);
}
else
{
return children[components[0]].ContainsKey(components[1]);
}
}
// public IDictionaryEnumerator GetEnumerator()
// {
// IDictionaryEnumerator[] des = new IDictionaryEnumerator[1 + children.Count];
// string[] pres = new string[1 + children.Count];
// int i = 1;
//
// pres[0] = "";
// des[0] = leaves.GetEnumerator();
// foreach (DictionaryEntry de in children)
// {
// pres[i] = ((string)de.Key) + ".";
// des[i] = ((DirectoryHT)de.Value).GetEnumerator();
// i++;
// }
//
// return new DirectoryHTEnumerator(pres,des);
// }
public ICollection<string> Keys
{
get
{
List<string> keys = new List<string>();
foreach (string k in leaves.Keys)
keys.Add(k);
foreach (KeyValuePair<string, DirectoryHT<TValue>> de in children)
foreach (string k in de.Value.Keys)
keys.Add(de.Key + "." + k);
return keys;
}
}
public bool Remove(string key)
{
if (!(key is string))
throw new Exception("The method or operation is not implemented.");
string[] components = ((string)key).Split(new char[] { '.' }, 2);
if (components.Length == 1)
{
return leaves.Remove(components[0]);
}
else
{
return children[components[0]].Remove(components[1]);
}
}
public ICollection<TValue> Values
{
get
{
List<TValue> vals = new List<TValue>();
foreach (TValue v in leaves.Values)
vals.Add(v);
foreach (KeyValuePair<string, DirectoryHT<TValue>> de in children)
foreach (TValue v in de.Value.Values)
vals.Add(v);
return vals;
}
}
public TValue this[string key]
{
get
{
// will throw KeyNotFound exception if not present
string[] components = key.Split(new char[] { '.' }, 2);
if (components.Length == 1)
{
TValue val = leaves[key];
// keving: this isn't typesafe!: return (val != null ? val : children[components[0]]);
return val;
}
else
{
DirectoryHT<TValue> child = children[components[0]];
return child[components[1]];
}
}
set
{
Add(key, value);
}
}
public bool TryGetValue(string key, out TValue value)
{
string[] components = key.Split(new char[] { '.' }, 2);
if (components.Length == 1)
{
return leaves.TryGetValue(key, out value);
}
else
{
if (children.ContainsKey(components[0]))
{
return children[components[0]].TryGetValue(components[1], out value);
}
else
{
value = default(TValue);
return false;
}
}
}
public void Add(KeyValuePair<string, TValue> item)
{
this.Add(item.Key, item.Value);
}
public bool Contains(KeyValuePair<string, TValue> item)
{
TValue value;
if (!this.TryGetValue(item.Key, out value))
return false;
return EqualityComparer<TValue>.Default.Equals(value, item.Value);
}
public bool Remove(KeyValuePair<string, TValue> item)
{
if (!this.Contains(item))
return false;
return this.Remove(item.Key);
}
public void CopyTo(KeyValuePair<string, TValue>[] array, int arrayIndex)
{
Copy(this, array, arrayIndex);
}
public void Add(string key, TValue value)
{
string[] components = key.Split(new char[] { '.' }, 2);
if (components.Length == 1)
{
leaves[components[0]] = value;
}
else
{
if (children[components[0]] == null)
children[components[0]] = new DirectoryHT(this);
((DirectoryHT)children[components[0]]).Add(components[1], value);
if (!children.ContainsKey(components[0]))
children[components[0]] = new DirectoryHT<TValue>(this);
children[components[0]].Add(components[1], value);
}
}
public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator()
{
foreach (KeyValuePair<string, DirectoryHT<TValue>> de in children)
{
foreach (KeyValuePair<string, TValue> cur in de.Value)
{
yield return new KeyValuePair<string, TValue>(de.Key + "." + cur.Key, cur.Value);
}
}
foreach (KeyValuePair<string, TValue> de in leaves)
{
yield return new KeyValuePair<string, TValue>(de.Key, de.Value);
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
#region ICollection Members
public int Count
{
get
{
int count = leaves.Count;
foreach (DirectoryHT<TValue> c in children.Values)
count += c.Count;
return count;
}
}
public bool IsReadOnly
{
get { return false; }
}
public void Clear()
{
leaves.Clear();
children.Clear();
}
public bool Contains(object key)
{
if (!(key is string))
throw new Exception("The method or operation is not implemented.");
string[] components = ((String)key).Split(new char[] { '.' }, 2);
if (components.Length == 1)
{
return leaves.Contains(components[0]);
}
else
{
return ((DirectoryHT)children[components[0]]).Contains(components[1]);
}
}
public IDictionaryEnumerator GetEnumerator()
{
IDictionaryEnumerator[] des = new IDictionaryEnumerator[1 + children.Count];
string[] pres = new string[1 + children.Count];
int i = 1;
pres[0] = "";
des[0] = leaves.GetEnumerator();
foreach (DictionaryEntry de in children)
{
pres[i] = ((string)de.Key) + ".";
des[i] = ((DirectoryHT)de.Value).GetEnumerator();
i++;
}
return new DirectoryHTEnumerator(pres,des);
}
public bool IsFixedSize
{
get { return false; }
}
public bool IsReadOnly
{
get { return false; }
}
public ICollection Keys
{
get {
ArrayList keys = new ArrayList();
foreach (object k in leaves.Keys)
keys.Add(k);
foreach (DictionaryEntry de in children)
foreach (string k in ((DirectoryHT)de.Value).Keys)
keys.Add(de.Key + "." + k);
return keys;
}
}
public void Remove(object key)
{
if (!(key is string))
throw new Exception("The method or operation is not implemented.");
string[] components = ((string)key).Split(new char[] { '.' }, 2);
if (components.Length == 1)
{
leaves.Remove(components[0]);
}
else
{
((DirectoryHT)children[components[0]]).Remove(components[1]);
}
}
public ICollection Values
{
get {
ArrayList vals = new ArrayList();
foreach (object v in leaves.Values)
vals.Add(v);
foreach (DictionaryEntry de in children)
foreach (object v in ((DirectoryHT)de.Value).Values)
vals.Add(v);
return vals;
}
}
public object this[object key]
{
get
{
if (!(key is string))
throw new Exception("The method or operation is not implemented.");
string[] components = ((string)key).Split(new char[] { '.' }, 2);
if (components.Length == 1)
{
object val = leaves[components[0]];
return (val != null ? val : children[components[0]]);
}
else
{
DirectoryHT child = (DirectoryHT)children[components[0]];
return (child == null ? null : child[components[1]]);
}
}
set
{
Add(key, value);
}
}
#endregion
#region ICollection Members
public void CopyTo(Array array, int index)
private static void Copy<T>(ICollection<T> source, T[] array, int arrayIndex)
{
throw new Exception("The method or operation is not implemented.");
if (array == null)
throw new ArgumentNullException("array");
if (arrayIndex < 0 || arrayIndex > array.Length)
throw new ArgumentOutOfRangeException("arrayIndex");
if ((array.Length - arrayIndex) < source.Count)
throw new ArgumentException("Destination array is not large enough. Check array.Length and arrayIndex.");
foreach (T item in source)
array[arrayIndex++] = item;
}
public int Count
{
get
{
int count = leaves.Count;
foreach (DirectoryHT c in children.Values)
count += c.Count;
return count;
}
}
public bool IsSynchronized
{
get { throw new Exception("The method or operation is not implemented."); }
}
public object SyncRoot
{
get { throw new Exception("The method or operation is not implemented."); }
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
public class DirectoryHTEnumerator : IDictionaryEnumerator
{
private int _pos = 0;
private string[] _prefixes;
private IDictionaryEnumerator[] _enums;
public DirectoryHTEnumerator(string[] pres, IDictionaryEnumerator[] des)
{
_prefixes = pres;
_enums = des;
}
#region IDictionaryEnumerator Members
public DictionaryEntry Entry
{
get { return (DictionaryEntry)Current; }
}
public object Key
{
get { return Entry.Key; }
}
public object Value
{
get { return Entry.Value; }
}
#endregion
#region IEnumerator Members
public object Current
{
get { ValidateIndex();
return new DictionaryEntry(_prefixes[_pos] + (string)((DictionaryEntry)_enums[_pos].Current).Key,
((DictionaryEntry)_enums[_pos].Current).Value);
}
}
public bool MoveNext()
{
if (_pos >= _enums.Length)
return false;
while (_pos < _enums.Length && !_enums[_pos].MoveNext())
_pos++;
return _pos != _enums.Length;
}
public void Reset()
{
_pos = 0;
}
// Validate the enumeration index and throw an exception if the index is out of range.
private void ValidateIndex()
{
if (_pos < 0 || _pos >= _enums.Length)
throw new InvalidOperationException("Enumerator is before or after the collection.");
}
#endregion
}
}

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>