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,274 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require "json"
5
+
6
+ # JSON text read as JavaScript's JSON.parse reads it, and refused in V8's
7
+ # words. A claim's json view reports why a text is no JSON, and that reason
8
+ # is part of the record both engines write, so the Ruby engine walks the
9
+ # grammar V8 walks and names the same place. Ruby's own parser accepts
10
+ # what JSON.parse refuses (comments, a NaN under some options), so the walk
11
+ # also decides what is valid; Ruby's parser only builds the value after it.
12
+ # Positions count UTF-16 code units, as a JavaScript string does.
13
+ module Nukadoko
14
+ module JsJson
15
+ class Error < JSON::ParserError; end
16
+
17
+ # Texts V8 names whole, since they are what a JavaScript value turns
18
+ # into when it is passed as text by mistake.
19
+ SPECIAL = ["NaN", "Infinity", "undefined", "[object Object]"].freeze
20
+ CONTEXT = 10
21
+ WHITESPACE = [0x20, 0x09, 0x0a, 0x0d].freeze
22
+ ESCAPES = '"\\/bfnrt'.codepoints.freeze
23
+ HEX = "0123456789abcdefABCDEF".codepoints.freeze
24
+
25
+ # A Failure carries where the walk stopped and what V8 says there.
26
+ Failure = Data.define(:pos, :message)
27
+
28
+ #: (String) -> untyped
29
+ def self.parse(text)
30
+ failure = Walk.new(text).failure
31
+ raise Error, failure if failure
32
+
33
+ begin
34
+ JSON.parse(text)
35
+ rescue JSON::ParserError => e
36
+ # JSON.parse keeps a lone surrogate escape (\ud800) in its string; a
37
+ # Ruby string has no way to hold one, so the text is refused here.
38
+ raise Error, "the text holds an unpaired surrogate escape, which a Ruby string cannot hold" if e.message.include?("surrogate")
39
+
40
+ raise
41
+ end
42
+ end
43
+
44
+ # A value as JavaScript's JSON.stringify writes it: a whole float is the
45
+ # integer its one number type holds (2.0 is 2), a float its shortest digits
46
+ # (1e20 is 100000000000000000000), and NaN or Infinity null, so a reason that
47
+ # quotes a value reads as the TypeScript engine's does.
48
+ #: (untyped) -> String
49
+ def self.generate(value)
50
+ case value
51
+ when Hash then "{#{value.map { |key, item| "#{JSON.generate(key.to_s)}:#{generate(item)}" }.join(",")}}"
52
+ when Array then "[#{value.map { generate(_1) }.join(",")}]"
53
+ when Float then value.finite? ? Engine::AriaSnapshot.js_number(value) : "null"
54
+ when Symbol then JSON.generate(value.to_s)
55
+ else JSON.generate(value)
56
+ end
57
+ end
58
+
59
+ # Walks the text once and gives V8's message for the first place it is
60
+ # no JSON, or nil.
61
+ class Walk
62
+ #: (String) -> void
63
+ def initialize(text)
64
+ @text = text
65
+ @units = text.encode("UTF-16LE").unpack("v*") #: Array[Integer]
66
+ @pos = 0
67
+ end
68
+
69
+ #: () -> String?
70
+ def failure
71
+ value
72
+ space
73
+ return nil if @pos >= @units.size
74
+
75
+ located("Unexpected non-whitespace character after JSON")
76
+ rescue Stop => e
77
+ e.message
78
+ end
79
+
80
+ private
81
+
82
+ class Stop < StandardError; end
83
+
84
+ #: () -> Integer?
85
+ def peek = @units[@pos]
86
+
87
+ #: () -> void
88
+ def space
89
+ @pos += 1 while (unit = peek) && WHITESPACE.include?(unit)
90
+ end
91
+
92
+ #: (String) -> String
93
+ def located(words)
94
+ before = @units.first(@pos)
95
+ lines = 0
96
+ column = 0
97
+ index = 0
98
+ while index < before.size
99
+ unit = before[index]
100
+ if unit == 0x0d || unit == 0x0a
101
+ lines += 1
102
+ column = 0
103
+ index += 1 if unit == 0x0d && before[index + 1] == 0x0a
104
+ else
105
+ column += 1
106
+ end
107
+ index += 1
108
+ end
109
+ "#{words}#{" in JSON" unless words.end_with?("JSON")} at position #{@pos} (line #{lines + 1} column #{column + 1})"
110
+ end
111
+
112
+ #: (?String?) -> bot
113
+ def stop(words = nil)
114
+ raise Stop, located(words) if words
115
+ raise Stop, "Unexpected end of JSON input" if @pos >= @units.size
116
+ # V8 names the kind of token it found where it names one.
117
+ raise Stop, located("Unexpected number") if peek == 0x2d || digit?
118
+ raise Stop, located("Unexpected string") if peek == 0x22
119
+
120
+ raise Stop, unexpected
121
+ end
122
+
123
+ # V8's message for a character no rule expected: the text itself when
124
+ # it is short, else ten units on each side of the character.
125
+ #: () -> String
126
+ def unexpected
127
+ source = @units
128
+ return "\"#{@text}\" is not valid JSON" if SPECIAL.include?(@text)
129
+
130
+ token = text_of([source[@pos].to_i])
131
+ return "Unexpected token '#{token}', \"#{@text}\" is not valid JSON" if source.size < CONTEXT * 2 + 1
132
+
133
+ if @pos < CONTEXT
134
+ "Unexpected token '#{token}', \"#{text_of(source[0, @pos + CONTEXT] || [])}\"... is not valid JSON"
135
+ elsif @pos < source.size - CONTEXT
136
+ "Unexpected token '#{token}', ...\"#{text_of(source[@pos - CONTEXT, CONTEXT * 2] || [])}\"... is not valid JSON"
137
+ else
138
+ "Unexpected token '#{token}', ...\"#{text_of(source[(@pos - CONTEXT)..] || [])}\" is not valid JSON"
139
+ end
140
+ end
141
+
142
+ # A run of UTF-16 units as text; half of a pair, which a UTF-8 string
143
+ # cannot hold, is the replacement character.
144
+ #: (Array[Integer]) -> String
145
+ def text_of(units)
146
+ units.pack("v*").force_encoding("UTF-16LE").encode("UTF-8", invalid: :replace, undef: :replace)
147
+ end
148
+
149
+ #: () -> void
150
+ def value
151
+ space
152
+ case peek
153
+ when nil then stop
154
+ when 0x22 then string
155
+ when 0x7b then object
156
+ when 0x5b then array
157
+ when 0x2d, 0x30..0x39 then number
158
+ when 0x74 then literal("true")
159
+ when 0x66 then literal("false")
160
+ when 0x6e then literal("null")
161
+ else stop
162
+ end
163
+ end
164
+
165
+ #: (String) -> void
166
+ def literal(word)
167
+ word.each_codepoint do |unit|
168
+ stop if peek != unit
169
+ @pos += 1
170
+ end
171
+ end
172
+
173
+ #: () -> void
174
+ def object
175
+ @pos += 1
176
+ space
177
+ return @pos += 1 if peek == 0x7d
178
+
179
+ first = true
180
+ loop do
181
+ stop(first ? "Expected property name or '}'" : "Expected double-quoted property name") unless peek == 0x22
182
+ first = false
183
+ string
184
+ space
185
+ stop("Expected ':' after property name") unless peek == 0x3a
186
+ @pos += 1
187
+ value
188
+ space
189
+ case peek
190
+ when 0x2c then @pos += 1
191
+ space
192
+ when 0x7d then return @pos += 1
193
+ else stop("Expected ',' or '}' after property value")
194
+ end
195
+ end
196
+ end
197
+
198
+ #: () -> void
199
+ def array
200
+ @pos += 1
201
+ space
202
+ return @pos += 1 if peek == 0x5d
203
+
204
+ loop do
205
+ value
206
+ space
207
+ case peek
208
+ when 0x2c then @pos += 1
209
+ when 0x5d then return @pos += 1
210
+ else stop("Expected ',' or ']' after array element")
211
+ end
212
+ end
213
+ end
214
+
215
+ #: () -> void
216
+ def string
217
+ @pos += 1
218
+ loop do
219
+ unit = peek
220
+ stop("Unterminated string") if unit.nil?
221
+ @pos += 1
222
+ return if unit == 0x22
223
+
224
+ if unit < 0x20
225
+ @pos -= 1
226
+ stop("Bad control character in string literal")
227
+ end
228
+ next unless unit == 0x5c
229
+
230
+ escape = peek
231
+ stop if escape.nil?
232
+ if escape == 0x75
233
+ @pos += 1
234
+ 4.times do
235
+ stop("Bad Unicode escape") unless (hex = peek) && HEX.include?(hex)
236
+ @pos += 1
237
+ end
238
+ else
239
+ stop("Bad escaped character") unless ESCAPES.include?(escape)
240
+ @pos += 1
241
+ end
242
+ end
243
+ end
244
+
245
+ #: () -> void
246
+ def number
247
+ if peek == 0x2d
248
+ @pos += 1
249
+ stop("No number after minus sign") unless digit?
250
+ end
251
+ if peek == 0x30
252
+ @pos += 1
253
+ raise Stop, located("Unexpected number") if digit?
254
+ else
255
+ @pos += 1 while digit?
256
+ end
257
+ if peek == 0x2e
258
+ @pos += 1
259
+ stop("Unterminated fractional number") unless digit?
260
+ @pos += 1 while digit?
261
+ end
262
+ return unless [0x65, 0x45].include?(peek)
263
+
264
+ @pos += 1
265
+ @pos += 1 if [0x2b, 0x2d].include?(peek)
266
+ stop("Exponent part is missing a number") unless digit?
267
+ @pos += 1 while digit?
268
+ end
269
+
270
+ #: () -> bool
271
+ def digit? = (unit = peek) ? unit.between?(0x30, 0x39) : false
272
+ end
273
+ end
274
+ end