Skip to content

Commit 2abcc5a

Browse files
committed
Simplify self.currentToken.isAtStartOfLine to self.atStrtOfLine
1 parent 75c42e4 commit 2abcc5a

12 files changed

+34
-30
lines changed

Sources/SwiftParser/Attributes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ extension Parser {
10231023
let (unexpectedBeforeColon, colon) = self.expect(.colon)
10241024
let base: RawTokenSyntax
10251025
let args: RawDeclNameArgumentsSyntax?
1026-
if label.isMissing && colon.isMissing && self.currentToken.isAtStartOfLine {
1026+
if label.isMissing && colon.isMissing && self.atStartOfLine {
10271027
base = RawTokenSyntax(missing: .identifier, arena: self.arena)
10281028
args = nil
10291029
} else {

Sources/SwiftParser/Declarations.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ extension Parser {
759759
do {
760760
var loopProgress = LoopProgressCondition()
761761
while !self.at(.endOfFile, .rightBrace) && loopProgress.evaluate(self) {
762-
let newItemAtStartOfLine = self.currentToken.isAtStartOfLine
762+
let newItemAtStartOfLine = self.atStartOfLine
763763
guard let newElement = self.parseMemberDeclListItem() else {
764764
break
765765
}
@@ -1341,7 +1341,7 @@ extension Parser {
13411341
value: initExpr,
13421342
arena: self.arena
13431343
)
1344-
} else if self.atStartOfExpression(), !self.at(.leftBrace), !self.currentToken.flags.contains(.isAtStartOfLine) {
1344+
} else if self.atStartOfExpression(), !self.at(.leftBrace), !self.atStartOfLine {
13451345
let missingEqual = RawTokenSyntax(missing: .equal, arena: self.arena)
13461346
let expr = self.parseExpression()
13471347
initializer = RawInitializerClauseSyntax(
@@ -2050,7 +2050,7 @@ extension Parser {
20502050
}
20512051
var unexpectedBeforeMacro: RawUnexpectedNodesSyntax?
20522052
var macro: RawTokenSyntax
2053-
if !self.currentToken.isAtStartOfLine {
2053+
if !self.atStartOfLine {
20542054
(unexpectedBeforeMacro, macro) = self.expectIdentifier(allowKeywordsAsIdentifier: true)
20552055
if macro.leadingTriviaByteLength != 0 {
20562056
unexpectedBeforeMacro = RawUnexpectedNodesSyntax(combining: unexpectedBeforeMacro, macro, arena: self.arena)

Sources/SwiftParser/Directives.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ extension Parser {
209209
&& !self.atElifTypo()
210210
&& elementsProgress.evaluate(self)
211211
{
212-
let newItemAtStartOfLine = self.currentToken.isAtStartOfLine
212+
let newItemAtStartOfLine = self.atStartOfLine
213213
guard let element = parseElement(&self, elements.isEmpty), !element.isEmpty else {
214214
break
215215
}

Sources/SwiftParser/Expressions.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension TokenConsumer {
2929
// Decide how we want to consume the 'try':
3030
// 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
3131
// 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.
32-
return backtrack.currentToken.isAtStartOfLine
32+
return backtrack.atStartOfLine
3333
} else {
3434
return true
3535
}
@@ -133,7 +133,7 @@ extension Parser {
133133
forDirective: Bool = false,
134134
pattern: PatternContext = .none
135135
) -> RawExprSyntax {
136-
if forDirective && self.currentToken.isAtStartOfLine {
136+
if forDirective && self.atStartOfLine {
137137
return RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
138138
}
139139

@@ -155,7 +155,7 @@ extension Parser {
155155
while loopCondition.evaluate(self) {
156156
guard
157157
!lastElement.is(RawMissingExprSyntax.self),
158-
!(forDirective && self.currentToken.isAtStartOfLine)
158+
!(forDirective && self.atStartOfLine)
159159
else {
160160
break
161161
}
@@ -175,7 +175,7 @@ extension Parser {
175175
if let rhsExpr {
176176
// Operator parsing returned the RHS.
177177
lastElement = rhsExpr
178-
} else if forDirective && self.currentToken.isAtStartOfLine {
178+
} else if forDirective && self.atStartOfLine {
179179
// Don't allow RHS at a newline for `#if` conditions.
180180
lastElement = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
181181
break
@@ -317,7 +317,7 @@ extension Parser {
317317
)
318318

319319
let rhs: RawExprSyntax?
320-
if colon.isMissing, currentToken.isAtStartOfLine {
320+
if colon.isMissing, self.atStartOfLine {
321321
rhs = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
322322
} else {
323323
rhs = nil
@@ -808,7 +808,7 @@ extension Parser {
808808
var leadingExpr = start
809809
var loopCondition = LoopProgressCondition()
810810
while loopCondition.evaluate(self) {
811-
if forDirective && self.currentToken.isAtStartOfLine {
811+
if forDirective && self.atStartOfLine {
812812
return leadingExpr
813813
}
814814

@@ -1426,7 +1426,7 @@ extension Parser {
14261426
}
14271427
var unexpectedBeforeMacro: RawUnexpectedNodesSyntax?
14281428
var macro: RawTokenSyntax
1429-
if !self.currentToken.isAtStartOfLine {
1429+
if !self.atStartOfLine {
14301430
(unexpectedBeforeMacro, macro) = self.expectIdentifier(allowKeywordsAsIdentifier: true)
14311431
if macro.leadingTriviaByteLength != 0 {
14321432
// If there're whitespaces after '#' diagnose.
@@ -1730,9 +1730,9 @@ extension Parser {
17301730
break
17311731
}
17321732

1733-
// If The next token is at the beginning of a new line and can never start
1733+
// If the next token is at the beginning of a new line and can never start
17341734
// an element, break.
1735-
if self.currentToken.isAtStartOfLine
1735+
if self.atStartOfLine
17361736
&& (self.at(.rightBrace, .poundEndif) || self.atStartOfDeclaration() || self.atStartOfStatement())
17371737
{
17381738
break
@@ -2266,7 +2266,7 @@ extension Parser.Lookahead {
22662266
TokenSpec(.equal),
22672267
TokenSpec(.postfixOperator),
22682268
TokenSpec(.binaryOperator):
2269-
return !backtrack.currentToken.isAtStartOfLine
2269+
return !backtrack.atStartOfLine
22702270
default:
22712271
return false
22722272
}

Sources/SwiftParser/Lookahead.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ extension Parser.Lookahead {
239239
}
240240
} while self.at(.poundElseif, .poundElse) && poundIfLoopProgress.evaluate(self)
241241

242-
return didSeeAnyAttributes && self.currentToken.isAtStartOfLine && self.consume(if: .poundEndif) != nil
242+
return didSeeAnyAttributes && self.atStartOfLine && self.consume(if: .poundEndif) != nil
243243
}
244244
}
245245

@@ -295,7 +295,7 @@ extension Parser.Lookahead {
295295
}
296296

297297
mutating func skipUntilEndOfLine() {
298-
while !self.at(.endOfFile) && !self.currentToken.isAtStartOfLine {
298+
while !self.at(.endOfFile) && !self.atStartOfLine {
299299
self.skipSingle()
300300
}
301301
}

Sources/SwiftParser/Nominals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ extension Parser {
211211
) -> T where T: NominalTypeDeclarationTrait {
212212
let (unexpectedBeforeIntroducerKeyword, introducerKeyword) = self.eat(introucerHandle)
213213
let (unexpectedBeforeName, name) = self.expectIdentifier(keywordRecovery: true)
214-
if unexpectedBeforeName == nil && name.isMissing && self.currentToken.isAtStartOfLine {
214+
if unexpectedBeforeName == nil && name.isMissing && self.atStartOfLine {
215215
return T.init(
216216
attributes: attrs.attributes,
217217
modifiers: attrs.modifiers,

Sources/SwiftParser/Parser.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,13 @@ extension Parser {
284284
/// Consumes remaining token on the line and returns a ``RawUnexpectedNodesSyntax``
285285
/// if there is any tokens consumed.
286286
mutating func consumeRemainingTokenOnLine() -> RawUnexpectedNodesSyntax? {
287-
guard !self.currentToken.isAtStartOfLine else {
287+
guard !self.atStartOfLine else {
288288
return nil
289289
}
290290

291291
var unexpectedTokens = [RawTokenSyntax]()
292292
var loopProgress = LoopProgressCondition()
293-
while !self.at(.endOfFile), !currentToken.isAtStartOfLine, loopProgress.evaluate(self) {
293+
while !self.at(.endOfFile), !atStartOfLine, loopProgress.evaluate(self) {
294294
unexpectedTokens += [self.consumeAnyToken()]
295295
}
296296

@@ -525,8 +525,8 @@ extension Parser {
525525
self.missingToken(.identifier)
526526
)
527527
} else if keywordRecovery,
528-
(self.currentToken.isLexerClassifiedKeyword || self.currentToken.rawTokenKind == .wildcard),
529-
!self.currentToken.isAtStartOfLine
528+
(self.currentToken.isLexerClassifiedKeyword || self.at(.wildcard)),
529+
!self.atStartOfLine
530530
{
531531
let keyword = self.consumeAnyToken()
532532
return (

Sources/SwiftParser/Patterns.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ extension Parser {
134134
)
135135
)
136136
case nil:
137-
if self.currentToken.isLexerClassifiedKeyword, !self.currentToken.isAtStartOfLine {
137+
if self.currentToken.isLexerClassifiedKeyword, !self.atStartOfLine {
138138
// Recover if a keyword was used instead of an identifier
139139
let keyword = self.consumeAnyToken()
140140
return RawPatternSyntax(
@@ -171,7 +171,7 @@ extension Parser {
171171
arena: self.arena
172172
)
173173
} else if allowRecoveryFromMissingColon
174-
&& !self.currentToken.isAtStartOfLine
174+
&& !self.atStartOfLine
175175
&& lookahead.canParseType()
176176
{
177177
let (unexpectedBeforeColon, colon) = self.expect(.colon)

Sources/SwiftParser/Recovery.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ extension Parser.Lookahead {
7777
let shouldSkipOverNewlines = recoveryPrecedence.shouldSkipOverNewlines && spec1.allowAtStartOfLine && spec2.allowAtStartOfLine && spec3.allowAtStartOfLine
7878

7979
while !self.at(.endOfFile) {
80-
if !shouldSkipOverNewlines, self.currentToken.isAtStartOfLine {
80+
if !shouldSkipOverNewlines, self.atStartOfLine {
8181
break
8282
}
8383
let matchedSpec: TokenSpec?
@@ -140,9 +140,7 @@ extension Parser.Lookahead {
140140
}).min()!
141141
var loopProgress = LoopProgressCondition()
142142
while !self.at(.endOfFile) && loopProgress.evaluate(self) {
143-
if !recoveryPrecedence.shouldSkipOverNewlines,
144-
self.currentToken.isAtStartOfLine
145-
{
143+
if !recoveryPrecedence.shouldSkipOverNewlines, self.atStartOfLine {
146144
break
147145
}
148146
if let (kind, handle) = self.at(anyIn: specSet) {

Sources/SwiftParser/StringLiterals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ extension Parser {
512512
// line if this is a single-line literal. Leading trivia is fine as
513513
// we allow e.g "\(foo )".
514514
let rightParen: Token
515-
if self.at(.rightParen) && self.currentToken.isAtStartOfLine && openQuote.tokenKind != .multilineStringQuote {
515+
if self.at(.rightParen) && self.atStartOfLine && openQuote.tokenKind != .multilineStringQuote {
516516
rightParen = missingToken(.rightParen)
517517
} else {
518518
rightParen = self.expectWithoutRecovery(.rightParen)

Sources/SwiftParser/TokenConsumer.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ extension TokenConsumer {
150150
return self.currentToken.tokenText.hasPrefix(prefix)
151151
}
152152

153+
/// Whether the current token is at the start of a line.
154+
@inline(__always)
155+
var atStartOfLine: Bool {
156+
return self.currentToken.isAtStartOfLine
157+
}
158+
153159
/// Eat a token that we know we are currently positioned at, based on `at(anyIn:)`.
154160
@inline(__always)
155161
mutating func eat(_ handle: TokenConsumptionHandle) -> Token {

Sources/SwiftParser/TopLevel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extension Parser {
6666
var elements = [RawCodeBlockItemSyntax]()
6767
var loopProgress = LoopProgressCondition()
6868
while !stopCondition(&self), loopProgress.evaluate(self) {
69-
let newItemAtStartOfLine = self.currentToken.isAtStartOfLine
69+
let newItemAtStartOfLine = self.atStartOfLine
7070
guard let newElement = self.parseCodeBlockItem(isAtTopLevel: isAtTopLevel, allowInitDecl: allowInitDecl) else {
7171
break
7272
}

0 commit comments

Comments
 (0)