// // (c) 2009, Sigbjorn Finne // // Generating Haskell wrapper modules for .NET classes/interfaces. // using System; using System.Collections; namespace HsWrap { public class HsTypeDetails { protected String m_hs_name; protected String m_ty_name; protected String m_orig_name; protected String m_inner_type; protected bool m_is_type_mod; protected int m_generic_args; static protected String m_prefix; static HsTypeDetails() { m_prefix = "NET."; } public HsTypeDetails() { m_hs_name = ""; m_ty_name = ""; m_orig_name = ""; m_inner_type = ""; m_is_type_mod = false; m_generic_args = 0; } public String HsTypeName { get { return (m_hs_name); } set { m_hs_name = value; } } public String HsTypeModName { get { return (( m_is_type_mod ? (m_hs_name + ".Type") : m_hs_name)); } } public String TypeName { get { return m_ty_name; } set { m_ty_name = value; if ( m_ty_name.StartsWith(m_prefix) ) { m_ty_name = m_ty_name.Substring(m_prefix.Length); } } } public String HsFullName { get { String res = m_ty_name; if ( m_inner_type != "" ) { if ( m_ty_name.EndsWith(m_inner_type) ) { res = m_ty_name.Substring(0,m_ty_name.Length - m_inner_type.Length-1); } } res += (m_generic_args > 0 ? ("`"+m_generic_args) : ""); if ( m_inner_type != "" ) { res += "+" + m_inner_type; } return res; } /* set { m_orig_name = value; m_hs_name = HsType.TypeToModuleName(value); m_is_type_mod = (value != "NET.System.Type" && value != "System.Type" && value.EndsWith(".Type")); m_generic_args = HsType.TypeParams(value); } */ } public String HsOrigName { get { return m_orig_name; } set { m_orig_name = value; } } public String NestedType { get { return m_inner_type; } set { m_inner_type = value; } } public bool IsTypeModule { get { return m_is_type_mod; } set { m_is_type_mod = value; } } public int GenericArgs { get { return m_generic_args; } set { m_generic_args = value; } } public static String DropPrefix(String s) { if ( s.StartsWith(m_prefix) ) { s = s.Substring(m_prefix.Length); } return s; } }; /// /// /// public class HsType { protected static String m_prefix; static HsType() { m_prefix = ""; } static public String ClassPrefix { get { return m_prefix; } set { m_prefix = value; } } static public String TypeToModuleFile(String tyName) { int idx; String fnName = tyName.Replace('.','\\').Replace('+','\\'); if ( (idx=fnName.IndexOf("`")) > 0 ) { fnName = fnName.Substring(0,idx); } return fnName; } static public HsTypeDetails ToTypeDetails(String tyName) { int idx; HsTypeDetails res = new HsTypeDetails(); res.HsOrigName = tyName; if ( (idx=tyName.IndexOf("+")) > 0 ) { res.NestedType = tyName.Substring(idx+1); tyName = tyName.Substring(0,idx); } res.HsTypeName = HsType.TypeToModuleName(tyName); res.TypeName = res.HsTypeName; if ( res.NestedType != "" ) { res.HsTypeName = res.HsTypeName + "." + res.NestedType; } res.IsTypeModule = false; res.GenericArgs = HsType.TypeParams(tyName); return res; } static public String TypeToModuleName(String tyName) { int idx; String modName = tyName; if ( (idx=modName.IndexOf("`")) > 0 ) { modName = modName.Substring(0,idx); } if ( (idx=tyName.IndexOf("+")) > 0 ) { modName = modName + "." + tyName.Substring(idx+1); } return modName; } static public int TypeParams(String tyName) { int st=0; if ( (st=tyName.IndexOf("`")) > 0 ) { int idx; for (idx=st+1;idx < tyName.Length; idx++) { if ( tyName[idx] < '0' || tyName[idx] > '9' ) { break; } } st=Int32.Parse(tyName.Substring(st+1,idx-1-st)); } return st; } }; }