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;
36 namespace Microsoft.Build.BuildEngine {
38 internal class ConditionParser {
40 ConditionTokenizer tokenizer;
42 ConditionParser (string condition)
44 tokenizer = new ConditionTokenizer ();
45 tokenizer.Tokenize (condition);
48 public static bool ParseAndEvaluate (string condition, Project context)
50 if (String.IsNullOrEmpty (condition))
53 ConditionExpression ce = ParseCondition (condition);
55 if (!ce.CanEvaluateToBool (context))
56 throw new InvalidProjectFileException (String.Format ("Can not evaluate \"{0}\" to bool.", condition));
58 return ce.BoolEvaluate (context);
61 public static ConditionExpression ParseCondition (string condition)
63 ConditionParser parser = new ConditionParser (condition);
64 ConditionExpression e = parser.ParseExpression ();
66 if (!parser.tokenizer.IsEOF ())
67 throw new ExpressionParseException (String.Format ("Unexpected token: \"{0}\"", parser.tokenizer.Token.Value));
72 ConditionExpression ParseExpression ()
74 return ParseBooleanExpression ();
77 ConditionExpression ParseBooleanExpression ()
79 return ParseBooleanAnd ();
82 ConditionExpression ParseBooleanAnd ()
84 ConditionExpression e = ParseBooleanOr ();
86 while (tokenizer.IsToken (TokenType.And)) {
87 tokenizer.GetNextToken ();
88 e = new ConditionAndExpression (e, ParseBooleanOr ());
94 ConditionExpression ParseBooleanOr ()
96 ConditionExpression e = ParseRelationalExpression ();
98 while (tokenizer.IsToken (TokenType.Or)) {
99 tokenizer.GetNextToken ();
100 e = new ConditionOrExpression (e, ParseRelationalExpression ());
106 ConditionExpression ParseRelationalExpression ()
108 ConditionExpression e = ParseFactorExpression ();
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)) {
119 opToken = tokenizer.Token;
120 tokenizer.GetNextToken ();
122 switch (opToken.Type) {
123 case TokenType.Equal:
124 op = RelationOperator.Equal;
126 case TokenType.NotEqual:
127 op = RelationOperator.NotEqual;
130 op = RelationOperator.Less;
132 case TokenType.LessOrEqual:
133 op = RelationOperator.LessOrEqual;
135 case TokenType.Greater:
136 op = RelationOperator.Greater;
138 case TokenType.GreaterOrEqual:
139 op = RelationOperator.GreaterOrEqual;
142 throw new ExpressionParseException (String.Format ("Wrong relation operator {0}", opToken.Value));
145 e = new ConditionRelationalExpression (e, ParseFactorExpression (), op);
151 ConditionExpression ParseFactorExpression ()
153 ConditionExpression e;
154 Token token = tokenizer.Token;
155 tokenizer.GetNextToken ();
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 ();
173 throw new ExpressionParseException (String.Format ("Unexpected token type {0}.", token.Type));
178 ConditionExpression ParseNotExpression ()
180 return new ConditionNotExpression (ParseFactorExpression ());
183 ConditionExpression ParseFunctionExpression ()
185 throw new NotImplementedException ();