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.
32 using System.Collections;
33 using System.Collections.Generic;
35 using System.Text.RegularExpressions;
36 using Mono.XBuild.Utilities;
38 namespace Microsoft.Build.BuildEngine {
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_
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
52 // The enum ExpressionOptions is for specifying this expansion of item references.
54 // GroupingCollection.Evaluate, evaluates all properties and then items
56 internal class Expression {
58 ExpressionCollection expressionCollection;
60 static Regex item_regex;
61 static Regex property_regex;
62 static Regex metadata_regex;
66 this.expressionCollection = new ExpressionCollection ();
69 // Split: Split on ';'
70 // Eg. Property values don't need to be split
72 // AllowItems: if false, item refs should not be treated as item refs!
73 // it converts them to strings in the final expressionCollection
75 // AllowMetadata: same as AllowItems, for metadata
76 public void Parse (string expression, ParseOptions options)
78 bool split = (options & ParseOptions.Split) == ParseOptions.Split;
79 bool allowItems = (options & ParseOptions.AllowItems) == ParseOptions.AllowItems;
80 bool allowMd = (options & ParseOptions.AllowMetadata) == ParseOptions.AllowMetadata;
82 expression = expression.Replace ('\\', Path.DirectorySeparatorChar);
86 parts = expression.Split (new char [] {';'}, StringSplitOptions.RemoveEmptyEntries);
88 parts = new string [] { expression };
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);
94 Prepare (p1, parts.Length);
95 Prepare (p2, parts.Length);
96 Prepare (p3, parts.Length);
98 for (int i = 0; i < parts.Length; i++)
99 p1 [i] = SplitItems (parts [i], allowItems);
101 for (int i = 0; i < parts.Length; i++) {
102 p2 [i] = new ArrayList ();
103 foreach (object o in p1 [i]) {
105 p2 [i].AddRange (SplitProperties ((string) o));
111 for (int i = 0; i < parts.Length; i++) {
112 p3 [i] = new ArrayList ();
113 foreach (object o in p2 [i]) {
115 p3 [i].AddRange (SplitMetadata ((string) o));
121 CopyToExpressionCollection (p3, allowItems, allowMd);
124 void Prepare (List <ArrayList> l, int length)
126 for (int i = 0; i < length; i++)
130 void CopyToExpressionCollection (List <ArrayList> lists, bool allowItems, bool allowMd)
132 for (int i = 0; i < lists.Count; i++) {
133 foreach (object o in lists [i]) {
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);
141 else if (o is IReference)
142 expressionCollection.Add ((IReference) o);
144 if (i < lists.Count - 1)
145 expressionCollection.Add (";");
149 ArrayList SplitItems (string text, bool allowItems)
151 ArrayList phase1 = new ArrayList ();
153 m = ItemRegex.Match (text);
156 string name = null, transform = null, separator = null;
159 name = m.Groups [ItemRegex.GroupNumberFromName ("itemname")].Value;
161 if (m.Groups [ItemRegex.GroupNumberFromName ("has_transform")].Success)
162 transform = m.Groups [ItemRegex.GroupNumberFromName ("transform")].Value;
164 if (m.Groups [ItemRegex.GroupNumberFromName ("has_separator")].Success)
165 separator = m.Groups [ItemRegex.GroupNumberFromName ("separator")].Value;
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);
173 ArrayList phase2 = new ArrayList ();
175 int end = text.Length - 1;
177 foreach (ItemReference ir in phase1) {
184 phase2.Add (text.Substring (a + 1, b - a - 1));
192 phase2.Add (text.Substring (last_end + 1, end - last_end));
197 ArrayList SplitProperties (string text)
199 ArrayList phase1 = new ArrayList ();
201 m = PropertyRegex.Match (text);
205 PropertyReference pr;
207 name = m.Groups [PropertyRegex.GroupNumberFromName ("name")].Value;
209 pr = new PropertyReference (name, m.Groups [0].Index, m.Groups [0].Length);
214 ArrayList phase2 = new ArrayList ();
216 int end = text.Length - 1;
218 foreach (PropertyReference pr in phase1) {
225 phase2.Add (text.Substring (a + 1, b - a - 1));
233 phase2.Add (text.Substring (last_end + 1, end - last_end));
238 ArrayList SplitMetadata (string text)
240 ArrayList phase1 = new ArrayList ();
242 m = MetadataRegex.Match (text);
245 string name = null, meta = null;
246 MetadataReference mr;
248 if (m.Groups [MetadataRegex.GroupNumberFromName ("name")].Success)
249 name = m.Groups [MetadataRegex.GroupNumberFromName ("name")].Value;
251 meta = m.Groups [MetadataRegex.GroupNumberFromName ("meta")].Value;
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);
259 ArrayList phase2 = new ArrayList ();
261 int end = text.Length - 1;
263 foreach (MetadataReference mr in phase1) {
270 phase2.Add (text.Substring (a + 1, b - a - 1));
278 phase2.Add (text.Substring (last_end + 1, end - last_end));
283 public object ConvertTo (Project project, Type type)
285 return ConvertTo (project, type, ExpressionOptions.ExpandItemRefs);
288 public object ConvertTo (Project project, Type type, ExpressionOptions options)
290 return expressionCollection.ConvertTo (project, type, options);
293 public ExpressionCollection Collection {
294 get { return expressionCollection; }
297 static Regex ItemRegex {
299 if (item_regex == null)
300 item_regex = new Regex (
302 + @"(?<itemname>[_A-Za-z][_\-0-9a-zA-Z]*)"
303 + @"(?<has_transform>\s*->\s*'(?<transform>[^']*)')?"
304 + @"(?<has_separator>\s*,\s*'(?<separator>[^']*)')?"
310 static Regex PropertyRegex {
312 if (property_regex == null)
313 property_regex = new Regex (
315 + @"(?<name>[_a-zA-Z][_\-0-9a-zA-Z]*)"
317 return property_regex;
321 static Regex MetadataRegex {
323 if (metadata_regex == null)
324 metadata_regex = new Regex (
326 + @"((?<name>[_a-zA-Z][_\-0-9a-zA-Z]*)\.)?"
327 + @"(?<meta>[_a-zA-Z][_\-0-9a-zA-Z]*)"
329 return metadata_regex;
336 // absence of one of these flags, means
337 // false for that option
342 None = 0x8, // == no items, no metadata, and no split
344 // commonly used options
345 AllowItemsMetadataAndSplit = AllowItems | Split | AllowMetadata,
346 AllowItemsNoMetadataAndSplit = AllowItems | Split
349 enum ExpressionOptions {