-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathJSON.NodeRule.Object.swift
More file actions
34 lines (32 loc) · 1.19 KB
/
JSON.NodeRule.Object.swift
File metadata and controls
34 lines (32 loc) · 1.19 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
internal import Grammar
extension JSON.NodeRule {
/// Matches an object literal.
///
/// Object literals begin and end with curly braces (`{` and `}`), and
/// contain instances of ``Item`` separated by ``JSON.CommaRule``s.
/// Trailing commas are not allowed.
enum Object {
}
}
extension JSON.NodeRule.Object: ParsingRule {
typealias Terminal = UInt8
static func parse<Source>(
_ input: inout ParsingInput<some ParsingDiagnostics<Source>>
) throws -> [(key: JSON.Key, value: JSON.Node)]
where Source.Index == Location, Source.Element == Terminal {
try input.parse(as: JSON.BraceLeftRule<Location>.self)
var items: [(key: JSON.Key, value: JSON.Node)]
if let head: (key: JSON.Key, value: JSON.Node) = try? input.parse(as: Item.self) {
items = [head]
while let (_, next): (Void, (key: JSON.Key, value: JSON.Node)) = try? input.parse(
as: (JSON.CommaRule<Location>, Item).self
) {
items.append(next)
}
} else {
items = []
}
try input.parse(as: JSON.BraceRightRule<Location>.self)
return items
}
}