3 // This file was derived from a file from #Develop.
5 // Copyright (C) 2001-2007 Mike Krüger <mkrueger@novell.com>
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 using System
.Collections
;
25 using MonoDevelop
.Projects
.Parser
;
26 using MonoDevelop
.Core
;
27 using MonoDevelop
.Core
;
28 using MonoDevelop
.Projects
.Ambience
;
30 namespace NemerleBinding
32 public class NemerleAmbience
: Ambience
34 static string[,] typeConversionList
= new string[,] {
35 {"System.Void", "void"}
,
36 {"System.Object", "object"}
,
37 {"System.Boolean", "bool"}
,
38 {"System.Byte", "byte"}
,
39 {"System.SByte", "sbyte"}
,
40 {"System.Char", "char"}
,
41 {"System.Enum", "enum"}
,
42 {"System.Int16", "short"}
,
43 {"System.Int32", "int"}
,
44 {"System.Int64", "long"}
,
45 {"System.UInt16", "ushort"}
,
46 {"System.UInt32", "uint"}
,
47 {"System.UInt64", "ulong"}
,
48 {"System.Single", "float"}
,
49 {"System.Double", "double"}
,
50 {"System.Decimal", "decimal"}
,
51 {"System.String", "string"}
54 static Hashtable typeConversionTable
= new Hashtable();
56 public static Hashtable TypeConversionTable
{
58 return typeConversionTable
;
62 static NemerleAmbience()
64 for (int i
= 0; i
< typeConversionList
.GetLength(0); ++i
) {
65 typeConversionTable
[typeConversionList
[i
, 0]] = typeConversionList
[i
, 1];
69 bool ModifierIsSet(ModifierEnum modifier
, ModifierEnum query
)
71 return (modifier
& query
) == query
;
74 public override string Convert(ModifierEnum modifier
, ConversionFlags conversionFlags
)
76 if (ShowAccessibility(conversionFlags
)) {
77 if (ModifierIsSet(modifier
, ModifierEnum
.Public
)) {
79 } else if (ModifierIsSet(modifier
, ModifierEnum
.Private
)) {
81 } else if (ModifierIsSet(modifier
, ModifierEnum
.ProtectedAndInternal
)) {
82 return "protected internal ";
83 } else if (ModifierIsSet(modifier
, ModifierEnum
.ProtectedOrInternal
)) {
84 return "internal protected ";
85 } else if (ModifierIsSet(modifier
, ModifierEnum
.Internal
)) {
87 } else if (ModifierIsSet(modifier
, ModifierEnum
.Protected
)) {
95 string GetModifier(IDecoration decoration
, ConversionFlags conversionFlags
)
99 if (decoration
.IsStatic
) mod
= "static ";
100 else if (decoration
.IsFinal
) mod
= "final ";
101 else if (decoration
.IsVirtual
) mod
= "virtual ";
102 else if (decoration
.IsOverride
) mod
= "override ";
103 else if (decoration
.IsNew
) mod
= "new ";
106 if (IncludeHTMLMarkup(conversionFlags
) | IncludePangoMarkup(conversionFlags
))
107 return "<i>" + mod
+ "</i>";
113 public override string Convert(IClass c
, ConversionFlags conversionFlags
)
115 StringBuilder builder
= new StringBuilder();
117 builder
.Append(Convert(c
.Modifiers
, conversionFlags
));
119 if (ShowClassModifiers(conversionFlags
)) {
121 switch (c
.ClassType
) {
122 case ClassType
.Delegate
:
123 case ClassType
.Struct
:
128 AppendPangoHtmlTag (builder
, "sealed ", "i", conversionFlags
);
131 } else if (c
.IsAbstract
&& c
.ClassType
!= ClassType
.Interface
) {
132 AppendPangoHtmlTag (builder
, "abstract ", "i", conversionFlags
);
136 if (ShowClassModifiers(conversionFlags
)) {
137 switch (c
.ClassType
) {
138 case ClassType
.Delegate
:
139 builder
.Append("delegate");
140 // Only display the return type when modifiers are to be
141 // shown - this fixes the vay delegates are shown in the
143 if (c
.Methods
.Count
> 0) {
144 foreach(IMethod m
in c
.Methods
) {
145 if (m
.Name
!= "Invoke") continue;
146 builder
.Append (' ');
147 builder
.Append (Convert(m
.ReturnType
));
151 case ClassType
.Class
:
152 builder
.Append("class");
154 case ClassType
.Struct
:
155 builder
.Append("struct");
157 case ClassType
.Interface
:
158 builder
.Append("interface");
161 builder
.Append("enum");
167 if (UseFullyQualifiedMemberNames(conversionFlags
)) {
168 AppendPangoHtmlTag (builder
, c
.FullyQualifiedName
, "b", conversionFlags
);
170 AppendPangoHtmlTag (builder
, c
.Name
, "b", conversionFlags
);
173 // Display generic parameters only if told so
174 if (ShowGenericParameters(conversionFlags
) && c
.GenericParameters
!= null && c
.GenericParameters
.Count
> 0) {
175 bool includeMarkup
= IncludeHTMLMarkup(conversionFlags
) || IncludePangoMarkup(conversionFlags
);
176 builder
.Append ("[");
177 // Since we know that there is at least one generic parameter in
178 // the list, we can add it outside the loop - so, we don't have
179 // to check whether we may append a comma or not
180 builder
.Append (c
.GenericParameters
[0].Name
);
181 // Now continue with the others, if there are any
182 for (int i
= 1; i
< c
.GenericParameters
.Count
; i
++) {
183 builder
.Append (", ");
184 builder
.Append (c
.GenericParameters
[i
].Name
);
186 builder
.Append ("]");
189 if (c
.ClassType
== ClassType
.Delegate
) {
191 foreach(IMethod m
in c
.Methods
) {
192 if (m
.Name
!= "Invoke") continue;
194 builder
.Append(" (");
195 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append("<br>");
197 for (int i
= 0; i
< m
.Parameters
.Count
; ++i
) {
198 // if (IncludeHTMLMarkup(conversionFlags)) builder.Append(" ");
200 builder
.Append(Convert(m
.Parameters
[i
], conversionFlags
));
201 if (i
+ 1 < m
.Parameters
.Count
) builder
.Append(", ");
203 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append("<br>");
206 builder
.Append(") : ");
208 builder
.Append(Convert(m
.ReturnType
, conversionFlags
));
212 } else if (ShowInheritanceList(conversionFlags
) && c
.ClassType
!= ClassType
.Enum
) {
213 if (c
.BaseTypes
.Count
> 0) {
214 builder
.Append (" : ");
215 builder
.Append (Convert(c
.BaseTypes
[0]));
216 for (int i
= 1; i
< c
.BaseTypes
.Count
; ++i
) {
217 builder
.Append (", ");
218 builder
.Append (Convert(c
.BaseTypes
[i
]));
223 if (IncludeBodies(conversionFlags
)) {
224 builder
.Append("\n{");
227 return builder
.ToString();
230 public override string ConvertEnd(IClass c
, ConversionFlags conversionFlags
)
235 public override string Convert(IField field
, ConversionFlags conversionFlags
)
237 StringBuilder builder
= new StringBuilder();
239 builder
.Append(Convert(field
.Modifiers
, conversionFlags
));
241 if (ShowMemberModifiers(conversionFlags
)) {
242 if (field
.IsStatic
&& field
.IsLiteral
)
243 AppendPangoHtmlTag (builder
, "const ", "i", conversionFlags
);
244 else if (field
.IsStatic
)
245 AppendPangoHtmlTag (builder
, "static ", "i", conversionFlags
);
247 if (!field
.IsReadonly
) {
248 AppendPangoHtmlTag (builder
, "mutable ", "i", conversionFlags
);
252 if (UseFullyQualifiedMemberNames(conversionFlags
))
253 AppendPangoHtmlTag (builder
, field
.FullyQualifiedName
, "b", conversionFlags
);
255 AppendPangoHtmlTag (builder
, field
.Name
, "b", conversionFlags
);
257 if (field
.ReturnType
!= null) {
258 builder
.Append(" : ");
259 builder
.Append(Convert(field
.ReturnType
, conversionFlags
));
263 if (IncludeBodies(conversionFlags
))
266 return builder
.ToString();
269 public override string Convert(IProperty property
, ConversionFlags conversionFlags
)
271 StringBuilder builder
= new StringBuilder();
273 builder
.Append(Convert(property
.Modifiers
, conversionFlags
));
275 if (ShowMemberModifiers(conversionFlags
)) {
276 builder
.Append(GetModifier(property
, conversionFlags
));
279 if (UseFullyQualifiedMemberNames(conversionFlags
))
280 AppendPangoHtmlTag (builder
, property
.FullyQualifiedName
, "b", conversionFlags
);
282 AppendPangoHtmlTag (builder
, property
.Name
, "b", conversionFlags
);
284 if (property
.Parameters
.Count
> 0) {
285 builder
.Append(" (");
287 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append("<br>");
288 builder
.Append (Convert (property
.Parameters
[0], conversionFlags
));
290 for (int i
= 0; i
< property
.Parameters
.Count
; ++i
) {
291 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append("<br>");
292 builder
.Append(", ");
293 builder
.Append(Convert(property
.Parameters
[i
], conversionFlags
));
295 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append("<br>");
300 if (property
.ReturnType
!= null) {
301 builder
.Append(" : ");
302 builder
.Append(Convert(property
.ReturnType
, conversionFlags
));
306 builder
.Append(" { ");
308 if (property
.CanGet
) {
309 builder
.Append("get; ");
311 if (property
.CanSet
) {
312 builder
.Append("set; ");
315 builder
.Append(" } ");
317 return builder
.ToString();
320 public override string Convert(IEvent e
, ConversionFlags conversionFlags
)
322 StringBuilder builder
= new StringBuilder();
324 builder
.Append(Convert(e
.Modifiers
, conversionFlags
));
326 if (ShowMemberModifiers(conversionFlags
)) {
327 builder
.Append(GetModifier(e
, conversionFlags
));
330 if (UseFullyQualifiedMemberNames(conversionFlags
))
331 AppendPangoHtmlTag (builder
, e
.FullyQualifiedName
, "b", conversionFlags
);
333 AppendPangoHtmlTag (builder
, e
.Name
, "b", conversionFlags
);
335 if (e
.ReturnType
!= null) {
336 builder
.Append(" : ");
337 builder
.Append(Convert(e
.ReturnType
, conversionFlags
));
341 if (IncludeBodies(conversionFlags
)) builder
.Append(";");
343 return builder
.ToString();
346 public override string Convert(IIndexer m
, ConversionFlags conversionFlags
)
348 StringBuilder builder
= new StringBuilder();
349 builder
.Append(Convert(m
.Modifiers
, conversionFlags
));
351 if (ShowMemberModifiers(conversionFlags
) && m
.IsStatic
)
352 AppendPangoHtmlTag (builder
, "static", "i", conversionFlags
);
354 if (UseFullyQualifiedMemberNames (conversionFlags
))
355 AppendPangoHtmlTag (builder
, m
.FullyQualifiedName
, "b", conversionFlags
);
357 AppendPangoHtmlTag (builder
, m
.Name
, "b", conversionFlags
);
359 builder
.Append(" [");
361 if (m
.Parameters
.Count
> 0) {
362 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append ("<br>");
363 builder
.Append (Convert (m
.Parameters
[0], conversionFlags
));
364 for (int i
= 1; i
< m
.Parameters
.Count
; ++i
) {
365 builder
.Append (", ");
366 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append("<br>");
367 builder
.Append(Convert(m
.Parameters
[i
], conversionFlags
));
369 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append("<br>");
374 if (m
.ReturnType
!= null) {
375 builder
.Append(" : ");
376 builder
.Append(Convert(m
.ReturnType
, conversionFlags
));
380 if (IncludeBodies(conversionFlags
)) builder
.Append(";");
382 return builder
.ToString();
385 public override string Convert(IMethod m
, ConversionFlags conversionFlags
)
387 StringBuilder builder
= new StringBuilder();
388 builder
.Append(Convert(m
.Modifiers
, conversionFlags
));
389 if (ShowMemberModifiers(conversionFlags
)) {
390 builder
.Append(GetModifier(m
, conversionFlags
));
393 if (m
.IsConstructor
) {
394 AppendPangoHtmlTag (builder
, "this", "b", conversionFlags
);
396 if (UseFullyQualifiedMemberNames(conversionFlags
))
397 AppendPangoHtmlTag (builder
, m
.FullyQualifiedName
, "b", conversionFlags
);
399 AppendPangoHtmlTag (builder
, m
.Name
, "b", conversionFlags
);
402 // Display generic parameters only if told so
403 if (ShowGenericParameters(conversionFlags
) && m
.GenericParameters
!= null && m
.GenericParameters
.Count
> 0) {
404 bool includeMarkup
= IncludeHTMLMarkup(conversionFlags
) || IncludePangoMarkup(conversionFlags
);
405 builder
.Append ("[");
406 // Since we know that there is at least one generic parameter in
407 // the list, we can add it outside the loop - so, we don't have
408 // to check whether we may append a comma or not
409 builder
.Append (m
.GenericParameters
[0].Name
);
410 // Now continue with the others, if there are any
411 for (int i
= 1; i
< m
.GenericParameters
.Count
; i
++) {
412 builder
.Append (", ");
413 builder
.Append (m
.GenericParameters
[i
].Name
);
415 builder
.Append ("]");
418 builder
.Append(" (");
420 if (m
.Parameters
.Count
> 0) {
421 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append ("<br>");
422 builder
.Append (Convert (m
.Parameters
[0], conversionFlags
));
423 for (int i
= 1; i
< m
.Parameters
.Count
; ++i
) {
424 builder
.Append (", ");
425 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append("<br>");
426 builder
.Append(Convert(m
.Parameters
[i
], conversionFlags
));
428 if (IncludeHTMLMarkup(conversionFlags
)) builder
.Append("<br>");
432 if (m
.ReturnType
!= null) {
433 builder
.Append(" : ");
434 builder
.Append(Convert(m
.ReturnType
, conversionFlags
));
438 if (IncludeBodies(conversionFlags
)) {
439 if (m
.DeclaringType
!= null) {
440 if (m
.DeclaringType
.ClassType
== ClassType
.Interface
) {
443 builder
.Append(" {");
446 builder
.Append(" {");
449 return builder
.ToString();
452 public override string ConvertEnd(IMethod m
, ConversionFlags conversionFlags
)
457 public override string Convert(IReturnType returnType
, ConversionFlags conversionFlags
)
459 if (returnType
== null) {
462 StringBuilder builder
= new StringBuilder();
464 //bool linkSet = false;
466 //if (UseLinkArrayList(conversionFlags)) {
467 //SharpAssemblyReturnType ret = returnType as SharpAssemblyReturnType;
469 // if (ret.UnderlyingClass != null) {
470 // builder.Append("<a href='as://" + linkArrayList.Add(ret.UnderlyingClass) + "'>");
476 if (typeConversionTable
[returnType
.FullyQualifiedName
] != null) {
477 builder
.Append(typeConversionTable
[returnType
.FullyQualifiedName
].ToString());
479 if (UseFullyQualifiedMemberNames(conversionFlags
)) {
480 builder
.Append (returnType
.FullyQualifiedName
);
482 builder
.Append (returnType
.Name
);
486 // Display generic parameters only if told so
487 if (ShowGenericParameters(conversionFlags
) && returnType
.GenericArguments
!= null && returnType
.GenericArguments
.Count
> 0) {
488 bool includeMarkup
= IncludeHTMLMarkup(conversionFlags
) || IncludePangoMarkup(conversionFlags
);
489 builder
.Append ("[");
490 // Since we know that there is at least one generic argument in
491 // the list, we can add it outside the loop - so, we don't have
492 // to check whether we may append a comma or not
493 builder
.Append (Convert(returnType
.GenericArguments
[0], conversionFlags
));
494 // Now continue with the others, if there are any
495 for (int i
= 1; i
< returnType
.GenericArguments
.Count
; i
++) {
496 builder
.Append (", ");
497 builder
.Append ( Convert(returnType
.GenericArguments
[i
], conversionFlags
));
499 builder
.Append ("]");
503 // builder.Append("</a>");
506 if (returnType
.ArrayCount
> 0)
507 return "array[" + builder
.ToString() + "]";
509 return builder
.ToString();
512 public override string Convert(IParameter param
, ConversionFlags conversionFlags
)
514 StringBuilder builder
= new StringBuilder();
517 AppendPangoHtmlTag (builder
, "ref ", "i", conversionFlags
);
518 else if (param
.IsOut
)
519 AppendPangoHtmlTag (builder
, "out ", "i", conversionFlags
);
520 else if (param
.IsParams
)
521 AppendPangoHtmlTag (builder
, "params ", "i", conversionFlags
);
523 if (ShowParameterNames(conversionFlags
)) {
525 builder
.Append(param
.Name
);
528 builder
.Append(" : ");
529 builder
.Append(Convert(param
.ReturnType
, conversionFlags
));
531 return builder
.ToString();
534 public override string Convert(LocalVariable localVariable
, ConversionFlags conversionFlags
)
536 StringBuilder builder
= new StringBuilder();
538 builder
.Append(localVariable
.Name
);
539 builder
.Append(" : ");
540 builder
.Append(Convert(localVariable
.ReturnType
, conversionFlags
));
542 return builder
.ToString();
545 // pango has some problems with
546 // <i>static </i>bool <b>Equals</b> (<i></i>object a, <i></i>object b)
547 // it will make "object a" italics. so rather tan appending a markup
548 // tag if there might be a modifier, we only do it if there is.
549 void AppendPangoHtmlTag (StringBuilder sb
, string str
, string tag
, ConversionFlags conversionFlags
)
551 if (IncludeHTMLMarkup(conversionFlags
) | IncludePangoMarkup(conversionFlags
)) sb
.Append ('<').Append (tag
).Append ('>');
555 if (IncludeHTMLMarkup(conversionFlags
) | IncludePangoMarkup(conversionFlags
)) sb
.Append ("</").Append (tag
).Append ('>');
558 public override string WrapAttribute(string attribute
)
560 return "[" + attribute
+ "]";
563 public override string WrapComment(string comment
)
565 return "// " + comment
;
568 public override string GetIntrinsicTypeName(string dotNetTypeName
)
570 if (typeConversionTable
[dotNetTypeName
] != null) {
571 return (string)typeConversionTable
[dotNetTypeName
];
573 return dotNetTypeName
;