module scheme import lexer import reader import unicode17 // This scanner finds boundaries only. The canonical lexer/reader validate each // completed frame once. No token list, growing-frame retries or copied payloads. enum InputFrameToken { atom open close prefix } @[direct_array_access] fn (mut port InputPort) input_boundary() !bool { if !port.ensure_bytes(1)! { return true } b := port.buffer[port.position] if input_delimiter(b) || b == `|` { return true } if b == `#` && port.ensure_bytes(2)! && port.buffer[port.position + 1] in [`|`, `;`, `!`] { return true } if b >= 0x80 { c, _ := port.peek_character()! return unicode17.whitespace(c) } return false } // Delimiter searches visit each payload byte once, even with one-byte chunks. // Backslash parity is carried only across the current run at the chunk boundary. @[direct_array_access] fn (mut port InputPort) frame_quoted(delimiter u8) ! { port.position++ mut escaped := false for port.ensure_bytes(1)! { start := port.position quote := input_quote_end(port.buffer, start, delimiter) mut slashes := quote for slashes > start && port.buffer[slashes - 1] == u8(92) { slashes-- } odd := ((quote - slashes) & 1) != 0 quoted := odd != (slashes == start && escaped) port.position = quote if quote == port.buffer.len { escaped = quoted continue } port.position++ escaped = false if !quoted { return } } } @[direct_array_access] fn (mut port InputPort) frame_word(escapes bool) ! { for port.ensure_bytes(1)! { port.position = input_event_end(port.buffer, port.position) if port.position == port.buffer.len { continue } if port.input_boundary()! { return } b := port.buffer[port.position] if escapes && b == u8(92) { // An inline identifier hex escape owns its terminating semicolon. port.position++ if port.ensure_bytes(1)! && port.buffer[port.position] in [`x`, `X`] { port.position++ for port.ensure_bytes(1)! { next := port.buffer[port.position] if next == `;` { port.position++ break } if input_delimiter(next) || next == `|` { break } port.position++ } } } else if b >= 0x80 { _, width := port.peek_character()! port.position += width } else { port.position++ } } } @[direct_array_access] fn (mut port InputPort) frame_token() !InputFrameToken { first := port.buffer[port.position] if first == `"` || first == `|` { port.frame_quoted(first)! return .atom } if first == `(` { port.position++ return .open } if first == `)` { port.position++ return .close } if first in [`'`, `\x60`, `,`] { port.position++ if first == `,` && port.ensure_bytes(1)! && port.buffer[port.position] == `@` { port.position++ } return .prefix } if first == `#` && port.ensure_bytes(2)! { second := port.buffer[port.position + 1] if second in [`'`, 0x60, `,`, `:`] { port.position += 2 if second == `,` && port.ensure_bytes(1)! && port.buffer[port.position] == `@` { port.position++ } return .prefix } if second == `(` { port.position += 2 return .open } if second in [`u`, `U`, `v`, `V`] { length := if second in [`u`, `U`] { 4 } else { 5 } if port.ensure_bytes(length)! { at := port.position if (length == 4 || port.buffer[at + 2] in [`u`, `U`]) && port.buffer[at + length - 2] == `8` && port.buffer[at + length - 1] == `(` { port.position += length return .open } } } if second in [`t`, `T`, `f`, `F`] { // Short forms are self-terminating, but may begin a long form. // Compare only the bounded alias; on mismatch leave all lookahead. word := if second in [`t`, `T`] { '#true' } else { '#false' } mut full := true for i in 2 .. word.len { if !port.ensure_bytes(i + 1)! || port.buffer[port.position + i] | 0x20 != word[i] { full = false break } } port.position += if full { word.len } else { 2 } if full && !port.input_boundary()! { port.frame_word(false)! } return .atom } if second >= `0` && second <= `9` { port.position += 2 for port.ensure_bytes(1)! && port.buffer[port.position] >= `0` && port.buffer[port.position] <= `9` { port.position++ } if port.ensure_bytes(1)! && port.buffer[port.position] in [`=`, `#`] { definition := port.buffer[port.position] == `=` port.position++ return if definition { .prefix } else { .atom } } port.frame_word(false)! return .atom } if second == u8(92) { port.position += 2 _, width := port.peek_character()! port.position += width port.frame_word(false)! return .atom } } port.frame_word(true)! return .atom } @[direct_array_access] fn (mut port InputPort) consume_input_directive() ! { old_mark := port.mark port.mark = if old_mark < 0 { port.position } else { old_mark } start := port.position defer { port.mark = old_mark } port.position += 2 port.frame_word(false)! source := unsafe { tos(&u8(port.buffer.data) + start, port.position - start) } mut scanner := lexer.new_scanner(source, fold_case: !port.no_fold) or { return port.absolute_input_error(err, start) } scanner.next() or { return port.absolute_input_error(err, start) } port.no_fold = !scanner.fold_case() } // Line/block comments and whitespace are not pinned, keeping trivia-only input // bounded. Quoted payloads and #; packets are validated by the canonical reader. @[direct_array_access] fn (mut port InputPort) skip_input_atmosphere() !bool { for port.ensure_bytes(1)! { b := port.buffer[port.position] if input_space(b) { port.position++ continue } if b >= 0x80 { c, width := port.peek_character()! if unicode17.whitespace(c) { port.position += width continue } } if b == `;` { port.position++ for { c, width := port.peek_character()! if width == 0 { return false } port.position += width if c in [`\r`, `\n`, rune(0x85), rune(0x2028), rune(0x2029)] { break } } continue } if b == `#` && port.ensure_bytes(2)! { next := port.buffer[port.position + 1] if next == `!` { port.consume_input_directive()! continue } if next == `|` { port.skip_input_block()! continue } } return true } return false } // The bounded stack exists only for compounds, prefixes and #; packets. // Pending comment counts are per context: #;#;a b discards two independent // datums without consuming nesting slots or leaking their label namespaces. @[direct_array_access] fn (mut port InputPort) frame_compound(comments_only bool) ! { mut kinds := [reader.max_depth + 2]bool{} // true = prefix, false = container/root mut ignored := [reader.max_depth + 2]int{} mut level := 0 for port.skip_input_atmosphere()! { if port.buffer[port.position] == `#` && port.ensure_bytes(2)! && port.buffer[port.position + 1] == `;` { if ignored[level] == max_int { return port.input_error(port.position, 'datum comment count exceeds supported range') } ignored[level]++ port.position += 2 continue } token := port.frame_token()! if token == .open || token == .prefix { level++ if level > reader.max_depth + 1 { return port.input_error(port.position, 'input datum nesting limit exceeded') } kinds[level] = token == .prefix ignored[level] = 0 continue } if token == .close { if level == 0 || kinds[level] { return } level-- } for { if ignored[level] > 0 { ignored[level]-- if comments_only && level == 0 && ignored[0] == 0 { return } break } if level == 0 { return } if !kinds[level] { break } level-- } } } @[direct_array_access] fn (mut port InputPort) frame_datum() !(int, int, bool, bool) { for port.skip_input_atmosphere()! { port.mark = port.position start := port.position initial_fold := !port.no_fold first := port.buffer[port.position] second := if first == `#` && port.ensure_bytes(2)! { port.buffer[port.position + 1] } else { u8(0) } if first == `#` && second == `;` { port.frame_compound(true)! source := unsafe { tos(&u8(port.buffer.data) + start, port.position - start) } doc := reader.read_all(source, fold_case: initial_fold) or { return port.absolute_input_error(err, start) } port.no_fold = !doc.final_fold_case() port.mark = -1 port.release_large_frame() continue } if first in [`(`, `)`, `'`, `\x60`, `,`] || (first == `#` && (second in [`(`, `'`, 0x60, `,`, `:`, `u`, `U`, `v`, `V`] || (second >= `0` && second <= `9`))) { port.frame_compound(false)! } else { port.frame_token()! } return start, port.position, initial_fold, true } return 0, 0, !port.no_fold, false } // Capture an opening location only before the first possible compaction, or // on failure. In-memory sequences of many comments never rescan their prefixes. @[direct_array_access] fn (mut port InputPort) skip_input_block() ! { opening_index := port.position mut opening := InputLocation{} mut saved := false port.position += 2 mut depth := 1 for depth > 0 { if !saved && port.mark < 0 && !port.ended && port.buffer.len - port.position < 4 { opening = port.input_location(opening_index) saved = true } if !port.ensure_bytes(1)! { if !saved { opening = port.input_location(opening_index) } return lexer.LexError{ message: 'unterminated block comment', offset: opening.offset, line: opening.line, column: opening.column, incomplete: true } } c := port.buffer[port.position] if c in [`#`, `|`] && port.ensure_bytes(2)! { d := port.buffer[port.position + 1] if c == `#` && d == `|` { depth++ port.position += 2 continue } if c == `|` && d == `#` { depth-- port.position += 2 continue } } _, width := port.peek_character()! port.position += width } }