vile / lexer / extensions_run_test.v
132 lines · 126 sloc · 4.6 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module lexer
2
3// Independent scalar oracle: source is valid UTF8 and semantic codepoints,
4// rather than the native kernel's byte masks, choose each stopping position.
5fn expected_unicode_run(source string, start int, string_mode bool) int {
6 mut at := start
7 view := unsafe { (&char(source.str + start)).vstring_literal_with_len(source.len - start) }
8 for cp in view.runes() {
9 if cp in [`\r`, `\n`, rune(0x85), rune(0x2028)] || (string_mode && cp in [`"`, `\\`]) || (!string_mode && cp == 0x2029) {
10 return at
11 }
12 at += cp.str().len
13 }
14 return source.len
15}
16
17fn test_unicode_run_kernels_skip_ordinary_unicode_and_keep_every_line_boundary() {
18 prefixes := ['λ😀x', '¢€\u2000', '\u2029λ', 'ASCII']
19 for padding in 0 .. 32 {
20 for prefix in prefixes {
21 for ending in ['', '"', '\\', '\n', '\r', '\r\n', '\xC2\x85', '\r\xC2\x85', '\u2028',
22 '\u2029'] {
23 for tail in 0 .. 18 {
24 source := 'a'.repeat(padding) + prefix.repeat(5) + ending + 'b'.repeat(tail)
25 for start in [0, padding] {
26 assert string_run_end_unicode(source, start) == expected_unicode_run(source, start, true), '${padding}/${tail}: ${source.bytes()}'
27 assert comment_end_unicode(source, start) == expected_unicode_run(source, start, false)
28 }
29 }
30 }
31 }
32 }
33}
34
35fn test_fast_atmosphere_and_string_paths_keep_raw_columns_after_unicode_runs() {
36 for prefix in ['λ😀x', '¢€\u2000', '\u2029λ'] {
37 for ending in ['\n', '\r', '\r\n', '\xC2\x85', '\r\xC2\x85', '\u2028'] {
38 for padding in [0, 1, 14, 15, 16, 17, 31] {
39 source := '"' + 'a'.repeat(padding) + prefix + ending + 'z" kept'
40 mut s := new_scanner(source) or { panic(err.msg()) }
41 assert s.next() or { panic(err.msg()) }.kind == .string_literal
42 t := s.next() or { panic(err.msg()) }
43 assert t.line == 2 && t.column == 4
44 assert t.text(source) == 'kept'
45 }
46 }
47 }
48 mut s := new_scanner(';λ😀\u2029 x') or { panic(err.msg()) }
49 t := s.next() or { panic(err.msg()) }
50 assert t.text(';λ😀\u2029 x') == 'x'
51 assert t.line == 1 && t.column == 12
52}
53
54// A pending-escape state machine is independent of the native block masks.
55// It returns the slash that starts an extended/incomplete escape, or the first
56// unescaped quote/line ending, and never treats an escaped quote as a stop.
57fn expected_legacy_run(source string, start int) int {
58 mut pending := false
59 for at in start .. source.len {
60 c := source[at]
61 if pending {
62 if c !in [`"`, `\\`] { return at - 1 }
63 pending = false
64 } else if c == `\\` {
65 pending = true
66 } else if c in [`"`, `\r`, `\n`] {
67 return at
68 }
69 }
70 return if pending { source.len - 1 } else { source.len }
71}
72
73fn test_legacy_escape_run_all_slash_parities_lanes_tails_and_cold_transitions() {
74 for padding in 0 .. 32 {
75 for slashes in 0 .. 12 {
76 for ending in ['', '"', '\\', '\\q', '\\x', '\\x1;', '\r', '\n'] {
77 for tail in 0 .. 18 {
78 source := 'a'.repeat(padding) + '\\'.repeat(slashes) + ending + 'z'.repeat(tail)
79 for start in [0, padding] {
80 assert string_legacy_run_end(source, start) == expected_legacy_run(source, start), '${padding}/${slashes}/${tail}: ${source.bytes()}'
81 }
82 }
83 }
84 }
85 for unit in [r'a\\b\"', r'\\\"x', r'\"', r'\\'] {
86 for ending in ['"', '\\q', '\\x', '\r', '\n', '\\'] {
87 source := 'a'.repeat(padding) + unit.repeat(17) + ending
88 assert string_legacy_run_end(source, 0) == expected_legacy_run(source, 0)
89 }
90 }
91 }
92}
93
94fn test_fused_legacy_strings_keep_spans_columns_and_exact_malformed_offsets() {
95 for padding in 0 .. 32 {
96 prefix := 'a'.repeat(padding) + r'\\\"x'.repeat(19)
97 source := '"' + prefix + '" kept'
98 mut scanner := new_scanner(source) or { panic(err.msg()) }
99 t := scanner.next() or { panic(err.msg()) }
100 assert t.start == 0 && t.end == prefix.len + 2
101 assert scanner.token_flags() == 0
102 next := scanner.next() or { panic(err.msg()) }
103 assert next.start == prefix.len + 3 && next.column == prefix.len + 4
104 for suffix in [r'\q"', r'\x"', r'\xg;"', r'\x110000;"', r'\x1'] {
105 bad := '"' + prefix + suffix
106 mut failed := new_scanner(bad) or { panic(err.msg()) }
107 if _ := failed.next() {
108 assert false, bad
109 } else {
110 assert err is LexError
111 if err is LexError {
112 assert err.offset == prefix.len + 1
113 assert err.line == 1 && err.column == prefix.len + 2
114 assert err.incomplete == (suffix == r'\x1')
115 }
116 }
117 }
118 for slashes in [1, 3, 5] {
119 bad := '"' + prefix + '\\'.repeat(slashes)
120 mut failed := new_scanner(bad) or { panic(err.msg()) }
121 if _ := failed.next() {
122 assert false
123 } else {
124 assert err is LexError
125 if err is LexError {
126 assert err.offset == 0 && err.incomplete
127 }
128 }
129 assert failed.source_state().consumed == bad.len
130 }
131 }
132}
133