module scheme import encoding.utf8 import strings import unicode17 struct ScalarPrintState { mut: preserve_case bool plan &GoopsPrintPlan = unsafe { nil } } // Use the stable, unambiguous ASCII identifier subset for bare output. Other // names use bars rather than invoking a fallible lexer just to choose quoting. // The reader remains the owner of the complete identifier/number grammar. fn symbol_initial_byte(byte u8) bool { return (byte >= `a` && byte <= `z`) || (byte >= `A` && byte <= `Z`) || byte in [`!`, `$`, `%`, `&`, `*`, `/`, `:`, `<`, `=`, `>`, `?`, `^`, `_`, `~`, `@`] } fn symbol_print_flags(text string) (bool, bool) { mut bare := text.len > 0 && symbol_initial_byte(text[0]) mut preserve := false mut offset := 0 for offset < text.len { byte := text[offset] if byte < 128 { if offset > 0 && !symbol_initial_byte(byte) && !(byte >= `0` && byte <= `9`) && byte !in [`+`, `-`, `.`] { bare = false } if byte >= `A` && byte <= `Z` { preserve = true } offset++ } else { bare = false cp, width := utf8.decode_rune_at(text, offset) folded, _, _, count := unicode17.fold(cp) if count != 1 || folded != i32(cp) { preserve = true } offset += width } } return bare || text in ['+', '-', '...'], preserve } fn print_hex_scalar(mut out strings.Buffer, value u32) { mut shift := 20 for shift > 0 && (value >> shift) == 0 { shift -= 4 } for shift >= 0 { digit := u8((value >> shift) & 15) out.write_u8(if digit < 10 { `0` + digit } else { `a` + digit - 10 }) shift -= 4 } } fn print_hex_escape(mut out strings.Buffer, value u32) { out.write_string(r'\x') print_hex_scalar(mut out, value) out.write_u8(`;`) } // Borrow exact runs only until Buffer copies them. In particular, no substring // end sentinel, per-character strings, rune arrays, or intermediate copy occurs. fn print_quoted_text(mut out strings.Buffer, text string, barred bool) { delimiter := if barred { u8(`|`) } else { u8(`"`) } out.write_u8(delimiter) mut offset := 0 mut start := 0 for offset < text.len { byte := text[offset] mut cp := u32(byte) mut width := 1 mut escape := byte == delimiter || byte == `\\` || byte < 32 || byte == 127 if byte == 0xc2 && offset + 1 < text.len && text[offset + 1] == 0x85 { cp = 0x85 width = 2 escape = true } else if byte == 0xe2 && offset + 2 < text.len && text[offset + 1] == 0x80 && text[offset + 2] in [u8(0xa8), 0xa9] { cp = if text[offset + 2] == 0xa8 { u32(0x2028) } else { u32(0x2029) } width = 3 escape = true } if escape { out.write_string(text_view(text, start, offset)) match cp { 7 { out.write_string(r'\a') } 8 { out.write_string(r'\b') } 9 { out.write_string(r'\t') } 10 { out.write_string(r'\n') } 13 { out.write_string(r'\r') } 34 { out.write_string(r'\"') } 92 { if barred { print_hex_escape(mut out, cp) } else { out.write_string(r'\\') } } 124 { out.write_string(r'\|') } else { print_hex_escape(mut out, cp) } } start = offset + width } offset += width } out.write_string(text_view(text, start, text.len)) out.write_u8(delimiter) } @[inline] fn preserve_symbol_case(mut out strings.Buffer, preserve bool, mut state ScalarPrintState) { if preserve && !state.preserve_case { out.write_string('#!no-fold-case ') state.preserve_case = true } } fn print_symbol(mut out strings.Buffer, text string, mut state ScalarPrintState, display_mode bool) { if display_mode { out.write_string(text) return } bare, preserve := symbol_print_flags(text) preserve_symbol_case(mut out, preserve, mut state) if bare { out.write_string(text) } else { print_quoted_text(mut out, text, true) } } fn print_keyword(mut out strings.Buffer, text string, mut state ScalarPrintState) { bare, preserve := symbol_print_flags(text) // Atmosphere precedes the complete printed atom. Display retains a readable // keyword spelling too, including empty, numeric, and escaped names. preserve_symbol_case(mut out, preserve, mut state) out.write_string('#:') if bare { out.write_string(text) } else { print_quoted_text(mut out, text, true) } } fn print_character(mut out strings.Buffer, cp rune, display_mode bool) { if !display_mode { if cp == ` ` { out.write_string(r'#\space') return } if cp == `\n` { out.write_string(r'#\newline') return } if cp < 32 || cp == 127 || cp in [rune(0x85), 0x2028, 0x2029] { out.write_string(r'#\x') print_hex_scalar(mut out, u32(cp)) return } out.write_string(r'#\') } if cp < 128 { out.write_u8(u8(cp)) } else { packed, width := unicode17.encode(cp) for i in 0 .. width { out.write_u8(u8(packed >> (i * 8))) } } }