aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.RegExpr/expression/CharClassSet.cs
blob: af6f48df7b6ca6fd3ab6ed6bd4be0411d7058bcb (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
307
308
309
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QtVsTools.SyntaxAnalysis
{
    using static CharClass.CharSetExprBuilder;
    using static CharClassSet;

    ////////////////////////////////////////////////////////////////////////////////////////////////
    ///
    /// CharClassSet ( -> CharClass -> RegExpr )
    ///
    ////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// Represents a complex class defined by a combination of elementary character classes.
    /// </summary>
    ///
    public partial class CharClassSet : CharClass, IEnumerable<IEnumerable<Element>>
    {
        private IEnumerable<Element> Positives { get; set; }
        private IEnumerable<Element> Negatives { get; set; }

        bool IsSubSet { get; set; }
        bool HasPositive => Positives?.Any() == true;
        bool HasNegative => Negatives?.Any() == true;

        protected override IEnumerable<RegExpr> OnRender(RegExpr defaultTokenWs, RegExpr parent,
            StringBuilder pattern, ref RenderMode mode, Stack<Token> tokenStack)
        {
            base.OnRender(defaultTokenWs, parent, pattern, ref mode, tokenStack);

            if (!HasPositive && !HasNegative)
                return null;

            if (!IsSubSet)
                pattern.Append(HasPositive ? "[" : "[^");

            IEnumerable<RegExpr> children = null;
            if (HasPositive && HasNegative) {
                children = Items(
                    new CharClassSet(positives: Positives) { IsSubSet = true },
                    new CharClassSet(negatives: Negatives) { IsSubSet = true });
            } else {
                if (HasPositive)
                    children = Positives;
                else if (HasNegative)
                    children = Negatives;
            }

            return children;
        }

        protected override void OnRenderNext(RegExpr defaultTokenWs, RegExpr parent,
            StringBuilder pattern, ref RenderMode mode, Stack<Token> tokenStack)
        {
            base.OnRenderNext(defaultTokenWs, parent, pattern, ref mode, tokenStack);
            if (!IsSubSet && HasPositive && HasNegative)
                pattern.Append("-[");
        }

        protected override void OnRenderEnd(RegExpr defaultTokenWs, RegExpr parent,
            StringBuilder pattern, ref RenderMode mode, Stack<Token> tokenStack)
        {
            base.OnRenderEnd(defaultTokenWs, parent, pattern, ref mode, tokenStack);
            if (!IsSubSet) {
                if (HasPositive && HasNegative)
                    pattern.Append("]]");
                else
                    pattern.Append("]");
            }
        }

        public const bool Invert = true;
        public const bool Positive = true;

        public CharClassSet(
            IEnumerable<Element> positives = null,
            IEnumerable<Element> negatives = null)
        {
            Positives = positives ?? Empty<Element>();
            Negatives = negatives ?? Empty<Element>();
        }

        public CharClassSet(Element element, bool negative = false) : this()
        {
            if (negative)
                Negatives = Items(element);
            else
                Positives = Items(element);
        }

        public CharClassSet(CharClassSet set, bool invert = false) : this()
        {
            Add(set, invert);
        }

        public void Add(Element element, bool negative = false)
        {
            if (negative)
                Negatives = Negatives.Concat(Items(element));
            else
                Positives = Positives.Concat(Items(element));
        }

        public void Add(CharClassSet set, bool invert = false)
        {
            Positives = Positives.Concat(!invert ? set.Positives : set.Negatives);
            Negatives = Negatives.Concat(!invert ? set.Negatives : set.Positives);
        }

        public IEnumerator<IEnumerable<Element>> GetEnumerator()
        {
            return new[] { Positives, Negatives }.AsEnumerable().GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public abstract class Element : CharClass
        {
            public static CharClassSet operator ~(Element x)
            { return new CharClassSet(x, negative: true); }

            public static PositiveSet operator +(Element x, Element y)
            { return new PositiveSet(Op.Plus, x, y); }

            public static PositiveSet operator +(Element x, PositiveSet y)
            { return new PositiveSet(Op.Plus, x, y); }

            public static PositiveSet operator +(PositiveSet x, Element y)
            { return new PositiveSet(Op.Plus, x, y); }

            public static Expr operator -(Element x, Element y)
            { return new Expr(Op.Minus, x, y); }

            public static Expr operator -(Element x, PositiveSet y)
            { return new Expr(Op.Minus, x, y); }

            public static Expr operator -(PositiveSet x, Element y)
            { return new Expr(Op.Minus, x, y); }
        }

        public static CharClassSet operator ~(CharClassSet x)
        { return new CharClassSet(x, invert: true); }

        public static Expr operator +(Element x, CharClassSet y)
        { return new Expr(Op.Plus, x, y); }

        public static Expr operator +(CharClassSet x, Element y)
        { return new Expr(Op.Plus, x, y); }

        public static Expr operator +(PositiveSet x, CharClassSet y)
        { return new Expr(Op.Plus, x, y); }

        public static Expr operator +(CharClassSet x, PositiveSet y)
        { return new Expr(Op.Plus, x, y); }

        public static Expr operator +(CharClassSet x, CharClassSet y)
        { return new Expr(Op.Plus, x, y); }

        public static Expr operator -(CharClassSet x, Element y)
        { return new Expr(Op.Minus, x, y); }

        public static Expr operator -(CharClassSet x, PositiveSet y)
        { return new Expr(Op.Minus, x, y); }
    }

    public abstract partial class CharClass : RegExpr
    {
        public partial class CharSetExprBuilder
        {
            public enum Op { Term, Tilde, Plus, Minus }

            public class Expr
            {
                public Op Operator { get; }

                public List<Expr> Factors { get; }
                public Expr(Op op, List<Expr> factors) { Operator = op; Factors = factors; }
                public Expr(Op op, params Expr[] factors) : this(op, factors.ToList()) { }

                public CharClass Term { get; }
                public Expr(CharClass c) { Operator = Op.Term; Term = c; }
                public static implicit operator Expr(CharClass c) { return new Expr(c); }
                public static implicit operator Expr(string s) { return Char[s]; }
                public static implicit operator Expr(char c) { return Char[c]; }
                public static Expr operator ~(Expr x)
                { return new Expr(Op.Tilde, x); }
            }

            public class PositiveSet : Expr
            {
                public PositiveSet(Op op, params Expr[] factors) : base(op, factors) { }

                public static PositiveSet operator +(PositiveSet x, PositiveSet y)
                { return new PositiveSet(Op.Plus, x, y); }

                public static Expr operator -(PositiveSet x, PositiveSet y)
                { return new Expr(Op.Minus, x, y); }
            }

            public CharClassSet this[params Expr[] exprs] => this[new PositiveSet(Op.Plus, exprs)];

            public CharClassSet this[Expr expr]
            {
                get
                {
                    var stack = new Stack<StackFrame>();
                    stack.Push(expr);
                    CharClassSet classSet = null;

                    while (stack.Any()) {
                        var context = stack.Pop();
                        expr = context.Expr;
                        if (expr == null)
                            continue;
                        if (context.Children == null) {
                            context.Children = new Queue<Expr>();
                            if (expr.Factors != null && expr.Factors.Any())
                                expr.Factors.ForEach(x => context.Children.Enqueue(x));
                            stack.Push(context);
                            continue;
                        }
                        if (context.Children.Any()) {
                            expr = context.Children.Dequeue();
                            stack.Push(context);
                            stack.Push(expr);
                            continue;
                        }

                        classSet = null;
                        if (expr.Operator == Op.Term) {
                            if (expr.Term is CharClassSet charClassSet)
                                classSet = charClassSet;
                            else
                                classSet = new CharClassSet(expr.Term as Element);
                        } else if (context.SubSets != null && context.SubSets.Any()) {
                            switch (expr.Operator) {
                            case Op.Tilde:
                                classSet = new CharClassSet
                                    {
                                        { context.SubSets.First(), Invert }
                                    };
                                break;
                            case Op.Plus:
                                classSet = new CharClassSet();
                                context.SubSets.ForEach(x => classSet.Add(x));
                                break;
                            case Op.Minus:
                                classSet = new CharClassSet
                                    {
                                        context.SubSets.First(),
                                        { context.SubSets.Last(), Invert }
                                    };
                                break;
                            }
                        }

                        var parentContext = stack.Any() ? stack.Peek() : null;
                        if (classSet != null && parentContext != null)
                            parentContext.SubSets.Add(classSet);
                    }

                    if (classSet == null)
                        throw new CharClassEvalException();

                    return classSet;
                }
            }

            public CharClassSet this[IEnumerable<char> chars] =>
                this[chars.Select(c => (Expr)c).ToArray()];

            class StackFrame
            {
                public Expr Expr { get; set; }
                public Queue<Expr> Children { get; set; }
                public List<CharClassSet> SubSets { get; }
                public StackFrame()
                {
                    Expr = null;
                    Children = null;
                    SubSets = new List<CharClassSet>();
                }
                public static implicit operator StackFrame(Expr e)
                {
                    return new StackFrame { Expr = e };
                }
            }
        }

        public partial class CharSetRawExprBuilder
        {
            public CharClassLiteral this[string s] => CharRawLiteral(s);
        }

        public class CharClassEvalException : RegExprException
        {
            public CharClassEvalException(string message = null) : base(message) { }
        }
    }
}