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