module scheme import io import lexer import reader @[heap] struct LocatedPortReader { data []u8 chunk int mut: position int } fn (mut source LocatedPortReader) read(mut destination []u8) !int { if source.position == source.data.len { return io.Eof{} } count := int_min(destination.len, int_min(source.chunk, source.data.len - source.position)) unsafe { vmemcpy(destination.data, &u8(source.data.data) + source.position, count) } source.position += count return count } fn test_port_errors_keep_absolute_byte_locations_after_compaction_and_mixed_operations() { mut vm := new() prefix := 'α\r\u0085;ignored\u2028 42\r\n ' for chunk in [1, 2, 3, 15, 16, 17, 4096] { mut source := &LocatedPortReader{ data: (prefix + '#u8(1 256)').bytes(), chunk: chunk } input := vm.input_port(source, read_size: 17) or { panic(err) } assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `α` assert vm.read(input) or { panic(err) }.whole() == 42 if _ := vm.read(input) { assert false } else { assert err is EvalError assert err.offset == prefix.len + 6, '${chunk}: ${err}' assert err.line == 4 && err.column == 9, '${chunk}: ${err}' } assert !input.input_pointer().busy && input.input_pointer().mark == -1 } // read-string and read-line advance the same cursor without eagerly storing // location metadata; subsequent refill must retain their consumed prefixes. for chunk in [1, 2, 3, 15, 16, 17, 4096] { prefix2 := 'λx\r\nrest\n ' mut source := &LocatedPortReader{ data: (prefix2 + '|bad\\x;|').bytes(), chunk: chunk } input := vm.input_port(source, read_size: 5) or { panic(err) } assert vm.read_string(input, 2) or { panic(err) }.as_string() or { panic(err) } == 'λx' assert vm.read_line(input) or { panic(err) }.as_string() or { panic(err) } == '' assert vm.read_line(input) or { panic(err) }.as_string() or { panic(err) } == 'rest' if _ := vm.read(input) { assert false } else { assert err is lexer.LexError assert err.offset == prefix2.len + 4 && err.line == 3 && err.column == 8, '${chunk}: ${err}' } } } fn test_port_unterminated_trivia_errors_retain_the_original_opening_after_many_refills() { mut vm := new() for suffix in ['#|' + ('ignored λ\r\n').repeat(20000), '#!unknown'] { mut source := &LocatedPortReader{ data: ('a\r\n ' + suffix).bytes(), chunk: 19 } input := vm.input_port(source, read_size: 23) or { panic(err) } assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `a` if _ := vm.read(input) { assert false } else { assert err is lexer.LexError assert err.offset == 5 && err.line == 2 && err.column == 3, '${err}' assert err.incomplete == suffix.starts_with('#|') } assert input.input_pointer().buffer.cap < 4096 } } fn test_reader_and_lexer_errors_are_absolute_after_large_frame_buffer_release() { mut vm := new() prefix := '"' + ('a\r\n').repeat(30000) + '"\r\n ' for bad in ['(a .)', '"bad\\q"'] { mut source := &LocatedPortReader{ data: (prefix + bad).bytes(), chunk: 997 } input := vm.input_port(source, read_size: 4096) or { panic(err) } vm.read(input) or { panic(err) } assert input.input_pointer().buffer.cap <= 65536 if _ := vm.read(input) { assert false } else { if err is reader.ReadError { assert err.offset == prefix.len + 4 && err.line == 30002 && err.column == 7, '${err}' } else if err is lexer.LexError { assert err.offset == prefix.len + 4 && err.line == 30002 && err.column == 7, '${err}' } else { assert false, '${err}' } } } } fn test_invalid_utf8_inside_quoted_frames_uses_the_failed_sequence_absolute_location() { mut vm := new() for bytes in [[u8(255)], [u8(0xc2), 0x20], [u8(0xe0), 0x80, 0x80], [u8(0xed), 0xa0, 0x80], [u8(0xf4), 0x90, 0x80, 0x80]] { for chunk in [1, 2, 3, 15, 16, 17, 4096] { mut data := 'x\r\n "ok'.bytes() data << bytes data << `"` mut source := &LocatedPortReader{ data: data, chunk: chunk } input := vm.input_port(source, read_size: 5) or { panic(err) } assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == 'x' if _ := vm.read(input) { assert false } else { assert err is lexer.LexError assert err.offset == 8 && err.line == 2 && err.column == 6, '${chunk}: ${err}' } } } } fn test_port_line_reading_uses_the_same_unicode_endings_without_consuming_next_line_data() { mut vm := new() for ending in ['\n', '\r', '\r\n', '\r\u0085', '\u0085', '\u2028'] { for chunk in [1, 2, 3, 15, 16, 17, 4096] { mut source := &LocatedPortReader{ data: ('a' + ending + 'b' + ending + ' 1/0 ').bytes(), chunk: chunk } input := vm.input_port(source, read_size: 3) or { panic(err) } assert vm.read_line(input) or { panic(err) }.as_string() or { panic(err) } == 'a' assert vm.read_line(input) or { panic(err) }.as_string() or { panic(err) } == 'b' if _ := vm.read(input) { assert false } else { assert err is EvalError assert err.line == 3 && err.column == 3 && err.offset == 4 + 2 * ending.len, '${ending.bytes()}: ${err}' } } } input := vm.open_input_string('42\n (a 1/0)') or { panic(err) } assert vm.read(input) or { panic(err) }.whole() == 42 if _ := vm.read(input) { assert false } else { assert err is EvalError assert err.line == 2 && err.column == 6 && err.offset == 8, '${err}' } }