module scheme import io import lexer import reader @[heap] struct ExtensionPortReader { data []u8 chunk int = 4096 split int = -1 mut: position int } fn (mut source ExtensionPortReader) read(mut destination []u8) !int { if source.position == source.data.len { return io.Eof{} } mut count := int_min(destination.len, int_min(source.chunk, source.data.len - source.position)) if source.position < source.split { count = int_min(count, source.split - source.position) } unsafe { vmemcpy(destination.data, &u8(source.data.data) + source.position, count) } source.position += count return count } fn extension_result(value Value) !string { return match value.tag { .symbol { value.symbol_name()! } .string { value.as_string()! } .character { int(value.as_character()!).str() } else { write(value)! } } } fn test_extended_port_datums_have_independent_expected_values_at_all_chunks_and_splits() { mut vm := new() // Expectations are fixed independently of the source reader. A trailing 91 // checks that framing leaves exactly the following datum, including bars, // labels and short boolean forms which do not need a delimiter themselves. for row in [ ['#TRUE ', '#t'], ['#False ', '#f'], ['#t', '#t'], ['#u8(0 #x41 #e1.0 255)', '#u8(0 65 1 255)'], ['#VU8(12 #b11)', '#u8(12 3)'], ['|Hello World|', 'hello world'], [r'|a\|b\x5c;c|', 'a|b\\c'], [r'|a\x0;b|', 'a\x00b'], [r'A\x20;B ', 'a b'], ['ΣTRASSE ', 'σtrasse'], ['Straße ', 'strasse'], ['a\u0085', 'a'], ['a\u2003', 'a'], ['a\u2028', 'a'], [r'"a\x0;b\a\b\|"', 'a\x00b\a\b|'], ['"a\r\nb\r\u0085c\u0085d\u2028e"', 'a\nb\nc\nd\ne'], ['"a\\ \t\r\u0085 \tb"', 'ab'], ['#\\alarm ', '7'], ['#\\nul ', '0'], ['#\\x1f600 ', '128512'], ['#\\| ', '124'], ['#| ( " #| nested |# ) |#42 ', '42'], ['; discarded\u008542 ', '42'], ['; discarded\u202842 ', '42'], ['#;#;a b 42 ', '42'], ["#;' #;a b 42 ", '42'], ['(#;#;a b 1 #| ) |# 2)', '(1 2)'], ["' #;a #;b c ", '(quote c)'], ['#;#1=(a . #1#) 42 ', '42'], ['#;(#1# #1=ignored) (#1=kept #1#)', '(kept kept)'], ['#12=42 ', '42'], ['#123456789012345678901234567890=17 ', '17'], ] { text := row[0] + '91 ' for chunk in [1, 2, 3, 15, 16, 17, 4096] { mut source := &ExtensionPortReader{ data: text.bytes(), chunk: chunk } input := vm.input_port(source, read_size: 19) or { panic(err) } actual := extension_result(vm.read(input) or { panic('${row[0]} chunk${chunk}: ${err}') }) or { panic(err) } assert actual == row[1], '${row[0]} chunk${chunk}: ${actual}' assert vm.read(input) or { panic(err) }.as_integer() or { panic(err) } == 91 assert vm.read(input) or { panic(err) }.tag == .eof } for split in 1 .. text.len { mut source := &ExtensionPortReader{ data: text.bytes(), split: split } input := vm.input_port(source, read_size: text.len) or { panic(err) } assert extension_result(vm.read(input) or { panic('${row[0]} split${split}: ${err}') }) or { panic(err) } == row[1] assert vm.read(input) or { panic(err) }.as_integer() or { panic(err) } == 91 } } } fn test_port_directives_persist_and_decode_each_frame_from_its_initial_mode() { mut vm := new() text := '#!no-fold-case A |B| C\\x44; (#;#!fold-case IGNORE E #!no-fold-case F) G #!fold-case H #!r6rs I ' for split in 0 .. text.len { chunk := [1, 2, 3, 15, 16, 17, 4096][split % 7] mut source := &ExtensionPortReader{ data: text.bytes(), chunk: chunk, split: split } input := vm.input_port(source, read_size: 17) or { panic(err) } for expected in ['A', 'B', 'CD'] { assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == expected } list := vm.read(input) or { panic(err) } assert list.pair_pointer().car.symbol_name() or { panic(err) } == 'e' assert list.pair_pointer().cdr.pair_pointer().car.symbol_name() or { panic(err) } == 'F' for expected in ['G', 'h', 'i'] { assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == expected } assert vm.read(input) or { panic(err) }.tag == .eof } mut source := &ExtensionPortReader{ data: 'Mixed #!fold-case Mixed '.bytes(), chunk: 1 } input := vm.input_port(source, read_size: 3, fold_case: false) or { panic(err) } assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == 'Mixed' assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == 'mixed' for only in ['#!no-fold-case', '#!fold-case #| hi |# #;x', '#;#;a b'] { mut source_only := &ExtensionPortReader{ data: only.bytes(), chunk: 1 } empty := vm.input_port(source_only, read_size: 2) or { panic(err) } assert vm.read(empty) or { panic(err) }.tag == .eof } } fn test_port_label_scopes_preserve_identity_and_return_mutable_graphs() { mut vm := new() for chunk in [1, 2, 3, 15, 16, 17, 4096] { mut source := &ExtensionPortReader{ data: '(#01=(a . #1#) #1# #2=#(1 #2#) #2# #3="hi" #3# #4=#u8(1 2) #4#) #1# '.bytes(), chunk: chunk } input := vm.input_port(source, read_size: 17) or { panic(err) } data := vm.read(input) or { panic(err) } mut items := [8]Value{} mut cursor := data for i in 0 .. 8 { items[i] = cursor.pair_pointer().car cursor = cursor.pair_pointer().cdr } assert cursor.tag == .nil for i in [0, 2, 4, 6] { assert items[i].pointer() == items[i + 1].pointer() } assert items[0].pair_pointer().cdr.pointer() == items[0].pointer() assert items[2].vector_pointer().items[1].pointer() == items[2].pointer() vm.define('graph', data) or { panic(err) } vm.eval('(set-car! (car graph) 9) (vector-set! (caddr graph) 0 8) (string-set! (list-ref graph 4) 0 #\\H) (bytevector-u8-set! (list-ref graph 6) 0 7)') or { panic(err) } assert items[1].pair_pointer().car.whole() == 9 assert items[3].vector_pointer().items[0].whole() == 8 assert items[5].as_string() or { panic(err) } == 'Hi' assert items[7].bytevector_pointer().bytes[0] == 7 if _ := vm.read(input) { assert false } else { assert err.msg().contains('label'), err.msg() } assert !input.input_pointer().busy && input.input_pointer().mark == -1 } } fn test_extended_port_errors_are_not_eof_and_leave_busy_guard_clean_at_all_chunks() { mut vm := new() for text in ['#trueX ', '#false#t ', '#u8(256)', '#vu8(-1)', '#u8(1.0)', '#u8(a)', '#u8(1 . 2)', '#u8(', '#|never closed', '#; ', '#;#;a ', '#;#1# 2 ', '#;(#1=x #1=y) 2 ', '#1# ', '#1=#1# ', '(#1=x #1=y)', '#1=', '|never closed', r'|bad\x;|', r'bad\xD800; ', r'"bad\x110000;"', '"bad\\ \t x"', '#!unknown ', '#!fold-cas ', '#\\xD800 ', '#\\', '#12345'] { for chunk in [1, 2, 3, 15, 16, 17, 4096] { mut source := &ExtensionPortReader{ data: text.bytes(), chunk: chunk } input := vm.input_port(source, read_size: 17) or { panic(err) } vm.set_input_port(input) or { panic(err) } if _ := vm.eval('(read)') { assert false, '${text} chunk${chunk}' } else { assert err is EvalError, '${text}: ${err}' assert err.line >= 1 && err.column >= 1, '${text}: ${err}' } assert !input.input_pointer().busy && input.input_pointer().mark == -1 } } // Public host reads preserve incomplete classifications; Scheme read wraps // them as runtime failures so its surrounding source is never replayed. for text in ['#|x', '|x', '#;', '#1=', '#u8('] { input := vm.open_input_string(text) or { panic(err) } if _ := vm.read(input) { assert false } else { if err is lexer.LexError { assert err.incomplete } else if err is reader.ReadError { assert err.incomplete } else { assert false, '${err}' } } } } fn test_extension_scan_kernels_are_bounded_at_all_offsets_and_block_edges() { for length in 0 .. 82 { for event in [u8(9), 10, 13, 32, 40, 41, 34, 59, 35, 124, 92, 128, 255] { for position in 0 .. length { mut bytes := []u8{len: length, init: `a`} bytes[position] = event for start in 0 .. length + 1 { assert input_event_end(bytes, start) == if start <= position { position } else { length } assert input_quote_end(bytes, start, event) == if start <= position { position } else { length } } } } } } fn test_raw_carriage_return_character_leaves_the_other_half_of_a_line_ending_unread() { mut vm := new() for ending in ['\n', '\u0085'] { text := '#\\\r' + ending + '42 ' for split in 0 .. text.len { for chunk in [1, 2, 3, 15, 16, 17, 4096] { mut source := &ExtensionPortReader{ data: text.bytes(), chunk: chunk, split: split } input := vm.input_port(source, read_size: 3) or { panic(err) } assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == `\r` assert vm.peek_char(input) or { panic(err) }.as_character() or { panic(err) } == ending.runes()[0] assert vm.read(input) or { panic(err) }.whole() == 42 } } } } fn test_port_fold_directives_consumed_before_failures_persist_for_the_next_read() { mut vm := new() for text in ['(#!no-fold-case a .) Mixed ', '#!no-fold-case #!bad Mixed ', '#u8(#!no-fold-case 256) Mixed '] { for chunk in [1, 2, 3, 15, 16, 17, 4096] { mut source := &ExtensionPortReader{ data: text.bytes(), chunk: chunk } input := vm.input_port(source, read_size: 3) or { panic(err) } if _ := vm.read(input) { assert false, text } assert vm.read(input) or { panic('${text}: ${err}') }.symbol_name() or { panic(err) } == 'Mixed' } } }