2 // ConditionTokenizer.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Jaroslaw Kowalski <jaak@jkowalski.net>
8 // (C) 2006 Marek Sieradzki
9 // (C) 2004-2006 Jaroslaw Kowalski
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Collections.Specialized;
38 namespace Microsoft.Build.BuildEngine {
40 internal sealed class ConditionTokenizer {
42 string inputString = null;
44 int tokenPosition = 0;
48 bool ignoreWhiteSpace = true;
50 static TokenType[] charIndexToTokenType = new TokenType[128];
51 static Dictionary <string, TokenType> keywords = new Dictionary <string, TokenType> (StringComparer.InvariantCultureIgnoreCase);
53 static ConditionTokenizer ()
55 for (int i = 0; i < 128; i++)
56 charIndexToTokenType [i] = TokenType.Invalid;
58 foreach (CharToTokenType cht in charToTokenType)
59 charIndexToTokenType [(int) cht.ch] = cht.tokenType;
61 keywords.Add ("and", TokenType.And);
62 keywords.Add ("or", TokenType.Or);
65 public ConditionTokenizer ()
67 this.ignoreWhiteSpace = true;
70 public void Tokenize (string s)
73 throw new ArgumentNullException ("s");
77 this.token = new Token (null, TokenType.BOF);
82 private void SkipWhiteSpace ()
86 while ((ch = PeekChar ()) != -1) {
87 if (!Char.IsWhiteSpace ((char)ch))
93 private int PeekChar ()
95 if (position < inputString.Length)
96 return (int) inputString [position];
101 private int ReadChar ()
103 if (position < inputString.Length)
104 return (int) inputString [position++];
109 public void Expect (TokenType type)
111 if (token.Type != type)
112 throw new ExpressionParseException ("Expected token type of type: " + type + ", got " + token.Type +
113 " (" + token.Value + ") .");
120 return token.Type == TokenType.EOF;
123 public bool IsNumber ()
125 return token.Type == TokenType.Number;
128 public bool IsToken (TokenType type)
130 return token.Type == type;
133 public bool IsPunctation ()
135 return (token.Type >= TokenType.FirstPunct && token.Type < TokenType.LastPunct);
138 // FIXME: add 'and' and 'or' tokens
139 public void GetNextToken ()
141 if (token.Type == TokenType.EOF)
142 throw new ExpressionParseException ("Cannot read past the end of stream.");
144 if (IgnoreWhiteSpace)
147 tokenPosition = position;
152 token = new Token (null, TokenType.EOF);
158 if (IgnoreWhiteSpace == false && Char.IsWhiteSpace (ch)) {
159 StringBuilder sb = new StringBuilder ();
162 while ((ch2 = PeekChar ()) != -1) {
163 if (!Char.IsWhiteSpace ((char) ch2))
166 sb.Append ((char)ch2);
170 token = new Token (sb.ToString (), TokenType.WhiteSpace);
174 // FIXME: looks like a hack: if '-' is here '->' won't be tokenized
175 // maybe we should treat item reference as a token
176 if (Char.IsDigit (ch) || ch == '-') {
177 StringBuilder sb = new StringBuilder ();
182 while ((i = PeekChar ()) != -1) {
185 if (Char.IsDigit (ch) || ch == '.')
186 sb.Append ((char) ReadChar ());
191 token = new Token (sb.ToString (), TokenType.Number);
196 StringBuilder sb = new StringBuilder ();
202 while ((i = PeekChar ()) != -1) {
205 sb.Append ((char) ReadChar ());
211 temp = sb.ToString ();
213 // FIXME: test extreme cases
214 // it fails on '$(something) == ''
215 token = new Token (temp.Substring (1, temp.Length - 2), TokenType.String);
220 if (ch == '_' || Char.IsLetter (ch)) {
221 StringBuilder sb = new StringBuilder ();
223 sb.Append ((char) ch);
226 while ((i = PeekChar ()) != -1) {
227 if ((char) i == '_' || Char.IsLetterOrDigit ((char) i))
228 sb.Append ((char) ReadChar ());
233 string temp = sb.ToString ();
235 if (keywords.ContainsKey (temp))
236 token = new Token (temp, keywords [temp]);
238 token = new Token (temp, TokenType.String);
245 if (ch == '!' && PeekChar () == (int) '=') {
246 token = new Token ("!=", TokenType.NotEqual);
251 if (ch == '<' && PeekChar () == (int) '=') {
252 token = new Token ("<=", TokenType.LessOrEqual);
257 if (ch == '>' && PeekChar () == (int) '=') {
258 token = new Token (">=", TokenType.GreaterOrEqual);
263 if (ch == '=' && PeekChar () == (int) '=') {
264 token = new Token ("==", TokenType.Equal);
269 if (ch == '-' && PeekChar () == (int) '>') {
270 token = new Token ("->", TokenType.Transform);
275 if (ch >= 32 && ch < 128) {
276 if (charIndexToTokenType [ch] != TokenType.Invalid) {
277 token = new Token (new String (ch, 1), charIndexToTokenType [ch]);
280 throw new ExpressionParseException (String.Format ("Invalid punctuation: {0}", ch));
283 throw new ExpressionParseException (String.Format ("Invalid token: {0}", ch));
286 public int TokenPosition {
287 get { return tokenPosition; }
291 get { return token; }
294 public bool IgnoreWhiteSpace {
295 get { return ignoreWhiteSpace; }
296 set { ignoreWhiteSpace = value; }
299 struct CharToTokenType {
301 public TokenType tokenType;
303 public CharToTokenType (char ch, TokenType tokenType)
306 this.tokenType = tokenType;
310 static CharToTokenType[] charToTokenType = {
311 new CharToTokenType ('<', TokenType.Less),
312 new CharToTokenType ('>', TokenType.Greater),
313 new CharToTokenType ('=', TokenType.Equal),
314 new CharToTokenType ('(', TokenType.LeftParen),
315 new CharToTokenType (')', TokenType.RightParen),
316 new CharToTokenType ('.', TokenType.Dot),
317 new CharToTokenType (',', TokenType.Comma),
318 new CharToTokenType ('!', TokenType.Not),
319 new CharToTokenType ('@', TokenType.Item),
320 new CharToTokenType ('$', TokenType.Property),
321 new CharToTokenType ('%', TokenType.Metadata),
322 new CharToTokenType ('\'', TokenType.Apostrophe),