From 3c5ec9fcb616901d7d830371354cc1fa83be9d97 Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Sun, 9 Oct 2011 19:24:45 +0200 Subject: [PATCH] Move additional code fragments into new base classes for JavaMaker and NetMaker --- .../src/CS2JTranslator/CS2JMain/Fragments.cs | 227 ---------------- .../CS2JTranslator/CS2JTransform/JavaMaker.g | 4 +- .../CS2JTransform/NetFragments.cs | 249 ++++++++++++++++++ .../CS2JTranslator/CS2JTransform/NetMaker.g | 2 +- .../CS2JTransform/SyntaxFragments.cs | 76 ++++++ 5 files changed, 328 insertions(+), 230 deletions(-) delete mode 100644 CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/Fragments.cs create mode 100755 CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetFragments.cs create mode 100644 CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/SyntaxFragments.cs diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/Fragments.cs b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/Fragments.cs deleted file mode 100644 index da6d3c8..0000000 --- a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JMain/Fragments.cs +++ /dev/null @@ -1,227 +0,0 @@ -/* - Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com) -*/ - -using System; -using System.Collections.Generic; -using Antlr.Runtime.Tree; - -namespace Twiglet.CS2J.Translator -{ - public class Fragments - { - - public static string DelegateObject(String delName, String args, String body) - { - return delegateObject.Replace("${D}",delName).Replace("${A}",args).Replace("${B}",body); - } - - private static string delegateObject = @" - new ${D}() { public void Invoke(${A}) { ${B}; } } -"; - - public static string GenericCollectorMethods(string T, string S) - { - return genericCollectorMethodsStr.Replace("${T}", T).Replace("${S}", S); - } - - private static string genericCollectorMethodsStr = @" - public Iterator<${T}> iterator() { - Iterator<${T}> ret = null; - try - { - ret = this.GetEnumerator().iterator(); - } - catch (Exception e) - { - e.printStackTrace(); - } - return ret; - } - - public boolean add(${T} el) { - try - { - this.Add(el); - } - catch (Exception e) - { - e.printStackTrace(); - } - return true; - } - - public boolean addAll(Collection c) { - for (${T} el : c) { - this.add(el); - } - return true; - } - - public void clear() { - try - { - this.Clear(); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - - public boolean contains(Object o) { - boolean ret = false; - try - { - ret = this.Contains((${T}) o); - } - catch (Exception e) - { - e.printStackTrace(); - } - return ret; - } - - public boolean containsAll(Collection c) { - boolean ret = true; - for (Object el : c) { - if (!this.contains(el)) { - ret = false; - break; - } - } - return ret; - } - - public boolean isEmpty() { - return this.size() == 0; - } - - public boolean remove(Object o) { - boolean ret = false; - try - { - ret = this.Remove((${T}) o); - } - catch (Exception e) - { - e.printStackTrace(); - } - return ret; - } - - public boolean removeAll(Collection c) { - boolean ret = false; - for (Object el : c) { - ret = ret | this.remove(el); - } - return ret; - } - - public boolean retainAll(Collection c) { - boolean ret = false; - Object[] thisCopy = this.toArray(); - for (Object el : thisCopy) { - if (!c.contains(el)) { - ret = ret | this.remove(el); - } - } - return false; - } - - public int size() { - int ret = -1; - try { - return this.getCount(); - } - catch (Exception e) - { - e.printStackTrace(); - } - return ret; - } - - - public Object[] toArray() { - Object[] ret = new Object[this.size()]; - int i = 0; - for (Object el : this) { - ret[i] = el; - i++; - } - return ret; - } - - // NOTE: Moved to after the method name, like C# not Java, to help the poor parser. - public ${S}[] toArray<${S}>(${S}[] a) { - ArrayList<${T}> ret = new ArrayList<${T}>(this.size()); - for (${T} el : this) { - ret.add(el); - } - return ret.toArray(a); - } -"; - - public static string MultiDelegateMethods(string Del, string DelClass, string TyArgs) - { - return multiDelegateMethodsStr.Replace("${Del}", Del).Replace("${DelClass}", DelClass).Replace("${TyArgs}", TyArgs); - } - - private static string multiDelegateMethodsStr = @" - private System.Collections.Generic.IList<${Del}> _invocationList = new ArrayList<${Del}>(); - - public static ${Del} Combine ${TyArgs} (${Del} a, ${Del} b) throws Exception { - if (a == null) return b; - if (b == null) return a; - ${DelClass} ret = new ${DelClass}(); - ret._invocationList = a.GetInvocationList(); - ret._invocationList.addAll(b.GetInvocationList()); - return ret; - } - - public static ${Del} Remove ${TyArgs} (${Del} a, ${Del} b) throws Exception { - if (a == null || b == null) return a; - System.Collections.Generic.IList<${Del}> aInvList = a.GetInvocationList(); - System.Collections.Generic.IList<${Del}> newInvList = ListSupport.removeFinalStretch(aInvList, b.GetInvocationList()); - if (aInvList == newInvList) { - return a; - } - else { - ${DelClass} ret = new ${DelClass}(); - ret._invocationList = newInvList; - return ret; - } - } - - public System.Collections.Generic.IList<${Del}> GetInvocationList() throws Exception { - return _invocationList; - } -"; - - public static string GenIterator = @" - public Iterator<${T}> iterator() { - Iterator<${T}> ret = null; - try - { - ret = this.GetEnumerator().iterator(); - } - catch (Exception e) - { - e.printStackTrace(); - } - return ret; - }"; - - private static Dictionary _fragmentsLibrary = null; - public static Dictionary FragmentsLibrary - { get - { - if (_fragmentsLibrary == null) - { - _fragmentsLibrary = new Dictionary(); - } - return _fragmentsLibrary; - } - } - } -} diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g index 1d6cfd9..0f56d76 100644 --- a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g +++ b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/JavaMaker.g @@ -12,7 +12,7 @@ options { tokenVocab=cs; ASTLabelType=CommonTree; language=CSharp2; - superClass='Twiglet.CS2J.Translator.Transform.CommonWalker'; + superClass='Twiglet.CS2J.Translator.Transform.SyntaxFragments'; output=AST; } @@ -1627,7 +1627,7 @@ scope TypeContext; magicMultiInvokerMethod[$d.token, $return_type.tree, $return_type.thetext == "Void" || $return_type.thetext == "System.Void", ifTree, $formal_parameter_list.tree, mkArgsFromParams($d.token, $formal_parameter_list.tree), $variant_generic_parameter_list.tyargs] { multiDelName = "__Multi" + delName; - delClassMemberNodes = this.parseString("class_member_declarations", Fragments.MultiDelegateMethods(mkTypeString(delName, $variant_generic_parameter_list.tyargs), mkTypeString(multiDelName, $variant_generic_parameter_list.tyargs),mkTypeArgString($variant_generic_parameter_list.tyargs))); + delClassMemberNodes = this.parseString("class_member_declarations", this.MultiDelegateMethods(mkTypeString(delName, $variant_generic_parameter_list.tyargs), mkTypeString(multiDelName, $variant_generic_parameter_list.tyargs),mkTypeArgString($variant_generic_parameter_list.tyargs))); AddToImports("java.util.List"); AddToImports("java.util.LinkedList"); AddToImports("java.util.ArrayList"); diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetFragments.cs b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetFragments.cs new file mode 100755 index 0000000..ef25615 --- /dev/null +++ b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetFragments.cs @@ -0,0 +1,249 @@ +/* + Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com) +*/ + +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +using Antlr.Runtime.Tree; +using Antlr.Runtime; + +namespace Twiglet.CS2J.Translator.Transform +{ + + + // Base Class for NetMaker phase, holds string literals that are spliced into + // target classes to support Java interfaces such as Iterable etc. + public class NetFragments : CommonWalker + { + + protected NetFragments(ITreeNodeStream input, RecognizerSharedState state) + : base(input, state) + { } + + + private class MethodMapper + { + + CommonWalker walker = null; + + public MethodMapper(CommonWalker inWalker) { + walker = inWalker; + } + + public string RewriteMethod(Match m) + { + if (walker.Cfg.TranslatorMakeJavaNamingConventions) + { + return walker.toJavaConvention(CSharpEntity.METHOD, m.Groups[1].Value); + } + return m.Groups[1].Value; + } + } + + + //////////////////// + /// + /// Methods to convert Interfaces to Java equivalents + /// + //////////////////// + + // Takes a list of interface names e.g. [System.Collections.Generic.ICollection, System.IEnumerable] + // and a list of type variables to substitute into fragments and returns a string containing the + // required methods + // TypeVars in fragments have names T, T1, T2, T3, etc. + public string getMethods(string iface, IList tyArgs) + { + string ret = ""; + MethodMapper mapper = new MethodMapper(this); + + if (InterfaceMap.ContainsKey(iface)) + { + string methods = InterfaceMap[iface]; + if (tyArgs != null) + { + int idx = 0; + foreach (string t in tyArgs) + { + string toReplace = "${T" + (idx == 0 ? "" : Convert.ToString(idx)) + "}"; + methods = methods.Replace(toReplace,tyArgs[idx]); + idx++; + } + } + // replace @m{} with (possibly transformed) + methods = Regex.Replace(methods, "(?:@m\\{(\\w+)\\})", new MatchEvaluator(mapper.RewriteMethod)); + ret += methods; + } + + return ret; + } + + + private static Dictionary interfaceMap = null; + public static Dictionary InterfaceMap + { + get + { + if (interfaceMap == null) + { + interfaceMap = new Dictionary(); + interfaceMap["System.Collections.Generic.IEnumerable"] = genericIteratorMethodsStr; + interfaceMap["System.Collections.Generic.ICollection"] = genericCollectorMethodsStr; + } + return interfaceMap; + } + + } + + public static string GenericIteratorMethods(string T, string S) + { + return genericIteratorMethodsStr.Replace("${T}", T); + } + + private static string genericIteratorMethodsStr = @" + public CS2J.java.util.Iterator<${T}> iterator() { + CS2J.java.util.Iterator<${T}> ret = null; + try + { + ret = CS2J.CS2JNet.JavaSupport.Collections.Generic.IteratorSupport.mk(this.@m{GetEnumerator}()); + } + catch (Exception e) + { + e.printStackTrace(); + } + return ret; + } +"; + + public static string GenericCollectorMethods(string T, string S) + { + return genericCollectorMethodsStr.Replace("${T}", T).Replace("${T1}", S); + } + + private static string genericCollectorMethodsStr = @" + public boolean add(${T} el) { + try + { + this.Add(el); + } + catch (Exception e) + { + e.printStackTrace(); + } + return true; + } + + public boolean addAll(Collection c) { + for (${T} el : c) { + this.add(el); + } + return true; + } + + public void clear() { + try + { + this.Clear(); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + public boolean contains(Object o) { + boolean ret = false; + try + { + ret = this.Contains((${T}) o); + } + catch (Exception e) + { + e.printStackTrace(); + } + return ret; + } + + public boolean containsAll(Collection c) { + boolean ret = true; + for (Object el : c) { + if (!this.contains(el)) { + ret = false; + break; + } + } + return ret; + } + + public boolean isEmpty() { + return this.size() == 0; + } + + public boolean remove(Object o) { + boolean ret = false; + try + { + ret = this.Remove((${T}) o); + } + catch (Exception e) + { + e.printStackTrace(); + } + return ret; + } + + public boolean removeAll(Collection c) { + boolean ret = false; + for (Object el : c) { + ret = ret | this.remove(el); + } + return ret; + } + + public boolean retainAll(Collection c) { + boolean ret = false; + Object[] thisCopy = this.toArray(); + for (Object el : thisCopy) { + if (!c.contains(el)) { + ret = ret | this.remove(el); + } + } + return false; + } + + public int size() { + int ret = -1; + try { + return this.getCount(); + } + catch (Exception e) + { + e.printStackTrace(); + } + return ret; + } + + + public Object[] toArray() { + Object[] ret = new Object[this.size()]; + int i = 0; + for (Object el : this) { + ret[i] = el; + i++; + } + return ret; + } + + // NOTE: Moved to after the method name, like C# not Java, to help the poor parser. + public ${T1}[] toArray<${T1}>(${T1}[] a) { + ArrayList<${T}> ret = new ArrayList<${T}>(this.size()); + for (${T} el : this) { + ret.add(el); + } + return ret.toArray(a); + } +"; + + } +} diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetMaker.g b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetMaker.g index 7583d1a..f5d9c1d 100644 --- a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetMaker.g +++ b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetMaker.g @@ -9,7 +9,7 @@ options { ASTLabelType=CommonTree; output=AST; language=CSharp2; - superClass='Twiglet.CS2J.Translator.Transform.CommonWalker'; + superClass='Twiglet.CS2J.Translator.Transform.NetFragments'; } // A scope to keep track of the namespace search path available at any point in the program diff --git a/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/SyntaxFragments.cs b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/SyntaxFragments.cs new file mode 100644 index 0000000..429efbe --- /dev/null +++ b/CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/SyntaxFragments.cs @@ -0,0 +1,76 @@ +/* + Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com) +*/ + +using System; +using System.Collections.Generic; +using Antlr.Runtime.Tree; +using Antlr.Runtime; + +namespace Twiglet.CS2J.Translator.Transform +{ + + + // Base Class for JavaMaker phase, holds string literals that are spliced into + // target classes to support Delegates + public class SyntaxFragments : CommonWalker + { + + + protected SyntaxFragments(ITreeNodeStream input, RecognizerSharedState state) + : base(input, state) + { } + + + //////////////////// + /// + /// Delegates + /// + //////////////////// + public string DelegateObject(String delName, String args, String body) + { + return delegateObject.Replace("${D}",delName).Replace("${A}",args).Replace("${B}",body); + } + + private static string delegateObject = @" + new ${D}() { public void Invoke(${A}) { ${B}; } } +"; + + public string MultiDelegateMethods(string Del, string DelClass, string TyArgs) + { + return multiDelegateMethodsStr.Replace("${Del}", Del).Replace("${DelClass}", DelClass).Replace("${TyArgs}", TyArgs); + } + + private string multiDelegateMethodsStr = @" + private System.Collections.Generic.IList<${Del}> _invocationList = new ArrayList<${Del}>(); + + public static ${Del} Combine ${TyArgs} (${Del} a, ${Del} b) throws Exception { + if (a == null) return b; + if (b == null) return a; + ${DelClass} ret = new ${DelClass}(); + ret._invocationList = a.GetInvocationList(); + ret._invocationList.addAll(b.GetInvocationList()); + return ret; + } + + public static ${Del} Remove ${TyArgs} (${Del} a, ${Del} b) throws Exception { + if (a == null || b == null) return a; + System.Collections.Generic.IList<${Del}> aInvList = a.GetInvocationList(); + System.Collections.Generic.IList<${Del}> newInvList = ListSupport.removeFinalStretch(aInvList, b.GetInvocationList()); + if (aInvList == newInvList) { + return a; + } + else { + ${DelClass} ret = new ${DelClass}(); + ret._invocationList = newInvList; + return ret; + } + } + + public System.Collections.Generic.IList<${Del}> GetInvocationList() throws Exception { + return _invocationList; + } +"; + + } +}