at main 1.4 kB view raw
1// SPDX-FileCopyrightText: 2025 The Project Pterodactyl Developers 2// 3// SPDX-License-Identifier: MPL-2.0 4 5import Foundation 6 7public enum Document: Grammar { 8 public static let kind = SyntaxTreeKind(name: "document") 9 public static let kinds = [kind] 10 11 public static func precondition(_ parser: inout Parser) -> Bool { 12 true 13 } 14 15 public static func inside(_ parser: inout Parser, recovery: Set<TokenKind>) -> ParseResult { 16 parser.eatTrivia() 17 18 // Parse imports 19 while !parser.isAt(kind: .eof) { 20 parser.eatTrivia() 21 if Theory.precondition(&parser) { break } 22 23 if !Import.tryParse(&parser, recovery: recovery) { 24 parser.advance(error: "Expected to see either an import or a theory declaration, but instead got \(parser.currentToken.kind): \(parser.currentToken.text)") 25 } 26 } 27 28 // Theories section 29 while !parser.isAt(kind: .eof) { 30 if !Theory.tryParse(&parser, recovery: recovery) { 31 if parser.isAt(kindSatisfying: \.isVisible) { 32 let token = parser.currentToken 33 parser.advance(error: "Unexpected token: \(token.kind)") 34 } else { 35 parser.advance(metadata: nil) 36 } 37 } 38 parser.eatTrivia() 39 } 40 41 parser.eatTrivia() 42 _ = parser.eat(kind: .eof, metadata: nil) 43 44 return ParseResult(kind: Self.kind) 45 } 46} 47 48extension SyntaxView<Document> { 49 var imports: [SyntaxView<Import>] { matchingSubviews() } 50 var theories: [SyntaxView<Theory>] { matchingSubviews() } 51}