Skip to content

Commit c64b8c7

Browse files
committed
Add trailing space for ! and ?
Fixes #853
1 parent 6656208 commit c64b8c7

File tree

6 files changed

+116
-6
lines changed

6 files changed

+116
-6
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/TokenSpec.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ public let SYNTAX_TOKENS: [TokenSpec] = [
275275
PunctuatorSpec(name: "Arrow", kind: "arrow", text: "->", requiresLeadingSpace: true, requiresTrailingSpace: true),
276276
PunctuatorSpec(name: "Backtick", kind: "backtick", text: "`"),
277277
PunctuatorSpec(name: "Backslash", kind: "backslash", text: "\\"),
278-
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!"),
279-
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?"),
278+
PunctuatorSpec(name: "ExclamationMark", kind: "exclaim_postfix", text: "!", requiresTrailingSpace: true),
279+
PunctuatorSpec(name: "PostfixQuestionMark", kind: "question_postfix", text: "?", requiresTrailingSpace: true),
280280
PunctuatorSpec(name: "InfixQuestionMark", kind: "question_infix", text: "?"),
281281
PunctuatorSpec(name: "StringQuote", kind: "string_quote", text: "\"", classification: "StringLiteral"),
282282
PunctuatorSpec(name: "SingleQuote", kind: "single_quote", text: "\'", classification: "StringLiteral"),

CodeGeneration/Sources/generate-swiftbasicformat/BasicFormatFile.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ let basicFormatFile = SourceFile {
159159
(.leftAngle, .identifier(_)),
160160
(.rightAngle, .leftParen),
161161
(.rightAngle, .postfixQuestionMark),
162+
(.postfixQuestionMark, .leftParen),
163+
(.postfixQuestionMark, .rightParen),
162164
(.tryKeyword, .exclamationMark),
163165
(.tryKeyword, .postfixQuestionMark):
164166
return false

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ open class BasicFormat: SyntaxRewriter {
165165
(.leftAngle, .identifier(_ )),
166166
(.rightAngle, .leftParen),
167167
(.rightAngle, .postfixQuestionMark),
168+
(.postfixQuestionMark, .leftParen),
169+
(.postfixQuestionMark, .rightParen),
168170
(.tryKeyword, .exclamationMark),
169171
(.tryKeyword, .postfixQuestionMark):
170172
return false
@@ -276,6 +278,10 @@ open class BasicFormat: SyntaxRewriter {
276278
return true
277279
case .arrow:
278280
return true
281+
case .exclamationMark:
282+
return true
283+
case .postfixQuestionMark:
284+
return true
279285
case .poundKeyPathKeyword:
280286
return true
281287
case .poundLineKeyword:

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9403,7 +9403,7 @@ public enum SyntaxFactory {
94039403
@available(*, deprecated, message: "Use TokenSyntax.exclamationMarkToken instead")
94049404
public static func makeExclamationMarkToken(
94059405
leadingTrivia: Trivia = [],
9406-
trailingTrivia: Trivia = []
9406+
trailingTrivia: Trivia = .space
94079407
) -> TokenSyntax {
94089408
return makeToken(.exclamationMark, presence: .present,
94099409
leadingTrivia: leadingTrivia,
@@ -9412,7 +9412,7 @@ public enum SyntaxFactory {
94129412
@available(*, deprecated, message: "Use TokenSyntax.postfixQuestionMarkToken instead")
94139413
public static func makePostfixQuestionMarkToken(
94149414
leadingTrivia: Trivia = [],
9415-
trailingTrivia: Trivia = []
9415+
trailingTrivia: Trivia = .space
94169416
) -> TokenSyntax {
94179417
return makeToken(.postfixQuestionMark, presence: .present,
94189418
leadingTrivia: leadingTrivia,

Tests/SwiftSyntaxBuilderTest/DoStmtTests.swift

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,104 @@ final class DoStmtTests: XCTestCase {
6464
"""
6565
)
6666
}
67+
68+
func testDoStmtWithExclamationMark() {
69+
let buildable = DoStmt(
70+
body: CodeBlock(statementsBuilder: {
71+
TryExpr(questionOrExclamationMark: .exclamationMark, expression: FunctionCallExpr(callee: ExprSyntax("a.b")))
72+
}),
73+
catchClauses: [
74+
CatchClause(
75+
CatchItemList {
76+
CatchItem(pattern: PatternSyntax("Error1"))
77+
CatchItem(pattern: PatternSyntax("Error2"))
78+
}
79+
) {
80+
FunctionCallExpr(callee: ExprSyntax("print")) {
81+
TupleExprElement(expression: StringLiteralExpr(content: "Known error"))
82+
}
83+
},
84+
CatchClause(
85+
CatchItemList {
86+
CatchItem(
87+
pattern: PatternSyntax("Error3"),
88+
whereClause: WhereClause(guardResult: MemberAccessExpr(base: "error", name: "isError4"))
89+
)
90+
}
91+
) {
92+
ThrowStmt(expression: MemberAccessExpr(base: "Error4", name: "error3"))
93+
},
94+
CatchClause {
95+
FunctionCallExpr(callee: ExprSyntax("print")) {
96+
TupleExprElement(expression: "error")
97+
}
98+
},
99+
]
100+
)
101+
102+
AssertBuildResult(
103+
buildable,
104+
"""
105+
do {
106+
try! a.b()
107+
} catch Error1, Error2 {
108+
print("Known error")
109+
} catch Error3 where error.isError4 {
110+
throw Error4.error3
111+
} catch {
112+
print(error)
113+
}
114+
"""
115+
)
116+
}
117+
118+
func testDoStmtWithPostfixQuestionMark() {
119+
let buildable = DoStmt(
120+
body: CodeBlock(statementsBuilder: {
121+
TryExpr(questionOrExclamationMark: .postfixQuestionMark, expression: FunctionCallExpr(callee: ExprSyntax("a.b")))
122+
}),
123+
catchClauses: [
124+
CatchClause(
125+
CatchItemList {
126+
CatchItem(pattern: PatternSyntax("Error1"))
127+
CatchItem(pattern: PatternSyntax("Error2"))
128+
}
129+
) {
130+
FunctionCallExpr(callee: ExprSyntax("print")) {
131+
TupleExprElement(expression: StringLiteralExpr(content: "Known error"))
132+
}
133+
},
134+
CatchClause(
135+
CatchItemList {
136+
CatchItem(
137+
pattern: PatternSyntax("Error3"),
138+
whereClause: WhereClause(guardResult: MemberAccessExpr(base: "error", name: "isError4"))
139+
)
140+
}
141+
) {
142+
ThrowStmt(expression: MemberAccessExpr(base: "Error4", name: "error3"))
143+
},
144+
CatchClause {
145+
FunctionCallExpr(callee: ExprSyntax("print")) {
146+
TupleExprElement(expression: "error")
147+
}
148+
},
149+
]
150+
)
151+
152+
AssertBuildResult(
153+
buildable,
154+
"""
155+
do {
156+
try? a.b()
157+
} catch Error1, Error2 {
158+
print("Known error")
159+
} catch Error3 where error.isError4 {
160+
throw Error4.error3
161+
} catch {
162+
print(error)
163+
}
164+
"""
165+
)
166+
}
67167
}

gyb_syntax_support/Token.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,11 @@ def macro_name(self):
277277

278278
Punctuator('Backslash', 'backslash', text='\\\\'),
279279

280-
Punctuator('ExclamationMark', 'exclaim_postfix', text='!'),
280+
Punctuator('ExclamationMark', 'exclaim_postfix', text='!',
281+
requires_trailing_space=True),
281282

282-
Punctuator('PostfixQuestionMark', 'question_postfix', text='?'),
283+
Punctuator('PostfixQuestionMark', 'question_postfix', text='?',
284+
requires_trailing_space=True),
283285
Punctuator('InfixQuestionMark', 'question_infix', text='?'),
284286

285287
Punctuator('StringQuote', 'string_quote', text='\\\"',

0 commit comments

Comments
 (0)