diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/TypeRep.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/TypeRep.cs index 2926f3d..852192b 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/TypeRep.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/TypeRep.cs @@ -14,7 +14,7 @@ namespace RusticiSoftware.Translator.CLR public abstract class RepBase { - protected static DirectoryHT TypeTemplateCache = new DirectoryHT(); + protected static DirectoryHT TypeTemplateCache = new DirectoryHT(); // 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 e) { TypeTemplateCache = e; } - public static DirectoryHT TypeEnv + public static DirectoryHT 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 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; diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs index b29cf77..dd7200a 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs @@ -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 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 remArgs = new List(); XmlTextWriter enumXmlWriter = null; - AppEnv = new DirectoryHT(null); + AppEnv = new DirectoryHT(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 de in AppEnv) { String xmlFName = Path.Combine(cfg.XmlDir, ((string)de.Key).Replace('.', Path.DirectorySeparatorChar) + ".xml"); diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/PreProcessor.cs b/CSharpTranslator/antlr3/src/cs2j/CSharp/PreProcessor.cs index 2646bca..07a550d 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/PreProcessor.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/PreProcessor.cs @@ -86,7 +86,6 @@ namespace RusticiSoftware.Translator.CSharp public override String GetErrorMessage(RecognitionException e, String[] tokenNames) { IList stack = GetRuleInvocationStack(e, this.GetType().Name); - String msg = null; StringBuilder sb = new StringBuilder(); sb.Append("\r\n"); foreach (object o in stack) diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g index a5bd614..3bae2f7 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/TemplateExtracter.g @@ -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 AppEnv {get; set;} + } /******************************************************************************************** diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g index cc4b902..a994391 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g @@ -15,7 +15,6 @@ tokens { @lexer::header { - using System.Collections.Generic; using Debug = System.Diagnostics.Debug; } diff --git a/CSharpTranslator/antlr3/src/cs2j/Utils/DirectoryHT.cs b/CSharpTranslator/antlr3/src/cs2j/Utils/DirectoryHT.cs index b6cfae4..f41cecc 100644 --- a/CSharpTranslator/antlr3/src/cs2j/Utils/DirectoryHT.cs +++ b/CSharpTranslator/antlr3/src/cs2j/Utils/DirectoryHT.cs @@ -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 : IDictionary { - private DirectoryHT _parent = null; + private DirectoryHT _parent = null; - private Hashtable leaves = new Hashtable(); + private Dictionary leaves = new Dictionary(); - private Hashtable children = new Hashtable(); + private Dictionary> children = new Dictionary>(); - public DirectoryHT(DirectoryHT p) + public DirectoryHT(DirectoryHT p) { _parent = p; } @@ -25,267 +25,249 @@ namespace RusticiSoftware.Translator.Utils { } - public Hashtable Leaves + public Dictionary Leaves { get { return leaves; } } // p is key to a sub directory - public DirectoryHT subDir(string p) + public DirectoryHT 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 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 Keys + { + get + { + List keys = new List(); + foreach (string k in leaves.Keys) + keys.Add(k); + foreach (KeyValuePair> 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 Values + { + + get + { + List vals = new List(); + foreach (TValue v in leaves.Values) + vals.Add(v); + foreach (KeyValuePair> 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 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 item) + { + this.Add(item.Key, item.Value); + } + + public bool Contains(KeyValuePair item) + { + TValue value; + if (!this.TryGetValue(item.Key, out value)) + return false; + + return EqualityComparer.Default.Equals(value, item.Value); + } + + public bool Remove(KeyValuePair item) + { + if (!this.Contains(item)) + return false; + + return this.Remove(item.Key); + } + + public void CopyTo(KeyValuePair[] 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(this); + children[components[0]].Add(components[1], value); } } + public IEnumerator> GetEnumerator() + { + foreach (KeyValuePair> de in children) + { + foreach (KeyValuePair cur in de.Value) + { + yield return new KeyValuePair(de.Key + "." + cur.Key, cur.Value); + } + } + foreach (KeyValuePair de in leaves) + { + yield return new KeyValuePair(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 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(ICollection 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 } } diff --git a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj index 330be4a..43ce5d8 100644 --- a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj +++ b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj @@ -1,4 +1,4 @@ - + Debug