2007-02-03 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / ConditionParser.cs
1 //
2 // ConditionParser.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.Text;
35
36 namespace Microsoft.Build.BuildEngine {
37
38         internal class ConditionParser {
39         
40                 ConditionTokenizer tokenizer;
41                 
42                 ConditionParser (string condition)
43                 {
44                         tokenizer = new ConditionTokenizer ();
45                         tokenizer.Tokenize (condition);
46                 }
47                 
48                 public static bool ParseAndEvaluate (string condition, Project context)
49                 {
50                         if (String.IsNullOrEmpty (condition))
51                                 return true;
52
53                         ConditionExpression ce = ParseCondition (condition);
54
55                         if (!ce.CanEvaluateToBool (context))
56                                 throw new InvalidProjectFileException (String.Format ("Can not evaluate \"{0}\" to bool.", condition));
57
58                         return ce.BoolEvaluate (context);
59                 }
60
61                 public static ConditionExpression ParseCondition (string condition)
62                 {
63                         ConditionParser parser = new ConditionParser (condition);
64                         ConditionExpression e = parser.ParseExpression ();
65                         
66                         if (!parser.tokenizer.IsEOF ())
67                                 throw new ExpressionParseException (String.Format ("Unexpected token: \"{0}\"", parser.tokenizer.Token.Value));
68                         
69                         return e;
70                 }
71                 
72                 ConditionExpression ParseExpression ()
73                 {
74                         return ParseBooleanExpression ();
75                 }
76                 
77                 ConditionExpression ParseBooleanExpression ()
78                 {
79                         return ParseBooleanAnd ();
80                 }
81                 
82                 ConditionExpression ParseBooleanAnd ()
83                 {
84                         ConditionExpression e = ParseBooleanOr ();
85                         
86                         while (tokenizer.IsToken (TokenType.And)) {
87                                 tokenizer.GetNextToken ();
88                                 e = new ConditionAndExpression (e, ParseBooleanOr ());
89                         }
90                         
91                         return e;
92                 }
93                 
94                 ConditionExpression ParseBooleanOr ()
95                 {
96                         ConditionExpression e = ParseRelationalExpression ();
97                         
98                         while (tokenizer.IsToken (TokenType.Or)) {
99                                 tokenizer.GetNextToken ();
100                                 e = new ConditionOrExpression (e, ParseRelationalExpression ());
101                         }
102                         
103                         return e;
104                 }
105                 
106                 ConditionExpression ParseRelationalExpression ()
107                 {
108                         ConditionExpression e = ParseFactorExpression ();
109                         Token opToken;
110                         RelationOperator op;
111                         
112                         if (tokenizer.IsToken (TokenType.Less) ||
113                                 tokenizer.IsToken (TokenType.Greater) ||
114                                 tokenizer.IsToken (TokenType.Equal) ||
115                                 tokenizer.IsToken (TokenType.NotEqual) ||
116                                 tokenizer.IsToken (TokenType.LessOrEqual) ||
117                                 tokenizer.IsToken (TokenType.GreaterOrEqual)) {
118                                 
119                                 opToken = tokenizer.Token;
120                                 tokenizer.GetNextToken ();
121                                                                 
122                                 switch (opToken.Type) {
123                                 case TokenType.Equal:
124                                         op = RelationOperator.Equal;
125                                         break;
126                                 case TokenType.NotEqual:
127                                         op = RelationOperator.NotEqual;
128                                         break;
129                                 case TokenType.Less:
130                                         op = RelationOperator.Less;
131                                         break;
132                                 case TokenType.LessOrEqual:
133                                         op = RelationOperator.LessOrEqual;
134                                         break;
135                                 case TokenType.Greater:
136                                         op = RelationOperator.Greater;
137                                         break;
138                                 case TokenType.GreaterOrEqual:
139                                         op = RelationOperator.GreaterOrEqual;
140                                         break;
141                                 default:
142                                         throw new ExpressionParseException (String.Format ("Wrong relation operator {0}", opToken.Value));
143                                 }
144                                 
145                                 e =  new ConditionRelationalExpression (e, ParseFactorExpression (), op);
146                         }
147                         
148                         return e;
149                 }
150                 
151                 ConditionExpression ParseFactorExpression ()
152                 {
153                         ConditionExpression e;
154                         Token token = tokenizer.Token;
155                         tokenizer.GetNextToken ();
156
157                         if (token.Type == TokenType.LeftParen) {
158                                 e = ParseExpression ();
159                                 tokenizer.Expect (TokenType.RightParen);
160                         } else if (token.Type == TokenType.String)
161                                 e = new ConditionFactorExpression (token);
162                         else if (token.Type == TokenType.Number)
163                                 e = new ConditionFactorExpression (token);
164                         else if (token.Type == TokenType.FunctionName) {
165                                 e = ParseFunctionExpression ();
166                         } else if (token.Type == TokenType.Item) {
167                                 throw new NotImplementedException ();
168                         } else if (token.Type == TokenType.Property) {
169                                 throw new NotImplementedException ();
170                         } else if (token.Type == TokenType.Not) {
171                                 e = ParseNotExpression ();
172                         } else
173                                 throw new ExpressionParseException (String.Format ("Unexpected token type {0}.", token.Type));
174                         
175                         return e;
176                 }
177
178                 ConditionExpression ParseNotExpression ()
179                 {
180                         return new ConditionNotExpression (ParseFactorExpression ());
181                 }
182
183                 ConditionExpression ParseFunctionExpression ()
184                 {
185                         throw new NotImplementedException ();
186                 }
187         }
188 }
189
190 #endif