Skip to content

Commit 7117363

Browse files
authored
Merge pull request #347 from hamishknight/pitter-patter
2 parents 24316cc + 19bc5ef commit 7117363

16 files changed

+275
-3
lines changed

Sources/SwiftSyntax/gyb_generated/Misc.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ extension SyntaxNode {
433433
return StringLiteralExprSyntax(asSyntaxData)
434434
}
435435

436+
public var isRegexLiteralExpr: Bool { return raw.kind == .regexLiteralExpr }
437+
public var asRegexLiteralExpr: RegexLiteralExprSyntax? {
438+
guard isRegexLiteralExpr else { return nil }
439+
return RegexLiteralExprSyntax(asSyntaxData)
440+
}
441+
436442
public var isKeyPathExpr: Bool { return raw.kind == .keyPathExpr }
437443
public var asKeyPathExpr: KeyPathExprSyntax? {
438444
guard isKeyPathExpr else { return nil }
@@ -1625,6 +1631,8 @@ extension Syntax {
16251631
return node
16261632
case .stringLiteralExpr(let node):
16271633
return node
1634+
case .regexLiteralExpr(let node):
1635+
return node
16281636
case .keyPathExpr(let node):
16291637
return node
16301638
case .keyPathBaseExpr(let node):

Sources/SwiftSyntax/gyb_generated/SyntaxAnyVisitor.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,13 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
547547
override open func visitPost(_ node: StringLiteralExprSyntax) {
548548
visitAnyPost(node._syntaxNode)
549549
}
550+
override open func visit(_ node: RegexLiteralExprSyntax) -> SyntaxVisitorContinueKind {
551+
return visitAny(node._syntaxNode)
552+
}
553+
554+
override open func visitPost(_ node: RegexLiteralExprSyntax) {
555+
visitAnyPost(node._syntaxNode)
556+
}
550557
override open func visit(_ node: KeyPathExprSyntax) -> SyntaxVisitorContinueKind {
551558
return visitAny(node._syntaxNode)
552559
}

Sources/SwiftSyntax/gyb_generated/SyntaxBaseNodes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
164164
/// `nil` if the conversion is not possible.
165165
public init?(_ syntax: Syntax) {
166166
switch syntax.raw.kind {
167-
case .unknownExpr, .inOutExpr, .poundColumnExpr, .tryExpr, .awaitExpr, .identifierExpr, .superRefExpr, .nilLiteralExpr, .discardAssignmentExpr, .assignmentExpr, .sequenceExpr, .poundLineExpr, .poundFileExpr, .poundFileIDExpr, .poundFilePathExpr, .poundFunctionExpr, .poundDsohandleExpr, .symbolicReferenceExpr, .prefixOperatorExpr, .binaryOperatorExpr, .arrowExpr, .floatLiteralExpr, .tupleExpr, .arrayExpr, .dictionaryExpr, .integerLiteralExpr, .booleanLiteralExpr, .ternaryExpr, .memberAccessExpr, .isExpr, .asExpr, .typeExpr, .closureExpr, .unresolvedPatternExpr, .functionCallExpr, .subscriptExpr, .optionalChainingExpr, .forcedValueExpr, .postfixUnaryExpr, .specializeExpr, .stringLiteralExpr, .keyPathExpr, .keyPathBaseExpr, .objcKeyPathExpr, .objcSelectorExpr, .postfixIfConfigExpr, .editorPlaceholderExpr, .objectLiteralExpr:
167+
case .unknownExpr, .inOutExpr, .poundColumnExpr, .tryExpr, .awaitExpr, .identifierExpr, .superRefExpr, .nilLiteralExpr, .discardAssignmentExpr, .assignmentExpr, .sequenceExpr, .poundLineExpr, .poundFileExpr, .poundFileIDExpr, .poundFilePathExpr, .poundFunctionExpr, .poundDsohandleExpr, .symbolicReferenceExpr, .prefixOperatorExpr, .binaryOperatorExpr, .arrowExpr, .floatLiteralExpr, .tupleExpr, .arrayExpr, .dictionaryExpr, .integerLiteralExpr, .booleanLiteralExpr, .ternaryExpr, .memberAccessExpr, .isExpr, .asExpr, .typeExpr, .closureExpr, .unresolvedPatternExpr, .functionCallExpr, .subscriptExpr, .optionalChainingExpr, .forcedValueExpr, .postfixUnaryExpr, .specializeExpr, .stringLiteralExpr, .regexLiteralExpr, .keyPathExpr, .keyPathBaseExpr, .objcKeyPathExpr, .objcSelectorExpr, .postfixIfConfigExpr, .editorPlaceholderExpr, .objectLiteralExpr:
168168
self._syntaxNode = syntax
169169
default:
170170
return nil
@@ -178,7 +178,7 @@ public struct ExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
178178
// Assert that the kind of the given data matches in debug builds.
179179
#if DEBUG
180180
switch data.raw.kind {
181-
case .unknownExpr, .inOutExpr, .poundColumnExpr, .tryExpr, .awaitExpr, .identifierExpr, .superRefExpr, .nilLiteralExpr, .discardAssignmentExpr, .assignmentExpr, .sequenceExpr, .poundLineExpr, .poundFileExpr, .poundFileIDExpr, .poundFilePathExpr, .poundFunctionExpr, .poundDsohandleExpr, .symbolicReferenceExpr, .prefixOperatorExpr, .binaryOperatorExpr, .arrowExpr, .floatLiteralExpr, .tupleExpr, .arrayExpr, .dictionaryExpr, .integerLiteralExpr, .booleanLiteralExpr, .ternaryExpr, .memberAccessExpr, .isExpr, .asExpr, .typeExpr, .closureExpr, .unresolvedPatternExpr, .functionCallExpr, .subscriptExpr, .optionalChainingExpr, .forcedValueExpr, .postfixUnaryExpr, .specializeExpr, .stringLiteralExpr, .keyPathExpr, .keyPathBaseExpr, .objcKeyPathExpr, .objcSelectorExpr, .postfixIfConfigExpr, .editorPlaceholderExpr, .objectLiteralExpr:
181+
case .unknownExpr, .inOutExpr, .poundColumnExpr, .tryExpr, .awaitExpr, .identifierExpr, .superRefExpr, .nilLiteralExpr, .discardAssignmentExpr, .assignmentExpr, .sequenceExpr, .poundLineExpr, .poundFileExpr, .poundFileIDExpr, .poundFilePathExpr, .poundFunctionExpr, .poundDsohandleExpr, .symbolicReferenceExpr, .prefixOperatorExpr, .binaryOperatorExpr, .arrowExpr, .floatLiteralExpr, .tupleExpr, .arrayExpr, .dictionaryExpr, .integerLiteralExpr, .booleanLiteralExpr, .ternaryExpr, .memberAccessExpr, .isExpr, .asExpr, .typeExpr, .closureExpr, .unresolvedPatternExpr, .functionCallExpr, .subscriptExpr, .optionalChainingExpr, .forcedValueExpr, .postfixUnaryExpr, .specializeExpr, .stringLiteralExpr, .regexLiteralExpr, .keyPathExpr, .keyPathBaseExpr, .objcKeyPathExpr, .objcSelectorExpr, .postfixIfConfigExpr, .editorPlaceholderExpr, .objectLiteralExpr:
182182
break
183183
default:
184184
fatalError("Unable to create ExprSyntax from \(data.raw.kind)")

Sources/SwiftSyntax/gyb_generated/SyntaxBuilders.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,6 +2673,44 @@ extension StringLiteralExprSyntax {
26732673
}
26742674
}
26752675

2676+
public struct RegexLiteralExprSyntaxBuilder {
2677+
private var layout =
2678+
Array<RawSyntax?>(repeating: nil, count: 1)
2679+
2680+
internal init() {}
2681+
2682+
public mutating func useRegex(_ node: TokenSyntax) {
2683+
let idx = RegexLiteralExprSyntax.Cursor.regex.rawValue
2684+
layout[idx] = node.raw
2685+
}
2686+
2687+
internal mutating func buildData() -> SyntaxData {
2688+
if (layout[0] == nil) {
2689+
layout[0] = RawSyntax.missingToken(TokenKind.regexLiteral(""))
2690+
}
2691+
2692+
return .forRoot(RawSyntax.createAndCalcLength(kind: .regexLiteralExpr,
2693+
layout: layout, presence: .present))
2694+
}
2695+
}
2696+
2697+
extension RegexLiteralExprSyntax {
2698+
/// Creates a `RegexLiteralExprSyntax` using the provided build function.
2699+
/// - Parameter:
2700+
/// - build: A closure that will be invoked in order to initialize
2701+
/// the fields of the syntax node.
2702+
/// This closure is passed a `RegexLiteralExprSyntaxBuilder` which you can use to
2703+
/// incrementally build the structure of the node.
2704+
/// - Returns: A `RegexLiteralExprSyntax` with all the fields populated in the builder
2705+
/// closure.
2706+
public init(_ build: (inout RegexLiteralExprSyntaxBuilder) -> Void) {
2707+
var builder = RegexLiteralExprSyntaxBuilder()
2708+
build(&builder)
2709+
let data = builder.buildData()
2710+
self.init(data)
2711+
}
2712+
}
2713+
26762714
public struct KeyPathExprSyntaxBuilder {
26772715
private var layout =
26782716
Array<RawSyntax?>(repeating: nil, count: 3)

Sources/SwiftSyntax/gyb_generated/SyntaxClassification.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ extension RawTokenKind {
342342
return .floatingLiteral
343343
case .stringLiteral:
344344
return .stringLiteral
345+
case .regexLiteral:
346+
return .none
345347
case .unknown:
346348
return .none
347349
case .identifier:

Sources/SwiftSyntax/gyb_generated/SyntaxEnum.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public enum SyntaxEnum {
9090
case stringSegment(StringSegmentSyntax)
9191
case expressionSegment(ExpressionSegmentSyntax)
9292
case stringLiteralExpr(StringLiteralExprSyntax)
93+
case regexLiteralExpr(RegexLiteralExprSyntax)
9394
case keyPathExpr(KeyPathExprSyntax)
9495
case keyPathBaseExpr(KeyPathBaseExprSyntax)
9596
case objcNamePiece(ObjcNamePieceSyntax)
@@ -419,6 +420,8 @@ public extension Syntax {
419420
return .expressionSegment(ExpressionSegmentSyntax(self)!)
420421
case .stringLiteralExpr:
421422
return .stringLiteralExpr(StringLiteralExprSyntax(self)!)
423+
case .regexLiteralExpr:
424+
return .regexLiteralExpr(RegexLiteralExprSyntax(self)!)
422425
case .keyPathExpr:
423426
return .keyPathExpr(KeyPathExprSyntax(self)!)
424427
case .keyPathBaseExpr:

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,23 @@ public enum SyntaxFactory {
12971297
], length: .zero, presence: .present))
12981298
return StringLiteralExprSyntax(data)
12991299
}
1300+
public static func makeRegexLiteralExpr(regex: TokenSyntax) -> RegexLiteralExprSyntax {
1301+
let layout: [RawSyntax?] = [
1302+
regex.raw,
1303+
]
1304+
let raw = RawSyntax.createAndCalcLength(kind: SyntaxKind.regexLiteralExpr,
1305+
layout: layout, presence: SourcePresence.present)
1306+
let data = SyntaxData.forRoot(raw)
1307+
return RegexLiteralExprSyntax(data)
1308+
}
1309+
1310+
public static func makeBlankRegexLiteralExpr() -> RegexLiteralExprSyntax {
1311+
let data = SyntaxData.forRoot(RawSyntax.create(kind: .regexLiteralExpr,
1312+
layout: [
1313+
RawSyntax.missingToken(TokenKind.regexLiteral("")),
1314+
], length: .zero, presence: .present))
1315+
return RegexLiteralExprSyntax(data)
1316+
}
13001317
public static func makeKeyPathExpr(backslash: TokenSyntax, rootExpr: ExprSyntax?, expression: ExprSyntax) -> KeyPathExprSyntax {
13011318
let layout: [RawSyntax?] = [
13021319
backslash.raw,
@@ -5516,6 +5533,12 @@ public enum SyntaxFactory {
55165533
leadingTrivia: leadingTrivia,
55175534
trailingTrivia: trailingTrivia)
55185535
}
5536+
public static func makeRegexLiteral(_ text: String,
5537+
leadingTrivia: Trivia = [], trailingTrivia: Trivia = []) -> TokenSyntax {
5538+
return makeToken(.regexLiteral(text), presence: .present,
5539+
leadingTrivia: leadingTrivia,
5540+
trailingTrivia: trailingTrivia)
5541+
}
55195542
public static func makeUnknown(_ text: String,
55205543
leadingTrivia: Trivia = [], trailingTrivia: Trivia = []) -> TokenSyntax {
55215544
return makeToken(.unknown(text), presence: .present,

Sources/SwiftSyntax/gyb_generated/SyntaxKind.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ internal enum SyntaxKind: CSyntaxKind {
9090
case stringSegment = 104
9191
case expressionSegment = 105
9292
case stringLiteralExpr = 48
93+
case regexLiteralExpr = 253
9394
case keyPathExpr = 65
9495
case keyPathBaseExpr = 66
9596
case objcNamePiece = 106

Sources/SwiftSyntax/gyb_generated/SyntaxRewriter.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,13 @@ open class SyntaxRewriter {
506506
return ExprSyntax(visitChildren(node))
507507
}
508508

509+
/// Visit a `RegexLiteralExprSyntax`.
510+
/// - Parameter node: the node that is being visited
511+
/// - Returns: the rewritten node
512+
open func visit(_ node: RegexLiteralExprSyntax) -> ExprSyntax {
513+
return ExprSyntax(visitChildren(node))
514+
}
515+
509516
/// Visit a `KeyPathExprSyntax`.
510517
/// - Parameter node: the node that is being visited
511518
/// - Returns: the rewritten node
@@ -2484,6 +2491,16 @@ open class SyntaxRewriter {
24842491
return Syntax(visit(node))
24852492
}
24862493

2494+
/// Implementation detail of visit(_:). Do not call directly.
2495+
private func visitImplRegexLiteralExprSyntax(_ data: SyntaxData) -> Syntax {
2496+
let node = RegexLiteralExprSyntax(data)
2497+
// Accessing _syntaxNode directly is faster than calling Syntax(node)
2498+
visitPre(node._syntaxNode)
2499+
defer { visitPost(node._syntaxNode) }
2500+
if let newNode = visitAny(node._syntaxNode) { return newNode }
2501+
return Syntax(visit(node))
2502+
}
2503+
24872504
/// Implementation detail of visit(_:). Do not call directly.
24882505
private func visitImplKeyPathExprSyntax(_ data: SyntaxData) -> Syntax {
24892506
let node = KeyPathExprSyntax(data)
@@ -4394,6 +4411,8 @@ open class SyntaxRewriter {
43944411
return visitImplExpressionSegmentSyntax
43954412
case .stringLiteralExpr:
43964413
return visitImplStringLiteralExprSyntax
4414+
case .regexLiteralExpr:
4415+
return visitImplRegexLiteralExprSyntax
43974416
case .keyPathExpr:
43984417
return visitImplKeyPathExprSyntax
43994418
case .keyPathBaseExpr:
@@ -4899,6 +4918,8 @@ open class SyntaxRewriter {
48994918
return visitImplExpressionSegmentSyntax(data)
49004919
case .stringLiteralExpr:
49014920
return visitImplStringLiteralExprSyntax(data)
4921+
case .regexLiteralExpr:
4922+
return visitImplRegexLiteralExprSyntax(data)
49024923
case .keyPathExpr:
49034924
return visitImplKeyPathExprSyntax(data)
49044925
case .keyPathBaseExpr:

Sources/SwiftSyntax/gyb_generated/SyntaxVisitor.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,16 @@ open class SyntaxVisitor {
722722
/// The function called after visiting `StringLiteralExprSyntax` and its descendents.
723723
/// - node: the node we just finished visiting.
724724
open func visitPost(_ node: StringLiteralExprSyntax) {}
725+
/// Visiting `RegexLiteralExprSyntax` specifically.
726+
/// - Parameter node: the node we are visiting.
727+
/// - Returns: how should we continue visiting.
728+
open func visit(_ node: RegexLiteralExprSyntax) -> SyntaxVisitorContinueKind {
729+
return .visitChildren
730+
}
731+
732+
/// The function called after visiting `RegexLiteralExprSyntax` and its descendents.
733+
/// - node: the node we just finished visiting.
734+
open func visitPost(_ node: RegexLiteralExprSyntax) {}
725735
/// Visiting `KeyPathExprSyntax` specifically.
726736
/// - Parameter node: the node we are visiting.
727737
/// - Returns: how should we continue visiting.
@@ -3269,6 +3279,17 @@ open class SyntaxVisitor {
32693279
visitPost(node)
32703280
}
32713281

3282+
/// Implementation detail of doVisit(_:_:). Do not call directly.
3283+
private func visitImplRegexLiteralExprSyntax(_ data: SyntaxData) {
3284+
let node = RegexLiteralExprSyntax(data)
3285+
let needsChildren = (visit(node) == .visitChildren)
3286+
// Avoid calling into visitChildren if possible.
3287+
if needsChildren && node.raw.numberOfChildren > 0 {
3288+
visitChildren(node)
3289+
}
3290+
visitPost(node)
3291+
}
3292+
32723293
/// Implementation detail of doVisit(_:_:). Do not call directly.
32733294
private func visitImplKeyPathExprSyntax(_ data: SyntaxData) {
32743295
let node = KeyPathExprSyntax(data)
@@ -5318,6 +5339,8 @@ open class SyntaxVisitor {
53185339
visitImplExpressionSegmentSyntax(data)
53195340
case .stringLiteralExpr:
53205341
visitImplStringLiteralExprSyntax(data)
5342+
case .regexLiteralExpr:
5343+
visitImplRegexLiteralExprSyntax(data)
53215344
case .keyPathExpr:
53225345
visitImplKeyPathExprSyntax(data)
53235346
case .keyPathBaseExpr:

0 commit comments

Comments
 (0)