strling 3.0.0
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.
- checksums.yaml +7 -0
- data/README.md +88 -0
- data/lib/strling/core/compiler.rb +96 -0
- data/lib/strling/core/errors.rb +174 -0
- data/lib/strling/core/hint_engine.rb +43 -0
- data/lib/strling/core/ir.rb +348 -0
- data/lib/strling/core/nodes.rb +445 -0
- data/lib/strling/core/parser.rb +579 -0
- data/lib/strling/core/validator.rb +101 -0
- data/lib/strling/emitters/pcre2.rb +246 -0
- data/lib/strling/ir.rb +201 -0
- data/lib/strling/nodes.rb +102 -0
- data/lib/strling/simply.rb +90 -0
- data/lib/strling.rb +25 -0
- metadata +55 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# STRling PCRE2 Emitter - IR to PCRE2 Regex Translation
|
|
4
|
+
#
|
|
5
|
+
# This module transforms Intermediate Representation (IR) nodes into
|
|
6
|
+
# PCRE2-compatible regex pattern strings. It handles:
|
|
7
|
+
# - Proper escaping of metacharacters
|
|
8
|
+
# - Flag modifiers
|
|
9
|
+
# - Character class syntax
|
|
10
|
+
# - Quantifier notation
|
|
11
|
+
# - Group and lookaround syntax
|
|
12
|
+
|
|
13
|
+
require_relative '../core/ir'
|
|
14
|
+
require_relative '../core/nodes'
|
|
15
|
+
require_relative '../ir'
|
|
16
|
+
|
|
17
|
+
module Strling
|
|
18
|
+
module Emitters
|
|
19
|
+
# PCRE2 emitter class
|
|
20
|
+
class PCRE2
|
|
21
|
+
# Emit IR as a PCRE2 regex pattern
|
|
22
|
+
#
|
|
23
|
+
# @param ir_root [IROp] The root IR node
|
|
24
|
+
# @param flags [Flags] The pattern flags
|
|
25
|
+
# @return [String] The PCRE2 regex pattern
|
|
26
|
+
def self.emit(ir_root, flags)
|
|
27
|
+
emitter = new(flags)
|
|
28
|
+
pattern = emitter.emit_node(ir_root)
|
|
29
|
+
|
|
30
|
+
# Add flag modifiers if any
|
|
31
|
+
flag_str = build_flags(flags)
|
|
32
|
+
flag_str.empty? ? pattern : "(?#{flag_str})#{pattern}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def initialize(flags)
|
|
36
|
+
@flags = flags
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Emit a single IR node
|
|
40
|
+
#
|
|
41
|
+
# @param node [IROp] The IR node to emit
|
|
42
|
+
# @return [String] The PCRE2 pattern fragment
|
|
43
|
+
def emit_node(node)
|
|
44
|
+
case node
|
|
45
|
+
when Strling::Core::IRAlt, Strling::IR::Alt
|
|
46
|
+
# (branch1|branch2|...)
|
|
47
|
+
branches = node.branches.map { |b| emit_node(b) }
|
|
48
|
+
branches.length == 1 ? branches[0] : "(#{branches.join('|')})"
|
|
49
|
+
when Strling::Core::IRSeq, Strling::IR::Seq
|
|
50
|
+
# Concatenate parts
|
|
51
|
+
node.parts.map { |p| emit_node(p) }.join
|
|
52
|
+
when Strling::Core::IRLit, Strling::IR::Lit
|
|
53
|
+
# Escape metacharacters
|
|
54
|
+
escape_literal(node.value)
|
|
55
|
+
when Strling::Core::IRDot, Strling::IR::Dot
|
|
56
|
+
'.'
|
|
57
|
+
when Strling::Core::IRAnchor, Strling::IR::Anchor
|
|
58
|
+
emit_anchor(node.at)
|
|
59
|
+
when Strling::Core::IRCharClass, Strling::IR::CharClass
|
|
60
|
+
emit_char_class(node)
|
|
61
|
+
when Strling::Core::IRQuant, Strling::IR::Quant
|
|
62
|
+
emit_quantifier(node)
|
|
63
|
+
when Strling::Core::IRGroup, Strling::IR::Group
|
|
64
|
+
emit_group(node)
|
|
65
|
+
when Strling::Core::IRBackref
|
|
66
|
+
emit_backref(node)
|
|
67
|
+
when Strling::IR::BackRef
|
|
68
|
+
emit_backref(node)
|
|
69
|
+
when Strling::Core::IRLook, Strling::IR::Look
|
|
70
|
+
emit_lookaround(node)
|
|
71
|
+
when Strling::IR::Esc
|
|
72
|
+
emit_escape(node)
|
|
73
|
+
else
|
|
74
|
+
raise "Unknown IR node type: #{node.class}"
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
# Build flag string from Flags object
|
|
81
|
+
def self.build_flags(flags)
|
|
82
|
+
str = ''
|
|
83
|
+
str += 'i' if flags.ignore_case
|
|
84
|
+
str += 'm' if flags.multiline
|
|
85
|
+
str += 's' if flags.dot_all
|
|
86
|
+
str += 'u' if flags.unicode
|
|
87
|
+
str += 'x' if flags.extended
|
|
88
|
+
str
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Escape a literal string for PCRE2
|
|
92
|
+
def escape_literal(str)
|
|
93
|
+
str.chars.map do |ch|
|
|
94
|
+
case ch
|
|
95
|
+
when '^', '$', '.', '*', '+', '?', '{', '}', '[', ']', '(', ')', '|', '\\'
|
|
96
|
+
"\\#{ch}"
|
|
97
|
+
else
|
|
98
|
+
ch
|
|
99
|
+
end
|
|
100
|
+
end.join
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Emit an anchor
|
|
104
|
+
def emit_anchor(at)
|
|
105
|
+
case at
|
|
106
|
+
when 'Start'
|
|
107
|
+
'^'
|
|
108
|
+
when 'End'
|
|
109
|
+
'$'
|
|
110
|
+
when 'WordBoundary'
|
|
111
|
+
'\\b'
|
|
112
|
+
when 'NotWordBoundary'
|
|
113
|
+
'\\B'
|
|
114
|
+
when 'AbsoluteStart'
|
|
115
|
+
'\\A'
|
|
116
|
+
when 'EndBeforeFinalNewline'
|
|
117
|
+
'\\Z'
|
|
118
|
+
when 'AbsoluteEnd'
|
|
119
|
+
'\\z'
|
|
120
|
+
else
|
|
121
|
+
raise "Unknown anchor type: #{at}"
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Emit a character class
|
|
126
|
+
def emit_char_class(node)
|
|
127
|
+
items_str = node.items.map { |item| emit_class_item(item) }.join
|
|
128
|
+
node.negated ? "[^#{items_str}]" : "[#{items_str}]"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Emit a character class item
|
|
132
|
+
def emit_class_item(item)
|
|
133
|
+
case item
|
|
134
|
+
when Strling::Core::IRClassRange
|
|
135
|
+
"#{item.from_ch}-#{item.to_ch}"
|
|
136
|
+
when Strling::IR::Range
|
|
137
|
+
"#{item.from}-#{item.to}"
|
|
138
|
+
when Strling::Core::IRClassLiteral
|
|
139
|
+
emit_class_literal(item.ch)
|
|
140
|
+
when Strling::IR::Char
|
|
141
|
+
emit_class_literal(item.char)
|
|
142
|
+
when Strling::Core::IRClassEscape
|
|
143
|
+
emit_escape(item)
|
|
144
|
+
when Strling::IR::Esc
|
|
145
|
+
emit_escape(item)
|
|
146
|
+
else
|
|
147
|
+
raise "Unknown class item type: #{item.class}"
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def emit_class_literal(ch)
|
|
152
|
+
case ch
|
|
153
|
+
when ']', '\\', '^', '-'
|
|
154
|
+
"\\#{ch}"
|
|
155
|
+
else
|
|
156
|
+
ch
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def emit_escape(item)
|
|
161
|
+
if %w[p P].include?(item.type)
|
|
162
|
+
"\\#{item.type}{#{item.property}}"
|
|
163
|
+
else
|
|
164
|
+
"\\#{item.type}"
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Emit a quantifier
|
|
169
|
+
def emit_quantifier(node)
|
|
170
|
+
child_str = emit_node(node.child)
|
|
171
|
+
|
|
172
|
+
# Determine quantifier syntax
|
|
173
|
+
quant_str = if node.min == 0 && node.max == 'Inf'
|
|
174
|
+
'*'
|
|
175
|
+
elsif node.min == 1 && node.max == 'Inf'
|
|
176
|
+
'+'
|
|
177
|
+
elsif node.min == 0 && node.max == 1
|
|
178
|
+
'?'
|
|
179
|
+
elsif node.max == 'Inf'
|
|
180
|
+
"{#{node.min},}"
|
|
181
|
+
elsif node.min == node.max
|
|
182
|
+
"{#{node.min}}"
|
|
183
|
+
else
|
|
184
|
+
"{#{node.min},#{node.max}}"
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Add lazy or possessive modifier
|
|
188
|
+
modifier = case node.mode
|
|
189
|
+
when 'Lazy'
|
|
190
|
+
'?'
|
|
191
|
+
when 'Possessive'
|
|
192
|
+
'+'
|
|
193
|
+
else
|
|
194
|
+
''
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
"#{child_str}#{quant_str}#{modifier}"
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Emit a group
|
|
201
|
+
def emit_group(node)
|
|
202
|
+
body_str = emit_node(node.body)
|
|
203
|
+
|
|
204
|
+
if node.atomic
|
|
205
|
+
"(?>#{body_str})"
|
|
206
|
+
elsif !node.capturing
|
|
207
|
+
"(?:#{body_str})"
|
|
208
|
+
elsif node.name
|
|
209
|
+
"(?<#{node.name}>#{body_str})"
|
|
210
|
+
else
|
|
211
|
+
"(#{body_str})"
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# Emit a backreference
|
|
216
|
+
def emit_backref(node)
|
|
217
|
+
if node.by_name
|
|
218
|
+
"\\k<#{node.by_name}>"
|
|
219
|
+
elsif node.by_index
|
|
220
|
+
"\\#{node.by_index}"
|
|
221
|
+
else
|
|
222
|
+
raise 'Backref must specify either by_index or by_name'
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# Emit a lookaround
|
|
227
|
+
def emit_lookaround(node)
|
|
228
|
+
body_str = emit_node(node.body)
|
|
229
|
+
|
|
230
|
+
if node.dir == 'Ahead'
|
|
231
|
+
node.neg ? "(?!#{body_str})" : "(?=#{body_str})"
|
|
232
|
+
else # Behind
|
|
233
|
+
node.neg ? "(?<!#{body_str})" : "(?<=#{body_str})"
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# Module-level emit function
|
|
239
|
+
def self.emit_pcre2(ir_root, flags)
|
|
240
|
+
PCRE2.emit(ir_root, flags)
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
# Alias for backward compatibility
|
|
244
|
+
Pcre2 = PCRE2
|
|
245
|
+
end
|
|
246
|
+
end
|
data/lib/strling/ir.rb
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'core/nodes'
|
|
4
|
+
|
|
5
|
+
module Strling
|
|
6
|
+
module IR
|
|
7
|
+
Lit = Data.define(:ir, :value)
|
|
8
|
+
Quant = Data.define(:ir, :child, :min, :max, :mode)
|
|
9
|
+
CharClass = Data.define(:ir, :negated, :items)
|
|
10
|
+
Char = Data.define(:ir, :char)
|
|
11
|
+
Group = Data.define(:ir, :capturing, :body, :name, :atomic)
|
|
12
|
+
Alt = Data.define(:ir, :branches)
|
|
13
|
+
Esc = Data.define(:ir, :type, :property)
|
|
14
|
+
Seq = Data.define(:ir, :parts)
|
|
15
|
+
Look = Data.define(:ir, :dir, :neg, :body)
|
|
16
|
+
Anchor = Data.define(:ir, :at)
|
|
17
|
+
BackRef = Data.define(:ir, :byIndex, :byName)
|
|
18
|
+
Range = Data.define(:ir, :from, :to)
|
|
19
|
+
Dot = Data.define(:ir)
|
|
20
|
+
# Add others as needed
|
|
21
|
+
|
|
22
|
+
class Compiler
|
|
23
|
+
def self.compile(ast_node)
|
|
24
|
+
return nil if ast_node.nil?
|
|
25
|
+
|
|
26
|
+
case ast_node
|
|
27
|
+
# Core Nodes
|
|
28
|
+
when Strling::Core::Lit
|
|
29
|
+
Lit.new(ir: 'Lit', value: ast_node.value)
|
|
30
|
+
when Strling::Core::Quant
|
|
31
|
+
Quant.new(
|
|
32
|
+
ir: 'Quant',
|
|
33
|
+
child: compile(ast_node.child),
|
|
34
|
+
min: ast_node.min,
|
|
35
|
+
max: ast_node.max,
|
|
36
|
+
mode: ast_node.mode
|
|
37
|
+
)
|
|
38
|
+
when Strling::Core::Group
|
|
39
|
+
Group.new(
|
|
40
|
+
ir: 'Group',
|
|
41
|
+
capturing: ast_node.capturing,
|
|
42
|
+
body: compile(ast_node.body),
|
|
43
|
+
name: ast_node.name,
|
|
44
|
+
atomic: ast_node.atomic
|
|
45
|
+
)
|
|
46
|
+
when Strling::Core::Alt
|
|
47
|
+
Alt.new(
|
|
48
|
+
ir: 'Alt',
|
|
49
|
+
branches: ast_node.branches.map { |b| compile(b) }
|
|
50
|
+
)
|
|
51
|
+
when Strling::Core::Seq
|
|
52
|
+
parts = ast_node.parts.map { |p| compile(p) }
|
|
53
|
+
merged_parts = []
|
|
54
|
+
parts.each do |part|
|
|
55
|
+
if part.is_a?(Lit) && merged_parts.last.is_a?(Lit)
|
|
56
|
+
last = merged_parts.pop
|
|
57
|
+
merged_parts << Lit.new(ir: 'Lit', value: last.value + part.value)
|
|
58
|
+
else
|
|
59
|
+
merged_parts << part
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
if merged_parts.size == 1
|
|
63
|
+
merged_parts.first
|
|
64
|
+
else
|
|
65
|
+
Seq.new(ir: 'Seq', parts: merged_parts)
|
|
66
|
+
end
|
|
67
|
+
when Strling::Core::Esc
|
|
68
|
+
Esc.new(ir: 'Esc', type: ast_node.type, property: ast_node.property)
|
|
69
|
+
when Strling::Core::Anchor
|
|
70
|
+
at = ast_node.at == 'NonWordBoundary' ? 'NotWordBoundary' : ast_node.at
|
|
71
|
+
Anchor.new(ir: 'Anchor', at: at)
|
|
72
|
+
when Strling::Core::Look
|
|
73
|
+
Look.new(
|
|
74
|
+
ir: 'Look',
|
|
75
|
+
dir: ast_node.dir,
|
|
76
|
+
neg: ast_node.neg,
|
|
77
|
+
body: compile(ast_node.body)
|
|
78
|
+
)
|
|
79
|
+
# Legacy Nodes
|
|
80
|
+
when Strling::Nodes::Literal
|
|
81
|
+
Lit.new(ir: 'Lit', value: ast_node.value)
|
|
82
|
+
when Strling::Nodes::Quantifier
|
|
83
|
+
mode = if ast_node.possessive
|
|
84
|
+
'Possessive'
|
|
85
|
+
elsif ast_node.lazy
|
|
86
|
+
'Lazy'
|
|
87
|
+
else
|
|
88
|
+
'Greedy'
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Handle max being null/nil -> "Inf"
|
|
92
|
+
max_val = ast_node.max.nil? ? 'Inf' : ast_node.max
|
|
93
|
+
|
|
94
|
+
Quant.new(
|
|
95
|
+
ir: 'Quant',
|
|
96
|
+
child: compile(ast_node.target),
|
|
97
|
+
min: ast_node.min,
|
|
98
|
+
max: max_val,
|
|
99
|
+
mode: mode
|
|
100
|
+
)
|
|
101
|
+
when Strling::Nodes::CharacterClass
|
|
102
|
+
items = ast_node.members.map do |member|
|
|
103
|
+
if member.is_a?(Strling::Nodes::Literal)
|
|
104
|
+
Char.new(ir: 'Char', char: member.value)
|
|
105
|
+
elsif member.is_a?(Strling::Nodes::UnicodeProperty)
|
|
106
|
+
type = member.negated ? 'P' : 'p'
|
|
107
|
+
Esc.new(ir: 'Esc', type: type, property: member.value)
|
|
108
|
+
elsif member.is_a?(Strling::Nodes::Escape)
|
|
109
|
+
compile(member)
|
|
110
|
+
else
|
|
111
|
+
# Fallback or recursive compile if members can be other things
|
|
112
|
+
compile(member)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
CharClass.new(ir: 'CharClass', negated: ast_node.negated, items: items)
|
|
116
|
+
when Strling::Nodes::Group
|
|
117
|
+
Group.new(
|
|
118
|
+
ir: 'Group',
|
|
119
|
+
capturing: ast_node.capturing,
|
|
120
|
+
body: compile(ast_node.body),
|
|
121
|
+
name: ast_node.name,
|
|
122
|
+
atomic: ast_node.atomic ? true : nil
|
|
123
|
+
)
|
|
124
|
+
when Strling::Nodes::Alternation
|
|
125
|
+
Alt.new(
|
|
126
|
+
ir: 'Alt',
|
|
127
|
+
branches: ast_node.alternatives.map { |a| compile(a) }
|
|
128
|
+
)
|
|
129
|
+
when Strling::Nodes::UnicodeProperty
|
|
130
|
+
# If it appears standalone (not in CharClass), it might still be Esc?
|
|
131
|
+
# In unicode_property_short.json it was inside CharacterClass.
|
|
132
|
+
# Let's assume it maps to Esc.
|
|
133
|
+
type = ast_node.negated ? 'P' : 'p'
|
|
134
|
+
Esc.new(ir: 'Esc', type: type, property: ast_node.value)
|
|
135
|
+
when Strling::Nodes::Sequence
|
|
136
|
+
parts = ast_node.parts.map { |p| compile(p) }
|
|
137
|
+
|
|
138
|
+
# Merge adjacent Lit nodes
|
|
139
|
+
merged_parts = []
|
|
140
|
+
parts.each do |part|
|
|
141
|
+
if part.is_a?(Lit) && merged_parts.last.is_a?(Lit)
|
|
142
|
+
last = merged_parts.pop
|
|
143
|
+
merged_parts << Lit.new(ir: 'Lit', value: last.value + part.value)
|
|
144
|
+
else
|
|
145
|
+
merged_parts << part
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
if merged_parts.size == 1
|
|
150
|
+
merged_parts.first
|
|
151
|
+
else
|
|
152
|
+
Seq.new(ir: 'Seq', parts: merged_parts)
|
|
153
|
+
end
|
|
154
|
+
when Strling::Nodes::Lookahead
|
|
155
|
+
Look.new(
|
|
156
|
+
ir: 'Look',
|
|
157
|
+
dir: 'Ahead',
|
|
158
|
+
neg: ast_node.negative || false, # AST might not have negative if it's a separate node type "NegativeLookahead"
|
|
159
|
+
body: compile(ast_node.body)
|
|
160
|
+
)
|
|
161
|
+
when Strling::Nodes::Lookbehind
|
|
162
|
+
Look.new(
|
|
163
|
+
ir: 'Look',
|
|
164
|
+
dir: 'Behind',
|
|
165
|
+
neg: ast_node.negative || false,
|
|
166
|
+
body: compile(ast_node.body)
|
|
167
|
+
)
|
|
168
|
+
when Strling::Nodes::Anchor
|
|
169
|
+
at = ast_node.at == 'NonWordBoundary' ? 'NotWordBoundary' : ast_node.at
|
|
170
|
+
Anchor.new(ir: 'Anchor', at: at)
|
|
171
|
+
when Strling::Nodes::Escape
|
|
172
|
+
type = case ast_node.kind
|
|
173
|
+
when 'digit' then 'd'
|
|
174
|
+
when 'not-digit' then 'D'
|
|
175
|
+
when 'space' then 's'
|
|
176
|
+
when 'not-space' then 'S'
|
|
177
|
+
when 'word' then 'w'
|
|
178
|
+
when 'not-word' then 'W'
|
|
179
|
+
# Add others if needed
|
|
180
|
+
else ast_node.kind # Fallback
|
|
181
|
+
end
|
|
182
|
+
Esc.new(ir: 'Esc', type: type, property: nil)
|
|
183
|
+
when Strling::Nodes::BackReference
|
|
184
|
+
BackRef.new(
|
|
185
|
+
ir: 'Backref',
|
|
186
|
+
byIndex: ast_node.index,
|
|
187
|
+
byName: ast_node.name
|
|
188
|
+
)
|
|
189
|
+
when Strling::Nodes::Range
|
|
190
|
+
Range.new(ir: 'Range', from: ast_node.from, to: ast_node.to)
|
|
191
|
+
when Strling::Nodes::Dot
|
|
192
|
+
Dot.new(ir: 'Dot')
|
|
193
|
+
else
|
|
194
|
+
# Fallback
|
|
195
|
+
# puts "Warning: Unknown AST node for compilation: #{ast_node.class}"
|
|
196
|
+
nil
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Strling
|
|
4
|
+
module Nodes
|
|
5
|
+
Literal = Data.define(:value)
|
|
6
|
+
Quantifier = Data.define(:target, :min, :max, :greedy, :lazy, :possessive)
|
|
7
|
+
CharacterClass = Data.define(:negated, :members)
|
|
8
|
+
Group = Data.define(:capturing, :body, :name, :atomic)
|
|
9
|
+
Alternation = Data.define(:alternatives)
|
|
10
|
+
UnicodeProperty = Data.define(:name, :value, :negated)
|
|
11
|
+
Sequence = Data.define(:parts)
|
|
12
|
+
Lookahead = Data.define(:body, :negative)
|
|
13
|
+
Lookbehind = Data.define(:body, :negative)
|
|
14
|
+
Anchor = Data.define(:at)
|
|
15
|
+
Escape = Data.define(:kind)
|
|
16
|
+
BackReference = Data.define(:name, :index)
|
|
17
|
+
Dot = Data.define(:value) # Placeholder
|
|
18
|
+
Range = Data.define(:from, :to)
|
|
19
|
+
|
|
20
|
+
class NodeFactory
|
|
21
|
+
def self.from_json(hash)
|
|
22
|
+
return nil if hash.nil?
|
|
23
|
+
|
|
24
|
+
case hash['type']
|
|
25
|
+
when 'Literal'
|
|
26
|
+
Literal.new(value: hash['value'])
|
|
27
|
+
when 'Quantifier'
|
|
28
|
+
Quantifier.new(
|
|
29
|
+
target: from_json(hash['target']),
|
|
30
|
+
min: hash['min'],
|
|
31
|
+
max: hash['max'],
|
|
32
|
+
greedy: hash['greedy'],
|
|
33
|
+
lazy: hash['lazy'],
|
|
34
|
+
possessive: hash['possessive']
|
|
35
|
+
)
|
|
36
|
+
when 'CharacterClass'
|
|
37
|
+
CharacterClass.new(
|
|
38
|
+
negated: hash['negated'],
|
|
39
|
+
members: hash['members'].map { |m| from_json(m) }
|
|
40
|
+
)
|
|
41
|
+
when 'Group'
|
|
42
|
+
# Note: 'expression' field in JSON seems redundant or specific, using 'body'
|
|
43
|
+
Group.new(
|
|
44
|
+
capturing: hash['capturing'],
|
|
45
|
+
body: from_json(hash['body'] || hash['expression']),
|
|
46
|
+
name: hash['name'],
|
|
47
|
+
atomic: hash['atomic']
|
|
48
|
+
)
|
|
49
|
+
when 'Alternation'
|
|
50
|
+
Alternation.new(
|
|
51
|
+
alternatives: hash['alternatives'].map { |a| from_json(a) }
|
|
52
|
+
)
|
|
53
|
+
when 'UnicodeProperty'
|
|
54
|
+
UnicodeProperty.new(
|
|
55
|
+
name: hash['name'],
|
|
56
|
+
value: hash['value'],
|
|
57
|
+
negated: hash['negated']
|
|
58
|
+
)
|
|
59
|
+
when 'Sequence'
|
|
60
|
+
Sequence.new(
|
|
61
|
+
parts: hash['parts'].map { |p| from_json(p) }
|
|
62
|
+
)
|
|
63
|
+
when 'Lookahead'
|
|
64
|
+
Lookahead.new(
|
|
65
|
+
body: from_json(hash['body']),
|
|
66
|
+
negative: hash['negative'] || false
|
|
67
|
+
)
|
|
68
|
+
when 'NegativeLookahead'
|
|
69
|
+
Lookahead.new(
|
|
70
|
+
body: from_json(hash['body']),
|
|
71
|
+
negative: true
|
|
72
|
+
)
|
|
73
|
+
when 'Lookbehind'
|
|
74
|
+
Lookbehind.new(
|
|
75
|
+
body: from_json(hash['body']),
|
|
76
|
+
negative: hash['negative'] || false
|
|
77
|
+
)
|
|
78
|
+
when 'NegativeLookbehind'
|
|
79
|
+
Lookbehind.new(
|
|
80
|
+
body: from_json(hash['body']),
|
|
81
|
+
negative: true
|
|
82
|
+
)
|
|
83
|
+
when 'Anchor'
|
|
84
|
+
Anchor.new(at: hash['at'])
|
|
85
|
+
when 'Escape'
|
|
86
|
+
Escape.new(kind: hash['kind'])
|
|
87
|
+
when 'BackReference', 'Backreference'
|
|
88
|
+
BackReference.new(name: hash['name'], index: hash['index'])
|
|
89
|
+
when 'Dot'
|
|
90
|
+
Dot.new(value: nil)
|
|
91
|
+
when 'Range'
|
|
92
|
+
Range.new(from: hash['from'], to: hash['to'])
|
|
93
|
+
else
|
|
94
|
+
# Generic fallback for unknown nodes to avoid crashing immediately,
|
|
95
|
+
# allowing us to see what's missing
|
|
96
|
+
# puts "Warning: Unknown node type #{hash['type']}"
|
|
97
|
+
Data.define(:type, :raw).new(type: hash['type'], raw: hash)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'core/nodes'
|
|
4
|
+
require_relative 'ir'
|
|
5
|
+
require_relative 'emitters/pcre2'
|
|
6
|
+
|
|
7
|
+
module Strling
|
|
8
|
+
# Fluent API for building STRling patterns
|
|
9
|
+
class Simply
|
|
10
|
+
attr_reader :node
|
|
11
|
+
|
|
12
|
+
def initialize(node)
|
|
13
|
+
@node = node
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Class methods (Constructors)
|
|
17
|
+
|
|
18
|
+
def self.merge(*patterns)
|
|
19
|
+
parts = patterns.map { |p| to_node(p) }
|
|
20
|
+
new(Strling::Core::Seq.new(parts))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.capture(pattern)
|
|
24
|
+
new(Strling::Core::Group.new(true, to_node(pattern)))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.may(pattern)
|
|
28
|
+
new(Strling::Core::Quant.new(to_node(pattern), 0, 1, 'Greedy'))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.digit
|
|
32
|
+
new(Strling::Core::Esc.new('d'))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.start
|
|
36
|
+
new(Strling::Core::Anchor.new('Start'))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.end
|
|
40
|
+
new(Strling::Core::Anchor.new('End'))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.any_of(*patterns)
|
|
44
|
+
branches = patterns.map { |p| to_node(p) }
|
|
45
|
+
new(Strling::Core::Alt.new(branches))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Helper to convert input to Node
|
|
49
|
+
def self.to_node(pattern)
|
|
50
|
+
if pattern.is_a?(Simply)
|
|
51
|
+
pattern.node
|
|
52
|
+
elsif pattern.is_a?(String)
|
|
53
|
+
Strling::Core::Lit.new(pattern)
|
|
54
|
+
else
|
|
55
|
+
raise ArgumentError, "Expected Simply instance or String, got #{pattern.class}"
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Instance methods (Chainable)
|
|
60
|
+
|
|
61
|
+
def merge(*others)
|
|
62
|
+
parts = [@node] + others.map { |p| Simply.to_node(p) }
|
|
63
|
+
Simply.new(Strling::Core::Seq.new(parts))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def capture
|
|
67
|
+
Simply.new(Strling::Core::Group.new(true, @node))
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def may
|
|
71
|
+
Simply.new(Strling::Core::Quant.new(@node, 0, 1, 'Greedy'))
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def any_of(*others)
|
|
75
|
+
branches = [@node] + others.map { |p| Simply.to_node(p) }
|
|
76
|
+
Simply.new(Strling::Core::Alt.new(branches))
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def times(min, max = nil)
|
|
80
|
+
max = min if max.nil?
|
|
81
|
+
Simply.new(Strling::Core::Quant.new(@node, min, max, 'Greedy'))
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def to_s
|
|
85
|
+
ir = Strling::IR::Compiler.compile(@node)
|
|
86
|
+
flags = Strling::Core::Flags.new
|
|
87
|
+
Strling::Emitters::PCRE2.emit(ir, flags)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
data/lib/strling.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# STRling Binding - Main Entry Point
|
|
4
|
+
#
|
|
5
|
+
# This is the root module for the STRling binding. It provides the foundation
|
|
6
|
+
# for the STRling DSL and compiler, offering a readable, maintainable
|
|
7
|
+
# alternative to traditional regular expressions.
|
|
8
|
+
#
|
|
9
|
+
# The STRling module serves as the namespace for all STRling functionality,
|
|
10
|
+
# including core data structures (nodes, IR, errors) and the parser/compiler
|
|
11
|
+
# pipeline.
|
|
12
|
+
|
|
13
|
+
require_relative 'strling/nodes'
|
|
14
|
+
require_relative 'strling/ir'
|
|
15
|
+
require_relative 'strling/simply'
|
|
16
|
+
|
|
17
|
+
module Strling
|
|
18
|
+
# Version constant
|
|
19
|
+
VERSION = '3.0.0'
|
|
20
|
+
|
|
21
|
+
# Convenience method for parsing
|
|
22
|
+
def self.parse(text)
|
|
23
|
+
Core.parse(text)
|
|
24
|
+
end
|
|
25
|
+
end
|