nukadoko 0.2.0 → 0.3.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.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +126 -0
  3. data/README.ja.md +5 -3
  4. data/README.md +5 -3
  5. data/lib/nukadoko/aria_snapshot.rb +4 -4
  6. data/lib/nukadoko/base.rb +49 -29
  7. data/lib/nukadoko/check.rb +4 -4
  8. data/lib/nukadoko/claim_check.rb +2 -2
  9. data/lib/nukadoko/claim_rules.rb +1 -1
  10. data/lib/nukadoko/claim_sides.rb +469 -0
  11. data/lib/nukadoko/claims.rb +182 -20
  12. data/lib/nukadoko/cli.rb +16 -7
  13. data/lib/nukadoko/codes.json +20 -15
  14. data/lib/nukadoko/criteria.rb +1 -1
  15. data/lib/nukadoko/env_check.rb +1 -3
  16. data/lib/nukadoko/fixtures.rb +53 -21
  17. data/lib/nukadoko/init.rb +0 -1
  18. data/lib/nukadoko/js_json.rb +274 -0
  19. data/lib/nukadoko/js_pattern.rb +531 -0
  20. data/lib/nukadoko/parts.rb +1 -1
  21. data/lib/nukadoko/pattern.rb +62 -2
  22. data/lib/nukadoko/poll.rb +5 -3
  23. data/lib/nukadoko/program.rb +5 -5
  24. data/lib/nukadoko/project_files.rb +17 -0
  25. data/lib/nukadoko/project_program.rb +119 -0
  26. data/lib/nukadoko/run.rb +47 -17
  27. data/lib/nukadoko/run_record.rb +25 -22
  28. data/lib/nukadoko/schema.rb +60 -18
  29. data/lib/nukadoko/secrets.rb +20 -4
  30. data/lib/nukadoko/spaces.rb +28 -0
  31. data/lib/nukadoko/step.rb +7 -1
  32. data/lib/nukadoko/text_fit.rb +1 -1
  33. data/lib/nukadoko/tree_passes.rb +6 -3
  34. data/lib/nukadoko/version.rb +1 -1
  35. data/lib/nukadoko.rb +24 -2
  36. data/llms.txt +12 -11
  37. data/sig/generated/nukadoko/aria_snapshot.rbs +2 -2
  38. data/sig/generated/nukadoko/base.rbs +24 -18
  39. data/sig/generated/nukadoko/check.rbs +1 -1
  40. data/sig/generated/nukadoko/claim_sides.rbs +207 -0
  41. data/sig/generated/nukadoko/claims.rbs +46 -3
  42. data/sig/generated/nukadoko/cli.rbs +1 -1
  43. data/sig/generated/nukadoko/env_check.rbs +0 -4
  44. data/sig/generated/nukadoko/fixtures.rbs +25 -2
  45. data/sig/generated/nukadoko/init.rbs +0 -2
  46. data/sig/generated/nukadoko/js_json.rbs +109 -0
  47. data/sig/generated/nukadoko/js_pattern.rbs +194 -0
  48. data/sig/generated/nukadoko/pattern.rbs +34 -0
  49. data/sig/generated/nukadoko/poll.rbs +3 -2
  50. data/sig/generated/nukadoko/program.rbs +1 -1
  51. data/sig/generated/nukadoko/project_files.rbs +19 -0
  52. data/sig/generated/nukadoko/project_program.rbs +55 -0
  53. data/sig/generated/nukadoko/run.rbs +10 -3
  54. data/sig/generated/nukadoko/run_record.rbs +5 -4
  55. data/sig/generated/nukadoko/schema.rbs +22 -4
  56. data/sig/generated/nukadoko/secrets.rbs +17 -2
  57. data/sig/generated/nukadoko/spaces.rbs +31 -0
  58. data/sig/generated/nukadoko/tree_passes.rbs +2 -2
  59. data/sig/generated/nukadoko.rbs +11 -0
  60. data/skills/nukadoko/SKILL.md +2 -2
  61. data/skills/nukadoko/references/diagnosing.md +1 -1
  62. data/skills/nukadoko/references/examples/http-api.md +19 -16
  63. data/skills/nukadoko/references/fixtures.md +1 -1
  64. data/skills/nukadoko/references/perturbed-worlds.md +8 -6
  65. data/skills/nukadoko/references/program.md +20 -20
  66. data/skills/nukadoko/references/running.md +2 -2
  67. data/skills/nukadoko/references/scenario-rules.md +2 -2
  68. data/skills/nukadoko/references/verdicts.md +3 -3
  69. data/skills/nukadoko/references/writing-steps.md +1 -1
  70. data/skills/nukadoko-verification/SKILL.md +6 -6
  71. metadata +13 -1
@@ -0,0 +1,531 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # Responsibility: read a `matches` claim's pattern as JavaScript's RegExp
5
+ # reads it, so the Ruby engine judges the claim the way the TypeScript
6
+ # engine does: `new RegExp(pattern)`, no flags, with the web's legacy
7
+ # grammar (a `{` that starts no quantifier is a `{`, `\A` is an `A`). Ruby's
8
+ # own grammar reads the same text otherwise: `^` and `$` there match at
9
+ # every line, `\s` takes no Unicode space, `\A` and `\h` mean something,
10
+ # `[^]` does not build, and an unnamed group stops capturing once a named
11
+ # one is present. So the pattern is parsed here and written again as a
12
+ # Ruby pattern that means what the JavaScript one does.
13
+ #
14
+ # JavaScript matches a text unit by UTF-16 unit, so `.` takes half of an
15
+ # emoji. Each half of a character past U+FFFF, in the pattern and in the
16
+ # text, is therefore written as one character of its own in plane 16's
17
+ # private use area (`units`).
18
+ #
19
+ # Honest limits: case-insensitive matching inside `(?i:...)` folds case as
20
+ # Ruby does, which differs for a few letters (the Kelvin sign, the long
21
+ # s); a lookbehind Ruby cannot run (one of unbounded length, or holding a
22
+ # backreference) is refused with Ruby's reason; a group inside a repeated
23
+ # group keeps its capture from an earlier round, where JavaScript clears it.
24
+ module Nukadoko
25
+ module Engine
26
+ module JsPattern
27
+ # A pattern JavaScript refuses, with V8's message.
28
+ class Invalid < StandardError; end
29
+
30
+ LINE_ENDS = "\\n\\r\\u{2028}\\u{2029}"
31
+ SPACE = "\\t\\n\\v\\f\\r \\u{a0}\\u{1680}\\u{2000}-\\u{200a}\\u{2028}\\u{2029}\\u{202f}\\u{205f}\\u{3000}\\u{feff}"
32
+ WORD = "A-Za-z0-9_"
33
+ SETS = { "d" => ["0-9", false], "D" => ["0-9", true], "w" => [WORD, false], "W" => [WORD, true], "s" => [SPACE, false], "S" => [SPACE, true] }.freeze
34
+ CONTROL = { "f" => 0x0c, "n" => 0x0a, "r" => 0x0d, "t" => 0x09, "v" => 0x0b }.freeze
35
+ # Where a UTF-16 surrogate unit is written as a character of its own.
36
+ UNIT_BASE = 0x10F000
37
+ SURROGATES = (0xD800..0xDFFF)
38
+ # Ruby refuses a repeat count past this; JavaScript takes any.
39
+ REPEAT_LIMIT = 100_000
40
+ GROUP_NAME = /\A[\p{L}\p{Nl}$_][\p{L}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}$‌‍]*\z/
41
+
42
+ # `text` with each character past U+FFFF written as its two UTF-16
43
+ # units, each in plane 16's private use area.
44
+ #: (String) -> String
45
+ def self.units(text)
46
+ return text unless text.match?(/[^\u0000-￿]/)
47
+
48
+ text.each_char.map do |char|
49
+ code = char.ord
50
+ next char if code <= 0xFFFF
51
+
52
+ code -= 0x10000
53
+ [unit(0xD800 + (code >> 10)), unit(0xDC00 + (code & 0x3FF))].join
54
+ end.join
55
+ end
56
+
57
+ # The character a UTF-16 unit is written as.
58
+ #: (Integer) -> String
59
+ def self.unit(code) = (SURROGATES.cover?(code) ? UNIT_BASE + code - 0xD800 : code).chr(Encoding::UTF_8)
60
+
61
+ # The Ruby pattern that matches what `new RegExp(source)` matches, in
62
+ # a text written through `units`.
63
+ #: (String, ?timeout: Float?) -> Regexp
64
+ def self.compile(source, timeout: nil)
65
+ written = Parser.new(source).call
66
+ verbose = $VERBOSE
67
+ $VERBOSE = nil
68
+ begin
69
+ Regexp.new(written, timeout: timeout)
70
+ rescue RegexpError => e
71
+ raise Invalid, "Invalid regular expression: /#{source}/: the Ruby engine cannot run this pattern (#{e.message.sub(/: \/.*\z/m, "")})"
72
+ ensure
73
+ $VERBOSE = verbose
74
+ end
75
+ end
76
+
77
+ # Reads a pattern in JavaScript's legacy grammar and writes it in
78
+ # Ruby's. Every group is written named, `g<n>` by JavaScript's group
79
+ # number, since Ruby stops counting unnamed groups once a named one is
80
+ # present, and a backreference to a group that has not matched
81
+ # matches nothing, as in JavaScript.
82
+ class Parser
83
+ #: (String) -> void
84
+ def initialize(source)
85
+ @source = source
86
+ @chars = JsPattern.units(source).chars
87
+ @at = 0
88
+ @group = 0
89
+ @names = {} #: Hash[String, Array[Integer]]
90
+ # The names a group may not take again here: those of groups that
91
+ # can match together with the one being read. Two groups may share
92
+ # a name only in different alternatives.
93
+ @visible = [] #: Array[String]
94
+ @count = count_groups
95
+ end
96
+
97
+ #: () -> String
98
+ def call
99
+ written = disjunction({ i: false, m: false, s: false })
100
+ fail!("Unmatched ')'") if peek == ")"
101
+ written
102
+ end
103
+
104
+ private
105
+
106
+ #: (String) -> bot
107
+ def fail!(reason) = raise(Invalid, "Invalid regular expression: /#{@source}/: #{reason}")
108
+
109
+ #: (?Integer) -> String?
110
+ def peek(ahead = 0) = @chars[@at + ahead]
111
+
112
+ #: () -> String?
113
+ def take
114
+ char = @chars[@at]
115
+ @at += 1
116
+ char
117
+ end
118
+
119
+ #: (String) -> bool
120
+ def taken?(text)
121
+ return false unless @chars[@at, text.length]&.join == text
122
+
123
+ @at += text.length
124
+ true
125
+ end
126
+
127
+ # The index of the `>` that ends a group name started at `from`.
128
+ #: (Integer) -> Integer?
129
+ def find_close(from) = (from...@chars.length).find { @chars[_1] == ">" }
130
+
131
+ # Every capturing group's number and every group name, read before
132
+ # the parse, since a backreference may name a later group.
133
+ #: () -> Integer
134
+ def count_groups
135
+ count = 0
136
+ index = 0
137
+ in_class = false
138
+ while index < @chars.length
139
+ char = @chars[index]
140
+ if char == "\\"
141
+ index += 2
142
+ next
143
+ end
144
+ if in_class
145
+ in_class = false if char == "]"
146
+ elsif char == "["
147
+ in_class = true
148
+ elsif char == "(" && @chars[index + 1] != "?"
149
+ count += 1
150
+ elsif char == "(" && @chars[index + 1] == "?" && @chars[index + 2] == "<" && !%w[= !].include?(@chars[index + 3])
151
+ count += 1
152
+ finish = find_close(index + 3)
153
+ name = finish ? @chars[(index + 3)...finish].to_a.join : ""
154
+ (@names[name] ||= []) << count
155
+ end
156
+ index += 1
157
+ end
158
+ count
159
+ end
160
+
161
+ #: (Hash[Symbol, bool]) -> String
162
+ def disjunction(flags)
163
+ before = @visible
164
+ named = [] #: Array[String]
165
+ alternatives = [] #: Array[String]
166
+ loop do
167
+ @visible = before.dup
168
+ alternatives << alternative(flags)
169
+ named |= @visible
170
+ break unless taken?("|")
171
+ end
172
+ @visible = named
173
+ alternatives.join("|")
174
+ end
175
+
176
+ #: (Hash[Symbol, bool]) -> String
177
+ def alternative(flags)
178
+ terms = +""
179
+ terms << term(flags) until peek.nil? || peek == "|" || peek == ")"
180
+ terms
181
+ end
182
+
183
+ #: (Hash[Symbol, bool]) -> String
184
+ def term(flags)
185
+ assertion = assertion(flags)
186
+ if assertion
187
+ fail!("Nothing to repeat") if quantifier_ahead?
188
+ return assertion
189
+ end
190
+
191
+ atom, quantifiable = atom(flags)
192
+ quantified = quantifier
193
+ return atom unless quantified
194
+
195
+ fail!(quantifiable == :lookbehind ? "Invalid quantifier" : "Nothing to repeat") unless quantifiable == true
196
+ fail!("Nothing to repeat") if quantifier_ahead?
197
+ "(?:#{atom})#{quantified}"
198
+ end
199
+
200
+ # `^`, `$`, `\b`, and `\B`, which no quantifier may follow.
201
+ #: (Hash[Symbol, bool]) -> String?
202
+ def assertion(flags)
203
+ case peek
204
+ when "^" then take && (flags[:m] ? "(?<![^#{LINE_ENDS}])" : "\\A")
205
+ when "$" then take && (flags[:m] ? "(?![^#{LINE_ENDS}])" : "\\z")
206
+ when "\\"
207
+ case peek(1)
208
+ when "b" then (@at += 2) && "(?:(?<=[#{WORD}])(?![#{WORD}])|(?<![#{WORD}])(?=[#{WORD}]))"
209
+ when "B" then (@at += 2) && "(?:(?<=[#{WORD}])(?=[#{WORD}])|(?<![#{WORD}])(?![#{WORD}]))"
210
+ end
211
+ end
212
+ end
213
+
214
+ # An atom as Ruby source, and whether a quantifier may follow it:
215
+ # true, false, or :lookbehind, whose refusal V8 words otherwise.
216
+ #: (Hash[Symbol, bool]) -> [String, (bool | Symbol)]
217
+ def atom(flags)
218
+ char = peek
219
+ case char
220
+ when "(" then group(flags)
221
+ when "[" then [character_class, true]
222
+ when "."
223
+ take
224
+ [flags[:s] ? "(?m:.)" : "[^#{LINE_ENDS}]", true]
225
+ when "\\" then [escape(flags), true]
226
+ when "*", "+", "?" then fail!("Nothing to repeat")
227
+ when "{"
228
+ fail!("Nothing to repeat") if quantifier_ahead?
229
+ take
230
+ [literal("{"), true]
231
+ else
232
+ take
233
+ [literal(char.to_s), true]
234
+ end
235
+ end
236
+
237
+ #: (Hash[Symbol, bool]) -> [String, (bool | Symbol)]
238
+ def group(flags)
239
+ take
240
+ if taken?("?=") || taken?("?!")
241
+ kind = @chars[@at - 1] == "=" ? "(?=" : "(?!"
242
+ return ["#{kind}#{closed(flags)})", true]
243
+ end
244
+ if taken?("?<=") || taken?("?<!")
245
+ kind = @chars[@at - 1] == "=" ? "(?<=" : "(?<!"
246
+ return ["#{kind}#{closed(flags)})", :lookbehind]
247
+ end
248
+ return ["(?:#{closed(flags)})", true] if taken?("?:")
249
+
250
+ if taken?("?<")
251
+ finish = find_close(@at)
252
+ name = finish ? @chars[@at...finish].to_a.join : ""
253
+ fail!("Invalid capture group name") unless finish && name.match?(GROUP_NAME)
254
+ fail!("Duplicate capture group name") if @visible.include?(name)
255
+
256
+ @visible += [name]
257
+ @at = finish + 1
258
+ @group += 1
259
+ number = @group
260
+ return ["(?<g#{number}>#{closed(flags)})", true]
261
+ end
262
+ return modified(flags) if peek == "?"
263
+
264
+ @group += 1
265
+ number = @group
266
+ ["(?<g#{number}>#{closed(flags)})", true]
267
+ end
268
+
269
+ # `(?ims-ims:...)`: the flags it turns on and off, for its body.
270
+ #: (Hash[Symbol, bool]) -> [String, bool]
271
+ def modified(flags)
272
+ take
273
+ on = read_flags
274
+ off = taken?("-") ? read_flags : nil
275
+ fail!("Invalid group") unless peek == ":"
276
+ fail!("Invalid flag group") if off && on.empty? && off.empty?
277
+ fail!("Repeated flag in flag group") if off&.intersect?(on)
278
+ take
279
+ inner = flags.merge(on.to_h { [_1.to_sym, true] }).merge((off || []).to_h { [_1.to_sym, false] })
280
+ body = closed(inner)
281
+ case_change = inner[:i] == flags[:i] ? nil : inner[:i]
282
+ [case_change.nil? ? "(?:#{body})" : "(?#{case_change ? "i" : "-i"}:#{body})", true]
283
+ end
284
+
285
+ #: () -> Array[String]
286
+ def read_flags
287
+ read = [] #: Array[String]
288
+ while (char = peek) && "ims".include?(char)
289
+ fail!("Repeated flag in flag group") if read.include?(char)
290
+ read << char
291
+ take
292
+ end
293
+ fail!("Invalid group") unless [":", "-"].include?(peek)
294
+ read
295
+ end
296
+
297
+ # The body of a group through its `)`.
298
+ #: (Hash[Symbol, bool]) -> String
299
+ def closed(flags)
300
+ body = disjunction(flags)
301
+ fail!("Unterminated group") unless taken?(")")
302
+ body
303
+ end
304
+
305
+ # A quantifier at this point, as Ruby source, or nil.
306
+ #: () -> String?
307
+ def quantifier
308
+ base = case peek
309
+ when "*", "+", "?" then take
310
+ when "{"
311
+ braced = braced_quantifier
312
+ return nil unless braced
313
+
314
+ braced
315
+ end
316
+ return nil unless base
317
+
318
+ # Ruby reads `{n}?` as an optional `{n}`; a lazy exact count is
319
+ # the count.
320
+ lazy = taken?("?")
321
+ return base if lazy && base.match?(/\A\{\d+\}/)
322
+
323
+ lazy ? "#{base}?" : base
324
+ end
325
+
326
+ #: () -> bool
327
+ def quantifier_ahead?
328
+ %w[* + ?].include?(peek) || (peek == "{" && !@chars[@at..].to_a.join.match(/\A\{\d+(?:,\d*)?\}/).nil?)
329
+ end
330
+
331
+ # `{n}`, `{n,}`, or `{n,m}`, read and written for Ruby, or nil when
332
+ # the brace starts none, which makes it a `{`.
333
+ #: () -> String?
334
+ def braced_quantifier
335
+ found = @chars[@at..].to_a.join.match(/\A\{(\d+)(?:(,)(\d*))?\}/)
336
+ return nil unless found
337
+
338
+ @at += found[0].to_s.length
339
+ low = found[1].to_i
340
+ written = found[3].to_s
341
+ high = written.empty? ? nil : written.to_i #: Integer?
342
+ if high
343
+ fail!("numbers out of order in {} quantifier") if high < low
344
+ high = nil if high > REPEAT_LIMIT
345
+ end
346
+ return "{0}(?!)" if low > REPEAT_LIMIT
347
+ return "{#{low}}" unless found[2]
348
+
349
+ high ? "{#{low},#{high}}" : "{#{low},}"
350
+ end
351
+
352
+ # An escape outside a class, from its backslash.
353
+ #: (Hash[Symbol, bool]) -> String
354
+ def escape(flags)
355
+ take
356
+ char = take
357
+ fail!("\\ at end of pattern") if char.nil?
358
+ if (set = SETS[char])
359
+ members, negated = set
360
+ return "[#{"^" if negated}#{members}]"
361
+ end
362
+ return literal(CONTROL.fetch(char).chr(Encoding::UTF_8)) if CONTROL.key?(char)
363
+
364
+ case char
365
+ when "c"
366
+ letter = peek
367
+ return literal((take.to_s.ord % 32).chr(Encoding::UTF_8)) if letter&.match?(/\A[A-Za-z]\z/)
368
+
369
+ @at -= 1
370
+ literal("\\")
371
+ when /\A[0-9]\z/ then decimal(char)
372
+ when "x" then hex(2) || literal("x")
373
+ when "u" then hex(4) || literal("u")
374
+ when "k" then named_reference(flags)
375
+ else literal(char)
376
+ end
377
+ end
378
+
379
+ # `\0`, a backreference, a legacy octal escape, or `\8` and `\9`.
380
+ #: (String) -> String
381
+ def decimal(first)
382
+ digits = first
383
+ digits += take.to_s while peek&.match?(/\A[0-9]\z/)
384
+ number = digits.to_i
385
+ return backreference([number]) if number >= 1 && number <= @count
386
+
387
+ @at -= digits.length - 1
388
+ return literal(first) if %w[8 9].include?(first)
389
+
390
+ literal(octal(first).chr(Encoding::UTF_8))
391
+ end
392
+
393
+ # A legacy octal escape from its first digit: at most three digits,
394
+ # and at most 0o377.
395
+ #: (String) -> Integer
396
+ def octal(first)
397
+ value = first.to_i
398
+ limit = first <= "3" ? 2 : 1
399
+ limit.times do
400
+ break unless peek&.match?(/\A[0-7]\z/)
401
+
402
+ value = (value * 8) + take.to_s.to_i
403
+ end
404
+ value
405
+ end
406
+
407
+ # `\xHH` or `\uHHHH` as the unit it names, or nil (and nothing
408
+ # read) when the digits are not there.
409
+ #: (Integer) -> String?
410
+ def hex(length)
411
+ digits = @chars[@at, length]&.join.to_s
412
+ return nil unless digits.match?(/\A\h{#{length}}\z/)
413
+
414
+ @at += length
415
+ literal(JsPattern.unit(digits.hex))
416
+ end
417
+
418
+ #: (Hash[Symbol, bool]) -> String
419
+ def named_reference(_flags)
420
+ return literal("k") if @names.empty?
421
+
422
+ fail!("Invalid named reference") unless taken?("<")
423
+ finish = find_close(@at)
424
+ name = finish ? @chars[@at...finish].to_a.join : ""
425
+ fail!("Invalid capture group name") unless finish && name.match?(GROUP_NAME)
426
+ @at = finish + 1
427
+ numbers = @names[name] or fail!("Invalid named capture referenced")
428
+ backreference(numbers)
429
+ end
430
+
431
+ # The first of `numbers` that matched, or nothing when none did. A
432
+ # group that opens after the reference has never matched there,
433
+ # since JavaScript clears a repeated group's captures each round, so
434
+ # it is left out (Ruby refuses a name it has not seen yet).
435
+ #: (Array[Integer]) -> String
436
+ def backreference(numbers)
437
+ numbers.select { _1 <= @group }.reverse.reduce("") { |rest, number| "(?(<g#{number}>)\\k<g#{number}>|#{rest})" }
438
+ end
439
+
440
+ #: () -> String
441
+ def character_class
442
+ take
443
+ negated = taken?("^")
444
+ parts = [] #: Array[String]
445
+ loop do
446
+ char = peek
447
+ fail!("Unterminated character class") if char.nil?
448
+ if char == "]"
449
+ take
450
+ break
451
+ end
452
+
453
+ low = class_atom
454
+ if peek == "-" && peek(1) && peek(1) != "]"
455
+ take
456
+ high = class_atom
457
+ if low.is_a?(Integer) && high.is_a?(Integer)
458
+ fail!("Range out of order in character class") if high < low
459
+ parts << range(low, high)
460
+ else
461
+ parts.push(class_part(low), class_part("-".ord), class_part(high))
462
+ end
463
+ else
464
+ parts << class_part(low)
465
+ end
466
+ end
467
+ written = parts.join
468
+ return(negated ? "(?m:.)" : "(?!)") if written.empty?
469
+
470
+ "[#{"^" if negated}#{written}]"
471
+ end
472
+
473
+ # One member of a class: a unit's code, or a set as Ruby source.
474
+ #: () -> (Integer | String)
475
+ def class_atom
476
+ char = take.to_s
477
+ return char.ord unless char == "\\"
478
+
479
+ char = take
480
+ fail!("\\ at end of pattern") if char.nil?
481
+ if (set = SETS[char])
482
+ members, negated = set
483
+ return negated ? "[^#{members}]" : members
484
+ end
485
+ return CONTROL.fetch(char) if CONTROL.key?(char)
486
+
487
+ case char
488
+ when "b" then 0x08
489
+ when "c"
490
+ letter = peek
491
+ return take.to_s.ord % 32 if letter&.match?(/\A[A-Za-z0-9_]\z/)
492
+
493
+ @at -= 1
494
+ "\\".ord
495
+ when "0".."7" then octal(char)
496
+ when "x", "u"
497
+ length = char == "x" ? 2 : 4
498
+ digits = @chars[@at, length]&.join.to_s
499
+ return char.ord unless digits.match?(/\A\h{#{length}}\z/)
500
+
501
+ @at += length
502
+ JsPattern.unit(digits.hex).ord
503
+ else char.ord
504
+ end
505
+ end
506
+
507
+ #: (Integer | String) -> String
508
+ def class_part(part) = part.is_a?(Integer) ? code(part) : part
509
+
510
+ # A range of units, with the surrogates in it where `units` writes
511
+ # them.
512
+ #: (Integer, Integer) -> String
513
+ def range(low, high)
514
+ pieces = [] #: Array[[Integer, Integer]]
515
+ pieces << [low, [high, 0xD7FF].min] if low < 0xD800
516
+ surrogate_low = [low, 0xD800].max
517
+ surrogate_high = [high, 0xDFFF].min
518
+ pieces << [UNIT_BASE + surrogate_low - 0xD800, UNIT_BASE + surrogate_high - 0xD800] if surrogate_low <= surrogate_high
519
+ pieces << [[low, 0xE000].max, high] if high > 0xDFFF
520
+ pieces.map { |from, to| from == to ? code(from) : "#{code(from)}-#{code(to)}" }.join
521
+ end
522
+
523
+ #: (Integer) -> String
524
+ def code(value) = format("\\u{%x}", value)
525
+
526
+ #: (String) -> String
527
+ def literal(char) = char.match?(/\A[A-Za-z0-9]\z/) ? char : code(char.ord)
528
+ end
529
+ end
530
+ end
531
+ end
@@ -55,7 +55,7 @@ module Nukadoko
55
55
  fail_with = lambda do |kind, message|
56
56
  entry.merge!(error: { message: message, kind: kind }, started_at: started, finished_at: Time.now.utc.iso8601(3), **(nested.any? ? { calls: nested } : {}))
57
57
  end
58
- problems = part.args.problems(args)
58
+ problems = part.args.problems(args, strict: true)
59
59
  if problems.any?
60
60
  message = "args validation failed: #{problems.join("; ")}"
61
61
  fail_with.("args_invalid", message)
@@ -10,8 +10,68 @@ require "cucumber/cucumber_expressions/parameter_type_registry"
10
10
  module Nukadoko
11
11
  module Engine
12
12
  class Pattern
13
+ CE = Cucumber::CucumberExpressions
14
+
15
+ # The built-in parameter types as the JavaScript package defines them.
16
+ # The gem's own differ: its \s takes no Unicode space, so {word} read
17
+ # "a<no-break space>b" as one word, and its float takes no "1." prefix
18
+ # or the lookahead JavaScript's does, so a line bound to one step in
19
+ # one engine and to none in the other. A value is what the JavaScript
20
+ # transformer gives: Number for an integer (a double past 2**53),
21
+ # parseFloat for a float, and the matched text for a bigdecimal.
22
+ class Registry < Cucumber::CucumberExpressions::ParameterTypeRegistry
23
+ INTEGER = [/-?\d+/, /\d+/].freeze
24
+ FLOAT = /(?=[^\n\r

]*\d[^\n\r

]*)[-+]?\d*(?:\.(?=\d[^\n\r

]*))?\d*(?:\d+[E][+-]?\d+)?/
25
+ WORD = /[^#{Spaces::CLASS}]+/
26
+ STRING = CE::ParameterTypeRegistry::STRING_REGEXP
27
+ ANONYMOUS = /[^\n\r

]*/
28
+ DECIMAL = /\A[-+]?(?:Infinity|(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)/
29
+
30
+ # JavaScript's Number of a text of digits.
31
+ #: (String?) -> Numeric?
32
+ def self.number(text)
33
+ return nil if text.nil?
34
+
35
+ value = Integer(text, 10)
36
+ value.abs > 2**53 ? value.to_f : value
37
+ end
38
+
39
+ # JavaScript's parseFloat: the longest number at the start, or NaN.
40
+ #: (String?) -> Float?
41
+ def self.parse_float(text)
42
+ return nil if text.nil?
43
+
44
+ found = text.sub(/\A[#{Spaces::CLASS}]+/, "")[DECIMAL]
45
+ return Float::NAN if found.nil?
46
+
47
+ sign = found.start_with?("-") ? -1 : 1
48
+ return sign * Float::INFINITY if found.end_with?("Infinity")
49
+
50
+ Float(found.sub(/\.(?=[eE]|\z)/, ""))
51
+ end
52
+
53
+ #: () -> void
54
+ def initialize
55
+ @parameter_type_by_name = {}
56
+ @parameter_types_by_regexp = Hash.new { |hash, regexp| hash[regexp] = [] }
57
+ number = ->(s = nil) { Registry.number(s) }
58
+ float = ->(s = nil) { Registry.parse_float(s) }
59
+ define_parameter_type(CE::ParameterType.new("int", INTEGER, Integer, number, true, true))
60
+ define_parameter_type(CE::ParameterType.new("float", FLOAT, Float, float, true, false))
61
+ define_parameter_type(CE::ParameterType.new("word", WORD, String, ->(s = nil) { s }, false, false))
62
+ define_parameter_type(CE::ParameterType.new("string", STRING, String, lambda { |s1, s2|
63
+ (s1 || s2 || "").gsub('\\"', '"').gsub("\\'", "'")
64
+ }, true, false))
65
+ define_parameter_type(CE::ParameterType.new("", ANONYMOUS, String, ->(s = nil) { s }, false, true))
66
+ define_parameter_type(CE::ParameterType.new("double", FLOAT, Float, float, false, false))
67
+ define_parameter_type(CE::ParameterType.new("bigdecimal", FLOAT, String, ->(s = nil) { s }, false, false))
68
+ %w[byte short long].each { define_parameter_type(CE::ParameterType.new(_1, INTEGER, Integer, number, false, false)) }
69
+ define_parameter_type(CE::ParameterType.new("biginteger", INTEGER, Integer, ->(s = nil) { s && Integer(s, 10) }, false, false))
70
+ end
71
+ end
72
+
13
73
  NAMED = /\{([A-Za-z_][\w-]*):([\w-]+)\}/
14
- REGISTRY = Cucumber::CucumberExpressions::ParameterTypeRegistry.new
74
+ REGISTRY = Registry.new
15
75
 
16
76
  attr_reader :source, :keys
17
77
 
@@ -19,7 +79,7 @@ module Nukadoko
19
79
  def initialize(source)
20
80
  @source = source
21
81
  @keys = source.scan(NAMED).map { Array(_1).first.to_s }
22
- @expression = Cucumber::CucumberExpressions::CucumberExpression.new(source.gsub(NAMED, '{\2}'), REGISTRY)
82
+ @expression = CE::CucumberExpression.new(source.gsub(NAMED, '{\2}'), REGISTRY)
23
83
  end
24
84
 
25
85
  #: (String) -> Hash[String, untyped]?
data/lib/nukadoko/poll.rb CHANGED
@@ -16,12 +16,14 @@ module Nukadoko
16
16
  class Poll
17
17
  INTERVAL = 0.1
18
18
 
19
- #: (Hash[Symbol, untyped], Array[Hash[Symbol, untyped]], Hash[Symbol, untyped]) -> void
20
- def initialize(options, claims, args)
19
+ # `sides` gives the claim inputs of one reading (Claims.judge).
20
+ #: (Hash[Symbol, untyped], Array[Hash[Symbol, untyped]], Hash[Symbol, untyped], ?(^(untyped) -> ClaimSides::Inputs)?) -> void
21
+ def initialize(options, claims, args, sides = nil)
21
22
  @timeout = options.fetch(:timeout_ms) / 1000.0
22
23
  @settled = options[:settled] || []
23
24
  @claims = claims
24
25
  @args = args
26
+ @sides = sides
25
27
  end
26
28
 
27
29
  # A reading whose observe raised is kept with the first line of the
@@ -60,7 +62,7 @@ module Nukadoko
60
62
 
61
63
  #: (untyped, Integer) -> Hash[Symbol, untyped]
62
64
  def judge(value, attempt)
63
- judged = Claims.judge(@claims, value, @args)
65
+ judged = Claims.judge(@claims, value, @args, @sides&.(value))
64
66
  reading = { attempt: attempt, at: Time.now.utc.iso8601(3), holds: judged.all? { _1[:holds] }, actuals: judged.map { _1[:actual] }, value: value,
65
67
  judged_now: judged.select { _1[:for_judgment] }.map { _1[:actual] } }
66
68
  reading[:settled] = @settled.all? { Claims.judge_one(_1, value, {})[:holds] } if @settled.any?
@@ -32,18 +32,18 @@ module Nukadoko
32
32
  declared = self.declared
33
33
  (selected.any? ? selected : default_names).map do |name|
34
34
  unless name == "default" || declared.key?(name)
35
- add = "add environments.#{name} to nukadoko.program.rb"
36
- fix = if declared.empty? then "Leave out --env or pass --env default (nukadoko.program.rb defines no other environment), or #{add}."
35
+ add = "add environments.#{name} to the program in nukadoko.config.rb"
36
+ fix = if declared.empty? then "Leave out --env or pass --env default (the program in nukadoko.config.rb defines no other environment), or #{add}."
37
37
  else "Pass --env with a name it accepts (#{["default", *declared.keys].uniq.sort.join(", ")}), or #{add}."
38
38
  end
39
- raise Refusal.new("unknown-environment", %(Unknown environment "#{name}": not defined in nukadoko.program.rb's "environments"), fix: fix)
39
+ raise Refusal.new("unknown-environment", %(Unknown environment "#{name}": not defined in the environments of the program in nukadoko.config.rb), fix: fix)
40
40
  end
41
41
 
42
42
  environment(name)
43
43
  end
44
44
  end
45
45
 
46
- # Whether a program file gave any options, so each step names its world.
46
+ # Whether the config gave a program, so each step names its world.
47
47
  #: () -> bool
48
48
  def file? = !@options.empty?
49
49
 
@@ -71,7 +71,7 @@ module Nukadoko
71
71
  # Why a run without --env starts the environments it starts.
72
72
  #: () -> String
73
73
  def chosen_by
74
- if @options[:defaultEnvironments] then "defaultEnvironments in nukadoko.program.rb"
74
+ if @options[:defaultEnvironments] then "defaultEnvironments in nukadoko.config.rb"
75
75
  elsif default_names == ["default"] then "no environment declares conditions"
76
76
  else "the environments that declare conditions"
77
77
  end