PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms 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
  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
 15using System;
 16using System.Collections.Generic;
 17using System.Runtime.CompilerServices;
 18using System.Text;
 19using IronPython;
 20using IronPython.Compiler;
 21using IronPython.Runtime;
 22using IronPython.Runtime.Types;
 23using Microsoft.Scripting;
 24using Microsoft.Scripting.Actions;
 25using Microsoft.Scripting.Runtime;
 26
 27namespace Microsoft.PyAnalysis.Values {
 28    internal static class Utils {
 29        internal static IList<string> DirHelper(object obj, bool showClr) {
 30            NamespaceTracker nt = obj as NamespaceTracker;
 31            if (nt != null) {
 32                return nt.GetMemberNames();
 33            }
 34
 35            var dir = showClr ? ClrModule.DirClr(obj) : ClrModule.Dir(obj);
 36            int len = dir.__len__();
 37            string[] result = new string[len];
 38            for (int i = 0; i < len; i++) {
 39                // TODO: validate
 40                result[i] = dir[i] as string;
 41            }
 42            return result;
 43        }
 44
 45        internal static List<object> MakeList(object obj) {
 46            var result = new List<object>();
 47            result.Add(obj);
 48            return result;
 49        }
 50
 51        internal static T[] RemoveFirst<T>(this T[] array) {
 52            if (array.Length < 1) {
 53                return new T[0];
 54            }
 55            T[] result = new T[array.Length - 1];
 56            Array.Copy(array, 1, result, 0, array.Length - 1);
 57            return result;
 58        }
 59
 60        internal static string StripDocumentation(string doc) {
 61            if (doc == null) {
 62                return String.Empty;
 63            }
 64            StringBuilder result = new StringBuilder(doc.Length);
 65            foreach (string line in doc.Split('\n')) {
 66                if (result.Length > 0) {
 67                    result.Append("\r\n");
 68                }
 69                result.Append(line.Trim());
 70            }
 71            return result.ToString();
 72        }
 73
 74        internal static string CleanDocumentation(string doc) {
 75            int ctr = 0;
 76            var result = new StringBuilder(doc.Length);
 77            foreach (char c in doc) {
 78                if (c == '\r') {
 79                    // pass
 80                } else if (c == '\n') {
 81                    ctr++;
 82                    if (ctr < 3) {
 83                        result.Append("\r\n");
 84                    }
 85                } else {
 86                    result.Append(c);
 87                    ctr = 0;
 88                }
 89            }
 90            return result.ToString().Trim();
 91        }
 92
 93        internal static string GetDocumentation(ProjectState projectState, object obj) {
 94            object doc;
 95            if (!projectState.TryGetMember(obj, "__doc__", out doc)) {
 96                return String.Empty;
 97            }
 98            return StripDocumentation(doc as string);
 99        }
100
101        internal static Parser CreateParser(SourceUnit sourceUnit, ErrorSink errorSink) {
102            return Parser.CreateParser(
103                new CompilerContext(sourceUnit, new PythonCompilerOptions(), errorSink),
104                new PythonOptions()
105                );
106        }
107
108
109        internal static ISet<Namespace> GetReturnTypes(BuiltinFunction func, ProjectState projectState) {
110            var result = new HashSet<Namespace>();
111            var found = new HashSet<Type>();
112            foreach (var target in func.Overloads.Targets) {
113                var targetInfo = (target as System.Reflection.MethodInfo);
114                if (targetInfo != null && !found.Contains(targetInfo.ReturnType)) {
115                    var pyType = ClrModule.GetPythonType(targetInfo.ReturnType);
116                    result.Add(((BuiltinClassInfo)projectState.GetNamespaceFromObjects(pyType)).Instance);
117                    found.Add(targetInfo.ReturnType);
118                }
119            }
120            return result;
121        }
122
123        internal static T First<T>(IEnumerable<T> sequence) where T : class {
124            if (sequence == null) {
125                return null;
126            }
127            var enumerator = sequence.GetEnumerator();
128            if (enumerator == null) {
129                return null;
130            }
131            try {
132                if (enumerator.MoveNext()) {
133                    return enumerator.Current;
134                } else {
135                    return null;
136                }
137            } finally {
138                enumerator.Dispose();
139            }
140        }
141
142        internal static T[] Concat<T>(T firstArg, T[] args) {
143            var newArgs = new T[args.Length + 1];
144            args.CopyTo(newArgs, 1);
145            newArgs[0] = firstArg;
146            return newArgs;
147        }
148
149        internal static T Peek<T>(this List<T> stack) {
150            return stack[stack.Count - 1];
151        }
152
153        internal static void Push<T>(this List<T> stack, T value) {
154            stack.Add(value);
155        }
156
157        internal static T Pop<T>(this List<T> stack) {
158            int pos = stack.Count - 1;
159            var result = stack[pos];
160            stack.RemoveAt(pos);
161            return result;
162        }
163    }
164
165    internal class ReferenceComparer<T> : IEqualityComparer<T> where T : class {
166        int IEqualityComparer<T>.GetHashCode(T obj) {
167            return RuntimeHelpers.GetHashCode(obj);
168        }
169
170        bool IEqualityComparer<T>.Equals(T x, T y) {
171            return Object.ReferenceEquals(x, y);
172        }
173    }
174}