Skip to content

Commit 46c7157

Browse files
committed
Patch a Regression in Declaration Lookahead
1 parent c624b79 commit 46c7157

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ extension TokenConsumer {
3030
_ = subparser.consumeAttributeList()
3131
}
3232

33-
var modifierProgress = LoopProgressCondition()
34-
while let (modifierKind, handle) = subparser.at(anyIn: DeclarationModifier.self),
35-
modifierKind != .classKeyword,
36-
modifierProgress.evaluate(subparser.currentToken) {
37-
subparser.eat(handle)
38-
if subparser.at(.leftParen) {
39-
subparser.consumeAnyToken()
40-
subparser.consume(to: .rightParen)
33+
if subparser.currentToken.isKeyword {
34+
var modifierProgress = LoopProgressCondition()
35+
while let (modifierKind, handle) = subparser.at(anyIn: DeclarationModifier.self),
36+
modifierKind != .classKeyword,
37+
modifierProgress.evaluate(subparser.currentToken) {
38+
subparser.eat(handle)
39+
if subparser.at(.leftParen) {
40+
subparser.consumeAnyToken()
41+
subparser.consume(to: .rightParen)
42+
}
4143
}
4244
}
4345

@@ -75,10 +77,17 @@ extension TokenConsumer {
7577
lookahead.consumeAnyToken()
7678
} while lookahead.atStartOfDeclaration()
7779
return lookahead.at(.identifier)
78-
case .caseKeyword, nil:
80+
case .caseKeyword:
7981
// When 'case' appears inside a function, it's probably a switch
8082
// case, not an enum case declaration.
8183
return false
84+
case nil:
85+
if subparser.at(anyIn: ContextualDeclKeyword.self)?.0 != nil {
86+
subparser.consumeAnyToken()
87+
return subparser.atStartOfDeclaration(
88+
isAtTopLevel: isAtTopLevel, allowRecovery: allowRecovery)
89+
}
90+
return false
8291
default: return true
8392
}
8493
}

Tests/SwiftParserTest/Declarations.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ final class DeclarationTests: XCTestCase {
148148
}
149149

150150
func testVariableDeclarations() {
151+
AssertParse(
152+
"""
153+
z
154+
155+
var x: Double = z
156+
""")
157+
151158
AssertParse(
152159
"""
153160
async let a = fetch("1.jpg")

Tests/SwiftParserTest/Expressions.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ final class ExpressionTests: XCTestCase {
4848

4949
func testSequenceExpressions() {
5050
AssertParse("await a()")
51+
AssertParse(
52+
"""
53+
async let child = testNestedTaskPriority(basePri: basePri, curPri: curPri)
54+
await child
55+
""")
5156
}
5257

5358
func testNestedTypeSpecialization() {
@@ -508,4 +513,13 @@ final class ExpressionTests: XCTestCase {
508513
AssertParse("if <#test#> {}")
509514
AssertParse("if <#b1#>, <#b2#> {}")
510515
}
516+
517+
func testKeywordApplyExpression() {
518+
AssertParse(
519+
"""
520+
optional(x: .some(23))
521+
optional(x: .none)
522+
var pair : (Int, Double) = makePair(a: 1, b: 2.5)
523+
""")
524+
}
511525
}

0 commit comments

Comments
 (0)