mirror of
https://github.com/twiglet/cs2j.git
synced 2025-01-18 13:15:17 +01:00
Move additional code fragments into new base classes for JavaMaker and NetMaker
This commit is contained in:
parent
3ef1333259
commit
3c5ec9fcb6
@ -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<? extends ${T}> 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 <S> 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<string, CommonTree> _fragmentsLibrary = null;
|
||||
public static Dictionary<string, CommonTree> FragmentsLibrary
|
||||
{ get
|
||||
{
|
||||
if (_fragmentsLibrary == null)
|
||||
{
|
||||
_fragmentsLibrary = new Dictionary<string, CommonTree>();
|
||||
}
|
||||
return _fragmentsLibrary;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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");
|
||||
|
249
CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetFragments.cs
Executable file
249
CSharpTranslator/antlr3/src/CS2JTranslator/CS2JTransform/NetFragments.cs
Executable file
@ -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<string> 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{<method name>} with (possibly transformed) <method name>
|
||||
methods = Regex.Replace(methods, "(?:@m\\{(\\w+)\\})", new MatchEvaluator(mapper.RewriteMethod));
|
||||
ret += methods;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
private static Dictionary<string,string> interfaceMap = null;
|
||||
public static Dictionary<string,string> InterfaceMap
|
||||
{
|
||||
get
|
||||
{
|
||||
if (interfaceMap == null)
|
||||
{
|
||||
interfaceMap = new Dictionary<string,string>();
|
||||
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<? extends ${T}> 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 <S> 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);
|
||||
}
|
||||
";
|
||||
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
";
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user