aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/regex/pire/regexp.h
blob: 3c4b3fe71c2a5396636f6bca7e4eed49a41b9f66 (plain) (blame)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#pragma once

#include "pire.h"

#include <library/cpp/charset/doccodes.h> 
#include <library/cpp/charset/recyr.hh> 
#include <util/generic/maybe.h>
#include <util/generic/strbuf.h>
#include <util/generic/string.h>
#include <util/generic/vector.h>
#include <util/generic/yexception.h>

namespace NRegExp {
    struct TMatcher;

    struct TFsmBase {
        struct TOptions {
            inline TOptions& SetCaseInsensitive(bool v) noexcept {
                CaseInsensitive = v;
                return *this;
            }

            inline TOptions& SetSurround(bool v) noexcept {
                Surround = v;
                return *this;
            }

            inline TOptions& SetCapture(size_t pos) noexcept {
                CapturePos = pos;
                return *this;
            }

            inline TOptions& SetCharset(ECharset charset) noexcept {
                Charset = charset;
                return *this;
            }

            inline TOptions& SetAndNotSupport(bool andNotSupport) noexcept {
                AndNotSupport = andNotSupport;
                return *this;
            }

            bool CaseInsensitive = false;
            bool Surround = false;
            TMaybe<size_t> CapturePos;
            ECharset Charset = CODES_UNKNOWN;
            bool AndNotSupport = false;
        };

        static inline NPire::TFsm Parse(const TStringBuf& regexp,
                                        const TOptions& opts, const bool needDetermine = true) {
            NPire::TLexer lexer;
            if (opts.Charset == CODES_UNKNOWN) {
                lexer.Assign(regexp.data(), regexp.data() + regexp.size());
            } else {
                TVector<wchar32> ucs4(regexp.size() + 1);
                size_t inRead = 0;
                size_t outWritten = 0;
                int recodeRes = RecodeToUnicode(opts.Charset, regexp.data(), ucs4.data(),
                                                regexp.size(), regexp.size(), inRead, outWritten);
                Y_ASSERT(recodeRes == RECODE_OK);
                Y_ASSERT(outWritten < ucs4.size());
                ucs4[outWritten] = 0;

                lexer.Assign(ucs4.begin(),
                             ucs4.begin() + std::char_traits<wchar32>::length(ucs4.data()));
            }

            if (opts.CaseInsensitive) {
                lexer.AddFeature(NPire::NFeatures::CaseInsensitive());
            }

            if (opts.CapturePos) {
                lexer.AddFeature(NPire::NFeatures::Capture(*opts.CapturePos));
            }

            if (opts.AndNotSupport) {
                lexer.AddFeature(NPire::NFeatures::AndNotSupport());
            }

            switch (opts.Charset) {
                case CODES_UNKNOWN:
                    break;
                case CODES_UTF8:
                    lexer.SetEncoding(NPire::NEncodings::Utf8());
                    break;
                case CODES_KOI8:
                    lexer.SetEncoding(NPire::NEncodings::Koi8r());
                    break;
                default:
                    lexer.SetEncoding(NPire::NEncodings::Get(opts.Charset));
                    break;
            }

            NPire::TFsm ret = lexer.Parse();

            if (opts.Surround) {
                ret.Surround();
            }

            if (needDetermine) {
                ret.Determine();
            }

            return ret;
        }
    };

    template <class TScannerType>
    class TFsmParser: public TFsmBase {
    public:
        typedef TScannerType TScanner;

    public:
        inline explicit TFsmParser(const TStringBuf& regexp,
                                   const TOptions& opts = TOptions(), bool needDetermine = true)
            : Scanner(Parse(regexp, opts, needDetermine).template Compile<TScanner>())
        {
        }

        inline const TScanner& GetScanner() const noexcept {
            return Scanner;
        }

        static inline TFsmParser False() {
            return TFsmParser(NPire::TFsm::MakeFalse().Compile<TScanner>());
        }

        inline explicit TFsmParser(const TScanner& compiled)
            : Scanner(compiled)
        {
            if (Scanner.Empty())
                ythrow yexception() << "Can't create fsm with empty scanner";
        }

    private:
        TScanner Scanner;
    };

    class TFsm: public TFsmParser<NPire::TNonrelocScanner> {
    public:
        inline explicit TFsm(const TStringBuf& regexp,
                             const TOptions& opts = TOptions())
            : TFsmParser<TScanner>(regexp, opts)
        {
        }

        inline TFsm(const TFsmParser<TScanner>& fsm)
            : TFsmParser<TScanner>(fsm)
        {
        }

        static inline TFsm Glue(const TFsm& l, const TFsm& r) {
            return TFsm(TScanner::Glue(l.GetScanner(), r.GetScanner()));
        }

        inline explicit TFsm(const TScanner& compiled)
            : TFsmParser<TScanner>(compiled)
        {
        }
    };

    static inline TFsm operator|(const TFsm& l, const TFsm& r) {
        return TFsm::Glue(l, r);
    }

    struct TCapturingFsm : TFsmParser<NPire::TCapturingScanner> {
        inline explicit TCapturingFsm(const TStringBuf& regexp,
                                      TOptions opts = TOptions())
            : TFsmParser<TScanner>(regexp,
                                   opts.SetSurround(true).CapturePos ? opts : opts.SetCapture(1)) {
        }

        inline TCapturingFsm(const TFsmParser<TScanner>& fsm)
            : TFsmParser<TScanner>(fsm)
        {
        }
    };

    struct TSlowCapturingFsm : TFsmParser<NPire::TSlowCapturingScanner> {
        inline explicit TSlowCapturingFsm(const TStringBuf& regexp,
                                          TOptions opts = TOptions())
                : TFsmParser<TScanner>(regexp,
                                       opts.SetSurround(true).CapturePos ? opts : opts.SetCapture(1), false) {
        }

        inline TSlowCapturingFsm(const TFsmParser<TScanner>& fsm)
                : TFsmParser<TScanner>(fsm)
        {
        }
    };

    template <class TFsm>
    class TMatcherBase {
    public:
        typedef typename TFsm::TScanner::State TState;

    public:
        inline explicit TMatcherBase(const TFsm& fsm)
            : Fsm(fsm)
        {
            Fsm.GetScanner().Initialize(State);
        }

        inline bool Final() const noexcept {
            return GetScanner().Final(GetState());
        }

    protected:
        inline void Run(const char* data, size_t len, bool addBegin, bool addEnd) noexcept {
            if (addBegin) {
                NPire::Step(GetScanner(), State, NPire::BeginMark);
            }
            NPire::Run(GetScanner(), State, data, data + len);
            if (addEnd) {
                NPire::Step(GetScanner(), State, NPire::EndMark);
            }
        }

        inline const typename TFsm::TScanner& GetScanner() const noexcept {
            return Fsm.GetScanner();
        }

        inline const TState& GetState() const noexcept {
            return State;
        }

    private:
        const TFsm& Fsm;
        TState State;
    };

    struct TMatcher : TMatcherBase<TFsm> {
        inline explicit TMatcher(const TFsm& fsm)
            : TMatcherBase<TFsm>(fsm)
        {
        }

        inline TMatcher& Match(const char* data, size_t len, bool addBegin = false, bool addEnd = false) noexcept {
            Run(data, len, addBegin, addEnd);
            return *this;
        }

        inline TMatcher& Match(const TStringBuf& s, bool addBegin = false, bool addEnd = false) noexcept {
            return Match(s.data(), s.size(), addBegin, addEnd);
        }

        inline const char* Find(const char* b, const char* e) noexcept {
            return NPire::ShortestPrefix(GetScanner(), b, e);
        }

        typedef std::pair<const size_t*, const size_t*> TMatchedRegexps;

        inline TMatchedRegexps MatchedRegexps() const noexcept {
            return GetScanner().AcceptedRegexps(GetState());
        }
    };

    class TSearcher: public TMatcherBase<TCapturingFsm> {
    public:
        inline explicit TSearcher(const TCapturingFsm& fsm)
            : TMatcherBase<TCapturingFsm>(fsm)
        {
        }

        inline bool Captured() const noexcept {
            return GetState().Captured();
        }

        inline TSearcher& Search(const char* data, size_t len, bool addBegin = true, bool addEnd = true) noexcept {
            Data = TStringBuf(data, len);
            Run(data, len, addBegin, addEnd);
            return *this;
        }

        inline TSearcher& Search(const TStringBuf& s) noexcept {
            return Search(s.data(), s.size());
        }

        inline TStringBuf GetCaptured() const noexcept {
            return TStringBuf(Data.data() + GetState().Begin() - 1,
                              Data.data() + GetState().End() - 1);
        }

    private:
        TStringBuf Data;
    };

    class TSlowSearcher : TMatcherBase<TSlowCapturingFsm>{
    public:
        typedef typename TSlowCapturingFsm::TScanner::State TState;
        inline explicit TSlowSearcher(const TSlowCapturingFsm& fsm)
                : TMatcherBase<TSlowCapturingFsm>(fsm)
                , HasCaptured(false)
        {
        }

        inline bool Captured() const noexcept {
            return HasCaptured;
        }

        inline TSlowSearcher& Search(const char* data, size_t len, bool addBegin = false, bool addEnd = false) noexcept {
            TStringBuf textData(data, len);
            Data = textData;
            Run(Data.begin(), Data.size(), addBegin, addEnd);
            return GetAns();
        }

        inline TSlowSearcher& Search(const TStringBuf& s) noexcept {
            return Search(s.data(), s.size());
        }

        inline TStringBuf GetCaptured() const noexcept {
            return Ans;
        }

    private:
        TStringBuf Data;
        TStringBuf Ans;
        bool HasCaptured;

        inline TSlowSearcher& GetAns() {
            auto state = GetState();
            Pire::SlowCapturingScanner::SingleState final;
            if (!GetScanner().GetCapture(state, final)) {
                HasCaptured = false;
            } else {
                if (!final.HasEnd()) {
                    final.SetEnd(Data.size());
                }
                Ans = TStringBuf(Data, final.GetBegin(), final.GetEnd() - final.GetBegin());
                HasCaptured = true;
            }
            return *this;
        }
    };
}