aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/IntermediateSymbol.cs
blob: 4be17094dcb33416824c73bcf470dcfe35a540f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Data
{
    using System;
    using System.Diagnostics;
    using SimpleJson;

    /// <summary>
    /// Intermediate symbol.
    /// </summary>
    [DebuggerDisplay("{DebuggerDisplay,nq}")]
    public class IntermediateSymbol
    {
        private object tags;

        /// <summary>
        /// Creates an intermediate symbol.
        /// </summary>
        /// <param name="definition">Symbol definition.</param>
        public IntermediateSymbol(IntermediateSymbolDefinition definition) : this(definition, null, null)
        {
        }

        /// <summary>
        /// Creates an intermediate symbol with source line number and identifier. 
        /// </summary>
        /// <param name="definition">Symbol definition.</param>
        /// <param name="sourceLineNumber">Source line number.</param>
        /// <param name="id">Symbol identifier.</param>
        public IntermediateSymbol(IntermediateSymbolDefinition definition, SourceLineNumber sourceLineNumber, Identifier id = null)
        {
            this.Definition = definition;
            this.Fields = new IntermediateField[definition.FieldDefinitions.Length];
            this.SourceLineNumbers = sourceLineNumber;
            this.Id = id;
        }

        /// <summary>
        /// Gets the symbol's definition.
        /// </summary>
        public IntermediateSymbolDefinition Definition { get; }

        /// <summary>
        /// Gets the symbol's fields.
        /// </summary>
        public IntermediateField[] Fields { get; }

        /// <summary>
        /// Gets the optional source line number of the symbol.
        /// </summary>
        public SourceLineNumber SourceLineNumbers { get; internal set; }

        /// <summary>
        /// Gets the optional identifier for the symbol.
        /// </summary>
        public Identifier Id { get; internal set; }

        /// <summary>
        /// Direct access by index to the symbol's fields.
        /// </summary>
        /// <param name="index">Index of the field to access.</param>
        /// <returns>Symbol's field.</returns>
        public IntermediateField this[int index] => this.Fields[index];

        private string DebuggerDisplay => $"{this.Definition?.Name} {this.Id?.Id}";

        /// <summary>
        /// Add a custom tag to the symbol.
        /// </summary>
        /// <param name="add">String tag to add to the symbol.</param>
        /// <returns>True if the tag was added; otherwise false if th tag was already present.</returns>
        public bool AddTag(string add)
        {
            if (this.tags == null)
            {
                this.tags = add;
            }
            else if (this.tags is string tag)
            {
                if (tag == add)
                {
                    return false;
                }

                this.tags = new[] { tag, add };
            }
            else
            {
                var tagsArray = (string[])this.tags;
                var array = new string[tagsArray.Length + 1];

                for (var i = 0; i < tagsArray.Length; ++i)
                {
                    if (tagsArray[i] == add)
                    {
                        return false;
                    }

                    array[i] = tagsArray[i];
                }

                array[tagsArray.Length] = add;

                this.tags = array;
            }

            return true;
        }

        /// <summary>
        /// Tests whether a symbol has a tag.
        /// </summary>
        /// <param name="has">String tag to find.</param>
        /// <returns>True if the symbol has the tag; otherwise false.</returns>
        public bool HasTag(string has)
        {
            if (this.tags == null)
            {
                return false;
            }
            else if (this.tags is string tag)
            {
                return tag == has;
            }
            else
            {
                foreach (var element in (string[])this.tags)
                {
                    if (element == has)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Removes a tag from the symbol.
        /// </summary>
        /// <param name="remove">String tag to remove.</param>
        /// <returns>True if the tag was removed; otherwise false if the tag was not present.</returns>
        public bool RemoveTag(string remove)
        {
            if (this.tags is string tag)
            {
                if (tag == remove)
                {
                    this.tags = null;
                    return true;
                }
            }
            else if (this.tags is string[] tagsArray)
            {
                if (tagsArray.Length == 2)
                {
                    if (tagsArray[0] == remove)
                    {
                        this.tags = tagsArray[1];
                        return true;
                    }
                    else if (tagsArray[1] == remove)
                    {
                        this.tags = tagsArray[0];
                        return true;
                    }
                }
                else
                {
                    var array = new string[tagsArray.Length - 1];
                    var arrayIndex = 0;
                    var found = false;

                    for (var i = 0; i < tagsArray.Length; ++i)
                    {
                        if (tagsArray[i] == remove)
                        {
                            found = true;
                            continue;
                        }
                        else if (arrayIndex == array.Length)
                        {
                            break;
                        }

                        array[arrayIndex++] = tagsArray[i];
                    }

                    if (found)
                    {
                        this.tags = array;
                        return true;
                    }
                }
            }

            return false;
        }

        internal static IntermediateSymbol Deserialize(ISymbolDefinitionCreator creator, Uri baseUri, JsonObject jsonObject)
        {
            var definitionName = jsonObject.GetValueOrDefault<string>("type");
            var idJson = jsonObject.GetValueOrDefault<JsonObject>("id");
            var sourceLineNumbersJson = jsonObject.GetValueOrDefault<JsonObject>("ln");
            var fieldsJson = jsonObject.GetValueOrDefault<JsonArray>("fields");
            var tagsJson = jsonObject.GetValueOrDefault<JsonArray>("tags");

            var id = (idJson == null) ? null : Identifier.Deserialize(idJson);
            var sourceLineNumbers = (sourceLineNumbersJson == null) ? null : SourceLineNumber.Deserialize(sourceLineNumbersJson);

            if (!creator.TryGetSymbolDefinitionByName(definitionName, out var definition))
            {
                throw new WixException(ErrorMessages.UnknownSymbolType(definitionName));
            }

            var symbol = definition.CreateSymbol(sourceLineNumbers, id);

            for (var i = 0; i < fieldsJson.Count && i < symbol.Fields.Length; ++i)
            {
                if (fieldsJson[i] is JsonObject fieldJson)
                {
                    symbol.Fields[i] = IntermediateField.Deserialize(symbol.Definition.FieldDefinitions[i], baseUri, fieldJson);
                }
            }

            if (tagsJson == null || tagsJson.Count == 0)
            {
            }
            else if (tagsJson.Count == 1)
            {
                symbol.tags = (string)tagsJson[0];
            }
            else
            {
                var tags = new string[tagsJson.Count];

                for (var i = 0; i < tagsJson.Count; ++i)
                {
                    tags[i] = (string)tagsJson[i];
                }

                symbol.tags = tags;
            }

            return symbol;
        }

        internal JsonObject Serialize()
        {
            var jsonObject = new JsonObject
            {
                { "type", this.Definition.Name }
            };

            var idJson = this.Id?.Serialize();
            if (idJson != null)
            {
                jsonObject.Add("id", idJson);
            }

            var lnJson = this.SourceLineNumbers?.Serialize();
            if (lnJson != null)
            {
                jsonObject.Add("ln", lnJson);
            }

            var fieldsJson = new JsonArray(this.Fields.Length);

            foreach (var field in this.Fields)
            {
                var fieldJson = field?.Serialize();
                fieldsJson.Add(fieldJson);
            }

            jsonObject.Add("fields", fieldsJson);

            if (this.tags is string || this.tags is string[])
            {
                JsonArray tagsJson;

                if (this.tags is string tag)
                {
                    tagsJson = new JsonArray(1) { tag };
                }
                else
                {
                    var array = (string[])this.tags;
                    tagsJson = new JsonArray(array.Length);
                    tagsJson.AddRange(array);
                }

                jsonObject.Add("tags", tagsJson);
            }

            return jsonObject;
        }
    }
}