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;
37 namespace Microsoft.Build.BuildEngine {
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_
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
51 // The enum ExpressionOptions is for specifying this expansion of item references.
53 // GroupingCollection.Evaluate, evaluates all properties and then items
55 internal class Expression {
57 ExpressionCollection expressionCollection;
59 static Regex item_regex;
60 static Regex property_regex;
61 static Regex metadata_regex;
65 this.expressionCollection = new ExpressionCollection ();
68 public void Parse (string expression, bool allowItems)
70 Parse (expression, allowItems, true);
73 // @split: Split on ';'
74 // Eg. Property values don't need to be split
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)
80 expression = expression.Replace ('/', Path.DirectorySeparatorChar);
81 expression = expression.Replace ('\\', Path.DirectorySeparatorChar);
85 parts = expression.Split (new char [] {';'}, StringSplitOptions.RemoveEmptyEntries);
87 parts = new string [] { expression };
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);
93 Prepare (p1, parts.Length);
94 Prepare (p2, parts.Length);
95 Prepare (p3, parts.Length);
97 for (int i = 0; i < parts.Length; i++)
98 p1 [i] = SplitItems (parts [i], allowItems);
100 for (int i = 0; i < parts.Length; i++) {
101 p2 [i] = new ArrayList ();
102 foreach (object o in p1 [i]) {
104 p2 [i].AddRange (SplitProperties ((string) o));
110 for (int i = 0; i < parts.Length; i++) {
111 p3 [i] = new ArrayList ();
112 foreach (object o in p2 [i]) {
114 p3 [i].AddRange (SplitMetadata ((string) o));
120 CopyToExpressionCollection (p3, allowItems);
123 void Prepare (List <ArrayList> l, int length)
125 for (int i = 0; i < length; i++)
129 void CopyToExpressionCollection (List <ArrayList> lists, bool allowItems)
131 for (int i = 0; i < lists.Count; i++) {
132 foreach (object o in lists [i]) {
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);
140 if (i < lists.Count - 1)
141 expressionCollection.Add (";");
145 ArrayList SplitItems (string text, bool allowItems)
147 ArrayList phase1 = new ArrayList ();
149 m = ItemRegex.Match (text);
152 string name = null, transform = null, separator = null;
155 name = m.Groups [ItemRegex.GroupNumberFromName ("itemname")].Value;
157 if (m.Groups [ItemRegex.GroupNumberFromName ("has_transform")].Success)
158 transform = m.Groups [ItemRegex.GroupNumberFromName ("transform")].Value;
160 if (m.Groups [ItemRegex.GroupNumberFromName ("has_separator")].Success)
161 separator = m.Groups [ItemRegex.GroupNumberFromName ("separator")].Value;
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);
169 ArrayList phase2 = new ArrayList ();
171 int end = text.Length - 1;
173 foreach (ItemReference ir in phase1) {
180 phase2.Add (text.Substring (a + 1, b - a - 1));
188 phase2.Add (text.Substring (last_end + 1, end - last_end));
193 ArrayList SplitProperties (string text)
195 ArrayList phase1 = new ArrayList ();
197 m = PropertyRegex.Match (text);
201 PropertyReference pr;
203 name = m.Groups [PropertyRegex.GroupNumberFromName ("name")].Value;
205 pr = new PropertyReference (name, m.Groups [0].Index, m.Groups [0].Length);
210 ArrayList phase2 = new ArrayList ();
212 int end = text.Length - 1;
214 foreach (PropertyReference pr in phase1) {
221 phase2.Add (text.Substring (a + 1, b - a - 1));
229 phase2.Add (text.Substring (last_end + 1, end - last_end));
234 ArrayList SplitMetadata (string text)
236 ArrayList phase1 = new ArrayList ();
238 m = MetadataRegex.Match (text);
241 string name = null, meta = null;
242 MetadataReference mr;
244 if (m.Groups [MetadataRegex.GroupNumberFromName ("name")].Success)
245 name = m.Groups [MetadataRegex.GroupNumberFromName ("name")].Value;
247 meta = m.Groups [MetadataRegex.GroupNumberFromName ("meta")].Value;
249 mr = new MetadataReference (name, meta, m.Groups [0].Index, m.Groups [0].Length);
254 ArrayList phase2 = new ArrayList ();
256 int end = text.Length - 1;
258 foreach (MetadataReference mr in phase1) {
265 phase2.Add (text.Substring (a + 1, b - a - 1));
273 phase2.Add (text.Substring (last_end + 1, end - last_end));
278 public object ConvertTo (Project project, Type type)
280 return ConvertTo (project, type, ExpressionOptions.ExpandItemRefs);
283 public object ConvertTo (Project project, Type type, ExpressionOptions options)
285 return expressionCollection.ConvertTo (project, type, options);
288 public ExpressionCollection Collection {
289 get { return expressionCollection; }
292 static Regex ItemRegex {
294 if (item_regex == null)
295 item_regex = new Regex (
297 + @"(?<itemname>[_A-Za-z][_0-9a-zA-Z]*)"
298 + @"(?<has_transform>\s*->\s*'(?<transform>[^']*)')?"
299 + @"(?<has_separator>\s*,\s*'(?<separator>[^']*)')?"
305 static Regex PropertyRegex {
307 if (property_regex == null)
308 property_regex = new Regex (
310 + @"(?<name>[_a-zA-Z][_0-9a-zA-Z]*)"
312 return property_regex;
316 static Regex MetadataRegex {
318 if (metadata_regex == null)
319 metadata_regex = new Regex (
321 + @"((?<name>[_a-zA-Z][_0-9a-zA-Z]*)\.)?"
322 + @"(?<meta>[_a-zA-Z][_0-9a-zA-Z]*)"
324 return metadata_regex;
329 enum ExpressionOptions {