Skip to content

Commit 89b80bf

Browse files
committed
Preparation for location aware diagnostics in the compiler.
Moving `libswiftLexRegexLiteral()` and `libswiftParseRegexLiteral()` to the compiler repository because these functions are basically just briding the compiler to the actual lexing/parsing function. * Make some Lexing error APIs public. * Make LocatedErrorProtocol public and expose the `location` property * Shift the location of `LocatedError` in `parseWithDelimiters` so the client can get the valid string indices of the passed-in literal string.
1 parent 8dd8470 commit 89b80bf

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

Sources/_RegexParser/Regex/Parse/DelimiterLexing.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
struct Delimiter: Hashable {
12+
public struct Delimiter: Hashable {
1313
let kind: Kind
1414
let poundCount: Int
1515

@@ -74,26 +74,26 @@ extension Delimiter {
7474
}
7575
}
7676

77-
struct DelimiterLexError: Error, CustomStringConvertible {
78-
enum Kind: Hashable {
77+
public struct DelimiterLexError: Error, CustomStringConvertible {
78+
public enum Kind: Hashable {
7979
case unterminated
8080
case invalidUTF8 // TODO: better range reporting
8181
case unknownDelimiter
8282
case unprintableASCII
8383
case multilineClosingNotOnNewline
8484
}
8585

86-
var kind: Kind
86+
public var kind: Kind
8787

8888
/// The pointer at which to resume lexing.
89-
var resumePtr: UnsafeRawPointer
89+
public var resumePtr: UnsafeRawPointer
9090

9191
init(_ kind: Kind, resumeAt resumePtr: UnsafeRawPointer) {
9292
self.kind = kind
9393
self.resumePtr = resumePtr
9494
}
9595

96-
var description: String {
96+
public var description: String {
9797
switch kind {
9898
case .unterminated: return "unterminated regex literal"
9999
case .invalidUTF8: return "invalid UTF-8 found in source file"
@@ -462,3 +462,9 @@ func lexRegex(
462462
var lexer = DelimiterLexer(start: start, end: end, delimiters: delimiters)
463463
return try lexer.lex()
464464
}
465+
466+
public func lexRegex(
467+
start: UnsafeRawPointer, end: UnsafeRawPointer
468+
) throws -> (contents: String, Delimiter, end: UnsafeRawPointer) {
469+
return try lexRegex(start: start, end: end, delimiters: Delimiter.enabledDelimiters)
470+
}

Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ API convention:
2424
extension Error {
2525
func addingLocation(_ loc: Range<Source.Position>) -> Error {
2626
// If we're already a LocatedError, don't change the location.
27-
if self is _LocatedErrorProtocol {
27+
if self is LocatedErrorProtocol {
2828
return self
2929
}
3030
return Source.LocatedError<Self>(self, loc)

Sources/_RegexParser/Regex/Parse/Mocking.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12+
@available(*, deprecated, message: "moving to SwiftCompilerModules")
1213
private func copyCString(_ str: String) -> UnsafePointer<CChar> {
1314
let count = str.utf8.count + 1
1415
return str.withCString {
@@ -36,6 +37,7 @@ private func copyCString(_ str: String) -> UnsafePointer<CChar> {
3637
/// - Returns: A bool indicating whether lexing was completely erroneous, and
3738
/// cannot be recovered from, or false if there either was no error,
3839
/// or there was a recoverable error.
40+
@available(*, deprecated, message: "moving to SwiftCompilerModules")
3941
func libswiftLexRegexLiteral(
4042
_ curPtrPtr: UnsafeMutablePointer<UnsafePointer<CChar>?>?,
4143
_ bufferEndPtr: UnsafePointer<CChar>?,
@@ -93,6 +95,7 @@ public let currentRegexLiteralFormatVersion: CUnsignedInt = 1
9395
/// capture structure.
9496
/// - captureStructureSize: The size of the capture structure buffer. Must be
9597
/// greater than or equal to `strlen(inputPtr)`.
98+
@available(*, deprecated, message: "moving to SwiftCompilerModules")
9699
func libswiftParseRegexLiteral(
97100
_ inputPtr: UnsafePointer<CChar>?,
98101
_ errOut: UnsafeMutablePointer<UnsafePointer<CChar>?>?,

Sources/_RegexParser/Regex/Parse/Parse.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,5 +583,15 @@ public func parseWithDelimiters<S: StringProtocol>(
583583
_ regex: S
584584
) throws -> AST where S.SubSequence == Substring {
585585
let (contents, delim) = droppingRegexDelimiters(String(regex))
586-
return try parse(contents, defaultSyntaxOptions(delim, contents: contents))
586+
do {
587+
return try parse(contents, defaultSyntaxOptions(delim, contents: contents))
588+
} catch let error as LocatedErrorProtocol {
589+
// Convert the range in 'contents' to the range in 'regex'.
590+
let delimCount = delim.opening.count
591+
let offsets = contents.offsets(of: error.location.range)
592+
let startIndex = regex.index(atOffset: delimCount + offsets.lowerBound)
593+
let endIndex = regex.index(atOffset: delimCount + offsets.upperBound)
594+
595+
throw error._typeErasedError.addingLocation(startIndex..<endIndex)
596+
}
587597
}

Sources/_RegexParser/Regex/Parse/SourceLocation.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ extension Source {
5656
var currentPosition: Position { bounds.lowerBound }
5757
}
5858

59-
protocol _LocatedErrorProtocol: Error {}
59+
public protocol LocatedErrorProtocol: Error {
60+
var location: SourceLocation { get }
61+
var _typeErasedError: Error { get }
62+
}
6063

6164
extension Source {
6265
/// An error with source location info
63-
public struct LocatedError<E: Error>: Error, _LocatedErrorProtocol {
66+
public struct LocatedError<E: Error>: Error, LocatedErrorProtocol {
6467
public let error: E
6568
public let location: SourceLocation
6669

@@ -118,4 +121,8 @@ extension Source.LocatedError: CustomStringConvertible {
118121
// we present the message to the compiler.
119122
"\(error)"
120123
}
124+
125+
public var _typeErasedError: Error {
126+
return error
127+
}
121128
}

0 commit comments

Comments
 (0)