module lexer import encoding.utf8 fn C.vile_block_alphanumeric_prefix(const_source &u8) int fn test_block_classifiers_handle_every_byte_in_every_lane() { $if @BACKEND == 'c' { for lane in 0 .. 16 { for byte in 0 .. 256 { mut bytes := []u8{len: 16, init: `x`} bytes[lane] = u8(byte) source := bytes.bytestr() assert block_non_ascii(source, 0) == (byte >= 128), 'lane ${lane}, byte ${byte}' alphanumeric := (byte >= int(`a`) && byte <= int(`z`)) || (byte >= int(`A`) && byte <= int(`Z`)) || (byte >= int(`0`) && byte <= int(`9`)) // The test owns exactly 16 bytes for this complete native load. assert unsafe { C.vile_block_alphanumeric_prefix(source.str) } == (if alphanumeric { 16 } else { lane }), 'lane ${lane}, byte ${byte}' // atom_test checks this V predicate against the explicit R5RS // alphabet; the native loop must stop at the identical byte. expected_run := if is_subsequent(u8(byte)) { 16 } else { lane } assert native_identifier_end(source, 0) == expected_run, 'lane ${lane}, byte ${byte}' expected := byte in [int(`"`), int(`\\`), int(`\r`), int(`\n`)] assert block_string_special(source, 0) == expected, 'lane ${lane}, byte ${byte}' } } } } // Reference positions are counted independently of the scanner's block/run // helpers, including CRLF crossing a SIMD block or a string escape. fn reference_position(source string, end int) (int, int) { mut line := 1 mut column := 1 for i in 0 .. end { if source[i] == `\r` { line++ column = 1 } else if source[i] == `\n` { if i == 0 || source[i - 1] != `\r` { line++ } column = 1 } else { column++ } } return line, column } fn assert_span_position(source string, span TokenSpan) { line, column := reference_position(source, span.start) assert span.line == line, 'offset ${span.start}, source ${source.bytes()}' assert span.column == column, 'offset ${span.start}, source ${source.bytes()}' } fn test_comment_blocks_preserve_positions_and_eof_at_every_alignment() { for padding in 0 .. 32 { for length in [0, 1, 2, 14, 15, 16, 17, 30, 31, 32, 33, 63, 64, 65, 127, 128, 129] { for body in ['x'.repeat(length), 'λ😀'.repeat(length), '\x00'.repeat(length)] { for ending in ['', '\r', '\n', '\r\n', '\n\r'] { prefix := ' '.repeat(padding) + ';' + body + ending source := prefix + if ending == '' { '' } else { 'after' } mut scanner := new_scanner(source) or { panic(err) } span := scanner.next() or { panic(err) } assert_span_position(source, span) if ending == '' { assert span.kind == .eof assert span.start == source.len } else { assert span.kind == .identifier assert span.start == prefix.len assert span.end == source.len } eof := scanner.next() or { panic(err) } assert eof.kind == .eof assert eof.start == source.len assert_span_position(source, eof) } } } } } fn test_string_blocks_handle_special_bytes_and_utf8_at_every_alignment() { for padding in 0 .. 32 { for length in [0, 1, 14, 15, 16, 17, 30, 31, 32, 33, 63, 64, 65, 127, 128, 129] { for special in [r'\"', r'\\', '\r', '\n', '\r\n', '\n\r', 'λ😀', '\x00'] { literal := '"' + 'a'.repeat(length) + special + 'b'.repeat(65 - length % 66) + '"' source := ' '.repeat(padding) + literal + ' after' mut scanner := new_scanner(source) or { panic(err) } span := scanner.next() or { panic(err) } assert span.kind == .string_literal assert span.start == padding assert span.end == padding + literal.len assert span.text(source) == literal assert_span_position(source, span) after := scanner.next() or { panic(err) } assert after.kind == .identifier assert after.start == source.len - 5 assert after.end == source.len assert_span_position(source, after) eof := scanner.next() or { panic(err) } assert eof.kind == .eof assert eof.start == source.len assert_span_position(source, eof) } } } } fn test_string_block_errors_keep_exact_escape_and_opening_positions() { for padding in 0 .. 32 { for length in 0 .. 66 { prefix := ' '.repeat(padding) + '"' + 'a'.repeat(length) + '\r\n' + 'b'.repeat(length) for suffix in [r'\q"', r'\', ''] { source := prefix + suffix mut scanner := new_scanner(source) or { panic(err) } if span := scanner.next() { assert false, 'expected a string error, got ${span}' } else { assert err is LexError if err is LexError { expected := if suffix == r'\q"' { prefix.len } else { padding } line, column := reference_position(source, expected) assert err.offset == expected assert err.line == line assert err.column == column } } } } } } fn test_ascii_validation_matches_utf8_validator_at_block_boundaries() { for offset in 0 .. 66 { for tail in 0 .. 18 { for sequence in ['λ', '😀', '\x00', [u8(0x80)].bytestr(), [u8(0xff)].bytestr(), [u8(0xc0), 0x80].bytestr(), [u8(0xed), 0xa0, 0x80].bytestr(), [u8(0xf4), 0x90, 0x80, 0x80].bytestr(), [u8(0xe2), 0x82].bytestr()] { source := 'a'.repeat(offset) + sequence + 'z'.repeat(tail) expected := utf8.validate_str(source) assert valid_source(source) == expected, '${source.bytes()}' if _ := new_scanner(source) { assert expected } else { assert !expected assert err is LexError if err is LexError { assert err.offset == 0 assert err.line == 1 assert err.column == 1 } } } } } } fn test_validation_accepts_all_ascii_bytes_and_rejects_invalid_single_bytes() { for offset in 0 .. 34 { for byte in 0 .. 256 { source := 'a'.repeat(offset) + [u8(byte)].bytestr() + 'z'.repeat(33) assert valid_source(source) == (byte < 128), 'offset ${offset}, byte ${byte}' } } } fn test_validation_matches_utf8_for_deterministic_random_bytes() { mut state := u32(0x31415926) for length in 0 .. 258 { mut bytes := []u8{len: length} for _ in 0 .. 8 { for i in 0 .. length { state = state * 1664525 + 1013904223 bytes[i] = u8(state >> 24) } source := bytes.bytestr() assert valid_source(source) == utf8.validate_str(source), '${bytes}' } } }