-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditr.cr
More file actions
290 lines (222 loc) · 6.59 KB
/
editr.cr
File metadata and controls
290 lines (222 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
require "./src/wirewright"
# Load codex
codex = ML.document(File.read("./runtime/codices/editR.codex.wwml"))
editR = Soma.editR(codex)
failures = [] of {expected: Term, got: Term, text: StringView}
# Load tests.
tests, srcmap = ML.document_and_srcmap(File.read("./tests/editR.test.wwml"))
state = Term.of
rwtime = 0.milliseconds
tests.items.each_with_index do |testcase, index|
Term.case(testcase) do
matchpi %{(edit seed_ (msgs_*) result_)} do
state = seed
msgs.items.each do |msg|
rwtime += Time.measure do
state = rewrite(Soma.dispatch(state, msg), editR)
end
end
unless state == result
print "X".colorize.red
failures << {expected: result, got: state, text: srcmap[{index}]}
next
end
print "·".colorize.green
end
end
end
puts
puts
path = "tests/editR.test.wwml"
failures.each do |failure|
_, line, column = ML::SyntaxError.lookaround(failure[:text])
puts "#{"▉ ".colorize.red}#{"#{path}:#{line}:#{column}".colorize.underline}"
puts " EXPECTED"
puts ML.display(failure[:expected], maxwidth: 80)
puts " GOT"
puts ML.display(failure[:got], maxwidth: 80)
puts
end
if failures.empty?
puts "OK #{tests.itemsize} #{path} #{rwtime.total_milliseconds}ms".colorize.green
else
puts "ERR #{tests.itemsize} #{path} #{rwtime.total_milliseconds}ms".colorize.red
end
puts
puts <<-BANNER
#{"# editR appender".colorize.bold}
Hit Ctrl-D to quit.
BANNER
puts
seed = state
msgs = Term[]
preview = -> do
msgs.items.reduce(seed) { |memo, msg| rewrite(Soma.dispatch(memo, msg), editR) }
end
show = ->(term : Term) do
ed = ->(full : String, range : Range(Int32, Int32)) do
print " ▍".colorize.dark_gray
full.each_char_with_index do |char, index|
if index.in?(range) || (range.size == 0 && range.begin == index)
if char == '\n'
print " ".colorize.back(:white).fore(:black)
print '\n'
else
print char.to_s.colorize.back(:white).fore(:black)
end
else
print char
end
if char == '\n'
print " ▍".colorize.dark_gray
end
end
if full.size.in?(range) || (range.size == 0 && range.begin == full.size)
print " ".colorize.back(:white).fore(:black)
end
puts
end
Term.case(term) do
matchpi %{[[I _*] (l_string * r_string) anchor←(%number +i32)]}, l: String, r: String, anchor: Int32 do
ed.call(l + r, Math.min(anchor, l.size)...Math.max(anchor, l.size))
end
otherwise do
puts ML.display(term, maxwidth: 80)
.each_line(chomp: false)
.map { |line| " ▍".colorize.dark_gray.to_s + line }
.join
end
end
end
show.call(seed)
loop do
print "> "
break unless input = gets
begin
command = ML.terms(input)
rescue e : ML::SyntaxError
e.humanize(STDERR)
next
end
Term.case(command) do
givenpi %{(%any s seed)} do
puts "Do you really want to discard #{msgs.size} message(s)? Type `s` to continue."
next unless gets == "s"
seed = preview.call
msgs = Term[]
show.call(seed)
end
givenpiT %{(%any s seed) line←(%number +i32) column←(%number +i32)} do
found = false
new_tests, new_srcmap = ML.document_and_srcmap(File.read("./tests/editR.test.wwml"))
new_tests.items.each_with_index do |test, index|
next unless text = new_srcmap.cd(index)[Tpath[]]?
_, its_line, its_column = ML::SyntaxError.lookaround(text)
next unless {line, column} == {its_line, its_column}
Term.matchpi(test, %{(edit its-seed_ _ _)}) do
puts "Using state as seed:"
show.call(its_seed)
seed = its_seed
end
found = true
break
end
unless found
puts "Seed not found"
end
end
givenpi %{(%any c change) new-seed_} do
unless msgs.empty?
puts "Are you sure? Messages in the current test case will be reset. Type `c` to continue."
next unless gets == "c"
end
msgs = Term[]
seed = new_seed
show.call(preview.call)
end
givenpi %{(%any u undo)} do
if msgs.empty?
puts "Nothing to undo"
next
end
msgs = msgs.items.grow(-1).collect
show.call(preview.call)
end
givenpi %{(%any p preview)} do
show.call(preview.call)
end
givenpi %{(%any t testcase)} do
result = preview.call
testcase = Term.of(:edit, seed, msgs, result)
show.call(testcase)
end
givenpi %{(%any w write)} do
result = preview.call
testcase = Term.of(:edit, seed, msgs, result)
File.open("./tests/editR.test.wwml", "a") do |io|
io.puts
ML.display(io, testcase, maxwidth: 80)
io.flush
end
puts "Wrote to disk:"
show.call(testcase)
seed = result
msgs = Term[]
show.call(preview.call)
end
givenpi %{(%any ws write-steps)} do
File.open("./tests/editR.test.wwml", "a") do |io|
result = msgs.items.reduce(seed) do |memo, msg|
step_result = rewrite(Soma.dispatch(memo, msg), editR)
testcase = Term.of(:edit, memo, Term[{msg}], step_result)
io.puts
ML.display(io, testcase, maxwidth: 80)
io.flush
puts "Wrote to disk:"
show.call(testcase)
step_result
end
seed = result
msgs = Term[]
end
show.call(preview.call)
end
givenpi %{(%any r reload)} do
codex = ML.document(File.read("./runtime/codices/editR.codex.wwml"))
editR = Soma.editR(codex)
puts "Reloaded codex from disk"
end
givenpi %{(%any ? h help)} do
puts <<-'HELP'
Commands:
[s]eed
Use the current preview as seed, discarding all messages that lead to it.
[s]eed line←(%number +i32) column←(%number +i32)
Use the test at line:column as seed.
c[hange] seed_
Changes the seed to seed_.
u[ndo]
Undo the last append.
p[review]
Show editor preview.
t[estcase]
Show the test case built so far.
w[rite]
Write the test case built so far to disk and use its result as the seed.
w[rite-]s[tep]
Write each message from the test case built so far as a separate test
case. Use the last result as the seed.
r[eload]
Reloads the codex file.
h[elp], ?
Print this message.
msg_
Append msg_ to the current message list.
HELP
end
otherwise do
msgs = msgs.append(command)
show.call(preview.call)
end
end
end