48 lines
1.7 KiB
Swift
48 lines
1.7 KiB
Swift
// DSLParseError.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Errors from lexing / parsing DSL source, each carrying the span to blame.
|
|
|
|
/// A lexing or parsing failure, with the source span to point at.
|
|
public struct DSLParseError: Error, Equatable, Sendable, CustomStringConvertible {
|
|
|
|
public enum Kind: Equatable, Sendable {
|
|
/// A character that starts no valid token.
|
|
case unexpectedCharacter(Character)
|
|
/// A malformed number literal.
|
|
case invalidNumber(String)
|
|
/// Expected `expected` but found `found`.
|
|
case expected(String, found: String)
|
|
/// A call argument list or parenthesised group was left open.
|
|
case unclosedParen
|
|
/// Expression nesting exceeded the parser's depth ceiling.
|
|
case tooDeeplyNested
|
|
/// A rate suffix other than `.Audio` / `.Control`.
|
|
case invalidRate(String)
|
|
}
|
|
|
|
public let kind: Kind
|
|
public let span: DSLSpan
|
|
|
|
public init(kind: Kind, span: DSLSpan) {
|
|
self.kind = kind
|
|
self.span = span
|
|
}
|
|
|
|
public var description: String {
|
|
let detail: String
|
|
switch kind {
|
|
case .unexpectedCharacter(let c): detail = "unexpected character '\(c)'"
|
|
case .invalidNumber(let s): detail = "invalid number '\(s)'"
|
|
case .expected(let e, let f): detail = "expected \(e), found \(f)"
|
|
case .unclosedParen: detail = "unclosed '('"
|
|
case .tooDeeplyNested: detail = "expression nested too deeply"
|
|
case .invalidRate(let r): detail = "unknown rate '.\(r)' (use .Audio or .Control)"
|
|
}
|
|
return "\(detail) (at \(span.start))"
|
|
}
|
|
}
|