~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Projects/MonoDevelop.Projects.Formats.MSBuild.Conditions/ConditionTokenizer.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-07-05 13:00:05 UTC
  • mfrom: (1.2.8 upstream) (1.3.9 experimental)
  • Revision ID: james.westby@ubuntu.com-20100705130005-d6hp4k5gcn1xkj8c
Tags: 2.4+dfsg-1ubuntu1
* debian/patches/remove_support_for_moonlight.patch,
  debian/patches/dont_add_moonlight_to_core_addins.patch,
  debian/control:
  + Enable support for Moonlight
* debian/rules:
  + Ensure Moonlight addin isn't shipped in main MonoDevelop package by
    mistake

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// ConditionTokenizer.cs
3
 
//
4
 
// Author:
5
 
//   Marek Sieradzki (marek.sieradzki@gmail.com)
6
 
//   Jaroslaw Kowalski <jaak@jkowalski.net>
7
 
// 
8
 
// (C) 2006 Marek Sieradzki
9
 
// (C) 2004-2006 Jaroslaw Kowalski
10
 
//
11
 
// Permission is hereby granted, free of charge, to any person obtaining
12
 
// a copy of this software and associated documentation files (the
13
 
// "Software"), to deal in the Software without restriction, including
14
 
// without limitation the rights to use, copy, modify, merge, publish,
15
 
// distribute, sublicense, and/or sell copies of the Software, and to
16
 
// permit persons to whom the Software is furnished to do so, subject to
17
 
// the following conditions:
18
 
//
19
 
// The above copyright notice and this permission notice shall be
20
 
// included in all copies or substantial portions of the Software.
21
 
//
22
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
 
 
30
 
using System;
31
 
using System.Collections;
32
 
using System.Collections.Generic;
33
 
using System.Collections.Specialized;
34
 
using System.Text;
35
 
 
36
 
namespace Microsoft.Build.BuildEngine {
37
 
 
38
 
        internal sealed class ConditionTokenizer {
39
 
        
40
 
                string  inputString = null;
41
 
                int     position = 0;
42
 
                int     tokenPosition = 0;
43
 
                
44
 
                Token   token;
45
 
                Token   putback = null;
46
 
                
47
 
//              bool    ignoreWhiteSpace = true;
48
 
                
49
 
                static TokenType[] charIndexToTokenType = new TokenType[128];
50
 
                static Dictionary <string, TokenType> keywords = new Dictionary <string, TokenType> (StringComparer.InvariantCultureIgnoreCase);
51
 
 
52
 
                static ConditionTokenizer ()
53
 
                {
54
 
                        for (int i = 0; i < 128; i++)
55
 
                                charIndexToTokenType [i] = TokenType.Invalid;
56
 
                        
57
 
                        foreach (CharToTokenType cht in charToTokenType)
58
 
                                charIndexToTokenType [(int) cht.ch] = cht.tokenType;
59
 
                        
60
 
                        keywords.Add ("and", TokenType.And);
61
 
                        keywords.Add ("or", TokenType.Or);
62
 
                }
63
 
                
64
 
                public ConditionTokenizer ()
65
 
                {
66
 
//                      this.ignoreWhiteSpace = true;
67
 
                }
68
 
                
69
 
                public void Tokenize (string s)
70
 
                {
71
 
                        if (s == null)
72
 
                                throw new ArgumentNullException ("s");
73
 
                
74
 
                        this.inputString = s;
75
 
                        this.position = 0;
76
 
                        this.token = new Token (null, TokenType.BOF);
77
 
 
78
 
                        GetNextToken ();
79
 
                }
80
 
                
81
 
                void SkipWhiteSpace ()
82
 
                {
83
 
                        int ch;
84
 
                        
85
 
                        while ((ch = PeekChar ()) != -1) {
86
 
                                if (!Char.IsWhiteSpace ((char)ch))
87
 
                                        break;
88
 
                                ReadChar ();
89
 
                        }
90
 
                }
91
 
                
92
 
                int PeekChar ()
93
 
                {
94
 
                        if (position < inputString.Length)
95
 
                                return (int) inputString [position];
96
 
                        else
97
 
                                return -1;
98
 
                }
99
 
                
100
 
                int ReadChar ()
101
 
                {
102
 
                        if (position < inputString.Length)
103
 
                                return (int) inputString [position++];
104
 
                        else
105
 
                                return -1;
106
 
                }
107
 
                
108
 
                public void Expect (TokenType type)
109
 
                {
110
 
                        if (token.Type != type)
111
 
                                throw new ExpressionParseException ("Expected token type of type: " + type + ", got " + token.Type +
112
 
                                        " (" + token.Value + ") .");
113
 
                        
114
 
                        GetNextToken ();
115
 
                }
116
 
                
117
 
                public bool IsEOF ()
118
 
                {
119
 
                        return token.Type == TokenType.EOF;
120
 
                }
121
 
                
122
 
                public bool IsNumber ()
123
 
                {
124
 
                        return token.Type == TokenType.Number;
125
 
                }
126
 
                
127
 
                public bool IsToken (TokenType type)
128
 
                {
129
 
                        return token.Type == type;
130
 
                }
131
 
                
132
 
                public bool IsPunctation ()
133
 
                {
134
 
                        return (token.Type >= TokenType.FirstPunct && token.Type < TokenType.LastPunct);
135
 
                }
136
 
                
137
 
                // FIXME test this
138
 
                public void Putback (Token token)
139
 
                {
140
 
                        putback = token;
141
 
                }
142
 
                
143
 
                public void GetNextToken ()
144
 
                {
145
 
                        if (putback != null) {
146
 
                                token = putback;
147
 
                                putback = null;
148
 
                                return;
149
 
                        }
150
 
                
151
 
                        if (token.Type == TokenType.EOF)
152
 
                                throw new ExpressionParseException ("Cannot read past the end of stream.");
153
 
                        
154
 
                        SkipWhiteSpace ();
155
 
                        
156
 
                        tokenPosition = position;
157
 
                        
158
 
//                      int i = PeekChar ();
159
 
                        int i = ReadChar ();
160
 
                        
161
 
                        if (i == -1) {
162
 
                                token = new Token (null, TokenType.EOF);
163
 
                                return;
164
 
                        }
165
 
                        
166
 
                        char ch = (char) i;
167
 
 
168
 
                        
169
 
                        // FIXME: looks like a hack: if '-' is here '->' won't be tokenized
170
 
                        // maybe we should treat item reference as a token
171
 
                        if (ch == '-' && PeekChar () == '>') {
172
 
                                ReadChar ();
173
 
                                token = new Token ("->", TokenType.Transform);
174
 
                        } else if (Char.IsDigit (ch) || ch == '-') {
175
 
                                StringBuilder sb = new StringBuilder ();
176
 
                                
177
 
                                sb.Append (ch);
178
 
                                
179
 
                                while ((i = PeekChar ()) != -1) {
180
 
                                        ch = (char) i;
181
 
                                        
182
 
                                        if (Char.IsDigit (ch) || ch == '.')
183
 
                                                sb.Append ((char) ReadChar ());
184
 
                                        else
185
 
                                                break;
186
 
                                }
187
 
                                
188
 
                                token = new Token (sb.ToString (), TokenType.Number);
189
 
                        } else if (ch == '\'') {
190
 
                                StringBuilder sb = new StringBuilder ();
191
 
                                string temp;
192
 
                                
193
 
                                sb.Append (ch);
194
 
                                bool is_itemref = (PeekChar () == '@');
195
 
                                int num_open_braces = 0;
196
 
                                bool in_literal = false;
197
 
                                
198
 
                                while ((i = PeekChar ()) != -1) {
199
 
                                        ch = (char) i;
200
 
                                        if (ch == '(' && !in_literal && is_itemref)
201
 
                                                num_open_braces ++;
202
 
                                        if (ch == ')' && !in_literal && is_itemref)
203
 
                                                num_open_braces --;
204
 
                                        
205
 
                                        sb.Append ((char) ReadChar ());
206
 
                                        
207
 
                                        if (ch == '\'') {
208
 
                                                if (num_open_braces == 0)
209
 
                                                        break;
210
 
                                                in_literal = !in_literal;
211
 
                                        }
212
 
                                }
213
 
                                
214
 
                                temp = sb.ToString ();
215
 
                                
216
 
                                token = new Token (temp.Substring (1, temp.Length - 2), TokenType.String);
217
 
                                
218
 
                        } else  if (ch == '_' || Char.IsLetter (ch)) {
219
 
                                StringBuilder sb = new StringBuilder ();
220
 
                                
221
 
                                sb.Append ((char) ch);
222
 
                                
223
 
                                while ((i = PeekChar ()) != -1) {
224
 
                                        if ((char) i == '_' || Char.IsLetterOrDigit ((char) i))
225
 
                                                sb.Append ((char) ReadChar ());
226
 
                                        else
227
 
                                                break;
228
 
                                }
229
 
                                
230
 
                                string temp = sb.ToString ();
231
 
                                
232
 
                                if (keywords.ContainsKey (temp))
233
 
                                        token = new Token (temp, keywords [temp]);
234
 
                                else
235
 
                                        token = new Token (temp, TokenType.String);
236
 
                                        
237
 
                        } else if (ch == '!' && PeekChar () == (int) '=') {
238
 
                                token = new Token ("!=", TokenType.NotEqual);
239
 
                                ReadChar ();
240
 
                        } else if (ch == '<' && PeekChar () == (int) '=') {
241
 
                                token = new Token ("<=", TokenType.LessOrEqual);
242
 
                                ReadChar ();
243
 
                        } else if (ch == '>' && PeekChar () == (int) '=') {
244
 
                                token = new Token (">=", TokenType.GreaterOrEqual);
245
 
                                ReadChar ();
246
 
                        } else if (ch == '=' && PeekChar () == (int) '=') {
247
 
                                token = new Token ("==", TokenType.Equal);
248
 
                                ReadChar ();
249
 
                        } else if (ch >= 32 && ch < 128) {
250
 
                                if (charIndexToTokenType [ch] != TokenType.Invalid) {
251
 
                                        token = new Token (new String (ch, 1), charIndexToTokenType [ch]);
252
 
                                        return;
253
 
                                } else
254
 
                                        throw new ExpressionParseException (String.Format ("Invalid punctuation: {0}", ch));
255
 
                        } else
256
 
                                throw new ExpressionParseException (String.Format ("Invalid token: {0}", ch));
257
 
                }
258
 
                
259
 
                public int TokenPosition {
260
 
                        get { return tokenPosition; }
261
 
                }
262
 
                
263
 
                public Token Token {
264
 
                        get { return token; }
265
 
                }
266
 
                
267
 
/*
268
 
                public bool IgnoreWhiteSpace {
269
 
                        get { return ignoreWhiteSpace; }
270
 
                        set { ignoreWhiteSpace = value; }
271
 
                }
272
 
*/
273
 
                
274
 
                struct CharToTokenType {
275
 
                        public char ch;
276
 
                        public TokenType tokenType;
277
 
                        
278
 
                        public CharToTokenType (char ch, TokenType tokenType)
279
 
                        {
280
 
                                this.ch = ch;
281
 
                                this.tokenType = tokenType;
282
 
                        }
283
 
                }
284
 
                
285
 
                static CharToTokenType[] charToTokenType = {
286
 
                        new CharToTokenType ('<', TokenType.Less),
287
 
                        new CharToTokenType ('>', TokenType.Greater),
288
 
                        new CharToTokenType ('=', TokenType.Equal),
289
 
                        new CharToTokenType ('(', TokenType.LeftParen),
290
 
                        new CharToTokenType (')', TokenType.RightParen),
291
 
                        new CharToTokenType ('.', TokenType.Dot),
292
 
                        new CharToTokenType (',', TokenType.Comma),
293
 
                        new CharToTokenType ('!', TokenType.Not),
294
 
                        new CharToTokenType ('@', TokenType.Item),
295
 
                        new CharToTokenType ('$', TokenType.Property),
296
 
                        new CharToTokenType ('%', TokenType.Metadata),
297
 
                        new CharToTokenType ('\'', TokenType.Apostrophe),
298
 
                };
299
 
        }
300
 
}
301