vile / scheme / port_frame.v
339 lines · 328 sloc · 9.53 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import lexer
4import reader
5import unicode17
6
7// This scanner finds boundaries only. The canonical lexer/reader validate each
8// completed frame once. No token list, growing-frame retries or copied payloads.
9enum InputFrameToken {
10 atom
11 open
12 close
13 prefix
14}
15
16@[direct_array_access]
17fn (mut port InputPort) input_boundary() !bool {
18 if !port.ensure_bytes(1)! { return true }
19 b := port.buffer[port.position]
20 if input_delimiter(b) || b == `|` { return true }
21 if b == `#` && port.ensure_bytes(2)! && port.buffer[port.position + 1] in [`|`, `;`, `!`] {
22 return true
23 }
24 if b >= 0x80 {
25 c, _ := port.peek_character()!
26 return unicode17.whitespace(c)
27 }
28 return false
29}
30
31// Delimiter searches visit each payload byte once, even with one-byte chunks.
32// Backslash parity is carried only across the current run at the chunk boundary.
33@[direct_array_access]
34fn (mut port InputPort) frame_quoted(delimiter u8) ! {
35 port.position++
36 mut escaped := false
37 for port.ensure_bytes(1)! {
38 start := port.position
39 quote := input_quote_end(port.buffer, start, delimiter)
40 mut slashes := quote
41 for slashes > start && port.buffer[slashes - 1] == u8(92) { slashes-- }
42 odd := ((quote - slashes) & 1) != 0
43 quoted := odd != (slashes == start && escaped)
44 port.position = quote
45 if quote == port.buffer.len {
46 escaped = quoted
47 continue
48 }
49 port.position++
50 escaped = false
51 if !quoted { return }
52 }
53}
54
55@[direct_array_access]
56fn (mut port InputPort) frame_word(escapes bool) ! {
57 for port.ensure_bytes(1)! {
58 port.position = input_event_end(port.buffer, port.position)
59 if port.position == port.buffer.len { continue }
60 if port.input_boundary()! { return }
61 b := port.buffer[port.position]
62 if escapes && b == u8(92) {
63 // An inline identifier hex escape owns its terminating semicolon.
64 port.position++
65 if port.ensure_bytes(1)! && port.buffer[port.position] in [`x`, `X`] {
66 port.position++
67 for port.ensure_bytes(1)! {
68 next := port.buffer[port.position]
69 if next == `;` {
70 port.position++
71 break
72 }
73 if input_delimiter(next) || next == `|` { break }
74 port.position++
75 }
76 }
77 } else if b >= 0x80 {
78 _, width := port.peek_character()!
79 port.position += width
80 } else {
81 port.position++
82 }
83 }
84}
85
86@[direct_array_access]
87fn (mut port InputPort) frame_token() !InputFrameToken {
88 first := port.buffer[port.position]
89 if first == `"` || first == `|` {
90 port.frame_quoted(first)!
91 return .atom
92 }
93 if first == `(` {
94 port.position++
95 return .open
96 }
97 if first == `)` {
98 port.position++
99 return .close
100 }
101 if first in [`'`, `\x60`, `,`] {
102 port.position++
103 if first == `,` && port.ensure_bytes(1)! && port.buffer[port.position] == `@` {
104 port.position++
105 }
106 return .prefix
107 }
108 if first == `#` && port.ensure_bytes(2)! {
109 second := port.buffer[port.position + 1]
110 if second in [`'`, 0x60, `,`, `:`] {
111 port.position += 2
112 if second == `,` && port.ensure_bytes(1)! && port.buffer[port.position] == `@` {
113 port.position++
114 }
115 return .prefix
116 }
117 if second == `(` {
118 port.position += 2
119 return .open
120 }
121 if second in [`u`, `U`, `v`, `V`] {
122 length := if second in [`u`, `U`] { 4 } else { 5 }
123 if port.ensure_bytes(length)! {
124 at := port.position
125 if (length == 4 || port.buffer[at + 2] in [`u`, `U`]) && port.buffer[at + length - 2] == `8` && port.buffer[at + length - 1] == `(` {
126 port.position += length
127 return .open
128 }
129 }
130 }
131 if second in [`t`, `T`, `f`, `F`] {
132 // Short forms are self-terminating, but may begin a long form.
133 // Compare only the bounded alias; on mismatch leave all lookahead.
134 word := if second in [`t`, `T`] { '#true' } else { '#false' }
135 mut full := true
136 for i in 2 .. word.len {
137 if !port.ensure_bytes(i + 1)! || port.buffer[port.position + i] | 0x20 != word[i] {
138 full = false
139 break
140 }
141 }
142 port.position += if full { word.len } else { 2 }
143 if full && !port.input_boundary()! { port.frame_word(false)! }
144 return .atom
145 }
146 if second >= `0` && second <= `9` {
147 port.position += 2
148 for port.ensure_bytes(1)! && port.buffer[port.position] >= `0` && port.buffer[port.position] <= `9` {
149 port.position++
150 }
151 if port.ensure_bytes(1)! && port.buffer[port.position] in [`=`, `#`] {
152 definition := port.buffer[port.position] == `=`
153 port.position++
154 return if definition { .prefix } else { .atom }
155 }
156 port.frame_word(false)!
157 return .atom
158 }
159 if second == u8(92) {
160 port.position += 2
161 _, width := port.peek_character()!
162 port.position += width
163 port.frame_word(false)!
164 return .atom
165 }
166 }
167 port.frame_word(true)!
168 return .atom
169}
170
171@[direct_array_access]
172fn (mut port InputPort) consume_input_directive() ! {
173 old_mark := port.mark
174 port.mark = if old_mark < 0 { port.position } else { old_mark }
175 start := port.position
176 defer { port.mark = old_mark }
177 port.position += 2
178 port.frame_word(false)!
179 source := unsafe { tos(&u8(port.buffer.data) + start, port.position - start) }
180 mut scanner := lexer.new_scanner(source, fold_case: !port.no_fold) or { return port.absolute_input_error(err, start) }
181 scanner.next() or { return port.absolute_input_error(err, start) }
182 port.no_fold = !scanner.fold_case()
183}
184
185// Line/block comments and whitespace are not pinned, keeping trivia-only input
186// bounded. Quoted payloads and #; packets are validated by the canonical reader.
187@[direct_array_access]
188fn (mut port InputPort) skip_input_atmosphere() !bool {
189 for port.ensure_bytes(1)! {
190 b := port.buffer[port.position]
191 if input_space(b) {
192 port.position++
193 continue
194 }
195 if b >= 0x80 {
196 c, width := port.peek_character()!
197 if unicode17.whitespace(c) {
198 port.position += width
199 continue
200 }
201 }
202 if b == `;` {
203 port.position++
204 for {
205 c, width := port.peek_character()!
206 if width == 0 { return false }
207 port.position += width
208 if c in [`\r`, `\n`, rune(0x85), rune(0x2028), rune(0x2029)] { break }
209 }
210 continue
211 }
212 if b == `#` && port.ensure_bytes(2)! {
213 next := port.buffer[port.position + 1]
214 if next == `!` {
215 port.consume_input_directive()!
216 continue
217 }
218 if next == `|` {
219 port.skip_input_block()!
220 continue
221 }
222 }
223 return true
224 }
225 return false
226}
227
228// The bounded stack exists only for compounds, prefixes and #; packets.
229// Pending comment counts are per context: #;#;a b discards two independent
230// datums without consuming nesting slots or leaking their label namespaces.
231@[direct_array_access]
232fn (mut port InputPort) frame_compound(comments_only bool) ! {
233 mut kinds := [reader.max_depth + 2]bool{} // true = prefix, false = container/root
234 mut ignored := [reader.max_depth + 2]int{}
235 mut level := 0
236 for port.skip_input_atmosphere()! {
237 if port.buffer[port.position] == `#` && port.ensure_bytes(2)! && port.buffer[port.position + 1] == `;` {
238 if ignored[level] == max_int {
239 return port.input_error(port.position, 'datum comment count exceeds supported range')
240 }
241 ignored[level]++
242 port.position += 2
243 continue
244 }
245 token := port.frame_token()!
246 if token == .open || token == .prefix {
247 level++
248 if level > reader.max_depth + 1 {
249 return port.input_error(port.position, 'input datum nesting limit exceeded')
250 }
251 kinds[level] = token == .prefix
252 ignored[level] = 0
253 continue
254 }
255 if token == .close {
256 if level == 0 || kinds[level] { return }
257 level--
258 }
259 for {
260 if ignored[level] > 0 {
261 ignored[level]--
262 if comments_only && level == 0 && ignored[0] == 0 { return }
263 break
264 }
265 if level == 0 { return }
266 if !kinds[level] { break }
267 level--
268 }
269 }
270}
271
272@[direct_array_access]
273fn (mut port InputPort) frame_datum() !(int, int, bool, bool) {
274 for port.skip_input_atmosphere()! {
275 port.mark = port.position
276 start := port.position
277 initial_fold := !port.no_fold
278 first := port.buffer[port.position]
279 second := if first == `#` && port.ensure_bytes(2)! {
280 port.buffer[port.position + 1]
281 } else {
282 u8(0)
283 }
284 if first == `#` && second == `;` {
285 port.frame_compound(true)!
286 source := unsafe { tos(&u8(port.buffer.data) + start, port.position - start) }
287 doc := reader.read_all(source, fold_case: initial_fold) or { return port.absolute_input_error(err, start) }
288 port.no_fold = !doc.final_fold_case()
289 port.mark = -1
290 port.release_large_frame()
291 continue
292 }
293 if first in [`(`, `)`, `'`, `\x60`, `,`] || (first == `#` && (second in [`(`, `'`, 0x60,
294 `,`, `:`, `u`, `U`, `v`, `V`] || (second >= `0` && second <= `9`))) {
295 port.frame_compound(false)!
296 } else {
297 port.frame_token()!
298 }
299 return start, port.position, initial_fold, true
300 }
301 return 0, 0, !port.no_fold, false
302}
303
304// Capture an opening location only before the first possible compaction, or
305// on failure. In-memory sequences of many comments never rescan their prefixes.
306@[direct_array_access]
307fn (mut port InputPort) skip_input_block() ! {
308 opening_index := port.position
309 mut opening := InputLocation{}
310 mut saved := false
311 port.position += 2
312 mut depth := 1
313 for depth > 0 {
314 if !saved && port.mark < 0 && !port.ended && port.buffer.len - port.position < 4 {
315 opening = port.input_location(opening_index)
316 saved = true
317 }
318 if !port.ensure_bytes(1)! {
319 if !saved { opening = port.input_location(opening_index) }
320 return lexer.LexError{ message: 'unterminated block comment', offset: opening.offset, line: opening.line, column: opening.column, incomplete: true }
321 }
322 c := port.buffer[port.position]
323 if c in [`#`, `|`] && port.ensure_bytes(2)! {
324 d := port.buffer[port.position + 1]
325 if c == `#` && d == `|` {
326 depth++
327 port.position += 2
328 continue
329 }
330 if c == `|` && d == `#` {
331 depth--
332 port.position += 2
333 continue
334 }
335 }
336 _, width := port.peek_character()!
337 port.position += width
338 }
339}
340