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.Specialized;
37 namespace Microsoft.Build.BuildEngine {
39 internal sealed class ConditionTokenizer {
40 string inputString = null;
42 int tokenPosition = 0;
46 bool ignoreWhiteSpace = true;
48 static TokenType[] charIndexToTokenType = new TokenType[128];
49 static Hashtable keywordToTokenType = CollectionsUtil.CreateCaseInsensitiveHashtable ();
51 static ConditionTokenizer ()
53 for (int i = 0; i < 128; i++)
54 charIndexToTokenType [i] = TokenType.Invalid;
56 foreach (CharToTokenType cht in charToTokenType)
57 charIndexToTokenType [(int) cht.ch] = cht.tokenType;
61 public ConditionTokenizer ()
63 this.ignoreWhiteSpace = true;
66 public void Tokenize (string s)
69 throw new ArgumentNullException ("s");
73 this.token = new Token (null, TokenType.BOF);
78 private void SkipWhiteSpace ()
82 while ((ch = PeekChar ()) != -1) {
83 if (!Char.IsWhiteSpace ((char)ch))
89 private int PeekChar ()
91 if (position < inputString.Length)
92 return (int) inputString [position];
97 private int ReadChar ()
99 if (position < inputString.Length)
100 return (int) inputString [position++];
105 public void Expect (TokenType type)
107 if (token.Type != type)
108 throw new ExpressionParseException ("Expected token type of type: " + type + ", got " + token.Type +
109 " (" + token.Value + ") .");
116 return token.Type == TokenType.EOF;
119 public bool IsNumber ()
121 return token.Type == TokenType.Number;
124 public bool IsToken (TokenType type)
126 return token.Type == type;
129 public bool IsPunctation ()
131 return (token.Type >= TokenType.FirstPunct && token.Type < TokenType.LastPunct);
134 // FIXME: add 'and' and 'or' tokens
135 public void GetNextToken ()
137 if (token.Type == TokenType.EOF)
138 throw new ExpressionParseException ("Cannot read past the end of stream.");
140 if (IgnoreWhiteSpace)
143 tokenPosition = position;
148 token = new Token (null, TokenType.EOF);
154 if (IgnoreWhiteSpace == false && Char.IsWhiteSpace (ch)) {
155 StringBuilder sb = new StringBuilder ();
158 while ((ch2 = PeekChar ()) != -1) {
159 if (!Char.IsWhiteSpace ((char) ch2))
162 sb.Append ((char)ch2);
166 token = new Token (sb.ToString (), TokenType.WhiteSpace);
170 if (Char.IsDigit (ch)) {
171 StringBuilder sb = new StringBuilder ();
176 while ((i = PeekChar ()) != -1) {
179 if (Char.IsDigit (ch) || ch == '.')
180 sb.Append ((char) ReadChar ());
185 token = new Token (sb.ToString (), TokenType.Number);
190 StringBuilder sb = new StringBuilder ();
196 while ((i = PeekChar ()) != -1) {
199 sb.Append ((char) ReadChar ());
205 temp = sb.ToString ();
207 // FIXME: test extreme cases
208 token = new Token (temp.Substring (1, temp.Length - 2), TokenType.String);
213 if (ch == '_' || Char.IsLetter (ch)) {
214 StringBuilder sb = new StringBuilder ();
216 sb.Append ((char) ch);
219 while ((i = PeekChar ()) != -1) {
220 if ((char) i == '_' || Char.IsLetterOrDigit ((char) i))
221 sb.Append ((char) ReadChar ());
226 string temp = sb.ToString ();
228 if (temp.ToLower () == "and")
229 token = new Token (temp, TokenType.And);
230 else if (temp.ToLower () == "or")
231 token = new Token (temp, TokenType.Or);
233 token = new Token (sb.ToString (), TokenType.String);
239 if (ch == '!' && PeekChar () == (int) '=') {
240 token = new Token ("!=", TokenType.NotEqual);
245 if (ch == '<' && PeekChar () == (int) '=') {
246 token = new Token ("<=", TokenType.LessOrEqual);
251 if (ch == '>' && PeekChar () == (int) '=') {
252 token = new Token (">=", TokenType.GreaterOrEqual);
257 if (ch == '=' && PeekChar () == (int) '=') {
258 token = new Token ("==", TokenType.Equal);
263 if (ch == '-' && PeekChar () == (int) '>') {
264 token = new Token ("->", TokenType.Transform);
269 if (ch >= 32 && ch < 128) {
270 if (charIndexToTokenType [ch] != TokenType.Invalid) {
271 token = new Token (new String (ch, 1), charIndexToTokenType [ch]);
274 throw new ExpressionParseException (String.Format ("Invalid punctuation: {0}", ch));
277 throw new ExpressionParseException (String.Format ("Invalid token: {0}", ch));
280 public int TokenPosition {
281 get { return tokenPosition; }
285 get { return token; }
288 public bool IgnoreWhiteSpace {
289 get { return ignoreWhiteSpace; }
290 set { ignoreWhiteSpace = value; }
293 struct CharToTokenType {
295 public TokenType tokenType;
297 public CharToTokenType (char ch, TokenType tokenType)
300 this.tokenType = tokenType;
304 static CharToTokenType[] charToTokenType = {
305 new CharToTokenType ('<', TokenType.Less),
306 new CharToTokenType ('>', TokenType.Greater),
307 new CharToTokenType ('=', TokenType.Equal),
308 new CharToTokenType ('(', TokenType.LeftParen),
309 new CharToTokenType (')', TokenType.RightParen),
310 new CharToTokenType ('.', TokenType.Dot),
311 new CharToTokenType (',', TokenType.Comma),
312 new CharToTokenType ('!', TokenType.Not),
313 new CharToTokenType ('@', TokenType.Item),
314 new CharToTokenType ('$', TokenType.Property),
315 new CharToTokenType ('%', TokenType.Metadata),
316 new CharToTokenType ('\'', TokenType.Apostrophe),