| 1 | module scheme |
| 2 | |
| 3 | import encoding.utf8 |
| 4 | import strings |
| 5 | import unicode17 |
| 6 | |
| 7 | struct ScalarPrintState { |
| 8 | mut: |
| 9 | preserve_case bool |
| 10 | plan &GoopsPrintPlan = unsafe { nil } |
| 11 | } |
| 12 | |
| 13 | // Use the stable, unambiguous ASCII identifier subset for bare output. Other |
| 14 | // names use bars rather than invoking a fallible lexer just to choose quoting. |
| 15 | // The reader remains the owner of the complete identifier/number grammar. |
| 16 | fn symbol_initial_byte(byte u8) bool { |
| 17 | return (byte >= `a` && byte <= `z`) || (byte >= `A` && byte <= `Z`) |
| 18 | || byte in [`!`, `$`, `%`, `&`, `*`, `/`, `:`, `<`, `=`, `>`, `?`, `^`, `_`, `~`, `@`] |
| 19 | } |
| 20 | |
| 21 | fn symbol_print_flags(text string) (bool, bool) { |
| 22 | mut bare := text.len > 0 && symbol_initial_byte(text[0]) |
| 23 | mut preserve := false |
| 24 | mut offset := 0 |
| 25 | for offset < text.len { |
| 26 | byte := text[offset] |
| 27 | if byte < 128 { |
| 28 | if offset > 0 && !symbol_initial_byte(byte) && !(byte >= `0` && byte <= `9`) |
| 29 | && byte !in [`+`, `-`, `.`] { |
| 30 | bare = false |
| 31 | } |
| 32 | if byte >= `A` && byte <= `Z` { preserve = true } |
| 33 | offset++ |
| 34 | } else { |
| 35 | bare = false |
| 36 | cp, width := utf8.decode_rune_at(text, offset) |
| 37 | folded, _, _, count := unicode17.fold(cp) |
| 38 | if count != 1 || folded != i32(cp) { preserve = true } |
| 39 | offset += width |
| 40 | } |
| 41 | } |
| 42 | return bare || text in ['+', '-', '...'], preserve |
| 43 | } |
| 44 | |
| 45 | fn print_hex_scalar(mut out strings.Buffer, value u32) { |
| 46 | mut shift := 20 |
| 47 | for shift > 0 && (value >> shift) == 0 { shift -= 4 } |
| 48 | for shift >= 0 { |
| 49 | digit := u8((value >> shift) & 15) |
| 50 | out.write_u8(if digit < 10 { `0` + digit } else { `a` + digit - 10 }) |
| 51 | shift -= 4 |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn print_hex_escape(mut out strings.Buffer, value u32) { |
| 56 | out.write_string(r'\x') |
| 57 | print_hex_scalar(mut out, value) |
| 58 | out.write_u8(`;`) |
| 59 | } |
| 60 | |
| 61 | // Borrow exact runs only until Buffer copies them. In particular, no substring |
| 62 | // end sentinel, per-character strings, rune arrays, or intermediate copy occurs. |
| 63 | fn print_quoted_text(mut out strings.Buffer, text string, barred bool) { |
| 64 | delimiter := if barred { u8(`|`) } else { u8(`"`) } |
| 65 | out.write_u8(delimiter) |
| 66 | mut offset := 0 |
| 67 | mut start := 0 |
| 68 | for offset < text.len { |
| 69 | byte := text[offset] |
| 70 | mut cp := u32(byte) |
| 71 | mut width := 1 |
| 72 | mut escape := byte == delimiter || byte == `\\` || byte < 32 || byte == 127 |
| 73 | if byte == 0xc2 && offset + 1 < text.len && text[offset + 1] == 0x85 { |
| 74 | cp = 0x85 |
| 75 | width = 2 |
| 76 | escape = true |
| 77 | } else if byte == 0xe2 && offset + 2 < text.len && text[offset + 1] == 0x80 |
| 78 | && text[offset + 2] in [u8(0xa8), 0xa9] { |
| 79 | cp = if text[offset + 2] == 0xa8 { u32(0x2028) } else { u32(0x2029) } |
| 80 | width = 3 |
| 81 | escape = true |
| 82 | } |
| 83 | if escape { |
| 84 | out.write_string(text_view(text, start, offset)) |
| 85 | match cp { |
| 86 | 7 { out.write_string(r'\a') } |
| 87 | 8 { out.write_string(r'\b') } |
| 88 | 9 { out.write_string(r'\t') } |
| 89 | 10 { out.write_string(r'\n') } |
| 90 | 13 { out.write_string(r'\r') } |
| 91 | 34 { out.write_string(r'\"') } |
| 92 | 92 { |
| 93 | if barred { print_hex_escape(mut out, cp) } else { out.write_string(r'\\') } |
| 94 | } |
| 95 | 124 { out.write_string(r'\|') } |
| 96 | else { print_hex_escape(mut out, cp) } |
| 97 | } |
| 98 | start = offset + width |
| 99 | } |
| 100 | offset += width |
| 101 | } |
| 102 | out.write_string(text_view(text, start, text.len)) |
| 103 | out.write_u8(delimiter) |
| 104 | } |
| 105 | |
| 106 | @[inline] |
| 107 | fn preserve_symbol_case(mut out strings.Buffer, preserve bool, mut state ScalarPrintState) { |
| 108 | if preserve && !state.preserve_case { |
| 109 | out.write_string('#!no-fold-case ') |
| 110 | state.preserve_case = true |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | fn print_symbol(mut out strings.Buffer, text string, mut state ScalarPrintState, display_mode bool) { |
| 115 | if display_mode { |
| 116 | out.write_string(text) |
| 117 | return |
| 118 | } |
| 119 | bare, preserve := symbol_print_flags(text) |
| 120 | preserve_symbol_case(mut out, preserve, mut state) |
| 121 | if bare { out.write_string(text) } else { print_quoted_text(mut out, text, true) } |
| 122 | } |
| 123 | |
| 124 | fn print_keyword(mut out strings.Buffer, text string, mut state ScalarPrintState) { |
| 125 | bare, preserve := symbol_print_flags(text) |
| 126 | // Atmosphere precedes the complete printed atom. Display retains a readable |
| 127 | // keyword spelling too, including empty, numeric, and escaped names. |
| 128 | preserve_symbol_case(mut out, preserve, mut state) |
| 129 | out.write_string('#:') |
| 130 | if bare { out.write_string(text) } else { print_quoted_text(mut out, text, true) } |
| 131 | } |
| 132 | |
| 133 | fn print_character(mut out strings.Buffer, cp rune, display_mode bool) { |
| 134 | if !display_mode { |
| 135 | if cp == ` ` { |
| 136 | out.write_string(r'#\space') |
| 137 | return |
| 138 | } |
| 139 | if cp == `\n` { |
| 140 | out.write_string(r'#\newline') |
| 141 | return |
| 142 | } |
| 143 | if cp < 32 || cp == 127 || cp in [rune(0x85), 0x2028, 0x2029] { |
| 144 | out.write_string(r'#\x') |
| 145 | print_hex_scalar(mut out, u32(cp)) |
| 146 | return |
| 147 | } |
| 148 | out.write_string(r'#\') |
| 149 | } |
| 150 | if cp < 128 { |
| 151 | out.write_u8(u8(cp)) |
| 152 | } else { |
| 153 | packed, width := unicode17.encode(cp) |
| 154 | for i in 0 .. width { out.write_u8(u8(packed >> (i * 8))) } |
| 155 | } |
| 156 | } |
| 157 | |