flagnetic 0.1.0 → 0.1.2

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.
@@ -0,0 +1,356 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flagnetic
4
+ # Eval is the evaluator; see flagnetic/eval.rb.
5
+ module Eval
6
+ module Regex
7
+ # CharSet is a set of code points: inclusive [lo, hi] ranges and Unicode
8
+ # properties, or everything else when negated. It answers membership in
9
+ # constant time for ASCII and in logarithmic time otherwise.
10
+ class CharSet
11
+ attr_reader :ranges
12
+
13
+ def initialize(ranges, props, negated)
14
+ @ranges = ranges.freeze
15
+ @los = ranges.map(&:first).freeze
16
+ @his = ranges.map(&:last).freeze
17
+ @props = props.freeze
18
+ @negated = negated
19
+ @ascii = Array.new(128) { |cp| member?(cp) }.freeze
20
+ freeze
21
+ end
22
+
23
+ # single reports the one code point the set holds, or nil.
24
+ def single
25
+ @ranges[0][0] if !@negated && @props.empty? && @ranges.size == 1 && @ranges[0][0] == @ranges[0][1]
26
+ end
27
+
28
+ def include?(cp)
29
+ cp < 128 ? @ascii[cp] : member?(cp)
30
+ end
31
+
32
+ private
33
+
34
+ def member?(cp)
35
+ found = in_ranges?(cp) || @props.any? { |prop| prop.include?(cp) }
36
+ found != @negated
37
+ end
38
+
39
+ def in_ranges?(cp)
40
+ i = @his.bsearch_index { |hi| hi >= cp }
41
+ !i.nil? && @los[i] <= cp
42
+ end
43
+ end
44
+
45
+ # Property is a \p or \P class: the code points with the Unicode
46
+ # property name, or without it when negated, and, under (?i), those
47
+ # an orbit of simple case folding ties to one that has it (extra).
48
+ class Property
49
+ def initialize(name, negated, extra)
50
+ @pattern = Regexp.new("\\p{#{name}}")
51
+ @negated = negated
52
+ @extra = extra.to_h { |cp| [cp, true] }.freeze
53
+ freeze
54
+ end
55
+
56
+ def include?(cp)
57
+ has = @pattern.match?([cp].pack("U")) || @extra.key?(cp)
58
+ has != @negated
59
+ end
60
+ end
61
+
62
+ # Node is a node of the parse tree. need is the smallest repeat budget
63
+ # Go's repeatIsValid accepts for the subtree, size its approximate
64
+ # length in instructions.
65
+ class Node
66
+ CHAR = 0
67
+ ASSERT = 1
68
+ CAT = 2
69
+ ALT = 3
70
+ REPEAT = 4
71
+
72
+ attr_reader :kind, :set, :what, :subs, :lo, :hi, :need, :size
73
+
74
+ def initialize(kind, set: nil, what: 0, subs: [], lo: 0, hi: 0, counted: false)
75
+ @kind = kind
76
+ @set = set
77
+ @what = what
78
+ @subs = subs
79
+ @lo = lo
80
+ @hi = hi
81
+ @need = 0
82
+ @size = 1
83
+ measure(counted)
84
+ freeze
85
+ end
86
+
87
+ private
88
+
89
+ def measure(counted)
90
+ case @kind
91
+ when CAT
92
+ @need = @subs.map(&:need).max || 0
93
+ @size = @subs.sum(&:size)
94
+ when ALT
95
+ @need = @subs.map(&:need).max || 0
96
+ @size = @subs.sum(&:size) + @subs.size - 1
97
+ when REPEAT
98
+ measure_repeat(@subs[0], counted)
99
+ end
100
+ end
101
+
102
+ def measure_repeat(inner, counted)
103
+ if counted
104
+ bound = @hi >= 0 ? @hi : @lo
105
+ @need = bound.zero? ? 0 : bound * [inner.need, 1].max
106
+ @size = (inner.size * [@hi >= 0 ? @hi : @lo + 1, 1].max) + 3
107
+ else
108
+ @need = inner.need
109
+ @size = inner.size + 2
110
+ end
111
+ end
112
+ end
113
+
114
+ # Program is a compiled pattern: parallel arrays of an opcode, two
115
+ # operands and a set per instruction.
116
+ Program = Struct.new(:ops, :x, :y, :sets)
117
+
118
+ # Compiler turns a parse tree into the instructions of a Pike VM.
119
+ # LIT matches one code point (operand x), SET one of a set, SPLIT forks
120
+ # to x and y, JMP goes to x, ASSERT checks the assertion x and goes on
121
+ # to the next instruction, MATCH ends.
122
+ class Compiler
123
+ LIT = 0
124
+ SET = 1
125
+ SPLIT = 2
126
+ JMP = 3
127
+ ASSERT = 4
128
+ MATCH = 5
129
+
130
+ def self.compile(root)
131
+ new.compile(root)
132
+ end
133
+
134
+ def initialize
135
+ @program = Program.new([], [], [], [])
136
+ end
137
+
138
+ # compile walks the tree with a work list, not recursion: a tree can
139
+ # be a few thousand levels deep, more than the stack of a thread or a
140
+ # fiber holds. An entry is a Node to emit or a Proc to run, such as
141
+ # one patching a jump once its target is known.
142
+ def compile(root)
143
+ @work = [root]
144
+ until @work.empty?
145
+ item = @work.pop
146
+ item.is_a?(Node) ? emit_node(item) : item.call
147
+ end
148
+ emit(MATCH)
149
+ @program.each(&:freeze)
150
+ @program.freeze
151
+ end
152
+
153
+ private
154
+
155
+ def emit(op, x = 0, y = 0, set = nil)
156
+ @program.ops << op
157
+ @program.x << x
158
+ @program.y << y
159
+ @program.sets << set
160
+ @program.ops.size - 1
161
+ end
162
+
163
+ def here = @program.ops.size
164
+
165
+ # schedule queues steps to run in the order given, before anything
166
+ # queued earlier.
167
+ def schedule(steps)
168
+ @work.concat(steps.reverse)
169
+ end
170
+
171
+ def emit_node(node)
172
+ case node.kind
173
+ when Node::CHAR then emit_char(node.set)
174
+ when Node::ASSERT then emit(ASSERT, node.what)
175
+ when Node::CAT then schedule(node.subs)
176
+ when Node::ALT then emit_alternation(node.subs)
177
+ else emit_repeat(node)
178
+ end
179
+ end
180
+
181
+ def emit_char(set)
182
+ cp = set.single
183
+ cp ? emit(LIT, cp) : emit(SET, 0, 0, set)
184
+ end
185
+
186
+ # split(a, L1); a; jmp end; L1: split(b, L2); b; jmp end; L2: c
187
+ def emit_alternation(subs)
188
+ exits = []
189
+ steps = []
190
+ subs.each_with_index do |sub, k|
191
+ next steps << sub if k == subs.size - 1
192
+
193
+ fork = nil
194
+ steps << -> { fork = emit(SPLIT, here + 1) }
195
+ steps << sub
196
+ steps << lambda {
197
+ exits << emit(JMP)
198
+ @program.y[fork] = here
199
+ }
200
+ end
201
+ steps << -> { exits.each { |e| @program.x[e] = here } }
202
+ schedule(steps)
203
+ end
204
+
205
+ def emit_repeat(node)
206
+ sub = node.subs[0]
207
+ steps = Array.new(node.lo, sub)
208
+ if node.hi.negative?
209
+ loop_steps(sub, steps)
210
+ else
211
+ # Each optional copy may skip to the end: the language of x{lo,hi}.
212
+ forks = []
213
+ (node.hi - node.lo).times do
214
+ steps << -> { forks << emit(SPLIT, here + 1) }
215
+ steps << sub
216
+ end
217
+ steps << -> { forks.each { |f| @program.y[f] = here } }
218
+ end
219
+ schedule(steps)
220
+ end
221
+
222
+ # L: split(body, out); body; jmp L
223
+ def loop_steps(sub, steps)
224
+ fork = nil
225
+ steps << -> { fork = emit(SPLIT, here + 1) }
226
+ steps << sub
227
+ steps << lambda {
228
+ emit(JMP, fork)
229
+ @program.y[fork] = here
230
+ }
231
+ end
232
+ end
233
+
234
+ # Vm is a Pike VM: it matches in time linear in the input for a given
235
+ # program, whatever the pattern, so nothing a context carries can make a
236
+ # match take longer.
237
+ module Vm
238
+ LIT = Compiler::LIT
239
+ SET = Compiler::SET
240
+ SPLIT = Compiler::SPLIT
241
+ JMP = Compiler::JMP
242
+ ASSERT = Compiler::ASSERT
243
+ MATCH = Compiler::MATCH
244
+
245
+ BEGIN_TEXT = 0
246
+ END_TEXT = 1
247
+ BEGIN_LINE = 2
248
+ END_LINE = 3
249
+ WORD_BOUNDARY = 4
250
+ NOT_WORD_BOUNDARY = 5
251
+
252
+ NEWLINE = 0x0A
253
+ REPLACEMENT_CHARACTER = 0xFFFD
254
+
255
+ module_function
256
+
257
+ # search reports whether program matches anywhere in cps, the code
258
+ # points of the text. Every thread advances in lockstep over them;
259
+ # the first to reach MATCH decides, as only whether it matches is
260
+ # asked.
261
+ def search(program, cps)
262
+ state = State.new(program, cps)
263
+ pos = 0
264
+ loop do
265
+ state.add(state.current, 0, pos) # unanchored: a new thread at every position
266
+ return true if state.step(pos)
267
+ return false if pos >= cps.size
268
+
269
+ pos += 1
270
+ state.advance
271
+ end
272
+ end
273
+
274
+ # State holds the threads of one search.
275
+ class State
276
+ attr_reader :current
277
+
278
+ def initialize(program, cps)
279
+ @ops = program.ops
280
+ @x = program.x
281
+ @y = program.y
282
+ @sets = program.sets
283
+ @cps = cps
284
+ @mark = Array.new(@ops.size, -1)
285
+ @current = []
286
+ @following = []
287
+ @stack = []
288
+ @gen = 0
289
+ end
290
+
291
+ def advance
292
+ @gen += 1
293
+ @current, @following = @following, @current
294
+ @following.clear
295
+ end
296
+
297
+ # step runs every thread on the code point at pos and queues the
298
+ # survivors; true when one has matched.
299
+ def step(pos)
300
+ cp = pos < @cps.size ? @cps[pos] : -1
301
+ @current.each do |pc|
302
+ op = @ops[pc]
303
+ return true if op == MATCH
304
+ next if cp.negative?
305
+
306
+ add(@following, pc + 1, pos + 1, @gen + 1) if consumes?(op, pc, cp)
307
+ end
308
+ false
309
+ end
310
+
311
+ # add queues the threads reachable from pc without consuming input.
312
+ def add(threads, pc, pos, gen = @gen)
313
+ @stack.push(pc)
314
+ until @stack.empty?
315
+ pc = @stack.pop
316
+ next if @mark[pc] == gen
317
+
318
+ @mark[pc] = gen
319
+ case @ops[pc]
320
+ when JMP then @stack.push(@x[pc])
321
+ when SPLIT then @stack.push(@y[pc], @x[pc])
322
+ when ASSERT then @stack.push(pc + 1) if holds?(@x[pc], pos)
323
+ else threads << pc
324
+ end
325
+ end
326
+ end
327
+
328
+ private
329
+
330
+ def consumes?(op, pc, cp)
331
+ op == LIT ? cp == @x[pc] : op == SET && @sets[pc].include?(cp)
332
+ end
333
+
334
+ # \b and \B read ASCII word characters, as RE2 does.
335
+ def holds?(assertion, pos)
336
+ n = @cps.size
337
+ case assertion
338
+ when BEGIN_TEXT then pos.zero?
339
+ when END_TEXT then pos == n
340
+ when BEGIN_LINE then pos.zero? || @cps[pos - 1] == NEWLINE
341
+ when END_LINE then pos == n || @cps[pos] == NEWLINE
342
+ else (word?(pos - 1) != word?(pos)) == (assertion == WORD_BOUNDARY)
343
+ end
344
+ end
345
+
346
+ def word?(pos)
347
+ return false if pos.negative? || pos >= @cps.size
348
+
349
+ cp = @cps[pos]
350
+ cp == 0x5F || cp.between?(0x30, 0x39) || cp.between?(0x41, 0x5A) || cp.between?(0x61, 0x7A)
351
+ end
352
+ end
353
+ end
354
+ end
355
+ end
356
+ end
@@ -88,6 +88,15 @@ module Flagnetic
88
88
  @index.unsupported
89
89
  end
90
90
 
91
+ # invalid_conditions reports, by flag key, why conditions of each flag
92
+ # cannot be evaluated by this evaluator, apart from the unsupported
93
+ # constructs unsupported_rules reports: a pattern outside what it
94
+ # reads, an undeclared attribute, a value of the wrong type. Each
95
+ # reason comes once, in document order; a flag with none is absent.
96
+ def invalid_conditions
97
+ @index.invalid
98
+ end
99
+
91
100
  # evaluate computes one flag for a context. It never reads a clock,
92
101
  # never records an exposure and never fails: problems are results with
93
102
  # reason error.