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 {
38 internal class Expression {
40 ExpressionCollection expressionCollection;
44 this.expressionCollection = new ExpressionCollection ();
47 public void Parse (string expression)
49 expression = expression.Replace ('/', Path.DirectorySeparatorChar);
50 expression = expression.Replace ('\\', Path.DirectorySeparatorChar);
52 string [] parts = expression.Split (';');
54 List <ArrayList> p1 = new List <ArrayList> (parts.Length);
55 List <ArrayList> p2 = new List <ArrayList> (parts.Length);
56 List <ArrayList> p3 = new List <ArrayList> (parts.Length);
58 Prepare (p1, parts.Length);
59 Prepare (p2, parts.Length);
60 Prepare (p3, parts.Length);
62 for (int i = 0; i < parts.Length; i++) {
63 p1 [i] = SplitItems (parts [i]);
66 for (int i = 0; i < parts.Length; i++) {
67 p2 [i] = new ArrayList ();
68 foreach (object o in p1 [i]) {
70 p2 [i].AddRange (SplitProperties ((string) o));
76 for (int i = 0; i < parts.Length; i++) {
77 p3 [i] = new ArrayList ();
78 foreach (object o in p2 [i]) {
80 p3 [i].AddRange (SplitMetadata ((string) o));
86 CopyToExpressionCollection (p3);
89 void Prepare (List <ArrayList> l, int length)
91 for (int i = 0; i < length; i++)
95 void CopyToExpressionCollection (List <ArrayList> lists)
97 for (int i = 0; i < lists.Count; i++) {
98 foreach (object o in lists [i]) {
100 expressionCollection.Add (Utilities.Unescape ((string) o));
101 else if (o is ItemReference)
102 expressionCollection.Add ((ItemReference) o);
103 else if (o is PropertyReference)
104 expressionCollection.Add ((PropertyReference) o);
105 else if (o is MetadataReference)
106 expressionCollection.Add ((MetadataReference) o);
108 if (i < lists.Count - 1)
109 expressionCollection.Add (";");
113 ArrayList SplitItems (string text)
115 ArrayList phase1 = new ArrayList ();
117 Regex item = new Regex (
119 + @"(?<itemname>[_A-Za-z][_0-9a-zA-Z]*)"
120 + @"(?<has_transform>\s*->\s*'(?<transform>[^']*)')?"
121 + @"(?<has_separator>\s*,\s*'(?<separator>[^']*)')?"
123 m = item.Match (text);
126 string name = null, transform = null, separator = null;
129 name = m.Groups [item.GroupNumberFromName ("itemname")].Value;
131 if (m.Groups [item.GroupNumberFromName ("has_transform")].Success)
132 transform = m.Groups [item.GroupNumberFromName ("transform")].Value;
134 if (m.Groups [item.GroupNumberFromName ("has_separator")].Success)
135 separator = m.Groups [item.GroupNumberFromName ("separator")].Value;
137 ir = new ItemReference (name, transform, separator, m.Groups [0].Index, m.Groups [0].Length);
142 ArrayList phase2 = new ArrayList ();
144 int end = text.Length - 1;
146 foreach (ItemReference ir in phase1) {
153 phase2.Add (text.Substring (a + 1, b - a - 1));
161 phase2.Add (text.Substring (last_end + 1, end - last_end));
166 ArrayList SplitProperties (string text)
168 ArrayList phase1 = new ArrayList ();
170 Regex property = new Regex (
172 + @"(?<name>[_a-zA-Z][_0-9a-zA-Z]*)"
174 m = property.Match (text);
178 PropertyReference pr;
180 name = m.Groups [property.GroupNumberFromName ("name")].Value;
182 pr = new PropertyReference (name, m.Groups [0].Index, m.Groups [0].Length);
187 ArrayList phase2 = new ArrayList ();
189 int end = text.Length - 1;
191 foreach (PropertyReference pr in phase1) {
198 phase2.Add (text.Substring (a + 1, b - a - 1));
206 phase2.Add (text.Substring (last_end + 1, end - last_end));
211 ArrayList SplitMetadata (string text)
213 ArrayList phase1 = new ArrayList ();
215 Regex metadata = new Regex (
217 + @"((?<name>[_a-zA-Z][_0-9a-zA-Z]*)\.)?"
218 + @"(?<meta>[_a-zA-Z][_0-9a-zA-Z]*)"
220 m = metadata.Match (text);
223 string name = null, meta = null;
224 MetadataReference mr;
226 if (m.Groups [metadata.GroupNumberFromName ("name")].Success)
227 name = m.Groups [metadata.GroupNumberFromName ("name")].Value;
229 meta = m.Groups [metadata.GroupNumberFromName ("meta")].Value;
231 mr = new MetadataReference (name, meta, m.Groups [0].Index, m.Groups [0].Length);
236 ArrayList phase2 = new ArrayList ();
238 int end = text.Length - 1;
240 foreach (MetadataReference mr in phase1) {
247 phase2.Add (text.Substring (a + 1, b - a - 1));
255 phase2.Add (text.Substring (last_end + 1, end - last_end));
260 public object ConvertTo (Project project, Type type)
262 return expressionCollection.ConvertTo (project, type);
265 public ExpressionCollection Collection {
266 get { return expressionCollection; }