Skip to content

[5.7]Preparation for location aware diagnostics in the compiler. #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions Sources/_RegexParser/Regex/Parse/DelimiterLexing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

struct Delimiter: Hashable {
public struct Delimiter: Hashable {
let kind: Kind
let poundCount: Int

Expand Down Expand Up @@ -74,26 +74,26 @@ extension Delimiter {
}
}

struct DelimiterLexError: Error, CustomStringConvertible {
enum Kind: Hashable {
public struct DelimiterLexError: Error, CustomStringConvertible {
public enum Kind: Hashable {
case unterminated
case invalidUTF8 // TODO: better range reporting
case unknownDelimiter
case unprintableASCII
case multilineClosingNotOnNewline
}

var kind: Kind
public var kind: Kind

/// The pointer at which to resume lexing.
var resumePtr: UnsafeRawPointer
public var resumePtr: UnsafeRawPointer

init(_ kind: Kind, resumeAt resumePtr: UnsafeRawPointer) {
self.kind = kind
self.resumePtr = resumePtr
}

var description: String {
public var description: String {
switch kind {
case .unterminated: return "unterminated regex literal"
case .invalidUTF8: return "invalid UTF-8 found in source file"
Expand Down Expand Up @@ -462,3 +462,9 @@ func lexRegex(
var lexer = DelimiterLexer(start: start, end: end, delimiters: delimiters)
return try lexer.lex()
}

public func lexRegex(
start: UnsafeRawPointer, end: UnsafeRawPointer
) throws -> (contents: String, Delimiter, end: UnsafeRawPointer) {
return try lexRegex(start: start, end: end, delimiters: Delimiter.enabledDelimiters)
}
2 changes: 1 addition & 1 deletion Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ API convention:
extension Error {
func addingLocation(_ loc: Range<Source.Position>) -> Error {
// If we're already a LocatedError, don't change the location.
if self is _LocatedErrorProtocol {
if self is LocatedErrorProtocol {
return self
}
return Source.LocatedError<Self>(self, loc)
Expand Down
3 changes: 3 additions & 0 deletions Sources/_RegexParser/Regex/Parse/Mocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//
//===----------------------------------------------------------------------===//

@available(*, deprecated, message: "moving to SwiftCompilerModules")
private func copyCString(_ str: String) -> UnsafePointer<CChar> {
let count = str.utf8.count + 1
return str.withCString {
Expand Down Expand Up @@ -36,6 +37,7 @@ private func copyCString(_ str: String) -> UnsafePointer<CChar> {
/// - Returns: A bool indicating whether lexing was completely erroneous, and
/// cannot be recovered from, or false if there either was no error,
/// or there was a recoverable error.
@available(*, deprecated, message: "moving to SwiftCompilerModules")
func libswiftLexRegexLiteral(
_ curPtrPtr: UnsafeMutablePointer<UnsafePointer<CChar>?>?,
_ bufferEndPtr: UnsafePointer<CChar>?,
Expand Down Expand Up @@ -93,6 +95,7 @@ public let currentRegexLiteralFormatVersion: CUnsignedInt = 1
/// capture structure.
/// - captureStructureSize: The size of the capture structure buffer. Must be
/// greater than or equal to `strlen(inputPtr)`.
@available(*, deprecated, message: "moving to SwiftCompilerModules")
func libswiftParseRegexLiteral(
_ inputPtr: UnsafePointer<CChar>?,
_ errOut: UnsafeMutablePointer<UnsafePointer<CChar>?>?,
Expand Down
12 changes: 11 additions & 1 deletion Sources/_RegexParser/Regex/Parse/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -583,5 +583,15 @@ public func parseWithDelimiters<S: StringProtocol>(
_ regex: S
) throws -> AST where S.SubSequence == Substring {
let (contents, delim) = droppingRegexDelimiters(String(regex))
return try parse(contents, defaultSyntaxOptions(delim, contents: contents))
do {
return try parse(contents, defaultSyntaxOptions(delim, contents: contents))
} catch let error as LocatedErrorProtocol {
// Convert the range in 'contents' to the range in 'regex'.
let delimCount = delim.opening.count
let offsets = contents.offsets(of: error.location.range)
let startIndex = regex.index(atOffset: delimCount + offsets.lowerBound)
let endIndex = regex.index(atOffset: delimCount + offsets.upperBound)

throw error._typeErasedError.addingLocation(startIndex..<endIndex)
}
}
11 changes: 9 additions & 2 deletions Sources/_RegexParser/Regex/Parse/SourceLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ extension Source {
var currentPosition: Position { bounds.lowerBound }
}

protocol _LocatedErrorProtocol: Error {}
public protocol LocatedErrorProtocol: Error {
var location: SourceLocation { get }
var _typeErasedError: Error { get }
}

extension Source {
/// An error with source location info
public struct LocatedError<E: Error>: Error, _LocatedErrorProtocol {
public struct LocatedError<E: Error>: Error, LocatedErrorProtocol {
public let error: E
public let location: SourceLocation

Expand Down Expand Up @@ -118,4 +121,8 @@ extension Source.LocatedError: CustomStringConvertible {
// we present the message to the compiler.
"\(error)"
}

public var _typeErasedError: Error {
return error
}
}