vile / lexer / scan_blocks_test.v
188 lines · 177 sloc · 6.1 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module lexer
2
3import encoding.utf8
4
5fn C.vile_block_alphanumeric_prefix(const_source &u8) int
6
7fn test_block_classifiers_handle_every_byte_in_every_lane() {
8 $if @BACKEND == 'c' {
9 for lane in 0 .. 16 {
10 for byte in 0 .. 256 {
11 mut bytes := []u8{len: 16, init: `x`}
12 bytes[lane] = u8(byte)
13 source := bytes.bytestr()
14 assert block_non_ascii(source, 0) == (byte >= 128), 'lane ${lane}, byte ${byte}'
15 alphanumeric := (byte >= int(`a`) && byte <= int(`z`))
16 || (byte >= int(`A`) && byte <= int(`Z`))
17 || (byte >= int(`0`) && byte <= int(`9`))
18 // The test owns exactly 16 bytes for this complete native load.
19 assert unsafe { C.vile_block_alphanumeric_prefix(source.str) } == (if alphanumeric {
20 16
21 } else {
22 lane
23 }), 'lane ${lane}, byte ${byte}'
24 // atom_test checks this V predicate against the explicit R5RS
25 // alphabet; the native loop must stop at the identical byte.
26 expected_run := if is_subsequent(u8(byte)) { 16 } else { lane }
27 assert native_identifier_end(source, 0) == expected_run, 'lane ${lane}, byte ${byte}'
28 expected := byte in [int(`"`), int(`\\`), int(`\r`), int(`\n`)]
29 assert block_string_special(source, 0) == expected, 'lane ${lane}, byte ${byte}'
30 }
31 }
32 }
33}
34
35// Reference positions are counted independently of the scanner's block/run
36// helpers, including CRLF crossing a SIMD block or a string escape.
37fn reference_position(source string, end int) (int, int) {
38 mut line := 1
39 mut column := 1
40 for i in 0 .. end {
41 if source[i] == `\r` {
42 line++
43 column = 1
44 } else if source[i] == `\n` {
45 if i == 0 || source[i - 1] != `\r` {
46 line++
47 }
48 column = 1
49 } else {
50 column++
51 }
52 }
53 return line, column
54}
55
56fn assert_span_position(source string, span TokenSpan) {
57 line, column := reference_position(source, span.start)
58 assert span.line == line, 'offset ${span.start}, source ${source.bytes()}'
59 assert span.column == column, 'offset ${span.start}, source ${source.bytes()}'
60}
61
62fn test_comment_blocks_preserve_positions_and_eof_at_every_alignment() {
63 for padding in 0 .. 32 {
64 for length in [0, 1, 2, 14, 15, 16, 17, 30, 31, 32, 33, 63, 64, 65, 127, 128, 129] {
65 for body in ['x'.repeat(length), 'λ😀'.repeat(length), '\x00'.repeat(length)] {
66 for ending in ['', '\r', '\n', '\r\n', '\n\r'] {
67 prefix := ' '.repeat(padding) + ';' + body + ending
68 source := prefix + if ending == '' { '' } else { 'after' }
69 mut scanner := new_scanner(source) or { panic(err) }
70 span := scanner.next() or { panic(err) }
71 assert_span_position(source, span)
72 if ending == '' {
73 assert span.kind == .eof
74 assert span.start == source.len
75 } else {
76 assert span.kind == .identifier
77 assert span.start == prefix.len
78 assert span.end == source.len
79 }
80 eof := scanner.next() or { panic(err) }
81 assert eof.kind == .eof
82 assert eof.start == source.len
83 assert_span_position(source, eof)
84 }
85 }
86 }
87 }
88}
89
90fn test_string_blocks_handle_special_bytes_and_utf8_at_every_alignment() {
91 for padding in 0 .. 32 {
92 for length in [0, 1, 14, 15, 16, 17, 30, 31, 32, 33, 63, 64, 65, 127, 128, 129] {
93 for special in [r'\"', r'\\', '\r', '\n', '\r\n', '\n\r', 'λ😀', '\x00'] {
94 literal := '"' + 'a'.repeat(length) + special + 'b'.repeat(65 - length % 66) + '"'
95 source := ' '.repeat(padding) + literal + ' after'
96 mut scanner := new_scanner(source) or { panic(err) }
97 span := scanner.next() or { panic(err) }
98 assert span.kind == .string_literal
99 assert span.start == padding
100 assert span.end == padding + literal.len
101 assert span.text(source) == literal
102 assert_span_position(source, span)
103 after := scanner.next() or { panic(err) }
104 assert after.kind == .identifier
105 assert after.start == source.len - 5
106 assert after.end == source.len
107 assert_span_position(source, after)
108 eof := scanner.next() or { panic(err) }
109 assert eof.kind == .eof
110 assert eof.start == source.len
111 assert_span_position(source, eof)
112 }
113 }
114 }
115}
116
117fn test_string_block_errors_keep_exact_escape_and_opening_positions() {
118 for padding in 0 .. 32 {
119 for length in 0 .. 66 {
120 prefix := ' '.repeat(padding) + '"' + 'a'.repeat(length) + '\r\n' + 'b'.repeat(length)
121 for suffix in [r'\q"', r'\', ''] {
122 source := prefix + suffix
123 mut scanner := new_scanner(source) or { panic(err) }
124 if span := scanner.next() {
125 assert false, 'expected a string error, got ${span}'
126 } else {
127 assert err is LexError
128 if err is LexError {
129 expected := if suffix == r'\q"' { prefix.len } else { padding }
130 line, column := reference_position(source, expected)
131 assert err.offset == expected
132 assert err.line == line
133 assert err.column == column
134 }
135 }
136 }
137 }
138 }
139}
140
141fn test_ascii_validation_matches_utf8_validator_at_block_boundaries() {
142 for offset in 0 .. 66 {
143 for tail in 0 .. 18 {
144 for sequence in ['λ', '😀', '\x00', [u8(0x80)].bytestr(), [u8(0xff)].bytestr(),
145 [u8(0xc0), 0x80].bytestr(), [u8(0xed), 0xa0, 0x80].bytestr(),
146 [u8(0xf4), 0x90, 0x80, 0x80].bytestr(), [u8(0xe2), 0x82].bytestr()] {
147 source := 'a'.repeat(offset) + sequence + 'z'.repeat(tail)
148 expected := utf8.validate_str(source)
149 assert valid_source(source) == expected, '${source.bytes()}'
150 if _ := new_scanner(source) {
151 assert expected
152 } else {
153 assert !expected
154 assert err is LexError
155 if err is LexError {
156 assert err.offset == 0
157 assert err.line == 1
158 assert err.column == 1
159 }
160 }
161 }
162 }
163 }
164}
165
166fn test_validation_accepts_all_ascii_bytes_and_rejects_invalid_single_bytes() {
167 for offset in 0 .. 34 {
168 for byte in 0 .. 256 {
169 source := 'a'.repeat(offset) + [u8(byte)].bytestr() + 'z'.repeat(33)
170 assert valid_source(source) == (byte < 128), 'offset ${offset}, byte ${byte}'
171 }
172 }
173}
174
175fn test_validation_matches_utf8_for_deterministic_random_bytes() {
176 mut state := u32(0x31415926)
177 for length in 0 .. 258 {
178 mut bytes := []u8{len: length}
179 for _ in 0 .. 8 {
180 for i in 0 .. length {
181 state = state * 1664525 + 1013904223
182 bytes[i] = u8(state >> 24)
183 }
184 source := bytes.bytestr()
185 assert valid_source(source) == utf8.validate_str(source), '${bytes}'
186 }
187 }
188}
189