75 lines
2.4 KiB
Swift
75 lines
2.4 KiB
Swift
// GraphDSLDiagnosticMapper.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// LyrebirdLiveCompile
|
|
//
|
|
// Turns a thrown `DSLParseError` / `DSLBuildError` into the same `[Diagnostic]`
|
|
// the swiftc path produces, so `LyrebirdScratchView`'s gutter renders DSL
|
|
// failures with no special-casing. Parse errors carry a source span → a real
|
|
// (line, column); build errors carry no span → `line: nil` with the message
|
|
// alone.
|
|
|
|
#if os(macOS)
|
|
import LyrebirdGraphDSLEmbedded
|
|
|
|
/// Maps interpreted-graph-DSL errors to editor diagnostics.
|
|
enum GraphDSLDiagnosticMapper {
|
|
|
|
/// Convert `error` (a `DSLParseError`, a `DSLBuildError`, or anything else)
|
|
/// into one or more ``Diagnostic``s, resolving parse-error spans against
|
|
/// `source` for a 1-based line/column.
|
|
static func diagnostics(for error: Error, source: String) -> [Diagnostic] {
|
|
if let parse: DSLParseError = error as? DSLParseError {
|
|
let (line, column): (Int, Int) = lineColumn(of: parse.span.start, in: source)
|
|
return [Diagnostic(
|
|
line: line,
|
|
column: column,
|
|
severity: .error,
|
|
message: parse.description,
|
|
rawLine: line
|
|
)]
|
|
}
|
|
if let build: DSLBuildError = error as? DSLBuildError {
|
|
// Build errors carry no span; report at file scope.
|
|
return [Diagnostic(
|
|
line: nil,
|
|
column: 0,
|
|
severity: .error,
|
|
message: build.description,
|
|
rawLine: 0
|
|
)]
|
|
}
|
|
return [Diagnostic(
|
|
line: nil,
|
|
column: 0,
|
|
severity: .error,
|
|
message: "\(error)",
|
|
rawLine: 0
|
|
)]
|
|
}
|
|
|
|
/// Map a character `offset` into `source` to a 1-based (line, column).
|
|
/// An offset past the end clamps to the final position.
|
|
private static func lineColumn(of offset: Int, in source: String) -> (line: Int, column: Int) {
|
|
var line: Int = 1
|
|
var column: Int = 1
|
|
var seen: Int = 0
|
|
for character: Character in source {
|
|
if seen >= offset { break }
|
|
if character == "\n" {
|
|
line += 1
|
|
column = 1
|
|
} else {
|
|
column += 1
|
|
}
|
|
seen += 1
|
|
}
|
|
return (line, column)
|
|
}
|
|
}
|
|
|
|
#endif
|