nukadoko 0.2.0 → 0.4.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +153 -0
  3. data/README.ja.md +6 -3
  4. data/README.md +6 -3
  5. data/lib/nukadoko/aria_snapshot.rb +4 -4
  6. data/lib/nukadoko/base.rb +49 -29
  7. data/lib/nukadoko/check.rb +62 -5
  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 +481 -0
  11. data/lib/nukadoko/claims.rb +182 -20
  12. data/lib/nukadoko/cli.rb +24 -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/parameter_types.rb +54 -0
  21. data/lib/nukadoko/parts.rb +1 -1
  22. data/lib/nukadoko/pattern.rb +112 -2
  23. data/lib/nukadoko/poll.rb +5 -3
  24. data/lib/nukadoko/program.rb +5 -5
  25. data/lib/nukadoko/project_files.rb +17 -0
  26. data/lib/nukadoko/project_program.rb +119 -0
  27. data/lib/nukadoko/run.rb +62 -17
  28. data/lib/nukadoko/run_record.rb +25 -22
  29. data/lib/nukadoko/scaffold.rb +2 -2
  30. data/lib/nukadoko/schema.rb +60 -18
  31. data/lib/nukadoko/secrets.rb +20 -4
  32. data/lib/nukadoko/spaces.rb +28 -0
  33. data/lib/nukadoko/step.rb +7 -1
  34. data/lib/nukadoko/text_fit.rb +1 -1
  35. data/lib/nukadoko/trace_actions.rb +4 -2
  36. data/lib/nukadoko/tree_passes.rb +6 -3
  37. data/lib/nukadoko/version.rb +1 -1
  38. data/lib/nukadoko.rb +26 -2
  39. data/llms.txt +20 -18
  40. data/sig/generated/nukadoko/aria_snapshot.rbs +2 -2
  41. data/sig/generated/nukadoko/base.rbs +24 -18
  42. data/sig/generated/nukadoko/check.rbs +11 -1
  43. data/sig/generated/nukadoko/claim_sides.rbs +216 -0
  44. data/sig/generated/nukadoko/claims.rbs +46 -3
  45. data/sig/generated/nukadoko/cli.rbs +1 -1
  46. data/sig/generated/nukadoko/env_check.rbs +0 -4
  47. data/sig/generated/nukadoko/fixtures.rbs +25 -2
  48. data/sig/generated/nukadoko/init.rbs +0 -2
  49. data/sig/generated/nukadoko/js_json.rbs +109 -0
  50. data/sig/generated/nukadoko/js_pattern.rbs +194 -0
  51. data/sig/generated/nukadoko/parameter_types.rbs +27 -0
  52. data/sig/generated/nukadoko/pattern.rbs +59 -1
  53. data/sig/generated/nukadoko/poll.rbs +3 -2
  54. data/sig/generated/nukadoko/program.rbs +1 -1
  55. data/sig/generated/nukadoko/project_files.rbs +19 -0
  56. data/sig/generated/nukadoko/project_program.rbs +55 -0
  57. data/sig/generated/nukadoko/run.rbs +17 -3
  58. data/sig/generated/nukadoko/run_record.rbs +5 -4
  59. data/sig/generated/nukadoko/schema.rbs +22 -4
  60. data/sig/generated/nukadoko/secrets.rbs +17 -2
  61. data/sig/generated/nukadoko/spaces.rbs +31 -0
  62. data/sig/generated/nukadoko/trace_actions.rbs +3 -1
  63. data/sig/generated/nukadoko/tree_passes.rbs +2 -2
  64. data/sig/generated/nukadoko.rbs +11 -0
  65. data/skills/nukadoko/SKILL.md +2 -2
  66. data/skills/nukadoko/references/diagnosing.md +1 -1
  67. data/skills/nukadoko/references/examples/http-api.md +19 -16
  68. data/skills/nukadoko/references/fixtures.md +1 -1
  69. data/skills/nukadoko/references/perturbed-worlds.md +8 -6
  70. data/skills/nukadoko/references/program.md +20 -20
  71. data/skills/nukadoko/references/running.md +2 -2
  72. data/skills/nukadoko/references/scenario-rules.md +2 -2
  73. data/skills/nukadoko/references/verdicts.md +3 -3
  74. data/skills/nukadoko/references/writing-steps.md +1 -1
  75. data/skills/nukadoko-verification/SKILL.md +6 -6
  76. metadata +19 -5
@@ -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
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # The config's parameterTypes, read and handed to Pattern: each entry a
5
+ # name, a regexp, and an optional transformer. A shape the TypeScript
6
+ # engine's schema refuses is refused here with its words, so a project
7
+ # moves between the engines without a config that one of them takes and
8
+ # the other does not. Boundary: reading the entries; registering them and
9
+ # matching lines are Pattern's.
10
+ module Nukadoko
11
+ module Engine
12
+ module ParameterTypes
13
+ NAME = /\A[a-zA-Z0-9_-]+\z/
14
+ KEYS = %i[name regexp transformer].freeze
15
+
16
+ #: (untyped) -> void
17
+ def self.read(types)
18
+ types = [] if types.nil?
19
+ invalid("parameterTypes: Invalid input: expected array, received #{kind(types)}") unless types.is_a?(Array)
20
+ types.each_with_index do |type, index|
21
+ at = "parameterTypes.#{index}"
22
+ invalid("#{at}: Invalid input: expected object, received #{kind(type)}") unless type.is_a?(Hash)
23
+ extra = type.keys.map(&:to_sym) - KEYS
24
+ invalid("unknown key(s): #{extra.join(", ")}") if extra.any?
25
+ name = type[:name]
26
+ invalid("#{at}.name: Invalid input: expected string, received #{kind(name)}") unless name.is_a?(String)
27
+ invalid("#{at}.name: Invalid string: must match pattern /^[a-zA-Z0-9_-]+$/") unless name.match?(NAME)
28
+ invalid("#{at}.regexp: must be a Regexp or a string") unless type[:regexp].is_a?(Regexp) || type[:regexp].is_a?(String)
29
+ invalid("#{at}.transformer: must be a function") unless type[:transformer].nil? || type[:transformer].respond_to?(:call)
30
+ end
31
+ Pattern.configure(types)
32
+ end
33
+
34
+ # A value's kind in the words zod uses for it.
35
+ #: (untyped) -> String
36
+ def self.kind(value)
37
+ case value
38
+ when nil then "undefined"
39
+ when String then "string"
40
+ when Numeric then "number"
41
+ when true, false then "boolean"
42
+ when Array then "array"
43
+ when Hash then "object"
44
+ else value.class.name.to_s.downcase
45
+ end
46
+ end
47
+
48
+ #: (String) -> bot
49
+ def self.invalid(detail)
50
+ raise Refusal.new("config-invalid", "Invalid config at #{CONFIG}: #{detail}", fix: "Correct the fields the message names in #{CONFIG}.")
51
+ end
52
+ end
53
+ end
54
+ 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)