2 // Expression.cs: Stores references to items or properties.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.Collections.Generic;
33 using System.Text.RegularExpressions;
34 using Mono.XBuild.Utilities;
36 namespace Microsoft.Build.BuildEngine {
38 // Properties and items are processed in two ways
39 // 1. Evaluate, Project calls evaluate on all the item and property groups.
40 // At this time, the items are fully expanded, all item and property
41 // references are expanded to get the item's value.
42 // Properties on the other hand, expand property refs, but _not_
45 // 2. After the 'evaluation' phase, this could be when executing a target/task,
46 // - Items : no expansion required, as they are already at final value
47 // - Properties: Item references get expanded now, in the context of the
50 // The enum ExpressionOptions is for specifying this expansion of item references.
52 // GroupingCollection.Evaluate, evaluates all properties and then items
54 internal class Expression {
56 ExpressionCollection expressionCollection;
58 static Regex item_regex;
59 static Regex property_regex;
60 static Regex metadata_regex;
64 this.expressionCollection = new ExpressionCollection ();
67 public static T ParseAs<T> (string expression, ParseOptions options, Project project)
69 Expression expr = new Expression ();
70 expr.Parse (expression, options);
71 return (T)expr.ConvertTo (project, typeof (T));
74 public static T ParseAs<T> (string expression, ParseOptions options, Project project, ExpressionOptions exprOptions)
76 Expression expr = new Expression ();
77 expr.Parse (expression, options);
78 return (T)expr.ConvertTo (project, typeof (T), exprOptions);
81 // Split: Split on ';'
82 // Eg. Property values don't need to be split
84 // AllowItems: if false, item refs should not be treated as item refs!
85 // it converts them to strings in the final expressionCollection
87 // AllowMetadata: same as AllowItems, for metadata
88 public void Parse (string expression, ParseOptions options)
90 bool split = (options & ParseOptions.Split) == ParseOptions.Split;
91 bool allowItems = (options & ParseOptions.AllowItems) == ParseOptions.AllowItems;
92 bool allowMd = (options & ParseOptions.AllowMetadata) == ParseOptions.AllowMetadata;
94 expression = expression.Replace ('\\', Path.DirectorySeparatorChar);
98 parts = expression.Split (new char [] {';'}, StringSplitOptions.RemoveEmptyEntries);
100 parts = new string [] { expression };
102 List <ArrayList> p1 = new List <ArrayList> (parts.Length);
103 List <ArrayList> p2 = new List <ArrayList> (parts.Length);
104 List <ArrayList> p3 = new List <ArrayList> (parts.Length);
106 Prepare (p1, parts.Length);
107 Prepare (p2, parts.Length);
108 Prepare (p3, parts.Length);
110 for (int i = 0; i < parts.Length; i++)
111 p1 [i] = SplitItems (parts [i], allowItems);
113 for (int i = 0; i < parts.Length; i++) {
114 p2 [i] = new ArrayList ();
115 foreach (object o in p1 [i]) {
117 p2 [i].AddRange (SplitProperties ((string) o));
123 for (int i = 0; i < parts.Length; i++) {
124 p3 [i] = new ArrayList ();
125 foreach (object o in p2 [i]) {
127 p3 [i].AddRange (SplitMetadata ((string) o));
133 CopyToExpressionCollection (p3, allowItems, allowMd);
136 void Prepare (List <ArrayList> l, int length)
138 for (int i = 0; i < length; i++)
142 void CopyToExpressionCollection (List <ArrayList> lists, bool allowItems, bool allowMd)
144 for (int i = 0; i < lists.Count; i++) {
145 foreach (object o in lists [i]) {
147 expressionCollection.Add (MSBuildUtils.Unescape ((string) o));
148 else if (!allowItems && o is ItemReference)
149 expressionCollection.Add (((ItemReference) o).OriginalString);
150 else if (!allowMd && o is MetadataReference) {
151 expressionCollection.Add (((MetadataReference) o).OriginalString);
153 else if (o is IReference)
154 expressionCollection.Add ((IReference) o);
156 if (i < lists.Count - 1)
157 expressionCollection.Add (";");
161 ArrayList SplitItems (string text, bool allowItems)
163 ArrayList phase1 = new ArrayList ();
165 m = ItemRegex.Match (text);
168 string name = null, transform = null, separator = null;
171 name = m.Groups [ItemRegex.GroupNumberFromName ("itemname")].Value;
173 if (m.Groups [ItemRegex.GroupNumberFromName ("has_transform")].Success)
174 transform = m.Groups [ItemRegex.GroupNumberFromName ("transform")].Value;
176 if (m.Groups [ItemRegex.GroupNumberFromName ("has_separator")].Success)
177 separator = m.Groups [ItemRegex.GroupNumberFromName ("separator")].Value;
179 ir = new ItemReference (text.Substring (m.Groups [0].Index, m.Groups [0].Length),
180 name, transform, separator, m.Groups [0].Index, m.Groups [0].Length);
185 ArrayList phase2 = new ArrayList ();
187 int end = text.Length - 1;
189 foreach (ItemReference ir in phase1) {
196 phase2.Add (text.Substring (a + 1, b - a - 1));
204 phase2.Add (text.Substring (last_end + 1, end - last_end));
209 ArrayList SplitProperties (string text)
211 ArrayList phase1 = new ArrayList ();
213 m = PropertyRegex.Match (text);
217 PropertyReference pr;
219 name = m.Groups [PropertyRegex.GroupNumberFromName ("name")].Value;
221 pr = new PropertyReference (name, m.Groups [0].Index, m.Groups [0].Length);
226 ArrayList phase2 = new ArrayList ();
228 int end = text.Length - 1;
230 foreach (PropertyReference pr in phase1) {
237 phase2.Add (text.Substring (a + 1, b - a - 1));
245 phase2.Add (text.Substring (last_end + 1, end - last_end));
250 ArrayList SplitMetadata (string text)
252 ArrayList phase1 = new ArrayList ();
254 m = MetadataRegex.Match (text);
257 string name = null, meta = null;
258 MetadataReference mr;
260 if (m.Groups [MetadataRegex.GroupNumberFromName ("name")].Success)
261 name = m.Groups [MetadataRegex.GroupNumberFromName ("name")].Value;
263 meta = m.Groups [MetadataRegex.GroupNumberFromName ("meta")].Value;
265 mr = new MetadataReference (text.Substring (m.Groups [0].Index, m.Groups [0].Length),
266 name, meta, m.Groups [0].Index, m.Groups [0].Length);
271 ArrayList phase2 = new ArrayList ();
273 int end = text.Length - 1;
275 foreach (MetadataReference mr in phase1) {
282 phase2.Add (text.Substring (a + 1, b - a - 1));
290 phase2.Add (text.Substring (last_end + 1, end - last_end));
295 public object ConvertTo (Project project, Type type)
297 return ConvertTo (project, type, ExpressionOptions.ExpandItemRefs);
300 public object ConvertTo (Project project, Type type, ExpressionOptions options)
302 return expressionCollection.ConvertTo (project, type, options);
305 public ExpressionCollection Collection {
306 get { return expressionCollection; }
309 static Regex ItemRegex {
311 if (item_regex == null)
312 item_regex = new Regex (
314 + @"(?<itemname>[_A-Za-z][_\-0-9a-zA-Z]*)"
315 + @"(?<has_transform>\s*->\s*'(?<transform>[^']*)')?"
316 + @"(?<has_separator>\s*,\s*'(?<separator>[^']*)')?"
322 static Regex PropertyRegex {
324 if (property_regex == null)
325 property_regex = new Regex (
327 + @"(?<name>[_a-zA-Z][_\-0-9a-zA-Z]*)"
329 return property_regex;
333 static Regex MetadataRegex {
335 if (metadata_regex == null)
336 metadata_regex = new Regex (
338 + @"((?<name>[_a-zA-Z][_\-0-9a-zA-Z]*)\.)?"
339 + @"(?<meta>[_a-zA-Z][_\-0-9a-zA-Z]*)"
341 return metadata_regex;
348 // absence of one of these flags, means
349 // false for that option
354 None = 0x8, // == no items, no metadata, and no split
356 // commonly used options
357 AllowItemsMetadataAndSplit = AllowItems | Split | AllowMetadata,
358 AllowItemsNoMetadataAndSplit = AllowItems | Split
361 enum ExpressionOptions {