strling 3.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,348 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # STRling Intermediate Representation (IR) Node Definitions
4
- #
5
- # This module defines the complete set of IR node classes that represent
6
- # language-agnostic regex constructs. The IR serves as an intermediate layer
7
- # between the parsed AST and the target-specific emitters (e.g., PCRE2).
8
- #
9
- # IR nodes are designed to be:
10
- # - Simple and composable
11
- # - Easy to serialize (via to_dict methods)
12
- # - Independent of any specific regex flavor
13
- # - Optimized for transformation and analysis
14
- #
15
- # Each IR node corresponds to a fundamental regex operation (alternation,
16
- # sequencing, character classes, quantification, etc.) and can be serialized
17
- # to a hash representation for further processing or debugging.
18
-
19
- module Strling
20
- module Core
21
- # Base class for all IR operations.
22
- #
23
- # All IR nodes extend this base class and must implement the to_dict() method
24
- # for serialization to a hash representation.
25
- class IROp
26
- # Serialize the IR node to a hash representation.
27
- #
28
- # @return [Hash] The hash representation of this IR node.
29
- # @raise [NotImplementedError] If not implemented by subclass.
30
- def to_dict
31
- raise NotImplementedError, 'Subclasses must implement to_dict'
32
- end
33
- end
34
-
35
- # Represents an alternation (OR) operation in the IR.
36
- #
37
- # Matches any one of the provided branches. Equivalent to the | operator
38
- # in traditional regex syntax.
39
- class IRAlt < IROp
40
- # @return [Array<IROp>] The alternative branches
41
- attr_accessor :branches
42
-
43
- # @param branches [Array<IROp>] The alternative branches
44
- def initialize(branches)
45
- @branches = branches
46
- end
47
-
48
- def to_dict
49
- { 'ir' => 'Alt', 'branches' => branches.map(&:to_dict) }
50
- end
51
- end
52
-
53
- # Represents a sequence of IR operations.
54
- #
55
- # Matches each part in order.
56
- class IRSeq < IROp
57
- # @return [Array<IROp>] The sequence parts
58
- attr_accessor :parts
59
-
60
- # @param parts [Array<IROp>] The sequence parts
61
- def initialize(parts)
62
- @parts = parts
63
- end
64
-
65
- def to_dict
66
- { 'ir' => 'Seq', 'parts' => parts.map(&:to_dict) }
67
- end
68
- end
69
-
70
- # Represents a literal string match in the IR.
71
- #
72
- # Matches the exact string value provided.
73
- class IRLit < IROp
74
- # @return [String] The literal string value
75
- attr_accessor :value
76
-
77
- # @param value [String] The literal string value
78
- def initialize(value)
79
- @value = value
80
- end
81
-
82
- def to_dict
83
- { 'ir' => 'Lit', 'value' => value }
84
- end
85
- end
86
-
87
- # Represents the dot metacharacter in the IR.
88
- #
89
- # Matches any single character.
90
- class IRDot < IROp
91
- def to_dict
92
- { 'ir' => 'Dot' }
93
- end
94
- end
95
-
96
- # Represents an anchor position assertion in the IR.
97
- #
98
- # Anchors match positions rather than characters.
99
- class IRAnchor < IROp
100
- # @return [String] The anchor type
101
- attr_accessor :at
102
-
103
- # @param at [String] The anchor type
104
- def initialize(at)
105
- @at = at
106
- end
107
-
108
- def to_dict
109
- { 'ir' => 'Anchor', 'at' => at }
110
- end
111
- end
112
-
113
- # Base class for character class items in the IR.
114
- class IRClassItem
115
- # Serialize the item to hash representation.
116
- #
117
- # @return [Hash] Hash representation of the item
118
- # @raise [NotImplementedError] Must be implemented by subclasses
119
- def to_dict
120
- raise NotImplementedError, 'Subclasses must implement to_dict'
121
- end
122
- end
123
-
124
- # Represents a character range in a character class in the IR.
125
- #
126
- # Matches characters from from_ch to to_ch (inclusive).
127
- class IRClassRange < IRClassItem
128
- # @return [String] The starting character
129
- attr_accessor :from_ch
130
-
131
- # @return [String] The ending character
132
- attr_accessor :to_ch
133
-
134
- # @param from_ch [String] The starting character
135
- # @param to_ch [String] The ending character
136
- def initialize(from_ch, to_ch)
137
- @from_ch = from_ch
138
- @to_ch = to_ch
139
- end
140
-
141
- def to_dict
142
- { 'ir' => 'Range', 'from' => from_ch, 'to' => to_ch }
143
- end
144
- end
145
-
146
- # Represents a literal character in a character class in the IR.
147
- #
148
- # Matches the exact character.
149
- class IRClassLiteral < IRClassItem
150
- # @return [String] The character
151
- attr_accessor :ch
152
-
153
- # @param ch [String] The character
154
- def initialize(ch)
155
- @ch = ch
156
- end
157
-
158
- def to_dict
159
- { 'ir' => 'Char', 'char' => ch }
160
- end
161
- end
162
-
163
- # Represents an escape sequence in a character class in the IR.
164
- #
165
- # Matches predefined character classes or Unicode properties.
166
- class IRClassEscape < IRClassItem
167
- # @return [String] The escape type
168
- attr_accessor :type
169
-
170
- # @return [String, nil] The Unicode property name (for \p and \P)
171
- attr_accessor :property
172
-
173
- # @param type [String] The escape type
174
- # @param property [String, nil] The Unicode property name
175
- def initialize(type, property: nil)
176
- @type = type
177
- @property = property
178
- end
179
-
180
- def to_dict
181
- data = { 'ir' => 'Esc', 'type' => type }
182
- data['property'] = property if property
183
- data
184
- end
185
- end
186
-
187
- # Represents a character class in the IR.
188
- #
189
- # Matches any character from the set defined by items.
190
- class IRCharClass < IROp
191
- # @return [Boolean] Whether the class is negated
192
- attr_accessor :negated
193
-
194
- # @return [Array<IRClassItem>] The items in the character class
195
- attr_accessor :items
196
-
197
- # @param negated [Boolean] Whether the class is negated
198
- # @param items [Array<IRClassItem>] The items in the character class
199
- def initialize(negated, items)
200
- @negated = negated
201
- @items = items
202
- end
203
-
204
- def to_dict
205
- {
206
- 'ir' => 'CharClass',
207
- 'negated' => negated,
208
- 'items' => items.map(&:to_dict)
209
- }
210
- end
211
- end
212
-
213
- # Represents a quantifier (repetition operator) in the IR.
214
- #
215
- # Specifies how many times a pattern element should match.
216
- class IRQuant < IROp
217
- # @return [IROp] The child node being quantified
218
- attr_accessor :child
219
-
220
- # @return [Integer] The minimum number of matches
221
- attr_accessor :min
222
-
223
- # @return [Integer, String] The maximum number of matches ("Inf" for unbounded)
224
- attr_accessor :max
225
-
226
- # @return [String] The quantifier mode: "Greedy", "Lazy", or "Possessive"
227
- attr_accessor :mode
228
-
229
- # @param child [IROp] The child node being quantified
230
- # @param min [Integer] The minimum number of matches
231
- # @param max [Integer, String] The maximum number of matches
232
- # @param mode [String] The quantifier mode
233
- def initialize(child, min, max, mode)
234
- @child = child
235
- @min = min
236
- @max = max
237
- @mode = mode
238
- end
239
-
240
- def to_dict
241
- {
242
- 'ir' => 'Quant',
243
- 'child' => child.to_dict,
244
- 'min' => min,
245
- 'max' => max,
246
- 'mode' => mode
247
- }
248
- end
249
- end
250
-
251
- # Represents a capturing or non-capturing group in the IR.
252
- #
253
- # Groups pattern elements together.
254
- class IRGroup < IROp
255
- # @return [Boolean] Whether the group is capturing
256
- attr_accessor :capturing
257
-
258
- # @return [IROp] The body of the group
259
- attr_accessor :body
260
-
261
- # @return [String, nil] The name of the group (for named captures)
262
- attr_accessor :name
263
-
264
- # @return [Boolean, nil] Whether the group is atomic
265
- attr_accessor :atomic
266
-
267
- # @param capturing [Boolean] Whether the group is capturing
268
- # @param body [IROp] The body of the group
269
- # @param name [String, nil] The name of the group
270
- # @param atomic [Boolean, nil] Whether the group is atomic
271
- def initialize(capturing, body, name: nil, atomic: nil)
272
- @capturing = capturing
273
- @body = body
274
- @name = name
275
- @atomic = atomic
276
- end
277
-
278
- def to_dict
279
- data = {
280
- 'ir' => 'Group',
281
- 'capturing' => capturing,
282
- 'body' => body.to_dict
283
- }
284
- data['name'] = name unless name.nil?
285
- data['atomic'] = atomic unless atomic.nil?
286
- data
287
- end
288
- end
289
-
290
- # Represents a backreference in the IR.
291
- #
292
- # Matches the same text as previously captured by a group.
293
- class IRBackref < IROp
294
- # @return [Integer, nil] The group index to reference
295
- attr_accessor :by_index
296
-
297
- # @return [String, nil] The group name to reference
298
- attr_accessor :by_name
299
-
300
- # @param by_index [Integer, nil] The group index to reference
301
- # @param by_name [String, nil] The group name to reference
302
- def initialize(by_index: nil, by_name: nil)
303
- @by_index = by_index
304
- @by_name = by_name
305
- end
306
-
307
- def to_dict
308
- data = { 'ir' => 'Backref' }
309
- data['byIndex'] = by_index unless by_index.nil?
310
- data['byName'] = by_name unless by_name.nil?
311
- data
312
- end
313
- end
314
-
315
- # Represents a lookaround assertion in the IR.
316
- #
317
- # Asserts that a pattern does (or does not) match ahead of or behind
318
- # the current position.
319
- class IRLook < IROp
320
- # @return [String] The direction: "Ahead" or "Behind"
321
- attr_accessor :dir
322
-
323
- # @return [Boolean] Whether the assertion is negative
324
- attr_accessor :neg
325
-
326
- # @return [IROp] The body of the assertion
327
- attr_accessor :body
328
-
329
- # @param dir [String] The direction
330
- # @param neg [Boolean] Whether the assertion is negative
331
- # @param body [IROp] The body of the assertion
332
- def initialize(dir, neg, body)
333
- @dir = dir
334
- @neg = neg
335
- @body = body
336
- end
337
-
338
- def to_dict
339
- {
340
- 'ir' => 'Look',
341
- 'dir' => dir,
342
- 'neg' => neg,
343
- 'body' => body.to_dict
344
- }
345
- end
346
- end
347
- end
348
- end