102 lines
4.4 KiB
Swift
102 lines
4.4 KiB
Swift
// DiagnosticParser.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// LyrebirdLiveCompile — Phase 144
|
|
//
|
|
// `DiagnosticParser.parse(stderr:userBodyLineOffset:)` walks the swiftc stderr
|
|
// stream line by line, extracts diagnostics matching the standard
|
|
// `<path>:<line>:<column>: <severity>: <message>` shape, and re-maps line
|
|
// numbers from wrapped-snippet coordinates back to user-snippet coordinates.
|
|
//
|
|
// swiftc stderr format (one diagnostic):
|
|
//
|
|
// /tmp/.../Snippet.swift:7:14: error: cannot convert value of type 'String' to 'Int'
|
|
// 7 | let x: Int = "nope"
|
|
// | `- error: cannot convert value of type 'String' to 'Int'
|
|
//
|
|
// We pick up the first line (the location/severity/message header) and ignore
|
|
// the source-context lines that follow.
|
|
|
|
import Foundation
|
|
|
|
#if os(macOS)
|
|
|
|
public enum DiagnosticParser {
|
|
|
|
/// Compiled once at first access. Pattern matches the standard swiftc
|
|
/// header line: `<path>:<line>:<column>: <severity>: <message>`.
|
|
/// `try!` is safe — the pattern is a compile-time literal that cannot fail
|
|
/// `NSRegularExpression`'s validator.
|
|
private static let _headerRegex: NSRegularExpression = {
|
|
// swiftlint:disable:next force_try
|
|
try! NSRegularExpression(
|
|
pattern: #"^(.+?):(\d+):(\d+):\s+(error|warning|note):\s+(.*)$"#
|
|
)
|
|
}()
|
|
|
|
/// Parse swiftc stderr into a list of structured diagnostics, with line
|
|
/// numbers translated to user-snippet coordinates.
|
|
///
|
|
/// - Parameters:
|
|
/// - stderr: Raw swiftc stderr text. May be empty.
|
|
/// - userBodyLineOffset: 1-based line on which the user's body starts in
|
|
/// the wrapped source — typically `SnippetHarness.wrap(userBody:).userBodyLineOffset`.
|
|
/// Diagnostics on `rawLine >= userBodyLineOffset` are mapped to
|
|
/// `rawLine - (userBodyLineOffset - 1)`; diagnostics on earlier lines
|
|
/// keep `line = nil` to signal "in the harness preamble".
|
|
/// - Returns: One `Diagnostic` per matched header line, in stderr order.
|
|
public static func parse(stderr: String, userBodyLineOffset: Int) -> [Diagnostic] {
|
|
var result: [Diagnostic] = []
|
|
for rawLine: String.SubSequence in stderr.split(separator: "\n", omittingEmptySubsequences: false) {
|
|
guard let diag: Diagnostic = parseLine(String(rawLine), userBodyLineOffset: userBodyLineOffset) else {
|
|
continue
|
|
}
|
|
result.append(diag)
|
|
}
|
|
return result
|
|
}
|
|
|
|
/// Parse one stderr line. Returns `nil` for non-header lines (source-context
|
|
/// rows, caret markers, blank lines).
|
|
static func parseLine(_ rawLine: String, userBodyLineOffset: Int) -> Diagnostic? {
|
|
// Pattern (anchored):
|
|
// <path>:<line>:<column>: <severity>: <message>
|
|
// Where <path> contains no colon-space combos, and <severity> ∈ {error, warning, note}.
|
|
//
|
|
// The path may legally contain `:` on macOS (filesystem allows it), so use a
|
|
// greedy-then-validate approach: scan from the right for the third-from-last
|
|
// `:` and split on that. Cheaper: regex with a non-greedy path segment.
|
|
let range: NSRange = NSRange(rawLine.startIndex..<rawLine.endIndex, in: rawLine)
|
|
guard let match: NSTextCheckingResult = _headerRegex.firstMatch(in: rawLine, range: range),
|
|
match.numberOfRanges == 6,
|
|
let lineRange: Range<String.Index> = Range(match.range(at: 2), in: rawLine),
|
|
let colRange: Range<String.Index> = Range(match.range(at: 3), in: rawLine),
|
|
let severityRange: Range<String.Index> = Range(match.range(at: 4), in: rawLine),
|
|
let messageRange: Range<String.Index> = Range(match.range(at: 5), in: rawLine),
|
|
let rawLineNumber: Int = Int(rawLine[lineRange]),
|
|
let column: Int = Int(rawLine[colRange]),
|
|
let severity: DiagnosticSeverity = DiagnosticSeverity(rawValue: String(rawLine[severityRange]))
|
|
else { return nil }
|
|
|
|
let userLine: Int?
|
|
if rawLineNumber >= userBodyLineOffset {
|
|
userLine = rawLineNumber - (userBodyLineOffset - 1)
|
|
} else {
|
|
userLine = nil
|
|
}
|
|
|
|
return Diagnostic(
|
|
line: userLine,
|
|
column: column,
|
|
severity: severity,
|
|
message: String(rawLine[messageRange]),
|
|
rawLine: rawLineNumber
|
|
)
|
|
}
|
|
}
|
|
|
|
#endif
|