Skip to content

Commit 4418dc6

Browse files
committed
Simplify self.currentToken.isAtStartOfLine to self.atStrtOfLine
1 parent 768c821 commit 4418dc6

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
@@ -777,7 +777,7 @@ extension Parser {
777777
do {
778778
var loopProgress = LoopProgressCondition()
779779
while !self.at(.endOfFile, .rightBrace) && loopProgress.evaluate(self) {
780-
let newItemAtStartOfLine = self.currentToken.isAtStartOfLine
780+
let newItemAtStartOfLine = self.atStartOfLine
781781
guard let newElement = self.parseMemberDeclListItem() else {
782782
break
783783
}
@@ -1359,7 +1359,7 @@ extension Parser {
13591359
value: initExpr,
13601360
arena: self.arena
13611361
)
1362-
} else if self.atStartOfExpression(), !self.at(.leftBrace), !self.currentToken.flags.contains(.isAtStartOfLine) {
1362+
} else if self.atStartOfExpression(), !self.at(.leftBrace), !self.atStartOfLine {
13631363
let missingEqual = RawTokenSyntax(missing: .equal, arena: self.arena)
13641364
let expr = self.parseExpression()
13651365
initializer = RawInitializerClauseSyntax(
@@ -2068,7 +2068,7 @@ extension Parser {
20682068
}
20692069
var unexpectedBeforeMacro: RawUnexpectedNodesSyntax?
20702070
var macro: RawTokenSyntax
2071-
if !self.currentToken.isAtStartOfLine {
2071+
if !self.atStartOfLine {
20722072
(unexpectedBeforeMacro, macro) = self.expectIdentifier(allowKeywordsAsIdentifier: true)
20732073
if macro.leadingTriviaByteLength != 0 {
20742074
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
@@ -210,7 +210,7 @@ extension Parser {
210210
&& !self.atElifTypo()
211211
&& elementsProgress.evaluate(self)
212212
{
213-
let newItemAtStartOfLine = self.currentToken.isAtStartOfLine
213+
let newItemAtStartOfLine = self.atStartOfLine
214214
guard let element = parseElement(&self, elements.isEmpty), !element.isEmpty else {
215215
break
216216
}

Sources/SwiftParser/Expressions.swift

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

@@ -154,7 +154,7 @@ extension Parser {
154154
while loopCondition.evaluate(self) {
155155
guard
156156
!lastElement.is(RawMissingExprSyntax.self),
157-
!(forDirective && self.currentToken.isAtStartOfLine)
157+
!(forDirective && self.atStartOfLine)
158158
else {
159159
break
160160
}
@@ -174,7 +174,7 @@ extension Parser {
174174
if let rhsExpr {
175175
// Operator parsing returned the RHS.
176176
lastElement = rhsExpr
177-
} else if forDirective && self.currentToken.isAtStartOfLine {
177+
} else if forDirective && self.atStartOfLine {
178178
// Don't allow RHS at a newline for `#if` conditions.
179179
lastElement = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
180180
break
@@ -316,7 +316,7 @@ extension Parser {
316316
)
317317

318318
let rhs: RawExprSyntax?
319-
if colon.isMissing, currentToken.isAtStartOfLine {
319+
if colon.isMissing, self.atStartOfLine {
320320
rhs = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
321321
} else {
322322
rhs = nil
@@ -807,7 +807,7 @@ extension Parser {
807807
var leadingExpr = start
808808
var loopCondition = LoopProgressCondition()
809809
while loopCondition.evaluate(self) {
810-
if forDirective && self.currentToken.isAtStartOfLine {
810+
if forDirective && self.atStartOfLine {
811811
return leadingExpr
812812
}
813813

@@ -1434,7 +1434,7 @@ extension Parser {
14341434
}
14351435
var unexpectedBeforeMacro: RawUnexpectedNodesSyntax?
14361436
var macro: RawTokenSyntax
1437-
if !self.currentToken.isAtStartOfLine {
1437+
if !self.atStartOfLine {
14381438
(unexpectedBeforeMacro, macro) = self.expectIdentifier(allowKeywordsAsIdentifier: true)
14391439
if macro.leadingTriviaByteLength != 0 {
14401440
// If there're whitespaces after '#' diagnose.
@@ -1738,9 +1738,9 @@ extension Parser {
17381738
break
17391739
}
17401740

1741-
// If The next token is at the beginning of a new line and can never start
1741+
// If the next token is at the beginning of a new line and can never start
17421742
// an element, break.
1743-
if self.currentToken.isAtStartOfLine
1743+
if self.atStartOfLine
17441744
&& (self.at(.rightBrace, .poundEndifKeyword) || self.atStartOfDeclaration() || self.atStartOfStatement())
17451745
{
17461746
break
@@ -2274,7 +2274,7 @@ extension Parser.Lookahead {
22742274
TokenSpec(.equal),
22752275
TokenSpec(.postfixOperator),
22762276
TokenSpec(.binaryOperator):
2277-
return !backtrack.currentToken.isAtStartOfLine
2277+
return !backtrack.atStartOfLine
22782278
default:
22792279
return false
22802280
}

Sources/SwiftParser/Lookahead.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ extension Parser.Lookahead {
235235
}
236236
} while self.at(.poundElseifKeyword, .poundElseKeyword) && poundIfLoopProgress.evaluate(self)
237237

238-
return didSeeAnyAttributes && self.currentToken.isAtStartOfLine && self.consume(if: .poundEndifKeyword) != nil
238+
return didSeeAnyAttributes && self.atStartOfLine && self.consume(if: .poundEndifKeyword) != nil
239239
}
240240
}
241241

@@ -291,7 +291,7 @@ extension Parser.Lookahead {
291291
}
292292

293293
mutating func skipUntilEndOfLine() {
294-
while !self.at(.endOfFile) && !self.currentToken.isAtStartOfLine {
294+
while !self.at(.endOfFile) && !self.atStartOfLine {
295295
self.skipSingle()
296296
}
297297
}

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

@@ -514,8 +514,8 @@ extension Parser {
514514
self.missingToken(.identifier)
515515
)
516516
} else if keywordRecovery,
517-
(self.currentToken.isLexerClassifiedKeyword || self.currentToken.rawTokenKind == .wildcard),
518-
!self.currentToken.isAtStartOfLine
517+
(self.currentToken.isLexerClassifiedKeyword || self.at(.wildcard)),
518+
!self.atStartOfLine
519519
{
520520
let keyword = self.consumeAnyToken()
521521
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
@@ -137,6 +137,12 @@ extension TokenConsumer {
137137
return nil
138138
}
139139

140+
/// Whether the current token is at the start of a line.
141+
@inline(__always)
142+
var atStartOfLine: Bool {
143+
return self.currentToken.isAtStartOfLine
144+
}
145+
140146
/// Eat a token that we know we are currently positioned at, based on `at(anyIn:)`.
141147
@inline(__always)
142148
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)