// p4rse, Copyright 2026, Will Hawkins // // This file is part of p4rse. // // This file is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . public enum DebugLevel { case Trace case Verbose case Debug case Error func value() -> UInt8 { return switch self { case DebugLevel.Trace: 3 case DebugLevel.Verbose: 2 case DebugLevel.Debug: 1 case DebugLevel.Error: 0 } } public func isTrace() -> Bool { if self.value() >= DebugLevel.Trace.value() { return true } return false } public func isVerbose() -> Bool { if self.value() >= DebugLevel.Verbose.value() { return true } return false } public func isDebug() -> Bool { if self.value() >= DebugLevel.Debug.value() { return true } return false } public func isError() -> Bool { return true } public var description: String { return switch self { case DebugLevel.Trace: "Trace" case DebugLevel.Verbose: "Verbose" case DebugLevel.Debug: "Debug" case DebugLevel.Error: "Error" } } } public struct Nothing: CustomStringConvertible { public var description: String { return "Nothing" } public init() {} } public func Map(input: T, block: (T) -> U) -> U { return block(input) } public func Fold(input: [T], initial: A, block: (T, A) -> A) -> A { var result = initial for i in input { result = block(i, result) } return result } @freestanding(expression) public macro RequireOkResult(_: Result) -> Bool = #externalMacro(module: "Macros", type: "RequireResult") @freestanding(expression) public macro RequireErrorResult( _: any Errorable, _: Result ) -> Bool = #externalMacro(module: "Macros", type: "RequireErrorResult") @freestanding(expression) public macro UseOkResult(_: Result) -> T = #externalMacro(module: "Macros", type: "UseOkResult") @freestanding(expression) public macro UseErrorResult(_: Result) -> any Errorable = #externalMacro(module: "Macros", type: "UseErrorResult") @freestanding(codeItem) public macro RequireNodeType( node: N, type: String, nice_type_name: String ) = #externalMacro(module: "Macros", type: "RequireNodeType") @freestanding(codeItem) public macro RequireNodesType( nodes: N, type: [String], nice_type_names: [String] ) = #externalMacro(module: "Macros", type: "RequireNodesType") @freestanding(codeItem) public macro SkipUnlessNodeType(node: N, type: String) = #externalMacro(module: "Macros", type: "SkipUnlessNodeType") @freestanding(codeItem) public macro SkipUnlessNodesTypes(node: N, types: [String]) = #externalMacro(module: "Macros", type: "SkipUnlessNodesTypes") @freestanding(codeItem) public macro MustOr(result: E, thing: E?, or: N) = #externalMacro(module: "Macros", type: "MustOr")