In class/Microsoft.Build.Engine/Microsoft.Build.BuildEngine:
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / Expression.cs
1 //
2 // Expression.cs: Stores references to items or properties.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 // 
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 #if NET_2_0
29
30 using System;
31 using System.IO;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Text;
35 using System.Text.RegularExpressions;
36
37 namespace Microsoft.Build.BuildEngine {
38
39         // Properties and items are processed in two ways
40         // 1. Evaluate, Project calls evaluate on all the item and property groups.
41         //    At this time, the items are fully expanded, all item and property
42         //    references are expanded to get the item's value.
43         //    Properties on the other hand, expand property refs, but _not_
44         //    item references.
45         //
46         // 2. After the 'evaluation' phase, this could be when executing a target/task,
47         //    - Items : no expansion required, as they are already at final value
48         //    - Properties: Item references get expanded now, in the context of the
49         //      batching
50         //
51         // The enum ExpressionOptions is for specifying this expansion of item references.
52         //
53         // GroupingCollection.Evaluate, evaluates all properties and then items
54
55         internal class Expression {
56         
57                 ExpressionCollection expressionCollection;
58
59                 static Regex item_regex;
60                 static Regex property_regex;
61                 static Regex metadata_regex;
62         
63                 public Expression ()
64                 {
65                         this.expressionCollection = new ExpressionCollection ();
66                 }
67
68                 public void Parse (string expression, bool allowItems)
69                 {
70                         Parse (expression, allowItems, true);
71                 }
72
73                 // @split: Split on ';'
74                 //         Eg. Property values don't need to be split
75                 //
76                 // @allowItems: item refs should not be treated as item refs!
77                 //              it converts them to strings in the final expressionCollection
78                 public void Parse (string expression, bool allowItems, bool split)
79                 {
80                         expression = expression.Replace ('/', Path.DirectorySeparatorChar);
81                         expression = expression.Replace ('\\', Path.DirectorySeparatorChar);
82                 
83                         string [] parts;
84                         if (split)
85                                 parts = expression.Split (new char [] {';'}, StringSplitOptions.RemoveEmptyEntries);
86                         else
87                                 parts = new string [] { expression };
88
89                         List <ArrayList> p1 = new List <ArrayList> (parts.Length);
90                         List <ArrayList> p2 = new List <ArrayList> (parts.Length);
91                         List <ArrayList> p3 = new List <ArrayList> (parts.Length);
92
93                         Prepare (p1, parts.Length);
94                         Prepare (p2, parts.Length);
95                         Prepare (p3, parts.Length);
96
97                         for (int i = 0; i < parts.Length; i++)
98                                 p1 [i] = SplitItems (parts [i], allowItems);
99
100                         for (int i = 0; i < parts.Length; i++) {
101                                 p2 [i] = new ArrayList ();
102                                 foreach (object o in p1 [i]) {
103                                         if (o is string)
104                                                 p2 [i].AddRange (SplitProperties ((string) o));
105                                         else
106                                                 p2 [i].Add (o);
107                                 }
108                         }
109
110                         for (int i = 0; i < parts.Length; i++) {
111                                 p3 [i] = new ArrayList ();
112                                 foreach (object o in p2 [i]) {
113                                         if (o is string)
114                                                 p3 [i].AddRange (SplitMetadata ((string) o));
115                                         else
116                                                 p3 [i].Add (o);
117                                 }
118                         }
119
120                         CopyToExpressionCollection (p3, allowItems);
121                 }
122
123                 void Prepare (List <ArrayList> l, int length)
124                 {
125                         for (int i = 0; i < length; i++)
126                                 l.Add (null);
127                 }
128                 
129                 void CopyToExpressionCollection (List <ArrayList> lists, bool allowItems)
130                 {
131                         for (int i = 0; i < lists.Count; i++) {
132                                 foreach (object o in lists [i]) {
133                                         if (o is string)
134                                                 expressionCollection.Add (Utilities.Unescape ((string) o));
135                                         else if (!allowItems && o is ItemReference)
136                                                 expressionCollection.Add (((ItemReference) o).OriginalString);
137                                         else if (o is IReference)
138                                                 expressionCollection.Add ((IReference) o);
139                                 }
140                                 if (i < lists.Count - 1)
141                                         expressionCollection.Add (";");
142                         }
143                 }
144
145                 ArrayList SplitItems (string text, bool allowItems)
146                 {
147                         ArrayList phase1 = new ArrayList ();
148                         Match m;
149                         m = ItemRegex.Match (text);
150
151                         while (m.Success) {
152                                 string name = null, transform = null, separator = null;
153                                 ItemReference ir;
154                                 
155                                 name = m.Groups [ItemRegex.GroupNumberFromName ("itemname")].Value;
156                                 
157                                 if (m.Groups [ItemRegex.GroupNumberFromName ("has_transform")].Success)
158                                         transform = m.Groups [ItemRegex.GroupNumberFromName ("transform")].Value;
159                                 
160                                 if (m.Groups [ItemRegex.GroupNumberFromName ("has_separator")].Success)
161                                         separator = m.Groups [ItemRegex.GroupNumberFromName ("separator")].Value;
162
163                                 ir = new ItemReference (text.Substring (m.Groups [0].Index, m.Groups [0].Length),
164                                                 name, transform, separator, m.Groups [0].Index, m.Groups [0].Length);
165                                 phase1.Add (ir);
166                                 m = m.NextMatch ();
167                         }
168
169                         ArrayList phase2 = new ArrayList ();
170                         int last_end = -1;
171                         int end = text.Length - 1;
172
173                         foreach (ItemReference ir in phase1) {
174                                 int a,b;
175
176                                 a = last_end;
177                                 b = ir.Start;
178
179                                 if (b - a - 1 > 0) {
180                                         phase2.Add (text.Substring (a + 1, b - a - 1));
181                                 }
182
183                                 last_end = ir.End;
184                                 phase2.Add (ir);
185                         }
186
187                         if (last_end < end)
188                                 phase2.Add (text.Substring (last_end + 1, end - last_end));
189
190                         return phase2;
191                 }
192
193                 ArrayList SplitProperties (string text)
194                 {
195                         ArrayList phase1 = new ArrayList ();
196                         Match m;
197                         m = PropertyRegex.Match (text);
198
199                         while (m.Success) {
200                                 string name = null;
201                                 PropertyReference pr;
202                                 
203                                 name = m.Groups [PropertyRegex.GroupNumberFromName ("name")].Value;
204                                 
205                                 pr = new PropertyReference (name, m.Groups [0].Index, m.Groups [0].Length);
206                                 phase1.Add (pr);
207                                 m = m.NextMatch ();
208                         }
209
210                         ArrayList phase2 = new ArrayList ();
211                         int last_end = -1;
212                         int end = text.Length - 1;
213
214                         foreach (PropertyReference pr in phase1) {
215                                 int a,b;
216
217                                 a = last_end;
218                                 b = pr.Start;
219
220                                 if (b - a - 1 > 0) {
221                                         phase2.Add (text.Substring (a + 1, b - a - 1));
222                                 }
223
224                                 last_end = pr.End;
225                                 phase2.Add (pr);
226                         }
227
228                         if (last_end < end)
229                                 phase2.Add (text.Substring (last_end + 1, end - last_end));
230
231                         return phase2;
232                 }
233
234                 ArrayList SplitMetadata (string text)
235                 {
236                         ArrayList phase1 = new ArrayList ();
237                         Match m;
238                         m = MetadataRegex.Match (text);
239
240                         while (m.Success) {
241                                 string name = null, meta = null;
242                                 MetadataReference mr;
243                                 
244                                 if (m.Groups [MetadataRegex.GroupNumberFromName ("name")].Success)
245                                         name = m.Groups [MetadataRegex.GroupNumberFromName ("name")].Value;
246                                 
247                                 meta = m.Groups [MetadataRegex.GroupNumberFromName ("meta")].Value;
248                                 
249                                 mr = new MetadataReference (name, meta, m.Groups [0].Index, m.Groups [0].Length);
250                                 phase1.Add (mr);
251                                 m = m.NextMatch ();
252                         }
253
254                         ArrayList phase2 = new ArrayList ();
255                         int last_end = -1;
256                         int end = text.Length - 1;
257
258                         foreach (MetadataReference mr in phase1) {
259                                 int a,b;
260
261                                 a = last_end;
262                                 b = mr.Start;
263
264                                 if (b - a - 1> 0) {
265                                         phase2.Add (text.Substring (a + 1, b - a - 1));
266                                 }
267
268                                 last_end = mr.End;
269                                 phase2.Add (mr);
270                         }
271
272                         if (last_end < end)
273                                 phase2.Add (text.Substring (last_end + 1, end - last_end));
274
275                         return phase2;
276                 }
277
278                 public object ConvertTo (Project project, Type type)
279                 {
280                         return ConvertTo (project, type, ExpressionOptions.ExpandItemRefs);
281                 }
282
283                 public object ConvertTo (Project project, Type type, ExpressionOptions options)
284                 {
285                         return expressionCollection.ConvertTo (project, type, options);
286                 }
287
288                 public ExpressionCollection Collection {
289                         get { return expressionCollection; }
290                 }
291
292                 static Regex ItemRegex {
293                         get {
294                                 if (item_regex == null)
295                                         item_regex = new Regex (
296                                                 @"@\(\s*"
297                                                 + @"(?<itemname>[_A-Za-z][_0-9a-zA-Z]*)"
298                                                 + @"(?<has_transform>\s*->\s*'(?<transform>[^']*)')?"
299                                                 + @"(?<has_separator>\s*,\s*'(?<separator>[^']*)')?"
300                                                 + @"\s*\)");
301                                 return item_regex;
302                         }
303                 }
304
305                 static Regex PropertyRegex {
306                         get {
307                                 if (property_regex == null)
308                                         property_regex = new Regex (
309                                                 @"\$\(\s*"
310                                                 + @"(?<name>[_a-zA-Z][_0-9a-zA-Z]*)"
311                                                 + @"\s*\)");
312                                 return property_regex;
313                         }
314                 }
315
316                 static Regex MetadataRegex {
317                         get {
318                                 if (metadata_regex == null)
319                                         metadata_regex = new Regex (
320                                                 @"%\(\s*"
321                                                 + @"((?<name>[_a-zA-Z][_0-9a-zA-Z]*)\.)?"
322                                                 + @"(?<meta>[_a-zA-Z][_0-9a-zA-Z]*)"
323                                                 + @"\s*\)");
324                                 return metadata_regex;
325                         }
326                 }
327         }
328
329         enum ExpressionOptions {
330                 ExpandItemRefs,
331                 DoNotExpandItemRefs
332         }
333 }
334
335 #endif