diff --git a/Sources/SwiftParser/Expressions.swift b/Sources/SwiftParser/Expressions.swift index 462c5f728dd..a7494d5c540 100644 --- a/Sources/SwiftParser/Expressions.swift +++ b/Sources/SwiftParser/Expressions.swift @@ -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 } @@ -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 } } @@ -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 @@ -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( @@ -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 } @@ -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): @@ -2053,7 +2053,7 @@ extension Parser.Lookahead { TokenSpec(.equal), TokenSpec(.postfixOperator), TokenSpec(.binaryOperator): - return !backtrack.atStartOfLine + return !lookahead.atStartOfLine default: return false } diff --git a/Sources/SwiftParser/Lookahead.swift b/Sources/SwiftParser/Lookahead.swift index 65cddc0eb1f..3d3f982c8e2 100644 --- a/Sources/SwiftParser/Lookahead.swift +++ b/Sources/SwiftParser/Lookahead.swift @@ -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 } diff --git a/Sources/SwiftParser/Names.swift b/Sources/SwiftParser/Names.swift index 86e8b24f598..19f7050db5d 100644 --- a/Sources/SwiftParser/Names.swift +++ b/Sources/SwiftParser/Names.swift @@ -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 } @@ -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 } diff --git a/Sources/SwiftParser/Statements.swift b/Sources/SwiftParser/Statements.swift index 36166acabee..3b10cf63c14 100644 --- a/Sources/SwiftParser/Statements.swift +++ b/Sources/SwiftParser/Statements.swift @@ -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)) } diff --git a/Sources/SwiftParser/Types.swift b/Sources/SwiftParser/Types.swift index e3764075a7b..197906a265f 100644 --- a/Sources/SwiftParser/Types.swift +++ b/Sources/SwiftParser/Types.swift @@ -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