PageRenderTime 36ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Tools/IronStudio/IronPythonToolsCore/PyAnalysis/Values/Utils.cs

#
C# | 174 lines | 141 code | 18 blank | 15 comment | 24 complexity | c66d9cee05f2586dfc2f54603ee96dfe MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. * ***************************************************************************/
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Runtime.CompilerServices;
  17. using System.Text;
  18. using IronPython;
  19. using IronPython.Compiler;
  20. using IronPython.Runtime;
  21. using IronPython.Runtime.Types;
  22. using Microsoft.Scripting;
  23. using Microsoft.Scripting.Actions;
  24. using Microsoft.Scripting.Runtime;
  25. namespace Microsoft.PyAnalysis.Values {
  26. internal static class Utils {
  27. internal static IList<string> DirHelper(object obj, bool showClr) {
  28. NamespaceTracker nt = obj as NamespaceTracker;
  29. if (nt != null) {
  30. return nt.GetMemberNames();
  31. }
  32. var dir = showClr ? ClrModule.DirClr(obj) : ClrModule.Dir(obj);
  33. int len = dir.__len__();
  34. string[] result = new string[len];
  35. for (int i = 0; i < len; i++) {
  36. // TODO: validate
  37. result[i] = dir[i] as string;
  38. }
  39. return result;
  40. }
  41. internal static List<object> MakeList(object obj) {
  42. var result = new List<object>();
  43. result.Add(obj);
  44. return result;
  45. }
  46. internal static T[] RemoveFirst<T>(this T[] array) {
  47. if (array.Length < 1) {
  48. return new T[0];
  49. }
  50. T[] result = new T[array.Length - 1];
  51. Array.Copy(array, 1, result, 0, array.Length - 1);
  52. return result;
  53. }
  54. internal static string StripDocumentation(string doc) {
  55. if (doc == null) {
  56. return String.Empty;
  57. }
  58. StringBuilder result = new StringBuilder(doc.Length);
  59. foreach (string line in doc.Split('\n')) {
  60. if (result.Length > 0) {
  61. result.Append("\r\n");
  62. }
  63. result.Append(line.Trim());
  64. }
  65. return result.ToString();
  66. }
  67. internal static string CleanDocumentation(string doc) {
  68. int ctr = 0;
  69. var result = new StringBuilder(doc.Length);
  70. foreach (char c in doc) {
  71. if (c == '\r') {
  72. // pass
  73. } else if (c == '\n') {
  74. ctr++;
  75. if (ctr < 3) {
  76. result.Append("\r\n");
  77. }
  78. } else {
  79. result.Append(c);
  80. ctr = 0;
  81. }
  82. }
  83. return result.ToString().Trim();
  84. }
  85. internal static string GetDocumentation(ProjectState projectState, object obj) {
  86. object doc;
  87. if (!projectState.TryGetMember(obj, "__doc__", out doc)) {
  88. return String.Empty;
  89. }
  90. return StripDocumentation(doc as string);
  91. }
  92. internal static Parser CreateParser(SourceUnit sourceUnit, ErrorSink errorSink) {
  93. return Parser.CreateParser(
  94. new CompilerContext(sourceUnit, new PythonCompilerOptions(), errorSink),
  95. new PythonOptions()
  96. );
  97. }
  98. internal static ISet<Namespace> GetReturnTypes(BuiltinFunction func, ProjectState projectState) {
  99. var result = new HashSet<Namespace>();
  100. var found = new HashSet<Type>();
  101. foreach (var target in func.Overloads.Targets) {
  102. var targetInfo = (target as System.Reflection.MethodInfo);
  103. if (targetInfo != null && !found.Contains(targetInfo.ReturnType)) {
  104. var pyType = ClrModule.GetPythonType(targetInfo.ReturnType);
  105. result.Add(((BuiltinClassInfo)projectState.GetNamespaceFromObjects(pyType)).Instance);
  106. found.Add(targetInfo.ReturnType);
  107. }
  108. }
  109. return result;
  110. }
  111. internal static T First<T>(IEnumerable<T> sequence) where T : class {
  112. if (sequence == null) {
  113. return null;
  114. }
  115. var enumerator = sequence.GetEnumerator();
  116. if (enumerator == null) {
  117. return null;
  118. }
  119. try {
  120. if (enumerator.MoveNext()) {
  121. return enumerator.Current;
  122. } else {
  123. return null;
  124. }
  125. } finally {
  126. enumerator.Dispose();
  127. }
  128. }
  129. internal static T[] Concat<T>(T firstArg, T[] args) {
  130. var newArgs = new T[args.Length + 1];
  131. args.CopyTo(newArgs, 1);
  132. newArgs[0] = firstArg;
  133. return newArgs;
  134. }
  135. internal static T Peek<T>(this List<T> stack) {
  136. return stack[stack.Count - 1];
  137. }
  138. internal static void Push<T>(this List<T> stack, T value) {
  139. stack.Add(value);
  140. }
  141. internal static T Pop<T>(this List<T> stack) {
  142. int pos = stack.Count - 1;
  143. var result = stack[pos];
  144. stack.RemoveAt(pos);
  145. return result;
  146. }
  147. }
  148. internal class ReferenceComparer<T> : IEqualityComparer<T> where T : class {
  149. int IEqualityComparer<T>.GetHashCode(T obj) {
  150. return RuntimeHelpers.GetHashCode(obj);
  151. }
  152. bool IEqualityComparer<T>.Equals(T x, T y) {
  153. return Object.ReferenceEquals(x, y);
  154. }
  155. }
  156. }