vile / lexer / extensions.v
340 lines · 316 sloc · 10.52 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module lexer
2
3import encoding.utf8
4import unicode17
5
6fn (s &Scanner) lexical_error(start int, line int, column int, message string, incomplete bool) LexError {
7 return LexError{ offset: start, line: line, column: column, message: message, incomplete: incomplete }
8}
9
10// Fast ASCII callers retain byte predicates; this path handles the stopping byte.
11@[inline]
12fn source_boundary(source string, pos int) bool {
13 if pos >= source.len { return true }
14 c := source[pos]
15 if is_delimiter(c) { return true }
16 if c == `#` && pos + 1 < source.len && source[pos + 1] in [`|`, `;`, `!`] { return true }
17 if c >= 128 { return unicode_source_boundary(source, pos) }
18 return false
19}
20
21// The caller has already rejected ordinary delimiters and EOF. Keep this
22// extension-only work away from the tiny validated ASCII token exits.
23@[noinline]
24fn extended_source_boundary(source string, pos int) bool {
25 c := source[pos]
26 if c == `#` && pos + 1 < source.len && source[pos + 1] in [`|`, `;`, `!`] { return true }
27 if c >= 128 { return unicode_source_boundary(source, pos) }
28 return false
29}
30
31@[noinline]
32fn unicode_source_boundary(source string, pos int) bool {
33 cp, _ := utf8.decode_rune_at(source, pos)
34 return unicode17.whitespace(cp)
35}
36
37fn line_ending_width(source string, pos int) int {
38 if pos >= source.len { return 0 }
39 c := source[pos]
40 if c == `\n` { return 1 }
41 if c == `\r` {
42 if pos + 1 < source.len && source[pos + 1] == `\n` { return 2 }
43 if pos + 2 < source.len && source[pos + 1] == 0xc2 && source[pos + 2] == 0x85 { return 3 }
44 return 1
45 }
46 if pos + 1 < source.len && c == 0xc2 && source[pos + 1] == 0x85 { return 2 }
47 if pos + 2 < source.len && c == 0xe2 && source[pos + 1] == 0x80 && source[pos + 2] == 0xa8 {
48 return 3
49 }
50 return 0
51}
52
53fn (mut s Scanner) skip_line_ending(width int) {
54 // A raw CR character literal consumes exactly one scalar, leaving its LF
55 // or NEL delimiter for atmosphere. That delimiter completes the same line.
56 trailing_cr := s.pos > 0 && s.source[s.pos - 1] == `\r` && s.source[s.pos] in [
57 `\n`,
58 u8(0xc2),
59 ]
60 s.pos += width
61 if !trailing_cr { s.line++ }
62 s.column = 1
63}
64
65fn (mut s Scanner) skip_scalar() {
66 width := line_ending_width(s.source, s.pos)
67 if width > 0 {
68 s.skip_line_ending(width)
69 return
70 }
71 _, size := utf8.decode_rune_at(s.source, s.pos)
72 s.skip_bytes(size)
73}
74
75fn (mut s Scanner) block_comment(start int, line int, column int) ! {
76 s.skip_bytes(2)
77 mut depth := 1
78 for !s.at_end() {
79 if s.pos + 1 < s.source.len {
80 if s.source[s.pos] == `#` && s.source[s.pos + 1] == `|` {
81 if depth == max_int {
82 return s.lexical_error(start, line, column, 'block comment nesting exceeds supported range', false)
83 }
84 depth++
85 s.skip_bytes(2)
86 continue
87 }
88 if s.source[s.pos] == `|` && s.source[s.pos + 1] == `#` {
89 depth--
90 s.skip_bytes(2)
91 if depth == 0 { return }
92 continue
93 }
94 }
95 s.skip_scalar()
96 }
97 return s.lexical_error(start, line, column, 'unterminated block comment', true)
98}
99
100fn (mut s Scanner) directive(start int, line int, column int) ! {
101 mut end := start + 2
102 for end < s.source.len && !source_boundary(s.source, end) { end++ }
103 // Special directive spelling ignores ASCII case independently of the
104 // current identifier-folding mode. It still needs its own delimiter.
105 if end < s.source.len && s.source[end] == `#` {
106 return s.lexical_error(start, line, column, 'reader directive needs a delimiter', false)
107 }
108 if matches_ascii_name(s.source, start, end, '#!fold-case') {
109 s.no_fold = false
110 } else if matches_ascii_name(s.source, start, end, '#!no-fold-case') {
111 s.no_fold = true
112 } else if !matches_ascii_name(s.source, start, end, '#!r6rs') {
113 return s.lexical_error(start, line, column, 'unknown reader directive', false)
114 }
115
116 s.skip_bytes(end - start)
117}
118
119fn hex_digit(c u8) int {
120 if c >= `0` && c <= `9` { return int(c - `0`) }
121 l := ascii_lower(c)
122 return if l >= `a` && l <= `f` { int(l - `a`) + 10 } else { -1 }
123}
124
125// Source UTF8 is already valid; escaped scalar arithmetic is checked separately.
126fn (mut s Scanner) hex_escape(start int, line int, column int, terminated bool) !rune {
127 first := s.pos
128 mut value := u32(0)
129 for s.pos < s.source.len {
130 digit := hex_digit(s.source[s.pos])
131 if digit < 0 { break }
132 if value > (u32(0x10ffff) - u32(digit)) / 16 {
133 return s.lexical_error(start, line, column, 'hex escape exceeds Unicode scalar range', false)
134 }
135 value = value * 16 + u32(digit)
136 s.skip_bytes(1)
137 }
138 if s.pos == first {
139 return s.lexical_error(start, line, column, 'hex escape needs a digit', s.at_end())
140 }
141 if terminated {
142 if s.at_end() {
143 return s.lexical_error(start, line, column, 'hex escape needs a terminating semicolon', true)
144 }
145 if s.source[s.pos] != `;` {
146 return s.lexical_error(start, line, column, 'hex escape needs a terminating semicolon', false)
147 }
148 s.skip_bytes(1)
149 }
150 if value >= 0xd800 && value <= 0xdfff {
151 return s.lexical_error(start, line, column, 'hex escape is a surrogate', false)
152 }
153 return rune(value)
154}
155
156fn mnemonic(c u8) rune {
157 return match c {
158 `a` { rune(7) }
159 `b` { rune(8) }
160 `t` { rune(9) }
161 `n` { rune(10) }
162 `v` { rune(11) }
163 `f` { rune(12) }
164 `r` { rune(13) }
165 `"` { rune(34) }
166 `\\` { rune(92) }
167 `|` { rune(124) }
168 else { rune(-1) }
169 }
170}
171
172// Cursor is immediately after the backslash. Simple quote/backslash stays in
173// the old hot path; this cold helper validates new escapes/line continuations.
174fn (mut s Scanner) extra_escape(start int, line int, column int, identifier bool) ! {
175 if s.at_end() { return s.lexical_error(start, line, column, 'unfinished escape', true) }
176 c := s.source[s.pos]
177 if c in [`x`, `X`] {
178 s.skip_bytes(1)
179 s.hex_escape(start, line, column, true)!
180 return
181 }
182 if mnemonic(c) != rune(-1) {
183 if identifier && c in [`"`, `\\`, `v`, `f`] {
184 return s.lexical_error(start, line, column, 'invalid escaped identifier character', false)
185 }
186 s.skip_bytes(1)
187 return
188 }
189 if identifier {
190 return s.lexical_error(start, line, column, 'invalid identifier escape', false)
191 }
192 for !s.at_end() {
193 cp, width := utf8.decode_rune_at(s.source, s.pos)
194 if !unicode17.intraline_space(cp) { break }
195 s.skip_bytes(width)
196 }
197 width := line_ending_width(s.source, s.pos)
198 if width == 0 {
199 return s.lexical_error(start, line, column, 'invalid string escape', s.at_end())
200 }
201 s.skip_line_ending(width)
202 for !s.at_end() {
203 cp, size := utf8.decode_rune_at(s.source, s.pos)
204 if !unicode17.intraline_space(cp) { break }
205 s.skip_bytes(size)
206 }
207}
208
209fn (mut s Scanner) barred_identifier(start int, line int, column int) ! {
210 s.flags |= token_identifier_barred
211 s.skip_bytes(1)
212 for !s.at_end() {
213 c := s.source[s.pos]
214 if c == `|` {
215 s.skip_bytes(1)
216 return
217 }
218 if c == `\\` {
219 s.flags |= token_identifier_escaped
220 position, l, col := s.pos, s.line, s.column
221 s.skip_bytes(1)
222 s.extra_escape(position, l, col, true)!
223 } else {
224 s.skip_scalar()
225 }
226 }
227 return s.lexical_error(start, line, column, 'unterminated vertical identifier', true)
228}
229
230fn (s &Scanner) initial_scalar(cp rune) bool {
231 return if cp < 128 { is_initial(u8(cp)) } else { unicode17.identifier_initial(cp) }
232}
233
234fn (s &Scanner) subsequent_scalar(cp rune) bool {
235 return if cp < 128 { is_subsequent(u8(cp)) } else { unicode17.identifier_subsequent(cp) }
236}
237
238// Called only after the original ASCII fast scan stops at an extension byte or
239// a peculiar identifier. It never allocates decoded spelling during scanning.
240fn (mut s Scanner) extended_identifier(start int, line int, column int) !TokenSpan {
241 s.pos = start
242 s.line = line
243 s.column = column
244 mut index := 0
245 mut peculiar := false
246 mut prefix := [3]rune{}
247 for !s.at_end() && !source_boundary(s.source, s.pos) {
248 if s.source[s.pos] == `#` { break }
249 mut cp := rune(0)
250 escaped := s.source[s.pos] == `\\`
251 if escaped {
252 s.flags |= token_identifier_escaped
253 x, l, col := s.pos, s.line, s.column
254 s.skip_bytes(1)
255 if s.at_end() || s.source[s.pos] !in [`x`, `X`] {
256 return s.lexical_error(x, l, col, 'identifier escapes must be hex scalars', s.at_end())
257 }
258 s.skip_bytes(1)
259 cp = s.hex_escape(x, l, col, true)!
260 } else {
261 c, w := utf8.decode_rune_at(s.source, s.pos)
262 cp = c
263 s.skip_bytes(w)
264 if c >= 128 { s.flags |= token_identifier_unicode }
265 }
266 if index < 3 { prefix[index] = cp }
267 if index == 0 {
268 peculiar = !escaped && cp in [`+`, `-`, `.`]
269 if !escaped && !peculiar && !s.initial_scalar(cp) {
270 return s.lexical_error(start, line, column, 'invalid identifier initial', false)
271 }
272 } else if !escaped && !s.subsequent_scalar(cp) {
273 return s.lexical_error(start, line, column, 'invalid identifier character', false)
274 }
275 index++
276 }
277 if index == 0 || (s.pos < s.source.len && !source_boundary(s.source, s.pos)) {
278 return s.lexical_error(start, line, column, 'invalid identifier token', false)
279 }
280 if peculiar && index > 1 {
281 second := prefix[1]
282 sign_subsequent := s.initial_scalar(second) || second in [`+`, `-`, `@`]
283 if prefix[0] == `.` {
284 if !sign_subsequent && second != `.` {
285 return s.lexical_error(start, line, column, 'invalid peculiar identifier', false)
286 }
287 } else if second == `.` {
288 if index < 3 || !(s.initial_scalar(prefix[2]) || prefix[2] in [`+`, `-`, `@`, `.`]) {
289 return s.lexical_error(start, line, column, 'invalid peculiar identifier', false)
290 }
291 } else if !sign_subsequent {
292 return s.lexical_error(start, line, column, 'invalid peculiar identifier', false)
293 }
294 }
295
296 return s.token(.identifier, start, line, column)
297}
298
299struct CharacterName {
300 name string
301 scalar rune
302}
303
304const character_names = [
305 CharacterName{'null', rune(0)},
306 CharacterName{'nul', rune(0)},
307 CharacterName{'alarm', rune(7)},
308 CharacterName{'backspace', rune(8)},
309 CharacterName{'tab', rune(9)},
310 CharacterName{'linefeed', rune(10)},
311 CharacterName{'newline', rune(10)},
312 CharacterName{'vtab', rune(11)},
313 CharacterName{'page', rune(12)},
314 CharacterName{'return', rune(13)},
315 CharacterName{'escape', rune(27)},
316 CharacterName{'esc', rune(27)},
317 CharacterName{'space', rune(32)},
318 CharacterName{'delete', rune(127)},
319]!
320
321fn character_name_value(source string, start int, end int, folding bool) rune {
322 for index in 0 .. character_names.len {
323 entry := &character_names[index]
324 if unicode17.matches_ascii(source, start, end, entry.name, folding) { return entry.scalar }
325 }
326 return rune(-1)
327}
328
329// Decodes a validated token without allocating or revalidating its spelling.
330pub fn character_value(source string, start int, end int, folding bool) rune {
331 first := start + 2
332 cp, width := utf8.decode_rune_at(source, first)
333 if end - first == width { return cp }
334 if cp in [`x`, `X`] {
335 mut value := u32(0)
336 for i in first + 1 .. end { value = value * 16 + u32(hex_digit(source[i])) }
337 return rune(value)
338 }
339 return character_name_value(source, first, end, folding)
340}
341