mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
This patch provides an API and Serialization for clang-style diagnostics from within Swift. Libraries can use this API to pop their own custom diagnostics that can be serialized to JSON.
53 lines
1.9 KiB
Swift
53 lines
1.9 KiB
Swift
//===--------------- SwiftLanguage.swift - Swift Syntax Library -----------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
// This file provides main entry point into the Syntax library.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Foundation
|
|
|
|
#if os(macOS)
|
|
import Darwin
|
|
#elseif os(Linux)
|
|
import Glibc
|
|
#endif
|
|
|
|
/// A list of possible errors that could be encountered while parsing a
|
|
/// Syntax tree.
|
|
public enum ParserError: Error {
|
|
case swiftcFailed(Int, String)
|
|
case invalidFile
|
|
}
|
|
|
|
extension Syntax {
|
|
/// Parses the Swift file at the provided URL into a full-fidelity `Syntax`
|
|
/// tree.
|
|
/// - Parameter url: The URL you wish to parse.
|
|
/// - Returns: A top-level Syntax node representing the contents of the tree,
|
|
/// if the parse was successful.
|
|
/// - Throws: `ParseError.couldNotFindSwiftc` if `swiftc` could not be
|
|
/// located, `ParseError.invalidFile` if the file is invalid.
|
|
/// FIXME: Fill this out with all error cases.
|
|
public static func parse(_ url: URL) throws -> SourceFileSyntax {
|
|
let swiftcRunner = try SwiftcRunner(sourceFile: url)
|
|
let result = try swiftcRunner.invoke()
|
|
guard result.wasSuccessful else {
|
|
throw ParserError.swiftcFailed(result.exitCode, result.stderr)
|
|
}
|
|
let decoder = JSONDecoder()
|
|
let raw = try decoder.decode(RawSyntax.self, from: result.stdoutData)
|
|
guard let file = Syntax.fromRaw(raw) as? SourceFileSyntax else {
|
|
throw ParserError.invalidFile
|
|
}
|
|
return file
|
|
}
|
|
}
|