vile / scheme / port_custom_datum.v
540 lines · 522 sloc · 16.44 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import lexer
4import reader
5import unicode17
6
7// Only boundary state survives suspension. The canonical reader still validates
8// each completed frame once. Trivia outside datums is never accumulated.
9enum CustomDatumMode {
10 atmosphere
11 line
12 block
13 hash
14 comma
15 quoted
16 word
17 word_escape
18 word_hex
19 word_hash
20 boolean_alias
21 vector_head
22 label
23 character
24}
25
26struct CustomDatumContext {
27mut:
28 prefix bool
29 ignored int
30}
31
32@[heap]
33struct CustomDatumFrame {
34mut:
35 mode CustomDatumMode
36 active bool
37 done bool
38 comments_only bool
39 initial_fold bool
40 opening InputLocation
41 hash_opening InputLocation
42 block_opening InputLocation
43 directive_point InputLocation
44 stack []CustomDatumContext
45 ignored int
46 hash_outside bool
47 block_depth int
48 block_previous rune
49 delimiter u8
50 escaped bool
51 word_escapes bool
52 directive_active bool
53 directive []u8
54 alias string
55 alias_index int
56 probe [4]u8
57 probe_len int
58 probe_point InputLocation
59 probe_epoch u64
60}
61
62// Return a bounded speculative prefix before the live callback tail. The tail
63// is never copied; its StringData identity and revision tracking stay intact.
64fn unread_custom_text_probe(value Value, bytes []u8, point InputLocation, epoch u64, positions &CustomTextProbePositions, raw_positions &CustomAttachedProbePositions) {
65 if bytes.len == 0 { return }
66 mut port := value.input_pointer()
67 mut resource := custom_input_resource(value)
68 current := port.input_location(port.position)
69 // Unresolved lookahead belongs to private scanner progress. A continuation
70 // may return after another operation moved or sought the shared cursor.
71 // Such an operation's committed history must never be rewound by this probe.
72 if resource.cursor_epoch != epoch || current.offset != point.offset + bytes.len
73 || current.line != point.line || current.column != point.column + bytes.len || current.previous_cr {
74 return
75 }
76 commit_custom_text_cursor(value)
77 remaining := if resource.replay_active { port.buffer.len } else { 0 }
78 mut prefix := []u8{len: bytes.len + remaining}
79 unsafe {
80 vmemcpy(prefix.data, bytes.data, bytes.len)
81 if remaining > 0 { vmemcpy(&u8(prefix.data) + bytes.len, port.buffer.data, remaining) }
82 }
83 prepend_custom_text_coordinates(resource, positions, bytes.len, remaining)
84 prepend_custom_attached_coordinates(resource, raw_positions, bytes.len, remaining)
85 if !resource.replay_active { resource.replay_tail = unsafe { port.buffer } }
86 resource.replay_active = true
87 port.buffer = prefix
88 port.position = 0
89 if isnil(port.location) { port.location = &InputLocation{} }
90 unsafe { *port.location = point }
91}
92
93fn custom_text_unread(value Value) ![]u8 {
94 port := value.input_pointer()
95 resource := custom_input_resource(value)
96 if !resource.replay_active {
97 return copy_byte_range(port.buffer, port.position, port.buffer.len)
98 }
99 mut tail := unsafe { resource.replay_tail }
100 if resource.text_view.tag == .string {
101 data := resource.text_view.string_pointer()
102 first := text_offset(data, resource.view_start)!
103 last := text_offset(data, resource.view_count)!
104 tail = unsafe { (data.value.str + first).vbytes(last - first) }
105 }
106 first_length := port.buffer.len - port.position
107 mut bytes := []u8{len: first_length + tail.len}
108 unsafe {
109 if first_length > 0 {
110 vmemcpy(bytes.data, &u8(port.buffer.data) + port.position, first_length)
111 }
112 if tail.len > 0 { vmemcpy(&u8(bytes.data) + first_length, tail.data, tail.len) }
113 }
114 return bytes
115}
116
117fn (mut state CustomPortOperation) datum_take(length int) {
118 mut port := state.target.input_pointer()
119 if state.frame.active {
120 state.bytes << unsafe { port.buffer[port.position..port.position + length] }
121 state.offset += length
122 }
123 if state.frame.directive_active {
124 state.frame.directive << unsafe { port.buffer[port.position..port.position + length] }
125 }
126 port.position += length
127}
128
129fn (mut state CustomPortOperation) datum_begin() {
130 mut frame := state.frame
131 if frame.active { return }
132 port := state.target.input_pointer()
133 frame.active = true
134 frame.initial_fold = !port.no_fold
135 frame.opening = port.input_location(port.position)
136}
137
138fn (mut state CustomPortOperation) datum_reset() {
139 state.bytes = []u8{}
140 unsafe { state.bytes.flags.set(.noslices) }
141 state.offset = 0
142 state.chunks = unsafe { nil }
143 mut frame := state.frame
144 frame.active = false
145 frame.done = false
146 frame.comments_only = false
147 frame.mode = .atmosphere
148 frame.ignored = 0
149 frame.stack.clear()
150 state.clear_text_probe()
151}
152
153fn (mut frame CustomDatumFrame) datum_token(token InputFrameToken) ! {
154 frame.mode = .atmosphere
155 if token in [.open, .prefix] {
156 if frame.stack.len >= reader.max_depth + 1 {
157 return failure('input datum nesting limit exceeded')
158 }
159 frame.stack << CustomDatumContext{ prefix: token == .prefix }
160 return
161 }
162 if token == .close {
163 if frame.stack.len == 0 || frame.stack.last().prefix {
164 frame.done = true
165 return
166 }
167 frame.stack.delete_last()
168 }
169 for {
170 if frame.stack.len == 0 {
171 if frame.ignored > 0 {
172 frame.ignored--
173 frame.done = frame.comments_only && frame.ignored == 0
174 } else {
175 frame.done = true
176 }
177 return
178 }
179 last := frame.stack.len - 1
180 if frame.stack[last].ignored > 0 {
181 frame.stack[last].ignored--
182 return
183 }
184 if !frame.stack[last].prefix { return }
185 frame.stack.delete_last()
186 }
187}
188
189fn (mut state CustomPortOperation) datum_token(token InputFrameToken) ! {
190 state.frame.datum_token(token) or {
191 port := state.target.input_pointer()
192 return port.input_error(port.position, err.msg())
193 }
194}
195
196fn (state &CustomPortOperation) datum_bytes() []u8 {
197 if isnil(state.chunks) { return unsafe { state.bytes } }
198 mut bytes := []u8{len: state.offset}
199 state.copy_text_bytes(mut bytes, 0)
200 return bytes
201}
202
203fn custom_datum_error(err IError, bytes []u8, point InputLocation) IError {
204 // This view only maps diagnostics; it never calls a synchronous Reader.
205 port := InputPort{ owner: unsafe { nil }, buffer: bytes, position: bytes.len, location: &point }
206 return port.absolute_input_error(err, 0)
207}
208
209fn custom_datum_operation_error(operation PortIoOperation, err IError) IError {
210 if operation != .read_datum { return err }
211 // Match the native read primitive's REPL-safe consumed-error conversion.
212 // get-datum and the public V read method retain typed reader errors.
213 if err is reader.ReadError {
214 return EvalError{ message: 'read: ' + err.message, offset: err.offset, line: err.line, column: err.column }
215 }
216 if err is lexer.LexError {
217 return EvalError{ message: 'read: ' + err.message, offset: err.offset, line: err.line, column: err.column }
218 }
219 return err
220}
221
222fn (mut state CustomPortOperation) datum_directive() ! {
223 mut frame := state.frame
224 bytes := unsafe { frame.directive }
225 source := unsafe { tos(bytes.data, bytes.len) }
226 mut port := state.target.input_pointer()
227 mut scanner := lexer.new_scanner(source, fold_case: !port.no_fold) or {
228 return custom_datum_error(err, bytes, frame.directive_point)
229 }
230 scanner.next() or { return custom_datum_error(err, bytes, frame.directive_point) }
231 port.no_fold = !scanner.fold_case()
232 frame.directive_active = false
233 frame.directive = []u8{}
234 if frame.hash_outside { state.datum_reset() } else { frame.mode = .atmosphere }
235}
236
237fn (mut state CustomPortOperation) datum_word_end() ! {
238 if state.frame.directive_active {
239 state.datum_directive()!
240 } else {
241 state.datum_token(.atom)!
242 }
243}
244
245fn (mut state CustomPortOperation) datum_eof() ! {
246 mut frame := state.frame
247 if frame.mode == .block {
248 p := frame.block_opening
249 return lexer.LexError{ message: 'unterminated block comment', offset: p.offset, line: p.line, column: p.column, incomplete: true }
250 }
251 if frame.mode == .boolean_alias {
252 unread_custom_text_probe(state.target, unsafe { (&frame.probe[0]).vbytes(frame.probe_len) }, frame.probe_point, frame.probe_epoch, state.text_probe_positions(), state.attached_probe_positions())
253 frame.probe_len = 0
254 state.clear_text_probe()
255 state.datum_token(.atom)!
256 return
257 }
258 if frame.mode == .word_hash {
259 state.bytes << u8(`#`)
260 state.offset++
261 if frame.directive_active { frame.directive << u8(`#`) }
262 }
263 if frame.directive_active {
264 state.datum_directive()!
265 return
266 }
267 frame.done = frame.active
268}
269
270// Captured callbacks detach both scanner state and the mutable suffix before
271// resuming. Input cursor effects stay shared, as they do for ordinary ports.
272@[direct_array_access]
273fn (mut m Machine) step_custom_datum_input(mut vm VM) ! {
274 mut state := custom_port_operation(m.env.slots[0])
275 mut frame := state.frame
276 mut port := state.target.input_pointer()
277 for {
278 refresh_custom_text_view(state.target)!
279 if frame.done {
280 bytes := state.datum_bytes()
281 source := unsafe { tos(bytes.data, bytes.len) }
282 if frame.comments_only {
283 doc := reader.read_all(source, fold_case: frame.initial_fold) or { return custom_datum_error(err, bytes, frame.opening) }
284 port.no_fold = !doc.final_fold_case()
285 state.datum_reset()
286 continue
287 }
288 mut scratch := InputFrameState{ no_fold: !frame.initial_fold }
289 result := scratch.read_frame(source, frame.initial_fold) or { return custom_datum_error(err, bytes, frame.opening) }
290 port.no_fold = scratch.no_fold
291 m.finish_custom_port_operation(result)
292 return
293 }
294 if port.position == port.buffer.len {
295 if !port.ended {
296 m.request_custom_text_input(mut vm)!
297 return
298 }
299 observe_custom_attached_eof(state.target)
300 state.datum_eof()!
301 if frame.done || port.position < port.buffer.len { continue }
302 m.finish_custom_port_operation(eof_value())
303 return
304 }
305 c, width := port.peek_character()!
306 match frame.mode {
307 .atmosphere {
308 if (c < 128 && input_space(u8(c))) || unicode17.whitespace(c) {
309 state.datum_take(width)
310 } else if c == `;` {
311 state.datum_take(width)
312 frame.mode = .line
313 } else {
314 outside := !frame.active
315 state.datum_begin()
316 if c == `#` {
317 frame.hash_outside = outside
318 frame.hash_opening = port.input_location(port.position)
319 state.datum_take(1)
320 frame.mode = .hash
321 } else if c in [`"`, `|`] {
322 frame.delimiter = u8(c)
323 frame.escaped = false
324 state.datum_take(1)
325 frame.mode = .quoted
326 } else if c in [`(`, `)`] {
327 state.datum_take(1)
328 state.datum_token(if c == `(` { .open } else { .close })!
329 } else if c in [`'`, `\x60`, `,`] {
330 state.datum_take(1)
331 if c == `,` { frame.mode = .comma } else { state.datum_token(.prefix)! }
332 } else {
333 frame.word_escapes = true
334 frame.mode = .word
335 }
336 }
337 }
338 .line {
339 state.datum_take(width)
340 if c in [`\r`, 10, 0x85, 0x2028, 0x2029] { frame.mode = .atmosphere }
341 }
342 .block {
343 state.datum_take(width)
344 if frame.block_previous == `#` && c == `|` {
345 frame.block_depth++
346 frame.block_previous = 0
347 } else if frame.block_previous == `|` && c == `#` {
348 frame.block_depth--
349 frame.block_previous = 0
350 if frame.block_depth == 0 { frame.mode = .atmosphere }
351 } else {
352 frame.block_previous = if c in [`#`, `|`] { c } else { rune(0) }
353 }
354 }
355 .hash {
356 if c == `|` {
357 frame.block_opening = frame.hash_opening
358 if frame.hash_outside { state.datum_reset() }
359 state.datum_take(1)
360 frame.block_depth = 1
361 frame.block_previous = 0
362 frame.mode = .block
363 } else if c == `!` {
364 state.datum_take(1)
365 frame.directive = [u8(`#`), `!`]
366 frame.directive_point = frame.hash_opening
367 frame.directive_active = true
368 frame.word_escapes = false
369 frame.mode = .word
370 } else if c == `;` {
371 state.datum_take(1)
372 if frame.hash_outside { frame.comments_only = true }
373 if frame.stack.len == 0 {
374 if frame.ignored == max_int {
375 return failure('datum comment count exceeds supported range')
376 }
377 frame.ignored++
378 } else {
379 last := frame.stack.len - 1
380 if frame.stack[last].ignored == max_int {
381 return failure('datum comment count exceeds supported range')
382 }
383 frame.stack[last].ignored++
384 }
385 frame.mode = .atmosphere
386 } else if c == `(` {
387 state.datum_take(1)
388 state.datum_token(.open)!
389 } else if c in [`'`, `\x60`, `,`, `:`] {
390 state.datum_take(1)
391 if c == `,` { frame.mode = .comma } else { state.datum_token(.prefix)! }
392 } else if c in [`u`, `U`, `v`, `V`] {
393 frame.alias = if c in [`u`, `U`] { '8(' } else { 'u8(' }
394 frame.alias_index = 0
395 state.datum_take(1)
396 frame.mode = .vector_head
397 } else if c in [`t`, `T`, `f`, `F`] {
398 frame.alias = if c in [`t`, `T`] { '#true' } else { '#false' }
399 frame.alias_index = 2
400 state.datum_take(1)
401 frame.probe_len = 0
402 state.clear_text_probe()
403 frame.probe_point = port.input_location(port.position)
404 frame.probe_epoch = state.resource.cursor_epoch
405 frame.mode = .boolean_alias
406 } else if c >= `0` && c <= `9` {
407 state.datum_take(1)
408 frame.mode = .label
409 } else if c == `\\` {
410 state.datum_take(1)
411 frame.mode = .character
412 } else {
413 frame.word_escapes = true
414 frame.mode = .word
415 }
416 }
417 .comma {
418 if c == `@` { state.datum_take(1) }
419 state.datum_token(.prefix)!
420 }
421 .quoted {
422 start := port.position
423 quote := input_quote_end(port.buffer, start, frame.delimiter)
424 mut slashes := quote
425 for slashes > start && port.buffer[slashes - 1] == u8(`\\`) { slashes-- }
426 odd := ((quote - slashes) & 1) != 0
427 quoted := odd != (slashes == start && frame.escaped)
428 state.datum_take(quote - start)
429 if quote == port.buffer.len {
430 frame.escaped = quoted
431 } else {
432 state.datum_take(1)
433 frame.escaped = false
434 if !quoted { state.datum_token(.atom)! }
435 }
436 }
437 .word {
438 end := input_event_end(port.buffer, port.position)
439 if end > port.position {
440 state.datum_take(end - port.position)
441 continue
442 }
443 if (c < 128 && (input_delimiter(u8(c)) || c == `|`)) || unicode17.whitespace(c) {
444 state.datum_word_end()!
445 } else if c == `#` {
446 frame.probe_point = port.input_location(port.position)
447 frame.probe_epoch = state.resource.cursor_epoch
448 state.remember_custom_text_probe(0)!
449 port.position++
450 frame.mode = .word_hash
451 } else if frame.word_escapes && c == `\\` {
452 state.datum_take(1)
453 frame.mode = .word_escape
454 } else {
455 state.datum_take(width)
456 }
457 }
458 .word_hash {
459 if c in [`|`, `;`, `!`] {
460 frame.probe[0] = u8(`#`)
461 unread_custom_text_probe(state.target, unsafe { (&frame.probe[0]).vbytes(1) }, frame.probe_point, frame.probe_epoch, state.text_probe_positions(), state.attached_probe_positions())
462 state.clear_text_probe()
463 state.datum_word_end()!
464 } else {
465 state.clear_text_probe()
466 state.bytes << u8(`#`)
467 state.offset++
468 if frame.directive_active { frame.directive << u8(`#`) }
469 frame.mode = .word
470 }
471 }
472 .word_escape {
473 if c in [`x`, `X`] {
474 state.datum_take(1)
475 frame.mode = .word_hex
476 } else {
477 frame.mode = .word
478 }
479 }
480 .word_hex {
481 if c == `;` {
482 state.datum_take(1)
483 frame.mode = .word
484 } else if c < 128 && (input_delimiter(u8(c)) || c == `|`) {
485 frame.mode = .word
486 } else {
487 state.datum_take(width)
488 }
489 }
490 .boolean_alias {
491 if c < 128 && u8(c) | 0x20 == frame.alias[frame.alias_index] {
492 state.remember_custom_text_probe(frame.probe_len)!
493 frame.probe[frame.probe_len] = u8(c)
494 frame.probe_len++
495 frame.alias_index++
496 port.position++
497 if frame.alias_index == frame.alias.len {
498 state.bytes << unsafe { (&frame.probe[0]).vbytes(frame.probe_len) }
499 state.offset += frame.probe_len
500 frame.probe_len = 0
501 state.clear_text_probe()
502 frame.word_escapes = false
503 frame.mode = .word
504 }
505 } else {
506 unread_custom_text_probe(state.target, unsafe { (&frame.probe[0]).vbytes(frame.probe_len) }, frame.probe_point, frame.probe_epoch, state.text_probe_positions(), state.attached_probe_positions())
507 frame.probe_len = 0
508 state.clear_text_probe()
509 state.datum_token(.atom)!
510 }
511 }
512 .vector_head {
513 if c < 128 && (u8(c) | if c == `(` { u8(0) } else { u8(0x20) }) == frame.alias[frame.alias_index] {
514 state.datum_take(1)
515 frame.alias_index++
516 if frame.alias_index == frame.alias.len { state.datum_token(.open)! }
517 } else {
518 frame.word_escapes = true
519 frame.mode = .word
520 }
521 }
522 .label {
523 if c >= `0` && c <= `9` {
524 state.datum_take(1)
525 } else if c in [`=`, `#`] {
526 state.datum_take(1)
527 state.datum_token(if c == `=` { .prefix } else { .atom })!
528 } else {
529 frame.word_escapes = false
530 frame.mode = .word
531 }
532 }
533 .character {
534 state.datum_take(width)
535 frame.word_escapes = false
536 frame.mode = .word
537 }
538 }
539 }
540}
541