mirror of
https://github.com/twiglet/cs2j.git
synced 2025-01-18 13:15:17 +01:00
77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
/*
|
|
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;
|
|
}
|
|
";
|
|
|
|
}
|
|
}
|