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