Skip to content

Issue #2494 Renamed backtrack variables to lookahead #2499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ extension TokenConsumer {
mutating func atStartOfExpression() -> Bool {
switch self.at(anyIn: ExpressionStart.self) {
case (.awaitTryMove, let handle)?:
var backtrack = self.lookahead()
backtrack.eat(handle)
var lookahead = self.lookahead()
lookahead.eat(handle)

// These can be parsed as expressions with try/await.
if backtrack.at(anyIn: SingleValueStatementExpression.self) != nil {
if lookahead.at(anyIn: SingleValueStatementExpression.self) != nil {
return true
}
// Note we currently pass `preferExpr: false` to prefer diagnosing `try then`
// as needing to be `then try`, rather than parsing `then` as an expression.
if backtrack.atStartOfDeclaration() || backtrack.atStartOfStatement(preferExpr: false) {
if lookahead.atStartOfDeclaration() || lookahead.atStartOfStatement(preferExpr: false) {
// If after the 'try' we are at a declaration or statement, it can't be a valid expression.
// Decide how we want to consume the 'try':
// 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
// 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.
return backtrack.atStartOfLine
return lookahead.atStartOfLine
} else {
return true
}
Expand All @@ -42,8 +42,8 @@ extension TokenConsumer {
break
}
if self.at(.atSign) || self.at(.keyword(.inout)) {
var backtrack = self.lookahead()
if backtrack.canParseType() {
var lookahead = self.lookahead()
if lookahead.canParseType() {
return true
}
}
Expand All @@ -54,8 +54,8 @@ extension TokenConsumer {
// expansion, but we need to do more lookahead to figure out
// whether the '{' is the start of a closure expression or a
// brace statement for 'repeat { ... } while'
let backtrack = self.lookahead()
return backtrack.peek().rawTokenKind != .leftBrace
let lookahead = self.lookahead()
return lookahead.peek().rawTokenKind != .leftBrace
}

return false
Expand Down Expand Up @@ -397,8 +397,8 @@ extension Parser {
) -> RawExprSyntax {
// Try to parse '@' sign or 'inout' as an attributed typerepr.
if self.at(.atSign, .keyword(.inout)) {
var backtrack = self.lookahead()
if backtrack.canParseType() {
var lookahead = self.lookahead()
if lookahead.canParseType() {
let type = self.parseType()
return RawExprSyntax(
RawTypeExprSyntax(
Expand Down Expand Up @@ -1965,12 +1965,12 @@ extension Parser.Lookahead {

// Do some tentative parsing to distinguish `label: { ... }` and
// `label: switch x { ... }`.
var backtrack = self.lookahead()
backtrack.consumeAnyToken()
if backtrack.peek().rawTokenKind == .leftBrace {
var lookahead = self.lookahead()
lookahead.consumeAnyToken()
if lookahead.peek().rawTokenKind == .leftBrace {
return true
}
if backtrack.peek().isEditorPlaceholder {
if lookahead.peek().isEditorPlaceholder {
// Editor placeholder can represent entire closures
return true
}
Expand Down Expand Up @@ -2022,21 +2022,21 @@ extension Parser.Lookahead {
// Determine if the {} goes with the expression by eating it, and looking
// to see if it is immediately followed by a token which indicates we should
// consider it part of the preceding expression
var backtrack = self.lookahead()
backtrack.eat(.leftBrace)
var lookahead = self.lookahead()
lookahead.eat(.leftBrace)
var loopProgress = LoopProgressCondition()
while !backtrack.at(.endOfFile, .rightBrace)
&& !backtrack.at(.poundEndif, .poundElse, .poundElseif)
&& backtrack.hasProgressed(&loopProgress)
while !lookahead.at(.endOfFile, .rightBrace)
&& !lookahead.at(.poundEndif, .poundElse, .poundElseif)
&& lookahead.hasProgressed(&loopProgress)
{
backtrack.skipSingle()
lookahead.skipSingle()
}

guard backtrack.consume(if: .rightBrace) != nil else {
guard lookahead.consume(if: .rightBrace) != nil else {
return false
}

switch backtrack.currentToken {
switch lookahead.currentToken {
case TokenSpec(.leftBrace),
TokenSpec(.where),
TokenSpec(.comma):
Expand All @@ -2053,7 +2053,7 @@ extension Parser.Lookahead {
TokenSpec(.equal),
TokenSpec(.postfixOperator),
TokenSpec(.binaryOperator):
return !backtrack.atStartOfLine
return !lookahead.atStartOfLine
default:
return false
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftParser/Lookahead.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ extension Parser.Lookahead {
// Recover by eating @foo(...)
self.eat(handle)
if self.at(.leftParen) {
var backtrack = self.lookahead()
backtrack.skipSingle()
var lookahead = self.lookahead()
lookahead.skipSingle()
// If we found '->', or 'throws' after paren, it's likely a parameter
// of function type.
guard backtrack.at(.arrow) || backtrack.at(.keyword(.throws), .keyword(.rethrows), .keyword(.throw)) else {
guard lookahead.at(.arrow) || lookahead.at(.keyword(.throws), .keyword(.rethrows), .keyword(.throw)) else {
self.skipSingle()
return
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftParser/Names.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ extension Parser {
// If qualified name base type cannot be parsed from the current
// point (i.e. the next type identifier is not followed by a '.'),
// then the next identifier is the final declaration name component.
var backtrack = self.lookahead()
var lookahead = self.lookahead()
guard
backtrack.consume(ifPrefix: ".", as: .period) != nil,
backtrack.canParseBaseTypeForQualifiedDeclName()
lookahead.consume(ifPrefix: ".", as: .period) != nil,
lookahead.canParseBaseTypeForQualifiedDeclName()
else {
return result
}
Expand Down Expand Up @@ -237,10 +237,10 @@ extension Parser {
// If qualified name base type cannot be parsed from the current
// point (i.e. the next type identifier is not followed by a '.'),
// then the next identifier is the final declaration name component.
var backtrack = self.lookahead()
var lookahead = self.lookahead()
guard
backtrack.consume(ifPrefix: ".", as: .period) != nil,
backtrack.canParseBaseTypeForQualifiedDeclName()
lookahead.consume(ifPrefix: ".", as: .period) != nil,
lookahead.canParseBaseTypeForQualifiedDeclName()
else {
break
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftParser/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ extension Parser {
// However, if this is the first clause, and we see "x = y", then this is
// almost certainly a typo for '==' and definitely not a continuation of
// another clause, so parse it as an expression. This also avoids
// lookahead + backtracking on simple if conditions that are obviously
// boolean conditions.
// lookahead on simple if conditions that are obviously boolean conditions.
return .expression(self.parseExpression(flavor: .stmtCondition, pattern: .none))
}

Expand Down
14 changes: 7 additions & 7 deletions Sources/SwiftParser/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -816,16 +816,16 @@ extension Parser.Lookahead {
}

if effect.spec.isThrowsSpecifier && self.peek().rawTokenKind == .leftParen {
var backtrack = self.lookahead()
backtrack.consumeAnyToken()
backtrack.skipSingle()
return backtrack.atFunctionTypeArrow()
var lookahead = self.lookahead()
lookahead.consumeAnyToken()
lookahead.skipSingle()
return lookahead.atFunctionTypeArrow()
}

if peek(isAtAnyIn: EffectSpecifier.self) != nil {
var backtrack = self.lookahead()
backtrack.consumeAnyToken()
return backtrack.atFunctionTypeArrow()
var lookahead = self.lookahead()
lookahead.consumeAnyToken()
return lookahead.atFunctionTypeArrow()
}

return false
Expand Down