-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathJSON.StringRule.swift
More file actions
38 lines (32 loc) · 1.28 KB
/
JSON.StringRule.swift
File metadata and controls
38 lines (32 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
internal import Grammar
extension JSON {
/// Matches a string literal.
///
/// String literals always begin and end with an ASCII double quote character (`"`).
enum StringRule<Location> {
}
}
extension JSON.StringRule: ParsingRule {
typealias Terminal = UInt8
static func parse<Source>(
_ input: inout ParsingInput<some ParsingDiagnostics<Source>>
) throws -> String
where Source.Element == Terminal,
Source.Index == Location {
typealias DoubleQuote = UnicodeEncoding<Location, UInt8>.DoubleQuote
try input.parse(as: DoubleQuote.self)
let start: Location = input.index
input.parse(as: CodeUnit.self, in: Void.self)
let end: Location = input.index
var string: String = .init(decoding: input[start ..< end], as: Unicode.UTF8.self)
while let next: String = input.parse(as: EscapeSequence?.self) {
string += next
let start: Location = input.index
input.parse(as: CodeUnit.self, in: Void.self)
let end: Location = input.index
string += .init(decoding: input[start ..< end], as: Unicode.UTF8.self)
}
try input.parse(as: DoubleQuote.self)
return string
}
}