Skip to content

Commit ff55c70

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 aae597b commit ff55c70

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
@@ -601,37 +601,20 @@ extension Parser {
601601
}
602602
}
603603

604-
public enum ASTStage {
605-
/// The regex is parsed, and a syntactically valid AST is returned. Otherwise
606-
/// an error is thrown. This is useful for e.g syntax coloring.
607-
case syntactic
608-
609-
/// The regex is parsed, and a syntactically and semantically valid AST is
610-
/// returned. Otherwise an error is thrown. A semantically valid AST has been
611-
/// checked for e.g unsupported constructs and invalid backreferences.
612-
case semantic
613-
}
614-
615604
public func parseWithRecovery<S: StringProtocol>(
616-
_ regex: S, _ syntax: SyntaxOptions, stage: ASTStage = .semantic
605+
_ regex: S, _ syntax: SyntaxOptions
617606
) -> AST where S.SubSequence == Substring
618607
{
619608
let source = Source(String(regex))
620609
var parser = Parser(source, syntax: syntax)
621-
let ast = parser.parse()
622-
switch stage {
623-
case .syntactic:
624-
return ast
625-
case .semantic:
626-
return validate(ast)
627-
}
610+
return validate(parser.parse())
628611
}
629612

630613
public func parse<S: StringProtocol>(
631-
_ regex: S, _ stage: ASTStage, _ syntax: SyntaxOptions
614+
_ regex: S, _ syntax: SyntaxOptions
632615
) throws -> AST where S.SubSequence == Substring
633616
{
634-
try parseWithRecovery(regex, syntax, stage: stage).ensureValid()
617+
try parseWithRecovery(regex, syntax).ensureValid()
635618
}
636619

637620
extension StringProtocol {
@@ -674,12 +657,12 @@ public func parseWithDelimitersWithRecovery<S: StringProtocol>(
674657
/// Parses a given regex string with delimiters, inferring the syntax options
675658
/// from the delimiters used.
676659
public func parseWithDelimiters<S: StringProtocol>(
677-
_ regex: S, _ stage: ASTStage
660+
_ regex: S
678661
) throws -> AST where S.SubSequence == Substring {
679662
let (contents, delim) = droppingRegexDelimiters(String(regex))
680663
let syntax = defaultSyntaxOptions(delim, contents: contents)
681664
do {
682-
return try parseWithRecovery(contents, syntax, stage: stage).ensureValid()
665+
return try parseWithRecovery(contents, syntax).ensureValid()
683666
} catch let error as LocatedErrorProtocol {
684667
// Convert the range in 'contents' to the range in 'regex'.
685668
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
internal init(_ pattern: String, syntax: SyntaxOptions) throws {
@@ -161,7 +161,7 @@ extension Regex {
161161
_ pattern: String,
162162
as: Output.Type = Output.self
163163
) throws {
164-
self.init(ast: try parse(pattern, .semantic, .traditional))
164+
self.init(ast: try parse(pattern, .traditional))
165165
}
166166

167167
/// 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)