// SPDX-FileCopyrightText: 2025 The Project Pterodactyl Developers // // SPDX-License-Identifier: MPL-2.0 import Foundation public protocol Grammar: Sendable { static var kinds: [SyntaxTreeKind] { get } static func before(_ parser: inout Parser) -> Bool static func inside(_ parser: inout Parser) -> ParseResult } public struct ParseResult { public var kind: SyntaxTreeKind public var metadata: SyntaxTreeMetadata? = nil public init(kind: SyntaxTreeKind, metadata: SyntaxTreeMetadata? = nil) { self.kind = kind self.metadata = metadata } } public extension Grammar { static func tryParse(_ parser: inout Parser) -> Bool { guard !parser.isEndOfFile && before(&parser) else { return false } parse(&parser) return true } static func parse(_ parser: inout Parser) { let mark = parser.open() let result = inside(&parser) parser.close(mark: mark, kind: result.kind, metadata: result.metadata) } }