[xbuild] Parsing conditions with property references. Fixes #20634
[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 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Text;
34
35 namespace Microsoft.Build.BuildEngine {
36
37         internal class ConditionParser {
38         
39                 ConditionTokenizer tokenizer;
40                 string conditionStr;
41                 
42                 ConditionParser (string condition)
43                 {
44                         tokenizer = new ConditionTokenizer ();
45                         tokenizer.Tokenize (condition);
46                         conditionStr = condition;
47                 }
48                 
49                 public static bool ParseAndEvaluate (string condition, Project context)
50                 {
51                         if (String.IsNullOrEmpty (condition))
52                                 return true;
53
54                         try {
55                                 ConditionExpression ce = ParseCondition (condition);
56
57                                 if (!ce.CanEvaluateToBool (context))
58                                         throw new InvalidProjectFileException (String.Format ("Can not evaluate \"{0}\" to bool.", condition));
59
60                                 return ce.BoolEvaluate (context);
61                         } catch (ExpressionParseException epe) {
62                                 throw new InvalidProjectFileException (
63                                                 String.Format ("Unable to parse condition \"{0}\" : {1}", condition, epe.Message),
64                                                 epe);
65                         } catch (ExpressionEvaluationException epe) {
66                                 throw new InvalidProjectFileException (
67                                                 String.Format ("Unable to evaluate condition \"{0}\" : {1}", condition, epe.Message),
68                                                 epe);
69                         }
70                 }
71
72                 public static ConditionExpression ParseCondition (string condition)
73                 {
74                         ConditionParser parser = new ConditionParser (condition);
75                         ConditionExpression e = parser.ParseExpression ();
76                         
77                         if (!parser.tokenizer.IsEOF ())
78                                 throw new ExpressionParseException (String.Format ("Unexpected token found, {0}, in condition \"{1}\"", parser.tokenizer.Token, condition));
79                         
80                         return e;
81                 }
82                 
83                 ConditionExpression ParseExpression ()
84                 {
85                         return ParseBooleanExpression ();
86                 }
87                 
88                 ConditionExpression ParseBooleanExpression ()
89                 {
90                         return ParseBooleanAnd ();
91                 }
92
93                 public static string And (string a, string b)
94                 {
95                         return a + " and " + b;
96                 }
97                 
98                 ConditionExpression ParseBooleanAnd ()
99                 {
100                         ConditionExpression e = ParseBooleanOr ();
101                         
102                         while (tokenizer.IsToken (TokenType.And)) {
103                                 tokenizer.GetNextToken ();
104                                 e = new ConditionAndExpression (e, ParseBooleanOr ());
105                         }
106                         
107                         return e;
108                 }
109                 
110                 ConditionExpression ParseBooleanOr ()
111                 {
112                         ConditionExpression e = ParseRelationalExpression ();
113                         
114                         while (tokenizer.IsToken (TokenType.Or)) {
115                                 tokenizer.GetNextToken ();
116                                 e = new ConditionOrExpression (e, ParseRelationalExpression ());
117                         }
118                         
119                         return e;
120                 }
121                 
122                 ConditionExpression ParseRelationalExpression ()
123                 {
124                         ConditionExpression e = ParseFactorExpression ();
125                         
126                         Token opToken;
127                         RelationOperator op;
128                         
129                         if (tokenizer.IsToken (TokenType.Less) ||
130                                 tokenizer.IsToken (TokenType.Greater) ||
131                                 tokenizer.IsToken (TokenType.Equal) ||
132                                 tokenizer.IsToken (TokenType.NotEqual) ||
133                                 tokenizer.IsToken (TokenType.LessOrEqual) ||
134                                 tokenizer.IsToken (TokenType.GreaterOrEqual)) {
135                                 
136                                 opToken = tokenizer.Token;
137                                 tokenizer.GetNextToken ();
138                                                                 
139                                 switch (opToken.Type) {
140                                 case TokenType.Equal:
141                                         op = RelationOperator.Equal;
142                                         break;
143                                 case TokenType.NotEqual:
144                                         op = RelationOperator.NotEqual;
145                                         break;
146                                 case TokenType.Less:
147                                         op = RelationOperator.Less;
148                                         break;
149                                 case TokenType.LessOrEqual:
150                                         op = RelationOperator.LessOrEqual;
151                                         break;
152                                 case TokenType.Greater:
153                                         op = RelationOperator.Greater;
154                                         break;
155                                 case TokenType.GreaterOrEqual:
156                                         op = RelationOperator.GreaterOrEqual;
157                                         break;
158                                 default:
159                                         throw new ExpressionParseException (String.Format ("Wrong relation operator {0}", opToken.Value));
160                                 }
161
162                                 e =  new ConditionRelationalExpression (e, ParseFactorExpression (), op);
163                         }
164                         
165                         return e;
166                 }
167                 
168                 ConditionExpression ParseFactorExpression ()
169                 {
170                         ConditionExpression e;
171                         Token token = tokenizer.Token;
172                         tokenizer.GetNextToken ();
173
174                         if (token.Type == TokenType.LeftParen) {
175                                 e = ParseExpression ();
176                                 tokenizer.Expect (TokenType.RightParen);
177                         } else if (token.Type == TokenType.String && tokenizer.Token.Type == TokenType.LeftParen) {
178                                 e = ParseFunctionExpression (token.Value);
179                         } else if (token.Type == TokenType.String) {
180                                 e = new ConditionFactorExpression (token);
181                         } else if (token.Type == TokenType.Number) {
182                                 e = new ConditionFactorExpression (token);
183                         } else if (token.Type == TokenType.Item || token.Type == TokenType.Property
184                                         || token.Type == TokenType.Metadata) {
185                                 e = ParseReferenceExpression (token.Value [0]);
186                         } else if (token.Type == TokenType.Not) {
187                                 e = ParseNotExpression ();
188                         } else
189                                 throw new ExpressionParseException (String.Format ("Unexpected token {0}, while parsing condition \"{1}\"", token, conditionStr));
190                         
191                         return e;
192                 }
193
194                 ConditionExpression ParseNotExpression ()
195                 {
196                         return new ConditionNotExpression (ParseFactorExpression ());
197                 }
198
199                 ConditionExpression ParseFunctionExpression (string function_name)
200                 {
201                         return new ConditionFunctionExpression (function_name, ParseFunctionArguments ());
202                 }
203                 
204                 List <ConditionFactorExpression> ParseFunctionArguments ()
205                 {
206                         List <ConditionFactorExpression> list = new List <ConditionFactorExpression> ();
207                         ConditionFactorExpression e;
208                         
209                         while (true) {
210                                 tokenizer.GetNextToken ();
211                                 if (tokenizer.Token.Type == TokenType.RightParen) {
212                                         tokenizer.GetNextToken ();
213                                         break;
214                                 }
215                                 if (tokenizer.Token.Type == TokenType.Comma)
216                                         continue;
217                                         
218                                 tokenizer.Putback (tokenizer.Token);
219                                 e = (ConditionFactorExpression) ParseFactorExpression ();
220                                 list.Add (e);
221                         }
222                         
223                         return list;
224                 }
225
226                 //@prefix: @ or $
227                 ConditionExpression ParseReferenceExpression (char prefix)
228                 {
229                         int token_pos = tokenizer.Token.Position;
230                         string ref_type = prefix == '$' ? "a property" : "an item list";
231                         IsAtToken (TokenType.LeftParen, String.Format (
232                                                 "Expected {0} at position {1} in condition \"{2}\". Missing opening parantheses after the '{3}'.",
233                                                 ref_type, token_pos, conditionStr, prefix));
234
235
236                         if (prefix == '$') {
237                                 //
238                                 // Tjhe scan should consider quoted parenthesis but it breaks on .net as well
239                                 // we are bug compatible
240                                 //
241                                 tokenizer.ScanForClosingParens ();
242                         } else {
243                                 tokenizer.GetNextToken ();
244                         }
245
246                         if (tokenizer.IsEOF ())
247                                 throw new ExpressionParseException ("Missing closing parenthesis in condition " + conditionStr);
248
249                         StringBuilder sb = new StringBuilder ();
250                         sb.AppendFormat ("{0}({1}", prefix, tokenizer.Token.Value);
251
252                         tokenizer.GetNextToken ();
253                         if (prefix == '@' && tokenizer.Token.Type == TokenType.Transform) {
254                                 tokenizer.GetNextToken ();
255                                 sb.AppendFormat ("->'{0}'", tokenizer.Token.Value);
256
257                                 tokenizer.GetNextToken ();
258                                 if (tokenizer.Token.Type == TokenType.Comma) {
259                                         tokenizer.GetNextToken ();
260                                         sb.AppendFormat (", '{0}'", tokenizer.Token.Value);
261                                         tokenizer.GetNextToken ();
262                                 }
263                         }
264
265                         IsAtToken (TokenType.RightParen, "Missing closing parenthesis in condition " + conditionStr);
266                         tokenizer.GetNextToken ();
267
268                         sb.Append (")");
269
270                         //FIXME: HACKY!
271                         return new ConditionFactorExpression (new Token (sb.ToString (), TokenType.String, token_pos));
272                 }
273
274                 // used to check current token type
275                 void IsAtToken (TokenType type, string error_msg)
276                 {
277                         if (tokenizer.Token.Type != type) {
278                                 if (!String.IsNullOrEmpty (error_msg))
279                                         throw new ExpressionParseException (error_msg);
280
281                                 if (tokenizer.Token.Type == TokenType.EOF)
282                                         throw new ExpressionParseException (String.Format (
283                                                                 "Expected a \"{0}\" but the condition ended abruptly, while parsing condition \"{1}\"",
284                                                                 Token.TypeAsString (type), conditionStr));
285
286                                 throw new ExpressionParseException (String.Format (
287                                                                 "Expected \"{0}\" token,  but got {1}, while parsing \"{2}\"",
288                                                                 Token.TypeAsString (type), tokenizer.Token, conditionStr));
289                         }
290                 }
291         }
292 }