module scheme import io import lexer import reader @[heap] struct HexCasePortReader { text string chunk int split int mut: position int } fn (mut source HexCasePortReader) read(mut destination []u8) !int { if source.position == source.text.len { return io.Eof{} } mut count := int_min(destination.len, int_min(source.chunk, source.text.len - source.position)) if source.position < source.split { count = int_min(count, source.split - source.position) } unsafe { vmemcpy(destination.data, source.text.str + source.position, count) } source.position += count return count } fn hex_case_input(mut vm VM, text string, chunk int, split int, fold bool) !Value { source := &HexCasePortReader{ text: text, chunk: chunk, split: split } return vm.input_port(source, read_size: 17, fold_case: fold) } fn test_port_uppercase_hex_identifier_framing_owns_the_whole_escape_at_every_split() { mut vm := new() for spelling in [r'A\X20;B', r'\X41;B', r'a\X3b;b', r'\X0;Name', r'|A\X20;B|', r'"\X41;\x42;\X0;"', r'#\X1F600', r'#\X'] { text := spelling + ' 91 ' for fold in [false, true] { for chunk in [1, 2, 3, 15, 16, 17, 4096] { for split in 0 .. text.len { input := hex_case_input(mut vm, text, chunk, split, fold) or { panic(err) } mut port := input.input_pointer() start, end, initial, present := port.frame_datum() or { panic(err) } assert present && initial == fold assert end - start == spelling.len, '${spelling}, chunk${chunk}, split${split}' framed := unsafe { tos(&u8(port.buffer.data) + start, end - start) } assert framed == spelling port.mark = -1 assert vm.read(input) or { panic(err) }.whole() == 91 } } } } } fn test_port_uppercase_hex_decoding_matches_fixed_values_in_both_fold_modes_at_all_splits() { mut vm := new() for row in [ [r'A\X20;B', 'A B', 'a b'], [r'\X41;B', 'AB', 'ab'], [r'a\X3b;b', 'a;b', 'a;b'], [r'\X0;Name', '\x00Name', '\x00name'], [r'|A\X20;B|', 'A B', 'a b'], [r'|A\X7c;B|', 'A|B', 'a|b'], [r'|\X5c;|', '\\', '\\'], [r'|\X1F600;Name|', '😀Name', '😀name'], ] { text := row[0] + ' 91 ' for fold in [false, true] { expected := row[if fold { 2 } else { 1 }] for chunk in [1, 2, 3, 15, 16, 17, 4096] { for split in 0 .. text.len { input := hex_case_input(mut vm, text, chunk, split, fold) or { panic(err) } assert vm.read(input) or { panic('${row[0]}, ${chunk}, ${split}: ${err}') }.symbol_name() or { panic(err) } == expected assert vm.read(input) or { panic(err) }.whole() == 91 } } } } for fold in [false, true] { for chunk in [1, 2, 3, 15, 16, 17, 4096] { for row in [[r'"\X41;\x42;\X0;"', 'AB\x00'], [r'"\X1f600;"', '😀']] { text := row[0] + ' 91 ' for split in 0 .. text.len { input := hex_case_input(mut vm, text, chunk, split, fold) or { panic(err) } assert vm.read(input) or { panic(err) }.as_string() or { panic(err) } == row[1] assert vm.read(input) or { panic(err) }.whole() == 91 } } } } } fn test_port_uppercase_hex_characters_and_literal_x_are_exact_in_both_fold_modes() { mut vm := new() spellings := [r'#\X41 ', r'#\X1F600 ', r'#\X ', r'#\x '] expected := [rune(65), rune(128512), rune(88), rune(120)] for fold in [false, true] { for chunk in [1, 2, 3, 15, 16, 17, 4096] { for i, spelling in spellings { text := spelling + '91 ' for split in 0 .. text.len { input := hex_case_input(mut vm, text, chunk, split, fold) or { panic(err) } assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == expected[i] assert vm.read(input) or { panic(err) }.whole() == 91 } } input := hex_case_input(mut vm, r'#\X', chunk, 0, fold) or { panic(err) } assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == `X` assert vm.read(input) or { panic(err) }.tag == .eof } } } fn test_port_uppercase_hex_errors_and_incomplete_escapes_are_not_eof_and_release_busy_state() { mut vm := new() for text in [r'a\X;', r'a\X110000; ', r'|\XD800;|', r'"\X110000;"', r'#\XD800 ', r'a\X41', r'|a\X41', r'"\X41'] { for fold in [false, true] { for chunk in [1, 2, 3, 15, 16, 17, 4096] { for split in 0 .. text.len { input := hex_case_input(mut vm, text, chunk, split, fold) or { panic(err) } if _ := vm.read(input) { assert false, text } else { if err is lexer.LexError { assert err.incomplete == text.ends_with('41') } else if err is reader.ReadError { assert err.incomplete == text.ends_with('41') } else { assert false, '${text}: ${err}' } } assert !input.input_pointer().busy && input.input_pointer().mark == -1 } } } } } fn test_port_uppercase_inline_hex_with_directives_inside_one_frame_decodes_from_initial_mode() { mut vm := new() text := r'(A\X41; #!no-fold-case B\X42; #!fold-case C\X43;) Mixed ' for chunk in [1, 2, 3, 15, 16, 17, 4096] { for split in 0 .. text.len { input := hex_case_input(mut vm, text, chunk, split, true) or { panic(err) } value := vm.read(input) or { panic(err) } assert value.pair_pointer().car.symbol_name() or { panic(err) } == 'aa' assert value.pair_pointer().cdr.pair_pointer().car.symbol_name() or { panic(err) } == 'BB' assert value.pair_pointer().cdr.pair_pointer().cdr.pair_pointer().car.symbol_name() or { panic(err) } == 'cc' assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == 'mixed' } } } fn test_port_mixed_case_directives_persist_across_chunks_and_do_not_fold_named_characters_when_disabled() { mut vm := new() text := r'#!NO-FOLD-CASE A\X41; #!fOlD-cAsE B\X42; #!No-FoLd-CaSe |C\X43;| #!R6RS Mixed #!FOLD-CASE #\SPACE ' for chunk in [1, 2, 3, 15, 16, 17, 4096] { for split in 0 .. text.len { input := hex_case_input(mut vm, text, chunk, split, true) or { panic(err) } for expected in ['AA', 'bb', 'CC', 'Mixed'] { assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == expected } assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == ` ` assert vm.read(input) or { panic(err) }.tag == .eof } } text2 := r'#!NO-FOLD-CASE #\SPACE #\X #!FOLD-CASE #\SPACE ' for chunk in [1, 2, 3, 15, 16, 17, 4096] { for split in 0 .. text2.len { input := hex_case_input(mut vm, text2, chunk, split, true) or { panic(err) } if _ := vm.read(input) { assert false } else { assert err is lexer.LexError } assert !input.input_pointer().busy && input.input_pointer().mark == -1 assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == `X` assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == ` ` } } }