|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Preprocesses JSONC (JSON with Comments) text into plain JSON `Data` that |
| 4 | +/// `JSONDecoder`/`JSONSerialization` can parse. |
| 5 | +/// |
| 6 | +/// Supports the two JSONC conveniences users actually reach for in hand-edited |
| 7 | +/// config files: `//` and `/* */` comments, and trailing commas before a |
| 8 | +/// closing `}` or `]`. Both are stripped with a string-literal-aware scanner |
| 9 | +/// so a comment marker or trailing comma inside a quoted string is left alone. |
| 10 | +/// |
| 11 | +/// Shared by `ProgramaConfigStore` (`~/.config/programa/programa.json` and |
| 12 | +/// project-local `programa.json`/`cmux.json`) and `ProgramaSettingsFileStore` |
| 13 | +/// (`~/.config/programa/settings.json`) so both config surfaces accept the |
| 14 | +/// same JSONC dialect. |
| 15 | +enum JSONCParser { |
| 16 | + static func preprocess(data: Data) throws -> Data { |
| 17 | + let source = try sourceString(from: data) |
| 18 | + let withoutBOM = source.hasPrefix("\u{feff}") ? String(source.dropFirst()) : source |
| 19 | + let stripped = try stripComments(from: withoutBOM) |
| 20 | + let normalized = stripTrailingCommas(from: stripped) |
| 21 | + return Data(normalized.utf8) |
| 22 | + } |
| 23 | + |
| 24 | + private static func sourceString(from data: Data) throws -> String { |
| 25 | + if let encoding = detectedJSONEncoding(for: data), |
| 26 | + let source = String(data: data, encoding: encoding) { |
| 27 | + return source |
| 28 | + } |
| 29 | + if let source = String(data: data, encoding: .utf8) { |
| 30 | + return source |
| 31 | + } |
| 32 | + |
| 33 | + var convertedString: NSString? |
| 34 | + var usedLossyConversion = ObjCBool(false) |
| 35 | + let encoding = NSString.stringEncoding( |
| 36 | + for: data, |
| 37 | + encodingOptions: [ |
| 38 | + .suggestedEncodingsKey: [ |
| 39 | + String.Encoding.utf8.rawValue, |
| 40 | + String.Encoding.utf16BigEndian.rawValue, |
| 41 | + String.Encoding.utf16LittleEndian.rawValue, |
| 42 | + String.Encoding.utf32BigEndian.rawValue, |
| 43 | + String.Encoding.utf32LittleEndian.rawValue, |
| 44 | + ], |
| 45 | + .useOnlySuggestedEncodingsKey: true, |
| 46 | + .allowLossyKey: false, |
| 47 | + ], |
| 48 | + convertedString: &convertedString, |
| 49 | + usedLossyConversion: &usedLossyConversion |
| 50 | + ) |
| 51 | + |
| 52 | + if let convertedString, !usedLossyConversion.boolValue { |
| 53 | + return convertedString as String |
| 54 | + } |
| 55 | + if encoding != 0, !usedLossyConversion.boolValue { |
| 56 | + let stringEncoding = String.Encoding(rawValue: encoding) |
| 57 | + if let source = String(data: data, encoding: stringEncoding) { |
| 58 | + return source |
| 59 | + } |
| 60 | + } |
| 61 | + throw JSONCError.invalidTextEncoding |
| 62 | + } |
| 63 | + |
| 64 | + private static func detectedJSONEncoding(for data: Data) -> String.Encoding? { |
| 65 | + let bytes = Array(data.prefix(4)) |
| 66 | + if bytes.starts(with: [0x00, 0x00, 0xFE, 0xFF]) { return .utf32BigEndian } |
| 67 | + if bytes.starts(with: [0xFF, 0xFE, 0x00, 0x00]) { return .utf32LittleEndian } |
| 68 | + if bytes.starts(with: [0xFE, 0xFF]) { return .utf16BigEndian } |
| 69 | + if bytes.starts(with: [0xFF, 0xFE]) { return .utf16LittleEndian } |
| 70 | + if bytes.starts(with: [0xEF, 0xBB, 0xBF]) { return .utf8 } |
| 71 | + guard bytes.count >= 4 else { return nil } |
| 72 | + |
| 73 | + switch (bytes[0] == 0, bytes[1] == 0, bytes[2] == 0, bytes[3] == 0) { |
| 74 | + case (true, true, true, false): |
| 75 | + return .utf32BigEndian |
| 76 | + case (false, true, true, true): |
| 77 | + return .utf32LittleEndian |
| 78 | + case (true, false, true, false): |
| 79 | + return .utf16BigEndian |
| 80 | + case (false, true, false, true): |
| 81 | + return .utf16LittleEndian |
| 82 | + default: |
| 83 | + return nil |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static func stripComments(from source: String) throws -> String { |
| 88 | + var result = "" |
| 89 | + var index = source.startIndex |
| 90 | + var inString = false |
| 91 | + var isEscaped = false |
| 92 | + |
| 93 | + while index < source.endIndex { |
| 94 | + let character = source[index] |
| 95 | + |
| 96 | + if inString { |
| 97 | + result.append(character) |
| 98 | + if isEscaped { |
| 99 | + isEscaped = false |
| 100 | + } else if character == "\\" { |
| 101 | + isEscaped = true |
| 102 | + } else if character == "\"" { |
| 103 | + inString = false |
| 104 | + } |
| 105 | + index = source.index(after: index) |
| 106 | + continue |
| 107 | + } |
| 108 | + |
| 109 | + if character == "\"" { |
| 110 | + inString = true |
| 111 | + result.append(character) |
| 112 | + index = source.index(after: index) |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + if character == "/" { |
| 117 | + let nextIndex = source.index(after: index) |
| 118 | + if nextIndex < source.endIndex { |
| 119 | + let next = source[nextIndex] |
| 120 | + if next == "/" { |
| 121 | + index = source.index(after: nextIndex) |
| 122 | + while index < source.endIndex && source[index] != "\n" { |
| 123 | + index = source.index(after: index) |
| 124 | + } |
| 125 | + continue |
| 126 | + } |
| 127 | + if next == "*" { |
| 128 | + index = source.index(after: nextIndex) |
| 129 | + var didClose = false |
| 130 | + while index < source.endIndex { |
| 131 | + let current = source[index] |
| 132 | + let followingIndex = source.index(after: index) |
| 133 | + if current == "*" && followingIndex < source.endIndex && source[followingIndex] == "/" { |
| 134 | + index = source.index(after: followingIndex) |
| 135 | + didClose = true |
| 136 | + break |
| 137 | + } |
| 138 | + index = followingIndex |
| 139 | + } |
| 140 | + guard didClose else { |
| 141 | + throw JSONCError.unterminatedBlockComment |
| 142 | + } |
| 143 | + continue |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + result.append(character) |
| 149 | + index = source.index(after: index) |
| 150 | + } |
| 151 | + |
| 152 | + return result |
| 153 | + } |
| 154 | + |
| 155 | + private static func stripTrailingCommas(from source: String) -> String { |
| 156 | + var result = "" |
| 157 | + var index = source.startIndex |
| 158 | + var inString = false |
| 159 | + var isEscaped = false |
| 160 | + |
| 161 | + while index < source.endIndex { |
| 162 | + let character = source[index] |
| 163 | + |
| 164 | + if inString { |
| 165 | + result.append(character) |
| 166 | + if isEscaped { |
| 167 | + isEscaped = false |
| 168 | + } else if character == "\\" { |
| 169 | + isEscaped = true |
| 170 | + } else if character == "\"" { |
| 171 | + inString = false |
| 172 | + } |
| 173 | + index = source.index(after: index) |
| 174 | + continue |
| 175 | + } |
| 176 | + |
| 177 | + if character == "\"" { |
| 178 | + inString = true |
| 179 | + result.append(character) |
| 180 | + index = source.index(after: index) |
| 181 | + continue |
| 182 | + } |
| 183 | + |
| 184 | + if character == "," { |
| 185 | + var lookahead = source.index(after: index) |
| 186 | + while lookahead < source.endIndex && source[lookahead].isWhitespace { |
| 187 | + lookahead = source.index(after: lookahead) |
| 188 | + } |
| 189 | + if lookahead < source.endIndex && (source[lookahead] == "}" || source[lookahead] == "]") { |
| 190 | + index = source.index(after: index) |
| 191 | + continue |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + result.append(character) |
| 196 | + index = source.index(after: index) |
| 197 | + } |
| 198 | + |
| 199 | + return result |
| 200 | + } |
| 201 | + |
| 202 | + enum JSONCError: LocalizedError { |
| 203 | + case invalidTextEncoding |
| 204 | + case unterminatedBlockComment |
| 205 | + |
| 206 | + var errorDescription: String? { |
| 207 | + switch self { |
| 208 | + case .invalidTextEncoding: |
| 209 | + return "config file text encoding is not supported" |
| 210 | + case .unterminatedBlockComment: |
| 211 | + return "unterminated block comment" |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments