Skip to content

Commit 635776f

Browse files
committed
Drop the ASTStage parameter
We now always run validation, which is fine because the resulting AST can still be returned.
1 parent 5945e76 commit 635776f

File tree

9 files changed

+18
-38
lines changed

9 files changed

+18
-38
lines changed

Sources/PatternConverter/PatternConverter.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ struct PatternConverter: ParsableCommand {
5050
print("Converting '\(delim)\(regex)\(delim)'")
5151

5252
let ast = try _RegexParser.parse(
53-
regex, .semantic,
54-
experimentalSyntax ? .experimental : .traditional)
53+
regex, experimentalSyntax ? .experimental : .traditional)
5554

5655
// Show rendered source ranges
5756
if renderSourceRanges {

Sources/_RegexParser/Regex/Parse/CompilerInterface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public func swiftCompilerParseRegexLiteral(
9696
_ input: String, captureBufferOut: UnsafeMutableRawBufferPointer
9797
) throws -> (regexToEmit: String, version: Int) {
9898
do {
99-
let ast = try parseWithDelimiters(input, .semantic)
99+
let ast = try parseWithDelimiters(input)
100100
// Serialize the capture structure for later type inference.
101101
assert(captureBufferOut.count >= input.utf8.count)
102102
ast.captureStructure.encode(to: captureBufferOut)

Sources/_RegexParser/Regex/Parse/Parse.swift

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -575,37 +575,20 @@ extension Parser {
575575
}
576576
}
577577

578-
public enum ASTStage {
579-
/// The regex is parsed, and a syntactically valid AST is returned. Otherwise
580-
/// an error is thrown. This is useful for e.g syntax coloring.
581-
case syntactic
582-
583-
/// The regex is parsed, and a syntactically and semantically valid AST is
584-
/// returned. Otherwise an error is thrown. A semantically valid AST has been
585-
/// checked for e.g unsupported constructs and invalid backreferences.
586-
case semantic
587-
}
588-
589578
public func parseWithRecovery<S: StringProtocol>(
590-
_ regex: S, _ syntax: SyntaxOptions, stage: ASTStage = .semantic
579+
_ regex: S, _ syntax: SyntaxOptions
591580
) -> AST where S.SubSequence == Substring
592581
{
593582
let source = Source(String(regex))
594583
var parser = Parser(source, syntax: syntax)
595-
let ast = parser.parse()
596-
switch stage {
597-
case .syntactic:
598-
return ast
599-
case .semantic:
600-
return validate(ast)
601-
}
584+
return validate(parser.parse())
602585
}
603586

604587
public func parse<S: StringProtocol>(
605-
_ regex: S, _ stage: ASTStage, _ syntax: SyntaxOptions
588+
_ regex: S, _ syntax: SyntaxOptions
606589
) throws -> AST where S.SubSequence == Substring
607590
{
608-
try parseWithRecovery(regex, syntax, stage: stage).ensureValid()
591+
try parseWithRecovery(regex, syntax).ensureValid()
609592
}
610593

611594
/// Retrieve the default set of syntax options that a delimiter and literal
@@ -642,12 +625,12 @@ public func parseWithDelimitersWithRecovery<S: StringProtocol>(
642625
/// Parses a given regex string with delimiters, inferring the syntax options
643626
/// from the delimiters used.
644627
public func parseWithDelimiters<S: StringProtocol>(
645-
_ regex: S, _ stage: ASTStage
628+
_ regex: S
646629
) throws -> AST where S.SubSequence == Substring {
647630
let (contents, delim) = droppingRegexDelimiters(String(regex))
648631
let syntax = defaultSyntaxOptions(delim, contents: contents)
649632
do {
650-
return try parseWithRecovery(contents, syntax, stage: stage).ensureValid()
633+
return try parseWithRecovery(contents, syntax).ensureValid()
651634
} catch let error as LocatedErrorProtocol {
652635
// Convert the range in 'contents' to the range in 'regex'.
653636
let delimCount = delim.opening.count

Sources/_StringProcessing/Compiler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Compiler {
3737
func _compileRegex(
3838
_ regex: String, _ syntax: SyntaxOptions = .traditional
3939
) throws -> Executor {
40-
let ast = try parse(regex, .semantic, syntax)
40+
let ast = try parse(regex, syntax)
4141
let program = try Compiler(ast: ast).emit()
4242
return Executor(program: program)
4343
}

Sources/_StringProcessing/Regex/AnyRegexOutput.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ extension Regex where Output == AnyRegexOutput {
143143
///
144144
/// - Parameter pattern: The regular expression.
145145
public init(_ pattern: String) throws {
146-
self.init(ast: try parse(pattern, .semantic, .traditional))
146+
self.init(ast: try parse(pattern, .traditional))
147147
}
148148
}
149149

@@ -157,7 +157,7 @@ extension Regex {
157157
_ pattern: String,
158158
as: Output.Type = Output.self
159159
) throws {
160-
self.init(ast: try parse(pattern, .semantic, .traditional))
160+
self.init(ast: try parse(pattern, .traditional))
161161
}
162162

163163
/// Produces a regex that matches `verbatim` exactly, as though every

Sources/_StringProcessing/Regex/Core.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public struct Regex<Output>: RegexComponent {
4444
// Compiler interface. Do not change independently.
4545
@usableFromInline
4646
init(_regexString pattern: String) {
47-
self.init(ast: try! parse(pattern, .semantic, .traditional))
47+
self.init(ast: try! parse(pattern, .traditional))
4848
}
4949

5050
// Compiler interface. Do not change independently.
@@ -53,7 +53,7 @@ public struct Regex<Output>: RegexComponent {
5353
assert(version == currentRegexLiteralFormatVersion)
5454
// The version argument is passed by the compiler using the value defined
5555
// in libswiftParseRegexLiteral.
56-
self.init(ast: try! parseWithDelimiters(pattern, .semantic))
56+
self.init(ast: try! parseWithDelimiters(pattern))
5757
}
5858

5959
public var regex: Regex<Output> {

Tests/RegexTests/CaptureTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func captureTest(
158158
file: StaticString = #file,
159159
line: UInt = #line
160160
) {
161-
let ast = try! parse(regex, .semantic, .traditional)
161+
let ast = try! parse(regex, .traditional)
162162
var capList = ast.captureList.withoutLocs
163163
// Peel off the whole match element.
164164
capList.captures.removeFirst()

Tests/RegexTests/DiagnosticTests.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension RegexTests {
2020
XCTAssert(SourceLocation.fake.isFake)
2121
XCTAssert(group(.capture, "a").location.isFake)
2222

23-
let ast = try! parse("(a)", .semantic, .traditional).root
23+
let ast = try! parse("(a)", .traditional).root
2424
XCTAssert(ast.location.isReal)
2525
}
2626

@@ -31,7 +31,7 @@ extension RegexTests {
3131
//
3232
// Input should be a concatenation or alternation
3333
func flatTest(_ str: String, _ expected: [String]) {
34-
guard let ast = try? parse(str, .semantic, .traditional).root else {
34+
guard let ast = try? parse(str, .traditional).root else {
3535
XCTFail("Fail to parse: \(str)")
3636
return
3737
}
@@ -53,9 +53,7 @@ extension RegexTests {
5353
flatTest("a|(b)|", ["a", "(b)", ""])
5454

5555
func renderTest(_ str: String, _ expected: [String]) {
56-
let lines = try! parse(
57-
str, .semantic, .traditional
58-
)._render(in: str)
56+
let lines = try! parse(str, .traditional)._render(in: str)
5957
func fail() {
6058
XCTFail("""
6159
expected:

Tests/RegexTests/RenderDSLTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func testConversion(
2121
_ expectedDSL: String,
2222
file: StaticString = #file, line: UInt = #line
2323
) throws {
24-
let ast = try _RegexParser.parse(regex, .semantic, .traditional)
24+
let ast = try _RegexParser.parse(regex, .traditional)
2525
let actualDSL = renderAsBuilderDSL(ast: ast)._trimmingSuffix(while: \.isWhitespace)
2626
XCTAssertEqual(actualDSL, expectedDSL[...], file: file, line: line)
2727
}

0 commit comments

Comments
 (0)