vile / lexer / alt.v
315 lines · 301 sloc · 10.16 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module lexer
2
3// AltToken uses Scheme's token vocabulary, with separate flags for brackets
4// and infix operators. Offsets always refer to the original source.
5pub struct AltToken {
6pub:
7 span TokenSpan
8 bracket bool
9 brace bool
10 operator bool
11}
12
13pub fn (t AltToken) text(source string) string {
14 if t.bracket || t.brace { return single_byte_spelling(source[t.span.start]) }
15 return t.span.text(source)
16}
17
18// This scanner never adds branches to the ordinary Scheme scanner.
19pub struct AltScanner {
20mut:
21 scanner Scanner
22 operand bool = true
23 groups int
24 bytevector_depth int
25 // Parenthesis depth outside each block. At a block's own level, newlines
26 // begin statements; inside its calls/parentheses they remain whitespace.
27 block_groups []int
28}
29
30pub fn new_alt_scanner(source string, options ScannerOptions) !AltScanner {
31 return AltScanner{ scanner: new_scanner(source, options)! }
32}
33
34pub fn (mut a AltScanner) next() !AltToken {
35 token := a.scan()!
36 if token.brace {
37 if token.span.kind == .lparen {
38 a.block_groups << a.groups
39 a.groups = 0
40 } else if a.block_groups.len > 0 {
41 a.groups = a.block_groups.pop()
42 }
43 } else {
44 match token.span.kind {
45 .lparen, .vector_start, .bytevector_start {
46 a.groups++
47 if token.span.kind == .bytevector_start || a.bytevector_depth > 0 {
48 a.bytevector_depth++
49 }
50 }
51 .rparen {
52 if a.groups > 0 { a.groups-- }
53 if a.bytevector_depth > 0 { a.bytevector_depth-- }
54 }
55 else {}
56 }
57 }
58 a.operand = token.operator || token.span.kind in [.lparen, .vector_start, .bytevector_start,
59 .datum_comment, .keyword_prefix, .label_definition, .quote, .quasiquote, .unquote,
60 .unquote_splicing, .syntax_quote, .syntax_quasiquote, .syntax_unquote,
61 .syntax_unquote_splicing, .dot]
62 return token
63}
64
65fn (mut a AltScanner) scan() !AltToken {
66 previous_end := a.scanner.pos
67 if a.scanner.skip_plain_atmosphere() { a.scanner.skip_atmosphere()! }
68 a.scanner.flags = if a.scanner.no_fold { token_preserve_case } else { u8(0) }
69 start := a.scanner.pos
70 if a.groups == 0 {
71 for i := previous_end; i < start; i++ {
72 if line_ending_width(a.scanner.source, i) > 0 {
73 a.operand = true
74 break
75 }
76 }
77 }
78 line := a.scanner.line
79 column := a.scanner.column
80 if a.scanner.at_end() { return AltToken{ span: a.scanner.token(.eof, start, line, column) } }
81 source := a.scanner.source
82 c := source[start]
83 if (c == `|` && (start + 1 >= source.len || source[start + 1] != `|`)) || (c == `#` && start + 1 < source.len && source[start + 1] in [
84 `;`,
85 `:`,
86 `u`,
87 `U`,
88 `v`,
89 `V`,
90 `0`,
91 `1`,
92 `2`,
93 `3`,
94 `4`,
95 `5`,
96 `6`,
97 `7`,
98 `8`,
99 `9`,
100 ]) {
101 return AltToken{ span: a.scanner.next()! }
102 }
103 if c in [`[`, `]`] {
104 a.scanner.skip_bytes(1)
105 return AltToken{ span: a.scanner.token(if c == `[` { .lparen } else { .rparen }, start, line, column), bracket: true }
106 }
107 if c in [`{`, `}`] {
108 a.scanner.skip_bytes(1)
109 return AltToken{ span: a.scanner.token(if c == `{` { .lparen } else { .rparen }, start, line, column), brace: true }
110 }
111 if c in [`(`, `)`, `'`, 0x60, `,`, `"`] || (c == `#` && start + 1 < source.len && source[start + 1] in [
112 `(`,
113 `'`,
114 0x60,
115 `,`,
116 `t`,
117 `T`,
118 `f`,
119 `F`,
120 ]) {
121 return AltToken{ span: a.scanner.next()! }
122 }
123 if c == `#` && start + 1 < source.len && source[start + 1] == `\\` {
124 // Bound the existing character validator at alt punctuation, retaining
125 // the first scalar even when it is itself a comma or bracket.
126 mut end := start + 2
127 if end < source.len {
128 b := source[end]
129 end += if b < 0x80 {
130 1
131 } else if b < 0xe0 {
132 2
133 } else if b < 0xf0 {
134 3
135 } else {
136 4
137 }
138 for end < source.len && !alt_boundary(source[end]) && !source_boundary(source, end) && !alt_operator_at(source, end) {
139 end++
140 }
141 }
142 view := unsafe { (&char(source.str)).vstring_literal_with_len(end) }
143 mut character := Scanner{ source: view, pos: start, line: line, column: column, no_fold: a.scanner.no_fold }
144 character.scan_character(start, line, column)!
145 a.scanner.pos = character.pos
146 a.scanner.line = character.line
147 a.scanner.column = character.column
148 return AltToken{ span: a.scanner.token(.character, start, line, column) }
149 }
150 if (c >= `0` && c <= `9`) || c in [`.`, `#`] || ((a.operand || a.bytevector_depth > 0) && c in [
151 `+`,
152 `-`,
153 ] && start + 1 < source.len && ((source[start + 1] >= `0` && source[start + 1] <= `9`) || source[start + 1] in [
154 `.`,
155 `i`,
156 `I`,
157 `n`,
158 `N`,
159 ])) {
160 end := alt_number_end(source, start, a.bytevector_depth > 0)
161 if end > start && (end == source.len || alt_boundary(source[end]) || source_boundary(source, end) || alt_operator_at(source, end)) {
162 a.scanner.skip_bytes(end - start)
163 return AltToken{ span: a.scanner.token(.number, start, line, column) }
164 }
165 }
166 if alt_operator_at(source, start) {
167 if c == `=` && start + 1 < source.len && source[start + 1] == `>` {
168 a.scanner.skip_bytes(2)
169 return AltToken{ span: a.scanner.token(.identifier, start, line, column) }
170 }
171 mut count := 1
172 if start + 1 < source.len && ((c in [`:`, `<`, `>`, `=`, `!`] && source[start + 1] == `=`) || (c in [
173 `&`,
174 `|`,
175 ] && source[start + 1] == c)) {
176 count = 2
177 }
178 if c in [`&`, `|`] && count == 1 {
179 return LexError{ offset: start, line: line, column: column, message: 'expected a doubled logical operator' }
180 }
181 a.scanner.skip_bytes(count)
182 return AltToken{ span: a.scanner.token(.identifier, start, line, column), operator: true }
183 }
184 mut end := start
185 for end < source.len && !alt_boundary(source[end]) && !source_boundary(source, end) {
186 if source[end] == `\\` && end + 1 < source.len && source[end + 1] in [`x`, `X`] {
187 end += 2
188 for end < source.len && source[end] != `;` { end++ }
189 if end < source.len { end++ }
190 continue
191 }
192 // Preserve Scheme hyphens, -> names and suffix !. Other operators
193 // delimit identifiers, permitting a+b and a*b without spaces.
194 if alt_operator_at(source, end) && !alt_name_punctuation(source, start, end) { break }
195 end++
196 }
197 mut kind := TokenKind.identifier
198 mut valid := end > start && is_initial(c)
199 for i := start + 1; i < end && valid; i++ { valid = is_subsequent(source[i]) }
200 if end - start == 1 && c == `.` {
201 kind = .dot
202 valid = true
203 } else if end - start == 3 && source[start] == `.` && source[start + 1] == `.` && source[start + 2] == `.` {
204 valid = true
205 }
206 if !valid && end > start && (c >= 128 || c in [`\\`, `.`] || is_initial(c)) {
207 view := unsafe { (&char(source.str)).vstring_literal_with_len(end) }
208 mut extended := Scanner{ source: view, pos: start, line: line, column: column, no_fold: a.scanner.no_fold, flags: a.scanner.flags }
209 span := extended.extended_identifier(start, line, column)!
210 a.scanner.pos = extended.pos
211 a.scanner.column = extended.column
212 a.scanner.flags = extended.flags
213 return AltToken{ span: span }
214 }
215 if !valid {
216 if end == start { end++ }
217 return LexError{ offset: start, line: line, column: column, message: 'invalid alt token `${spelling_view(source, start, end)}`' }
218 }
219 a.scanner.skip_bytes(end - start)
220 return AltToken{ span: a.scanner.token(kind, start, line, column) }
221}
222
223fn alt_boundary(c u8) bool {
224 return is_delimiter(c) || c in [`[`, `]`, `{`, `}`, `,`, `'`, 0x60]
225}
226
227fn alt_operator(c u8) bool {
228 return c in [`+`, `-`, `*`, `/`, `%`, `<`, `>`, `=`, `!`, `&`, `|`]
229}
230
231fn alt_operator_at(source string, at int) bool {
232 return alt_operator(source[at]) || (source[at] == `:` && at + 1 < source.len && source[at + 1] == `=`)
233}
234
235fn alt_name_punctuation(source string, start int, at int) bool {
236 if at == start { return false }
237 c := source[at]
238 if c == `-` || (c == `>` && source[at - 1] == `-`) { return true }
239 if c == `!` && (at + 1 == source.len || source[at + 1] != `=`) { return true }
240 if c in [`<`, `>`, `=`] {
241 if at + 1 < source.len && source[at + 1] == `?` { return true }
242 if at + 2 < source.len && source[at + 1] == `=` && source[at + 2] == `?` { return true }
243 }
244 // These standard names would otherwise conflict with arithmetic tokens.
245 if c == `*` && ((at - start == 3 && matches_ascii_name(source, start, at, 'let'))
246 || (at - start == 6 && matches_ascii_name(source, start, at, 'letrec'))) {
247 return true
248 }
249 if c == `/` && at - start == 4 && matches_ascii_name(source, start, at, 'call') {
250 end := at + 3
251 return end <= source.len && matches_ascii_name(source, at + 1, end, 'cc')
252 && (end == source.len || alt_boundary(source[end]) || extended_source_boundary(source, end))
253 }
254 return false
255}
256
257// Probe at most two real components using the existing numeric grammar. A long
258// chain such as 1+2+3 must not rescan its remaining suffix at every token.
259fn alt_number_end(source string, start int, literal bool) int {
260 mut pos := start
261 mut radix := 10
262 mut seen_radix := false
263 mut seen_exactness := false
264 for pos + 1 < source.len && source[pos] == `#` {
265 prefix := ascii_lower(source[pos + 1])
266 match prefix {
267 `b`, `o`, `d`, `x` {
268 if seen_radix { return start }
269 seen_radix = true
270 radix = match prefix {
271 `b` { 2 }
272 `o` { 8 }
273 `x` { 16 }
274 else { 10 }
275 }
276 }
277 `e`, `i` {
278 if seen_exactness { return start }
279 seen_exactness = true
280 }
281 else { return start }
282 }
283 pos += 2
284 }
285 mut n := NumberScanner{ text: source, radix: radix, pos: pos, end: source.len }
286 has_sign := n.take(`+`) || n.take(`-`)
287 if has_sign && alt_unit_imaginary(source, n.pos) { return n.pos + 1 }
288 if !n.unsigned_real() { return start }
289 real_end := n.pos
290 if has_sign && n.take(`i`) { return n.pos }
291 if n.take(`@`) {
292 if n.real() { return n.pos }
293 return start
294 }
295 if n.take(`+`) || n.take(`-`) {
296 if alt_unit_imaginary(source, n.pos) { return n.pos + 1 }
297 if n.unsigned_real() && n.take(`i`) { return n.pos }
298 }
299 // Real division is infix unless a # prefix explicitly introduces a
300 // Scheme number. Complex fractional components remain one number.
301 if !literal && source[start] != `#` {
302 for i := pos; i < real_end; i++ { if source[i] == `/` { return i } }
303 }
304 return real_end
305}
306
307pub fn (a &AltScanner) token_flags() u8 { return a.scanner.token_flags() }
308
309pub fn (a &AltScanner) fold_case() bool { return a.scanner.fold_case() }
310
311fn alt_unit_imaginary(source string, pos int) bool {
312 if pos >= source.len || ascii_lower(source[pos]) != `i` { return false }
313 next := pos + 1
314 return next == source.len || alt_boundary(source[next]) || source_boundary(source, next) || alt_operator_at(source, next)
315}
316