Skip to content

Commit 57d102f

Browse files
authored
Merge pull request #2499 from dhruvviee/backtrack-lookahead-#2494
Issue #2494 Renamed backtrack variables to lookahead
2 parents 8802f12 + aa0e131 commit 57d102f

File tree

5 files changed

+41
-42
lines changed

5 files changed

+41
-42
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ extension TokenConsumer {
1616
mutating func atStartOfExpression() -> Bool {
1717
switch self.at(anyIn: ExpressionStart.self) {
1818
case (.awaitTryMove, let handle)?:
19-
var backtrack = self.lookahead()
20-
backtrack.eat(handle)
19+
var lookahead = self.lookahead()
20+
lookahead.eat(handle)
2121

2222
// These can be parsed as expressions with try/await.
23-
if backtrack.at(anyIn: SingleValueStatementExpression.self) != nil {
23+
if lookahead.at(anyIn: SingleValueStatementExpression.self) != nil {
2424
return true
2525
}
2626
// Note we currently pass `preferExpr: false` to prefer diagnosing `try then`
2727
// as needing to be `then try`, rather than parsing `then` as an expression.
28-
if backtrack.atStartOfDeclaration() || backtrack.atStartOfStatement(preferExpr: false) {
28+
if lookahead.atStartOfDeclaration() || lookahead.atStartOfStatement(preferExpr: false) {
2929
// If after the 'try' we are at a declaration or statement, it can't be a valid expression.
3030
// Decide how we want to consume the 'try':
3131
// If the declaration or statement starts at a new line, the user probably just forgot to write the expression after 'try' -> parse it as a TryExpr
3232
// If the declaration or statement starts at the same line, the user maybe tried to use 'try' as a modifier -> parse it as unexpected text in front of that decl or stmt.
33-
return backtrack.atStartOfLine
33+
return lookahead.atStartOfLine
3434
} else {
3535
return true
3636
}
@@ -42,8 +42,8 @@ extension TokenConsumer {
4242
break
4343
}
4444
if self.at(.atSign) || self.at(.keyword(.inout)) {
45-
var backtrack = self.lookahead()
46-
if backtrack.canParseType() {
45+
var lookahead = self.lookahead()
46+
if lookahead.canParseType() {
4747
return true
4848
}
4949
}
@@ -54,8 +54,8 @@ extension TokenConsumer {
5454
// expansion, but we need to do more lookahead to figure out
5555
// whether the '{' is the start of a closure expression or a
5656
// brace statement for 'repeat { ... } while'
57-
let backtrack = self.lookahead()
58-
return backtrack.peek().rawTokenKind != .leftBrace
57+
let lookahead = self.lookahead()
58+
return lookahead.peek().rawTokenKind != .leftBrace
5959
}
6060

6161
return false
@@ -397,8 +397,8 @@ extension Parser {
397397
) -> RawExprSyntax {
398398
// Try to parse '@' sign or 'inout' as an attributed typerepr.
399399
if self.at(.atSign, .keyword(.inout)) {
400-
var backtrack = self.lookahead()
401-
if backtrack.canParseType() {
400+
var lookahead = self.lookahead()
401+
if lookahead.canParseType() {
402402
let type = self.parseType()
403403
return RawExprSyntax(
404404
RawTypeExprSyntax(
@@ -1965,12 +1965,12 @@ extension Parser.Lookahead {
19651965

19661966
// Do some tentative parsing to distinguish `label: { ... }` and
19671967
// `label: switch x { ... }`.
1968-
var backtrack = self.lookahead()
1969-
backtrack.consumeAnyToken()
1970-
if backtrack.peek().rawTokenKind == .leftBrace {
1968+
var lookahead = self.lookahead()
1969+
lookahead.consumeAnyToken()
1970+
if lookahead.peek().rawTokenKind == .leftBrace {
19711971
return true
19721972
}
1973-
if backtrack.peek().isEditorPlaceholder {
1973+
if lookahead.peek().isEditorPlaceholder {
19741974
// Editor placeholder can represent entire closures
19751975
return true
19761976
}
@@ -2022,21 +2022,21 @@ extension Parser.Lookahead {
20222022
// Determine if the {} goes with the expression by eating it, and looking
20232023
// to see if it is immediately followed by a token which indicates we should
20242024
// consider it part of the preceding expression
2025-
var backtrack = self.lookahead()
2026-
backtrack.eat(.leftBrace)
2025+
var lookahead = self.lookahead()
2026+
lookahead.eat(.leftBrace)
20272027
var loopProgress = LoopProgressCondition()
2028-
while !backtrack.at(.endOfFile, .rightBrace)
2029-
&& !backtrack.at(.poundEndif, .poundElse, .poundElseif)
2030-
&& backtrack.hasProgressed(&loopProgress)
2028+
while !lookahead.at(.endOfFile, .rightBrace)
2029+
&& !lookahead.at(.poundEndif, .poundElse, .poundElseif)
2030+
&& lookahead.hasProgressed(&loopProgress)
20312031
{
2032-
backtrack.skipSingle()
2032+
lookahead.skipSingle()
20332033
}
20342034

2035-
guard backtrack.consume(if: .rightBrace) != nil else {
2035+
guard lookahead.consume(if: .rightBrace) != nil else {
20362036
return false
20372037
}
20382038

2039-
switch backtrack.currentToken {
2039+
switch lookahead.currentToken {
20402040
case TokenSpec(.leftBrace),
20412041
TokenSpec(.where),
20422042
TokenSpec(.comma):
@@ -2053,7 +2053,7 @@ extension Parser.Lookahead {
20532053
TokenSpec(.equal),
20542054
TokenSpec(.postfixOperator),
20552055
TokenSpec(.binaryOperator):
2056-
return !backtrack.atStartOfLine
2056+
return !lookahead.atStartOfLine
20572057
default:
20582058
return false
20592059
}

Sources/SwiftParser/Lookahead.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ extension Parser.Lookahead {
178178
// Recover by eating @foo(...)
179179
self.eat(handle)
180180
if self.at(.leftParen) {
181-
var backtrack = self.lookahead()
182-
backtrack.skipSingle()
181+
var lookahead = self.lookahead()
182+
lookahead.skipSingle()
183183
// If we found '->', or 'throws' after paren, it's likely a parameter
184184
// of function type.
185-
guard backtrack.at(.arrow) || backtrack.at(.keyword(.throws), .keyword(.rethrows), .keyword(.throw)) else {
185+
guard lookahead.at(.arrow) || lookahead.at(.keyword(.throws), .keyword(.rethrows), .keyword(.throw)) else {
186186
self.skipSingle()
187187
return
188188
}

Sources/SwiftParser/Names.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,10 @@ extension Parser {
205205
// If qualified name base type cannot be parsed from the current
206206
// point (i.e. the next type identifier is not followed by a '.'),
207207
// then the next identifier is the final declaration name component.
208-
var backtrack = self.lookahead()
208+
var lookahead = self.lookahead()
209209
guard
210-
backtrack.consume(ifPrefix: ".", as: .period) != nil,
211-
backtrack.canParseBaseTypeForQualifiedDeclName()
210+
lookahead.consume(ifPrefix: ".", as: .period) != nil,
211+
lookahead.canParseBaseTypeForQualifiedDeclName()
212212
else {
213213
return result
214214
}
@@ -237,10 +237,10 @@ extension Parser {
237237
// If qualified name base type cannot be parsed from the current
238238
// point (i.e. the next type identifier is not followed by a '.'),
239239
// then the next identifier is the final declaration name component.
240-
var backtrack = self.lookahead()
240+
var lookahead = self.lookahead()
241241
guard
242-
backtrack.consume(ifPrefix: ".", as: .period) != nil,
243-
backtrack.canParseBaseTypeForQualifiedDeclName()
242+
lookahead.consume(ifPrefix: ".", as: .period) != nil,
243+
lookahead.canParseBaseTypeForQualifiedDeclName()
244244
else {
245245
break
246246
}

Sources/SwiftParser/Statements.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ extension Parser {
208208
// However, if this is the first clause, and we see "x = y", then this is
209209
// almost certainly a typo for '==' and definitely not a continuation of
210210
// another clause, so parse it as an expression. This also avoids
211-
// lookahead + backtracking on simple if conditions that are obviously
212-
// boolean conditions.
211+
// lookahead on simple if conditions that are obviously boolean conditions.
213212
return .expression(self.parseExpression(flavor: .stmtCondition, pattern: .none))
214213
}
215214

Sources/SwiftParser/Types.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -816,16 +816,16 @@ extension Parser.Lookahead {
816816
}
817817

818818
if effect.spec.isThrowsSpecifier && self.peek().rawTokenKind == .leftParen {
819-
var backtrack = self.lookahead()
820-
backtrack.consumeAnyToken()
821-
backtrack.skipSingle()
822-
return backtrack.atFunctionTypeArrow()
819+
var lookahead = self.lookahead()
820+
lookahead.consumeAnyToken()
821+
lookahead.skipSingle()
822+
return lookahead.atFunctionTypeArrow()
823823
}
824824

825825
if peek(isAtAnyIn: EffectSpecifier.self) != nil {
826-
var backtrack = self.lookahead()
827-
backtrack.consumeAnyToken()
828-
return backtrack.atFunctionTypeArrow()
826+
var lookahead = self.lookahead()
827+
lookahead.consumeAnyToken()
828+
return lookahead.atFunctionTypeArrow()
829829
}
830830

831831
return false

0 commit comments

Comments
 (0)