aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.RegExpr/production/Production.cs
blob: 324f0c5afc5c4ebcc89f0b85f8eb23ef6ec2c259 (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
301
302
303
304
305
306
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt VS Tools.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace QtVsTools.SyntaxAnalysis
{
    public abstract partial class RegExpr
    {
        public partial class Parser
        {
            ////////////////////////////////////////////////////////////////////////////////////////
            ///
            /// RegExpr.Parser.GetProductionObjects()
            ///
            ////////////////////////////////////////////////////////////////////////////////////////
            /// <summary>
            /// Extract productions from a parse tree.
            /// </summary>
            /// <param name="root">
            /// Root node of parse tree, obtained from parsing an input text with a RegExpr
            /// </param>
            /// <returns>Productions by token id</returns>
            ProductionObjects GetProductionObjects(ParseTree parseTree)
            {
                var outputProductions = new ProductionObjects();

                var stack = new Stack<ParseTree.Node>();
                stack.Push(parseTree.Root);
                while (stack.Any()) {
                    var node = stack.Pop();

                    // Depth-first traversal
                    if (node.TokenStream == null) {
                        node.TokenStream = new Queue<ParseTree.Node>(node.ChildNodes.Values);
                        node.OperandStack = new Stack<ParseTree.Node>();
                        node.OperatorStack = new Stack<ParseTree.Node>();
                        stack.Push(node);
                        continue;
                    } else if (node.TokenStream.Any()) {
                        var nextNode = node.TokenStream.Dequeue();
                        stack.Push(node);
                        stack.Push(nextNode);
                        continue;
                    }

                    if (node.Parent == null)
                        continue;

                    var operatorStack = node.Parent.OperatorStack;
                    var operandStack = node.Parent.OperandStack;
                    var rule = node.Rule;
                    if (rule == null) {
                        // Default token (without rule definitions)
                        // just use captured value as production
                        node.Production = node.Value;
                        operandStack.Push(node);
                    } else if (rule.Delimiters != Delimiter.None) {
                        // Delimiter token
                        if (rule.Delimiters == Delimiter.Left) {
                            // if left delim, push to operator stack
                            operatorStack.Push(node);
                        } else {
                            // if right delim, unwind operator stack until left delim
                            UnwindOperatorStack(HaltUnwind.AtLeftDelimiter,
                                operatorStack, operandStack);
                            // set left delim as left operand, delimited expr as right operand
                            operandStack.ReverseTop();
                            // execute delimiter rule
                            node.Production = rule.Execute(node);
                            operandStack.Push(node);
                        }
                    } else if (rule.Operands != Operand.None) {
                        // Operator token
                        // unwind operator stack until lower priority operator or empty
                        UnwindOperatorStack(HaltUnwind.AtLowerPriority,
                            operatorStack, operandStack, rule.Priority);

                        // if operator needs left operand but none is available, error out
                        if (rule.Operands.HasFlag(Operand.Left) && !operandStack.Any())
                            throw new ParseErrorException();

                        if (rule.Operands.HasFlag(Operand.Right)) {
                            // if needs right operand, push to operator stack
                            operatorStack.Push(node);
                        } else {
                            // if left operand only, execute rule immediately
                            node.Production = rule.Execute(node);
                            operandStack.Push(node);
                        }
                    } else {
                        // Captured value or embedded captures ("nullary operator")
                        // execute rule immediately
                        node.Production = rule.Execute(node);
                        operandStack.Push(node);
                    }

                    if (node.IsLast) {
                        // Last token
                        // unwind operator stack until empty
                        UnwindOperatorStack(HaltUnwind.WhenEmpty,
                            operatorStack, operandStack);

                        // get output from operand stack
                        foreach (var operand in operandStack.Reverse()) {

                            // check if it's a dangling left delimiter
                            if (operand.Rule != null && operand.Rule.Delimiters == Delimiter.Left)
                                throw new ParseErrorException();

                            // add production to parent context
                            node.Parent.ChildProductions.Add(operand.TokenId, operand.Production);

                            // add production to output list
                            outputProductions.Add(operand.TokenId, operand.Production);
                        }
                        operandStack.Clear();
                    }
                }

                return outputProductions;
            }

            enum HaltUnwind
            {
                WhenEmpty,
                AtLeftDelimiter,
                AtLowerPriority
            }

            void UnwindOperatorStack(
                HaltUnwind haltingCondition,
                Stack<ParseTree.Node> operatorStack,
                Stack<ParseTree.Node> operandStack,
                int priority = int.MinValue)
            {
                while (operatorStack.Any()) {
                    var node = operatorStack.Pop();
                    Debug.Assert(node != null);

                    var rule = node.Rule;
                    Debug.Assert(rule != null);

                    if (haltingCondition == HaltUnwind.AtLeftDelimiter
                        && rule.Delimiters == Delimiter.Left
                    ) {
                        // Halting stack unwind: left delimiter found
                        // check if an operand (i.e. delimited expression) is available
                        if (!operandStack.Any())
                            throw new ParseErrorException();

                        // execute left delimiter rule
                        node.Production = rule.Execute(node);

                        // add to operands (will be picked up by right delimiter rule)
                        operandStack.Push(node);
                        return;
                    }

                    if (haltingCondition == HaltUnwind.AtLowerPriority
                        && rule.Priority < priority
                    ) {
                        // Halting stack unwind: lower priority operator found
                        // push operator back into stack
                        operatorStack.Push(node);
                        return;
                    }

                    // still haven't found what we're looking for; continue stack unwind
                    node.Production = rule.Execute(node);
                    operandStack.Push(node);
                }

                // error-out if didn't find left delimiter
                if (haltingCondition == HaltUnwind.AtLeftDelimiter)
                    throw new ParseErrorException();
            }
        }

        /// <summary>
        /// Collection of production objects, grouped by token ID
        /// </summary>
        public partial class ProductionObjects : IEnumerable<KeyValuePair<string, object>>
        {
            List<KeyValuePair<string, object>> Productions { get; }
            Dictionary<string, List<object>> ProductionsByTokenId { get; }

            public ProductionObjects()
            {
                Productions = new List<KeyValuePair<string, object>>();
                ProductionsByTokenId = new Dictionary<string, List<object>>();
            }

            public void Add(string tokenId, object prodObj)
            {
                Productions.Add(new KeyValuePair<string, object>(tokenId, prodObj));
                if (!ProductionsByTokenId.TryGetValue(tokenId, out List<object> prodObjs))
                    ProductionsByTokenId.Add(tokenId, prodObjs = new List<object>());
                prodObjs.Add(prodObj);
            }

            public IEnumerable<T> GetValues<T>(string tokenId)
            {
                if (string.IsNullOrEmpty(tokenId))
                    return Empty<T>();

                if (!ProductionsByTokenId.TryGetValue(tokenId, out List<object> tokenProds))
                    return Empty<T>();

                return tokenProds
                    .Where(x => x != null && x is T)
                    .Select(x => (T)x);
            }

            public IEnumerable<T> GetValues<T>(Enum tokenId)
            {
                return GetValues<T>(tokenId.ToString());
            }

            public IEnumerable<T> GetValues<T>(Token token)
            {
                return GetValues<T>(token.Id);
            }

            public IEnumerable<T> GetValues<T>(ProductionRule<T> production)
            {
                return GetValues<T>(production.Token.Id);
            }

            public IEnumerable<object> GetValues(string tokenId)
            {
                return GetValues<object>(tokenId);
            }

            public IEnumerable<object> GetValues(Enum tokenId)
            {
                return GetValues<object>(tokenId.ToString());
            }

            public IEnumerable<object> GetValues(Token token)
            {
                return GetValues<object>(token.Id);
            }

            public IEnumerable<object> GetValues(IProductionRule production)
            {
                return GetValues<object>(production.Token.Id);
            }

            public object this[string tokenId, int index = 0]
            {
                get
                {
                    return GetValues(tokenId).ElementAtOrDefault(index);
                }
            }

            public object this[Enum tokenId, int index = 0]
            {
                get
                {
                    return this[tokenId.ToString(), index];
                }
            }

            public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
            {
                return ((IEnumerable<KeyValuePair<string, object>>)Productions).GetEnumerator();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return ((IEnumerable<KeyValuePair<string, object>>)Productions).GetEnumerator();
            }
        }
    }
}