module scheme import lexer import reader // Cold checkpoint for bytes no longer in the input buffer. Short memory ports // never allocate it. Cursor users continue to advance only their byte position. struct InputLocation { mut: offset int line int = 1 column int = 1 previous_cr bool } // Locations count UTF-8 bytes, with CRLF/CRNEL counting as one line ending. // Compaction occurs at scalar boundaries; diagnostics may also name a byte // inside an invalid sequence, which is counted as an ordinary byte here. @[direct_array_access] fn (port &InputPort) input_location(end int) InputLocation { mut point := InputLocation{} if !isnil(port.location) { point = *port.location } mut i := 0 for i < end { b := port.buffer[i] mut width := 1 mut c := rune(b) if b == 0xc2 && i + 1 < end && port.buffer[i + 1] == 0x85 { c = 0x85 width = 2 } else if b == 0xe2 && i + 2 < end && port.buffer[i + 1] == 0x80 && port.buffer[i + 2] == 0xa8 { c = 0x2028 width = 3 } if c == `\n` || c == 0x85 { if !point.previous_cr { point.line++ } point.column = 1 point.previous_cr = false } else if c == `\r` || c == 0x2028 { point.line++ point.column = 1 point.previous_cr = c == `\r` } else { point.column += width point.previous_cr = false } i += width } point.offset += end return point } fn (mut port InputPort) checkpoint_input() { if port.binary || port.position == 0 { return } point := port.input_location(port.position) if isnil(port.location) { port.location = &InputLocation{} } unsafe { *port.location = point } } fn (port &InputPort) input_error(index int, message string) IError { point := port.input_location(index) return EvalError{ message: message, offset: point.offset, line: point.line, column: point.column } } fn (port &InputPort) absolute_input_error(err IError, start int) IError { if err is reader.ReadError { point := port.input_location(start + err.offset) return reader.ReadError{ message: err.message, offset: point.offset, line: point.line, column: point.column, incomplete: err.incomplete } } if err is lexer.LexError { index := if err.message == 'source is not valid UTF-8' { port.invalid_input_utf8(start, port.position) } else { start + err.offset } point := port.input_location(index) return lexer.LexError{ message: err.message, offset: point.offset, line: point.line, column: point.column, incomplete: err.incomplete } } if err is EvalError { return port.input_error(start + if err.line > 0 { err.offset } else { 0 }, err.message) } return err } // The source scanner validates UTF-8 once without storing its failed index. // Recover it only on this error path; quoted payloads keep their memchr path. @[direct_array_access] fn (port &InputPort) invalid_input_utf8(start int, end int) int { mut i := start for i < end { b := port.buffer[i] if b < 128 { i++ continue } width := if b >= 0xc2 && b <= 0xdf { 2 } else if b >= 0xe0 && b <= 0xef { 3 } else if b >= 0xf0 && b <= 0xf4 { 4 } else { return i } if width > end - i { return i } mut scalar := u32(b & u8((1 << (7 - width)) - 1)) for j in 1 .. width { next := port.buffer[i + j] if next & 0xc0 != 0x80 { return i } scalar = (scalar << 6) | u32(next & 0x3f) } if (width == 3 && scalar < 0x800) || (width == 4 && scalar < 0x10000) || scalar > 0x10ffff || (scalar >= 0xd800 && scalar <= 0xdfff) { return i } i += width } return start }