~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Parser/mcs/support.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// support.cs: Support routines to work around the fact that System.Reflection.Emit
3
 
// can not introspect types that are being constructed
4
 
//
5
 
// Author:
6
 
//   Miguel de Icaza (miguel@ximian.com)
7
 
//   Marek Safar (marek.safar@gmail.com)
8
 
//
9
 
// Copyright 2001 Ximian, Inc (http://www.ximian.com)
10
 
// Copyright 2003-2009 Novell, Inc
11
 
// Copyright 2011 Xamarin Inc
12
 
//
13
 
 
14
 
using System;
15
 
using System.Linq;
16
 
using System.IO;
17
 
using System.Text;
18
 
using System.Collections.Generic;
19
 
 
20
 
namespace Mono.CSharp {
21
 
 
22
 
        sealed class ReferenceEquality<T> : IEqualityComparer<T> where T : class
23
 
        {
24
 
                public static readonly IEqualityComparer<T> Default = new ReferenceEquality<T> ();
25
 
 
26
 
                private ReferenceEquality ()
27
 
                {
28
 
                }
29
 
 
30
 
                public bool Equals (T x, T y)
31
 
                {
32
 
                        return ReferenceEquals (x, y);
33
 
                }
34
 
 
35
 
                public int GetHashCode (T obj)
36
 
                {
37
 
                        return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode (obj);
38
 
                }
39
 
        }
40
 
#if !NET_4_0 && !MONODROID
41
 
        public class Tuple<T1, T2> : IEquatable<Tuple<T1, T2>>
42
 
        {
43
 
                public Tuple (T1 item1, T2 item2)
44
 
                {
45
 
                        Item1 = item1;
46
 
                        Item2 = item2;
47
 
                }
48
 
 
49
 
                public T1 Item1 { get; private set; }
50
 
                public T2 Item2 { get; private set; }
51
 
 
52
 
                public override int GetHashCode ()
53
 
                {
54
 
                        return ((object)Item1 ?? 0) .GetHashCode () ^ ((object)Item2 ?? 0).GetHashCode ();
55
 
                }
56
 
 
57
 
                #region IEquatable<Tuple<T1,T2>> Members
58
 
 
59
 
                public bool Equals (Tuple<T1, T2> other)
60
 
                {
61
 
                        return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
62
 
                                EqualityComparer<T2>.Default.Equals (Item2, other.Item2);
63
 
                }
64
 
 
65
 
                #endregion
66
 
        }
67
 
 
68
 
        public class Tuple<T1, T2, T3> : IEquatable<Tuple<T1, T2, T3>>
69
 
        {
70
 
                public Tuple (T1 item1, T2 item2, T3 item3)
71
 
                {
72
 
                        Item1 = item1;
73
 
                        Item2 = item2;
74
 
                        Item3 = item3;
75
 
                }
76
 
 
77
 
                public T1 Item1 { get; private set; }
78
 
                public T2 Item2 { get; private set; }
79
 
                public T3 Item3 { get; private set; }
80
 
 
81
 
                public override int GetHashCode ()
82
 
                {
83
 
                        return Item1.GetHashCode () ^ Item2.GetHashCode () ^ Item3.GetHashCode ();
84
 
                }
85
 
 
86
 
                #region IEquatable<Tuple<T1,T2>> Members
87
 
 
88
 
                public bool Equals (Tuple<T1, T2, T3> other)
89
 
                {
90
 
                        return EqualityComparer<T1>.Default.Equals (Item1, other.Item1) &&
91
 
                                EqualityComparer<T2>.Default.Equals (Item2, other.Item2) &&
92
 
                                EqualityComparer<T3>.Default.Equals (Item3, other.Item3);
93
 
                }
94
 
 
95
 
                #endregion
96
 
        }
97
 
 
98
 
        static class Tuple
99
 
        {
100
 
                public static Tuple<T1, T2> Create<T1, T2> (T1 item1, T2 item2)
101
 
                {
102
 
                        return new Tuple<T1, T2> (item1, item2);
103
 
                }
104
 
 
105
 
                public static Tuple<T1, T2, T3> Create<T1, T2, T3> (T1 item1, T2 item2, T3 item3)
106
 
                {
107
 
                        return new Tuple<T1, T2, T3> (item1, item2, item3);
108
 
                }
109
 
        }
110
 
#endif
111
 
 
112
 
        static class ArrayComparer
113
 
        {
114
 
                public static bool IsEqual<T> (T[] array1, T[] array2)
115
 
                {
116
 
                        if (array1 == null || array2 == null)
117
 
                                return array1 == array2;
118
 
 
119
 
                        var eq = EqualityComparer<T>.Default;
120
 
 
121
 
                        for (int i = 0; i < array1.Length; ++i) {
122
 
                                if (!eq.Equals (array1[i], array2[i])) {
123
 
                                        return false;
124
 
                                }
125
 
                        }
126
 
 
127
 
                        return true;
128
 
                }
129
 
        }
130
 
 
131
 
        /// <summary>
132
 
        ///   This is an arbitrarily seekable StreamReader wrapper.
133
 
        ///
134
 
        ///   It uses a self-tuning buffer to cache the seekable data,
135
 
        ///   but if the seek is too far, it may read the underly
136
 
        ///   stream all over from the beginning.
137
 
        /// </summary>
138
 
        public class SeekableStreamReader : IDisposable
139
 
        {
140
 
                StreamReader reader;
141
 
                Stream stream;
142
 
 
143
 
                static char[] buffer;
144
 
                int read_ahead_length;  // the length of read buffer
145
 
                int buffer_start;       // in chars
146
 
                int char_count;         // count of filled characters in buffer[]
147
 
                int pos;                // index into buffer[]
148
 
 
149
 
                public SeekableStreamReader (Stream stream, Encoding encoding)
150
 
                {
151
 
                        this.stream = stream;
152
 
 
153
 
                        const int default_read_ahead = 2048;
154
 
                        InitializeStream (default_read_ahead);
155
 
                        reader = new StreamReader (stream, encoding, true);
156
 
                }
157
 
 
158
 
                public void Dispose ()
159
 
                {
160
 
                        // Needed to release stream reader buffers
161
 
                        reader.Dispose ();
162
 
                }
163
 
 
164
 
                void InitializeStream (int read_length_inc)
165
 
                {
166
 
                        read_ahead_length += read_length_inc;
167
 
 
168
 
                        int required_buffer_size = read_ahead_length * 2;
169
 
 
170
 
                        if (buffer == null || buffer.Length < required_buffer_size)
171
 
                                buffer = new char [required_buffer_size];
172
 
 
173
 
                        stream.Position = 0;
174
 
                        buffer_start = char_count = pos = 0;
175
 
                }
176
 
 
177
 
                /// <remarks>
178
 
                ///   This value corresponds to the current position in a stream of characters.
179
 
                ///   The StreamReader hides its manipulation of the underlying byte stream and all
180
 
                ///   character set/decoding issues.  Thus, we cannot use this position to guess at
181
 
                ///   the corresponding position in the underlying byte stream even though there is
182
 
                ///   a correlation between them.
183
 
                /// </remarks>
184
 
                public int Position {
185
 
                        get {
186
 
                                return buffer_start + pos;
187
 
                        }
188
 
 
189
 
                        set {
190
 
                                //
191
 
                                // If the lookahead was too small, re-read from the beginning. Increase the buffer size while we're at it
192
 
                                // This should never happen until we are parsing some weird source code
193
 
                                //
194
 
                                if (value < buffer_start) {
195
 
                                        InitializeStream (read_ahead_length);
196
 
 
197
 
                                        //
198
 
                                        // Discard buffer data after underlying stream changed position
199
 
                                        // Cannot use handy reader.DiscardBufferedData () because it for
200
 
                                        // some strange reason resets encoding as well
201
 
                                        //
202
 
                                        reader = new StreamReader (stream, reader.CurrentEncoding, true);
203
 
                                }
204
 
 
205
 
                                while (value > buffer_start + char_count) {
206
 
                                        pos = char_count;
207
 
                                        if (!ReadBuffer ())
208
 
                                                throw new InternalErrorException ("Seek beyond end of file: " + (buffer_start + char_count - value));
209
 
                                }
210
 
 
211
 
                                pos = value - buffer_start;
212
 
                        }
213
 
                }
214
 
 
215
 
                bool ReadBuffer ()
216
 
                {
217
 
                        int slack = buffer.Length - char_count;
218
 
 
219
 
                        //
220
 
                        // read_ahead_length is only half of the buffer to deal with
221
 
                        // reads ahead and moves back without re-reading whole buffer
222
 
                        //
223
 
                        if (slack <= read_ahead_length) {
224
 
                                //
225
 
                                // shift the buffer to make room for read_ahead_length number of characters
226
 
                                //
227
 
                                int shift = read_ahead_length - slack;
228
 
                                Array.Copy (buffer, shift, buffer, 0, char_count - shift);
229
 
 
230
 
                                // Update all counters
231
 
                                pos -= shift;
232
 
                                char_count -= shift;
233
 
                                buffer_start += shift;
234
 
                                slack += shift;
235
 
                        }
236
 
 
237
 
                        char_count += reader.Read (buffer, char_count, slack);
238
 
 
239
 
                        return pos < char_count;
240
 
                }
241
 
                
242
 
                public char GetChar (int position)
243
 
                {
244
 
                        if (buffer_start <= position && position < buffer.Length)
245
 
                                return buffer[position];
246
 
                        return '\0';
247
 
                }
248
 
                
249
 
                public char[] ReadChars (int fromPosition, int toPosition)
250
 
                {
251
 
                        char[] chars = new char[toPosition - fromPosition];
252
 
                        if (buffer_start <= fromPosition && toPosition <= buffer_start + buffer.Length) {
253
 
                                Array.Copy (buffer, fromPosition - buffer_start, chars, 0, chars.Length);
254
 
                        } else {
255
 
                                throw new NotImplementedException ();
256
 
                        }
257
 
 
258
 
                        return chars;
259
 
                }
260
 
 
261
 
                public int Peek ()
262
 
                {
263
 
                        if ((pos >= char_count) && !ReadBuffer ())
264
 
                                return -1;
265
 
 
266
 
                        return buffer [pos];
267
 
                }
268
 
 
269
 
                public int Read ()
270
 
                {
271
 
                        if ((pos >= char_count) && !ReadBuffer ())
272
 
                                return -1;
273
 
 
274
 
                        return buffer [pos++];
275
 
                }
276
 
        }
277
 
 
278
 
        public class UnixUtils {
279
 
                [System.Runtime.InteropServices.DllImport ("libc", EntryPoint="isatty")]
280
 
                extern static int _isatty (int fd);
281
 
                        
282
 
                public static bool isatty (int fd)
283
 
                {
284
 
                        try {
285
 
                                return _isatty (fd) == 1;
286
 
                        } catch {
287
 
                                return false;
288
 
                        }
289
 
                }
290
 
        }
291
 
 
292
 
        /// <summary>
293
 
        ///   An exception used to terminate the compiler resolution phase and provide completions
294
 
        /// </summary>
295
 
        /// <remarks>
296
 
        ///   This is thrown when we want to return the completions or
297
 
        ///   terminate the completion process by AST nodes used in
298
 
        ///   the completion process.
299
 
        /// </remarks>
300
 
        public class CompletionResult : Exception {
301
 
                string [] result;
302
 
                string base_text;
303
 
                
304
 
                public CompletionResult (string base_text, string [] res)
305
 
                {
306
 
                        if (base_text == null)
307
 
                                throw new ArgumentNullException ("base_text");
308
 
                        this.base_text = base_text;
309
 
 
310
 
                        result = res;
311
 
                        Array.Sort (result);
312
 
                }
313
 
 
314
 
                public string [] Result {
315
 
                        get {
316
 
                                return result;
317
 
                        }
318
 
                }
319
 
 
320
 
                public string BaseText {
321
 
                        get {
322
 
                                return base_text;
323
 
                        }
324
 
                }
325
 
        }
326
 
}