bbda1ac7da31ff8a8eea86d729561fb0cb28a02a
[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 using Mono.XBuild.Utilities;
37
38 namespace Microsoft.Build.BuildEngine {
39
40         // Properties and items are processed in two ways
41         // 1. Evaluate, Project calls evaluate on all the item and property groups.
42         //    At this time, the items are fully expanded, all item and property
43         //    references are expanded to get the item's value.
44         //    Properties on the other hand, expand property refs, but _not_
45         //    item references.
46         //
47         // 2. After the 'evaluation' phase, this could be when executing a target/task,
48         //    - Items : no expansion required, as they are already at final value
49         //    - Properties: Item references get expanded now, in the context of the
50         //      batching
51         //
52         // The enum ExpressionOptions is for specifying this expansion of item references.
53         //
54         // GroupingCollection.Evaluate, evaluates all properties and then items
55
56         internal class Expression {
57         
58                 ExpressionCollection expressionCollection;
59
60                 static Regex item_regex;
61                 static Regex property_regex;
62                 static Regex metadata_regex;
63         
64                 public Expression ()
65                 {
66                         this.expressionCollection = new ExpressionCollection ();
67                 }
68
69                 // Split: Split on ';'
70                 //         Eg. Property values don't need to be split
71                 //
72                 // AllowItems: if false, item refs should not be treated as item refs!
73                 //              it converts them to strings in the final expressionCollection
74                 //
75                 // AllowMetadata: same as AllowItems, for metadata
76                 public void Parse (string expression, ParseOptions options)
77                 {
78                         bool split = (options & ParseOptions.Split) == ParseOptions.Split;
79                         bool allowItems = (options & ParseOptions.AllowItems) == ParseOptions.AllowItems;
80                         bool allowMd = (options & ParseOptions.AllowMetadata) == ParseOptions.AllowMetadata;
81
82                         expression = expression.Replace ('\\', Path.DirectorySeparatorChar);
83                 
84                         string [] parts;
85                         if (split)
86                                 parts = expression.Split (new char [] {';'}, StringSplitOptions.RemoveEmptyEntries);
87                         else
88                                 parts = new string [] { expression };
89
90                         List <ArrayList> p1 = new List <ArrayList> (parts.Length);
91                         List <ArrayList> p2 = new List <ArrayList> (parts.Length);
92                         List <ArrayList> p3 = new List <ArrayList> (parts.Length);
93
94                         Prepare (p1, parts.Length);
95                         Prepare (p2, parts.Length);
96                         Prepare (p3, parts.Length);
97
98                         for (int i = 0; i < parts.Length; i++)
99                                 p1 [i] = SplitItems (parts [i], allowItems);
100
101                         for (int i = 0; i < parts.Length; i++) {
102                                 p2 [i] = new ArrayList ();
103                                 foreach (object o in p1 [i]) {
104                                         if (o is string)
105                                                 p2 [i].AddRange (SplitProperties ((string) o));
106                                         else
107                                                 p2 [i].Add (o);
108                                 }
109                         }
110
111                         for (int i = 0; i < parts.Length; i++) {
112                                 p3 [i] = new ArrayList ();
113                                 foreach (object o in p2 [i]) {
114                                         if (o is string)
115                                                 p3 [i].AddRange (SplitMetadata ((string) o));
116                                         else
117                                                 p3 [i].Add (o);
118                                 }
119                         }
120
121                         CopyToExpressionCollection (p3, allowItems, allowMd);
122                 }
123
124                 void Prepare (List <ArrayList> l, int length)
125                 {
126                         for (int i = 0; i < length; i++)
127                                 l.Add (null);
128                 }
129                 
130                 void CopyToExpressionCollection (List <ArrayList> lists, bool allowItems, bool allowMd)
131                 {
132                         for (int i = 0; i < lists.Count; i++) {
133                                 foreach (object o in lists [i]) {
134                                         if (o is string)
135                                                 expressionCollection.Add (MSBuildUtils.Unescape ((string) o));
136                                         else if (!allowItems && o is ItemReference)
137                                                 expressionCollection.Add (((ItemReference) o).OriginalString);
138                                         else if (!allowMd && o is MetadataReference) {
139                                                 expressionCollection.Add (((MetadataReference) o).OriginalString);
140                                         }
141                                         else if (o is IReference)
142                                                 expressionCollection.Add ((IReference) o);
143                                 }
144                                 if (i < lists.Count - 1)
145                                         expressionCollection.Add (";");
146                         }
147                 }
148
149                 ArrayList SplitItems (string text, bool allowItems)
150                 {
151                         ArrayList phase1 = new ArrayList ();
152                         Match m;
153                         m = ItemRegex.Match (text);
154
155                         while (m.Success) {
156                                 string name = null, transform = null, separator = null;
157                                 ItemReference ir;
158                                 
159                                 name = m.Groups [ItemRegex.GroupNumberFromName ("itemname")].Value;
160                                 
161                                 if (m.Groups [ItemRegex.GroupNumberFromName ("has_transform")].Success)
162                                         transform = m.Groups [ItemRegex.GroupNumberFromName ("transform")].Value;
163                                 
164                                 if (m.Groups [ItemRegex.GroupNumberFromName ("has_separator")].Success)
165                                         separator = m.Groups [ItemRegex.GroupNumberFromName ("separator")].Value;
166
167                                 ir = new ItemReference (text.Substring (m.Groups [0].Index, m.Groups [0].Length),
168                                                 name, transform, separator, m.Groups [0].Index, m.Groups [0].Length);
169                                 phase1.Add (ir);
170                                 m = m.NextMatch ();
171                         }
172
173                         ArrayList phase2 = new ArrayList ();
174                         int last_end = -1;
175                         int end = text.Length - 1;
176
177                         foreach (ItemReference ir in phase1) {
178                                 int a,b;
179
180                                 a = last_end;
181                                 b = ir.Start;
182
183                                 if (b - a - 1 > 0) {
184                                         phase2.Add (text.Substring (a + 1, b - a - 1));
185                                 }
186
187                                 last_end = ir.End;
188                                 phase2.Add (ir);
189                         }
190
191                         if (last_end < end)
192                                 phase2.Add (text.Substring (last_end + 1, end - last_end));
193
194                         return phase2;
195                 }
196
197                 ArrayList SplitProperties (string text)
198                 {
199                         ArrayList phase1 = new ArrayList ();
200                         Match m;
201                         m = PropertyRegex.Match (text);
202
203                         while (m.Success) {
204                                 string name = null;
205                                 PropertyReference pr;
206                                 
207                                 name = m.Groups [PropertyRegex.GroupNumberFromName ("name")].Value;
208                                 
209                                 pr = new PropertyReference (name, m.Groups [0].Index, m.Groups [0].Length);
210                                 phase1.Add (pr);
211                                 m = m.NextMatch ();
212                         }
213
214                         ArrayList phase2 = new ArrayList ();
215                         int last_end = -1;
216                         int end = text.Length - 1;
217
218                         foreach (PropertyReference pr in phase1) {
219                                 int a,b;
220
221                                 a = last_end;
222                                 b = pr.Start;
223
224                                 if (b - a - 1 > 0) {
225                                         phase2.Add (text.Substring (a + 1, b - a - 1));
226                                 }
227
228                                 last_end = pr.End;
229                                 phase2.Add (pr);
230                         }
231
232                         if (last_end < end)
233                                 phase2.Add (text.Substring (last_end + 1, end - last_end));
234
235                         return phase2;
236                 }
237
238                 ArrayList SplitMetadata (string text)
239                 {
240                         ArrayList phase1 = new ArrayList ();
241                         Match m;
242                         m = MetadataRegex.Match (text);
243
244                         while (m.Success) {
245                                 string name = null, meta = null;
246                                 MetadataReference mr;
247                                 
248                                 if (m.Groups [MetadataRegex.GroupNumberFromName ("name")].Success)
249                                         name = m.Groups [MetadataRegex.GroupNumberFromName ("name")].Value;
250                                 
251                                 meta = m.Groups [MetadataRegex.GroupNumberFromName ("meta")].Value;
252                                 
253                                 mr = new MetadataReference (text.Substring (m.Groups [0].Index, m.Groups [0].Length),
254                                                                 name, meta, m.Groups [0].Index, m.Groups [0].Length);
255                                 phase1.Add (mr);
256                                 m = m.NextMatch ();
257                         }
258
259                         ArrayList phase2 = new ArrayList ();
260                         int last_end = -1;
261                         int end = text.Length - 1;
262
263                         foreach (MetadataReference mr in phase1) {
264                                 int a,b;
265
266                                 a = last_end;
267                                 b = mr.Start;
268
269                                 if (b - a - 1> 0) {
270                                         phase2.Add (text.Substring (a + 1, b - a - 1));
271                                 }
272
273                                 last_end = mr.End;
274                                 phase2.Add (mr);
275                         }
276
277                         if (last_end < end)
278                                 phase2.Add (text.Substring (last_end + 1, end - last_end));
279
280                         return phase2;
281                 }
282
283                 public object ConvertTo (Project project, Type type)
284                 {
285                         return ConvertTo (project, type, ExpressionOptions.ExpandItemRefs);
286                 }
287
288                 public object ConvertTo (Project project, Type type, ExpressionOptions options)
289                 {
290                         return expressionCollection.ConvertTo (project, type, options);
291                 }
292
293                 public ExpressionCollection Collection {
294                         get { return expressionCollection; }
295                 }
296
297                 static Regex ItemRegex {
298                         get {
299                                 if (item_regex == null)
300                                         item_regex = new Regex (
301                                                 @"@\(\s*"
302                                                 + @"(?<itemname>[_A-Za-z][_\-0-9a-zA-Z]*)"
303                                                 + @"(?<has_transform>\s*->\s*'(?<transform>[^']*)')?"
304                                                 + @"(?<has_separator>\s*,\s*'(?<separator>[^']*)')?"
305                                                 + @"\s*\)");
306                                 return item_regex;
307                         }
308                 }
309
310                 static Regex PropertyRegex {
311                         get {
312                                 if (property_regex == null)
313                                         property_regex = new Regex (
314                                                 @"\$\(\s*"
315                                                 + @"(?<name>[_a-zA-Z][_\-0-9a-zA-Z]*)"
316                                                 + @"\s*\)");
317                                 return property_regex;
318                         }
319                 }
320
321                 static Regex MetadataRegex {
322                         get {
323                                 if (metadata_regex == null)
324                                         metadata_regex = new Regex (
325                                                 @"%\(\s*"
326                                                 + @"((?<name>[_a-zA-Z][_\-0-9a-zA-Z]*)\.)?"
327                                                 + @"(?<meta>[_a-zA-Z][_\-0-9a-zA-Z]*)"
328                                                 + @"\s*\)");
329                                 return metadata_regex;
330                         }
331                 }
332         }
333
334         [Flags]
335         enum ParseOptions {
336                 // absence of one of these flags, means
337                 // false for that option
338                 AllowItems = 0x1,
339                 Split = 0x2,
340                 AllowMetadata = 0x4,
341
342                 None = 0x8, // == no items, no metadata, and no split
343
344                 // commonly used options
345                 AllowItemsMetadataAndSplit = AllowItems | Split | AllowMetadata,
346                 AllowItemsNoMetadataAndSplit = AllowItems | Split
347         }
348
349         enum ExpressionOptions {
350                 ExpandItemRefs,
351                 DoNotExpandItemRefs
352         }
353 }
354
355 #endif