vile / scheme / port_location.v
118 lines · 111 sloc · 3.41 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import lexer
4import reader
5
6// Cold checkpoint for bytes no longer in the input buffer. Short memory ports
7// never allocate it. Cursor users continue to advance only their byte position.
8struct InputLocation {
9mut:
10 offset int
11 line int = 1
12 column int = 1
13 previous_cr bool
14}
15
16// Locations count UTF-8 bytes, with CRLF/CRNEL counting as one line ending.
17// Compaction occurs at scalar boundaries; diagnostics may also name a byte
18// inside an invalid sequence, which is counted as an ordinary byte here.
19@[direct_array_access]
20fn (port &InputPort) input_location(end int) InputLocation {
21 mut point := InputLocation{}
22 if !isnil(port.location) { point = *port.location }
23 mut i := 0
24 for i < end {
25 b := port.buffer[i]
26 mut width := 1
27 mut c := rune(b)
28 if b == 0xc2 && i + 1 < end && port.buffer[i + 1] == 0x85 {
29 c = 0x85
30 width = 2
31 } else if b == 0xe2 && i + 2 < end && port.buffer[i + 1] == 0x80 && port.buffer[i + 2] == 0xa8 {
32 c = 0x2028
33 width = 3
34 }
35 if c == `\n` || c == 0x85 {
36 if !point.previous_cr { point.line++ }
37 point.column = 1
38 point.previous_cr = false
39 } else if c == `\r` || c == 0x2028 {
40 point.line++
41 point.column = 1
42 point.previous_cr = c == `\r`
43 } else {
44 point.column += width
45 point.previous_cr = false
46 }
47 i += width
48 }
49 point.offset += end
50 return point
51}
52
53fn (mut port InputPort) checkpoint_input() {
54 if port.binary || port.position == 0 { return }
55 point := port.input_location(port.position)
56 if isnil(port.location) { port.location = &InputLocation{} }
57 unsafe { *port.location = point }
58}
59
60fn (port &InputPort) input_error(index int, message string) IError {
61 point := port.input_location(index)
62 return EvalError{ message: message, offset: point.offset, line: point.line, column: point.column }
63}
64
65fn (port &InputPort) absolute_input_error(err IError, start int) IError {
66 if err is reader.ReadError {
67 point := port.input_location(start + err.offset)
68 return reader.ReadError{ message: err.message, offset: point.offset, line: point.line, column: point.column, incomplete: err.incomplete }
69 }
70 if err is lexer.LexError {
71 index := if err.message == 'source is not valid UTF-8' {
72 port.invalid_input_utf8(start, port.position)
73 } else {
74 start + err.offset
75 }
76 point := port.input_location(index)
77 return lexer.LexError{ message: err.message, offset: point.offset, line: point.line, column: point.column, incomplete: err.incomplete }
78 }
79 if err is EvalError {
80 return port.input_error(start + if err.line > 0 { err.offset } else { 0 }, err.message)
81 }
82 return err
83}
84
85// The source scanner validates UTF-8 once without storing its failed index.
86// Recover it only on this error path; quoted payloads keep their memchr path.
87@[direct_array_access]
88fn (port &InputPort) invalid_input_utf8(start int, end int) int {
89 mut i := start
90 for i < end {
91 b := port.buffer[i]
92 if b < 128 {
93 i++
94 continue
95 }
96 width := if b >= 0xc2 && b <= 0xdf {
97 2
98 } else if b >= 0xe0 && b <= 0xef {
99 3
100 } else if b >= 0xf0 && b <= 0xf4 {
101 4
102 } else {
103 return i
104 }
105 if width > end - i { return i }
106 mut scalar := u32(b & u8((1 << (7 - width)) - 1))
107 for j in 1 .. width {
108 next := port.buffer[i + j]
109 if next & 0xc0 != 0x80 { return i }
110 scalar = (scalar << 6) | u32(next & 0x3f)
111 }
112 if (width == 3 && scalar < 0x800) || (width == 4 && scalar < 0x10000) || scalar > 0x10ffff || (scalar >= 0xd800 && scalar <= 0xdfff) {
113 return i
114 }
115 i += width
116 }
117 return start
118}
119