New test.
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / ConditionTokenizer.cs
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 #if NET_2_0
31
32 using System;
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Collections.Specialized;
36 using System.Text;
37
38 namespace Microsoft.Build.BuildEngine {
39
40         internal sealed class ConditionTokenizer {
41         
42                 string  inputString = null;
43                 int     position = 0;
44                 int     tokenPosition = 0;
45                 
46                 Token   token;
47                 
48                 bool    ignoreWhiteSpace = true;
49                 
50                 static TokenType[] charIndexToTokenType = new TokenType[128];
51                 static Dictionary <string, TokenType> keywords = new Dictionary <string, TokenType> (StringComparer.InvariantCultureIgnoreCase);
52
53                 static ConditionTokenizer ()
54                 {
55                         for (int i = 0; i < 128; i++)
56                                 charIndexToTokenType [i] = TokenType.Invalid;
57                         
58                         foreach (CharToTokenType cht in charToTokenType)
59                                 charIndexToTokenType [(int) cht.ch] = cht.tokenType;
60                         
61                         keywords.Add ("and", TokenType.And);
62                         keywords.Add ("or", TokenType.Or);
63                 }
64                 
65                 public ConditionTokenizer ()
66                 {
67                         this.ignoreWhiteSpace = true;
68                 }
69                 
70                 public void Tokenize (string s)
71                 {
72                         if (s == null)
73                                 throw new ArgumentNullException ("s");
74                 
75                         this.inputString = s;
76                         this.position = 0;
77                         this.token = new Token (null, TokenType.BOF);
78
79                         GetNextToken ();
80                 }
81                 
82                 private void SkipWhiteSpace ()
83                 {
84                         int ch;
85                         
86                         while ((ch = PeekChar ()) != -1) {
87                                 if (!Char.IsWhiteSpace ((char)ch))
88                                         break;
89                                 ReadChar ();
90                         }
91                 }
92                 
93                 private int PeekChar ()
94                 {
95                         if (position < inputString.Length)
96                                 return (int) inputString [position];
97                         else
98                                 return -1;
99                 }
100                 
101                 private int ReadChar ()
102                 {
103                         if (position < inputString.Length)
104                                 return (int) inputString [position++];
105                         else
106                                 return -1;
107                 }
108                 
109                 public void Expect (TokenType type)
110                 {
111                         if (token.Type != type)
112                                 throw new ExpressionParseException ("Expected token type of type: " + type + ", got " + token.Type +
113                                         " (" + token.Value + ") .");
114                         
115                         GetNextToken ();
116                 }
117                 
118                 public bool IsEOF ()
119                 {
120                         return token.Type == TokenType.EOF;
121                 }
122                 
123                 public bool IsNumber ()
124                 {
125                         return token.Type == TokenType.Number;
126                 }
127                 
128                 public bool IsToken (TokenType type)
129                 {
130                         return token.Type == type;
131                 }
132                 
133                 public bool IsPunctation ()
134                 {
135                         return (token.Type >= TokenType.FirstPunct && token.Type < TokenType.LastPunct);
136                 }
137                 
138                 // FIXME: add 'and' and 'or' tokens
139                 public void GetNextToken ()
140                 {
141                         if (token.Type == TokenType.EOF)
142                                 throw new ExpressionParseException ("Cannot read past the end of stream.");
143                         
144                         if (IgnoreWhiteSpace)
145                                 SkipWhiteSpace ();
146                         
147                         tokenPosition = position;
148                         
149                         int i = PeekChar ();
150                         
151                         if (i == -1) {
152                                 token = new Token (null, TokenType.EOF);
153                                 return;
154                         }
155                         
156                         char ch = (char) i;
157                         
158                         if (IgnoreWhiteSpace == false && Char.IsWhiteSpace (ch)) {
159                                 StringBuilder sb = new StringBuilder ();
160                                 int ch2;
161
162                                 while ((ch2 = PeekChar ()) != -1)  {
163                                         if (!Char.IsWhiteSpace ((char) ch2))
164                                                 break;
165
166                                         sb.Append ((char)ch2);
167                                         ReadChar();
168                                 }
169                                 
170                                 token = new Token (sb.ToString (), TokenType.WhiteSpace);
171                                 return;
172                         }
173                         
174                         if (Char.IsDigit (ch)) {
175                                 StringBuilder sb = new StringBuilder ();
176                                 
177                                 sb.Append (ch);
178                                 ReadChar ();
179                                 
180                                 while ((i = PeekChar ()) != -1) {
181                                         ch = (char) i;
182                                         
183                                         if (Char.IsDigit (ch) || ch == '.')
184                                                 sb.Append ((char) ReadChar ());
185                                         else
186                                                 break;
187                                 }
188                                 
189                                 token = new Token (sb.ToString (), TokenType.Number);
190                                 return;
191                         }
192                         
193                         if (ch == '\'') {
194                                 StringBuilder sb = new StringBuilder ();
195                                 string temp;
196                                 
197                                 sb.Append (ch);
198                                 ReadChar ();
199                                 
200                                 while ((i = PeekChar ()) != -1) {
201                                         ch = (char) i;
202                                         
203                                         sb.Append ((char) ReadChar ());
204                                         
205                                         if (ch == '\'')
206                                                 break;
207                                 }
208                                 
209                                 temp = sb.ToString ();
210                                 
211                                 // FIXME: test extreme cases
212                                 // it fails on '$(something) == ''
213                                 token = new Token (temp.Substring (1, temp.Length - 2), TokenType.String);
214                                 
215                                 return;
216                         }
217                         
218                         if (ch == '_' || Char.IsLetter (ch)) {
219                                 StringBuilder sb = new StringBuilder ();
220                                 
221                                 sb.Append ((char) ch);
222                                 ReadChar ();
223                                 
224                                 while ((i = PeekChar ()) != -1) {
225                                         if ((char) i == '_' || Char.IsLetterOrDigit ((char) i))
226                                                 sb.Append ((char) ReadChar ());
227                                         else
228                                                 break;
229                                 }
230                                 
231                                 string temp = sb.ToString ();
232                                 
233                                 if (keywords.ContainsKey (temp))
234                                         token = new Token (temp, keywords [temp]);
235                                 else
236                                         token = new Token (temp, TokenType.String);
237                                         
238                                 return;
239                         }
240                         
241                         ReadChar ();
242                         
243                         if (ch == '!' && PeekChar () == (int) '=') {
244                                 token = new Token ("!=", TokenType.NotEqual);
245                                 ReadChar ();
246                                 return;
247                         }
248                         
249                         if (ch == '<' && PeekChar () == (int) '=') {
250                                 token = new Token ("<=", TokenType.LessOrEqual);
251                                 ReadChar ();
252                                 return;
253                         }
254                         
255                         if (ch == '>' && PeekChar () == (int) '=') {
256                                 token = new Token (">=", TokenType.GreaterOrEqual);
257                                 ReadChar ();
258                                 return;
259                         }
260                         
261                         if (ch == '=' && PeekChar () == (int) '=') {
262                                 token = new Token ("==", TokenType.Equal);
263                                 ReadChar ();
264                                 return;
265                         }
266                         
267                         if (ch == '-' && PeekChar () == (int) '>') {
268                                 token = new Token ("->", TokenType.Transform);
269                                 ReadChar ();
270                                 return;
271                         }
272                         
273                         if (ch >= 32 && ch < 128) {
274                                 if (charIndexToTokenType [ch] != TokenType.Invalid) {
275                                         token = new Token (new String (ch, 1), charIndexToTokenType [ch]);
276                                         return;
277                                 } else
278                                         throw new ExpressionParseException (String.Format ("Invalid punctuation: {0}", ch));
279                         }
280                         
281                         throw new ExpressionParseException (String.Format ("Invalid token: {0}", ch));
282                 }
283                 
284                 public int TokenPosition {
285                         get { return tokenPosition; }
286                 }
287                 
288                 public Token Token {
289                         get { return token; }
290                 }
291                 
292                 public bool IgnoreWhiteSpace {
293                         get { return ignoreWhiteSpace; }
294                         set { ignoreWhiteSpace = value; }
295                 }
296                 
297                 struct CharToTokenType {
298                         public char ch;
299                         public TokenType tokenType;
300                         
301                         public CharToTokenType (char ch, TokenType tokenType)
302                         {
303                                 this.ch = ch;
304                                 this.tokenType = tokenType;
305                         }
306                 }
307                 
308                 static CharToTokenType[] charToTokenType = {
309                         new CharToTokenType ('<', TokenType.Less),
310                         new CharToTokenType ('>', TokenType.Greater),
311                         new CharToTokenType ('=', TokenType.Equal),
312                         new CharToTokenType ('(', TokenType.LeftParen),
313                         new CharToTokenType (')', TokenType.RightParen),
314                         new CharToTokenType ('.', TokenType.Dot),
315                         new CharToTokenType (',', TokenType.Comma),
316                         new CharToTokenType ('!', TokenType.Not),
317                         new CharToTokenType ('@', TokenType.Item),
318                         new CharToTokenType ('$', TokenType.Property),
319                         new CharToTokenType ('%', TokenType.Metadata),
320                         new CharToTokenType ('\'', TokenType.Apostrophe),
321                 };
322         }
323 }
324
325 #endif