* Makefile: Don't build make-map.exe.
[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 = new ConditionTokenizer ();
41                 
42                 private ConditionParser (string condition)
43                 {
44                         tokenizer.Tokenize (condition);
45                 }
46                 
47                 public static ConditionExpression ParseCondition (string condition)
48                 {
49                         ConditionParser parser = new ConditionParser (condition);
50                         ConditionExpression e = parser.ParseExpression ();
51                         
52                         if (!parser.tokenizer.IsEOF ())
53                                 throw new ExpressionParseException (String.Format ("Unexpected token: {0}", parser.tokenizer.Token.Value));
54                         
55                         return e;
56                 }
57                 
58                 private ConditionExpression ParseExpression ()
59                 {
60                         return ParseBooleanExpression ();
61                 }
62                 
63                 private ConditionExpression ParseBooleanExpression ()
64                 {
65                         return ParseBooleanAnd ();
66                 }
67                 
68                 private ConditionExpression ParseBooleanAnd ()
69                 {
70                         ConditionExpression e = ParseBooleanOr ();
71                         
72                         while (tokenizer.IsToken (TokenType.And)) {
73                                 tokenizer.GetNextToken ();
74                                 e = new ConditionAndExpression ((ConditionExpression) e, (ConditionExpression) ParseBooleanOr ());
75                         }
76                         
77                         return e;
78                 }
79                 
80                 private ConditionExpression ParseBooleanOr ()
81                 {
82                         ConditionExpression e = ParseRelationalExpression ();
83                         
84                         while (tokenizer.IsToken (TokenType.Or)) {
85                                 tokenizer.GetNextToken ();
86                                 e = new ConditionOrExpression ((ConditionExpression) e, (ConditionExpression) ParseRelationalExpression ());
87                         }
88                         
89                         return e;
90                 }
91                 
92                 private ConditionExpression ParseRelationalExpression ()
93                 {
94                         ConditionExpression e = ParseFactorExpression ();
95                         Token opToken;
96                         RelationOperator op;
97                         
98                         if (tokenizer.IsToken (TokenType.Less) ||
99                                 tokenizer.IsToken (TokenType.Greater) ||
100                                 tokenizer.IsToken (TokenType.Equal) ||
101                                 tokenizer.IsToken (TokenType.NotEqual) ||
102                                 tokenizer.IsToken (TokenType.LessOrEqual) ||
103                                 tokenizer.IsToken (TokenType.GreaterOrEqual)) {
104                                 
105                                 opToken = tokenizer.Token;
106                                 tokenizer.GetNextToken ();
107                                                                 
108                                 switch (opToken.Type) {
109                                 case TokenType.Equal:
110                                         op = RelationOperator.Equal;
111                                         break;
112                                 case TokenType.NotEqual:
113                                         op = RelationOperator.NotEqual;
114                                         break;
115                                 case TokenType.Less:
116                                         op = RelationOperator.Less;
117                                         break;
118                                 case TokenType.LessOrEqual:
119                                         op = RelationOperator.LessOrEqual;
120                                         break;
121                                 case TokenType.Greater:
122                                         op = RelationOperator.Greater;
123                                         break;
124                                 case TokenType.GreaterOrEqual:
125                                         op = RelationOperator.GreaterOrEqual;
126                                         break;
127                                 default:
128                                         throw new ExpressionParseException (String.Format ("Wrong relation operator {0}", opToken.Value));
129                                 }
130                                 
131                                 e =  new ConditionRelationalExpression ((ConditionExpression) e, ParseFactorExpression (), op);
132                         }
133                         
134                         return e;
135                 }
136                 
137                 // FIXME: parse sub expression in parens, parse TokenType.Not, parse functions
138                 private ConditionExpression ParseFactorExpression ()
139                 {
140                         Token token = tokenizer.Token;
141                         tokenizer.GetNextToken ();
142                         
143                         if (token.Type != TokenType.String && token.Type != TokenType.Number)
144                                 throw new ExpressionParseException (String.Format ("Unexpected token type {0}.", token.Type));
145                         
146                         return new ConditionFactorExpression (token);
147                 }
148         }
149 }
150
151 #endif