Skip to content

Commit ec4fea0

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 eb2b008 commit ec4fea0

File tree

9 files changed

+19
-39
lines changed

9 files changed

+19
-39
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
@@ -636,37 +636,20 @@ extension Parser {
636636
}
637637
}
638638

639-
public enum ASTStage {
640-
/// The regex is parsed, and a syntactically valid AST is returned. Otherwise
641-
/// an error is thrown. This is useful for e.g syntax coloring.
642-
case syntactic
643-
644-
/// The regex is parsed, and a syntactically and semantically valid AST is
645-
/// returned. Otherwise an error is thrown. A semantically valid AST has been
646-
/// checked for e.g unsupported constructs and invalid backreferences.
647-
case semantic
648-
}
649-
650639
public func parseWithRecovery<S: StringProtocol>(
651-
_ regex: S, _ syntax: SyntaxOptions, stage: ASTStage = .semantic
640+
_ regex: S, _ syntax: SyntaxOptions
652641
) -> AST where S.SubSequence == Substring
653642
{
654643
let source = Source(String(regex))
655644
var parser = Parser(source, syntax: syntax)
656-
let ast = parser.parse()
657-
switch stage {
658-
case .syntactic:
659-
return ast
660-
case .semantic:
661-
return validate(ast)
662-
}
645+
return validate(parser.parse())
663646
}
664647

665648
public func parse<S: StringProtocol>(
666-
_ regex: S, _ stage: ASTStage, _ syntax: SyntaxOptions
649+
_ regex: S, _ syntax: SyntaxOptions
667650
) throws -> AST where S.SubSequence == Substring
668651
{
669-
try parseWithRecovery(regex, syntax, stage: stage).ensureValid()
652+
try parseWithRecovery(regex, syntax).ensureValid()
670653
}
671654

672655
extension StringProtocol {
@@ -709,12 +692,12 @@ public func parseWithDelimitersWithRecovery<S: StringProtocol>(
709692
/// Parses a given regex string with delimiters, inferring the syntax options
710693
/// from the delimiters used.
711694
public func parseWithDelimiters<S: StringProtocol>(
712-
_ regex: S, _ stage: ASTStage
695+
_ regex: S
713696
) throws -> AST where S.SubSequence == Substring {
714697
let (contents, delim) = droppingRegexDelimiters(String(regex))
715698
let syntax = defaultSyntaxOptions(delim, contents: contents)
716699
do {
717-
return try parseWithRecovery(contents, syntax, stage: stage).ensureValid()
700+
return try parseWithRecovery(contents, syntax).ensureValid()
718701
} catch let error as LocatedErrorProtocol {
719702
// Convert the range in 'contents' to the range in 'regex'.
720703
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ 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 {
150-
self.init(ast: try parse(pattern, .semantic, syntax))
150+
self.init(ast: try parse(pattern, syntax))
151151
}
152152
}
153153

@@ -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)