Skip to content

Commit e8b12a6

Browse files
authored
Merge pull request #1909 from ahoppen/ahoppen/parser-instead-of-currenttoken
Reduce usage of `currentToken`
2 parents 0c41fe6 + 0554638 commit e8b12a6

21 files changed

+159
-125
lines changed

Sources/SwiftParser/Attributes.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension Parser {
2323
repeat {
2424
let attribute = self.parseAttribute()
2525
elements.append(attribute)
26-
} while self.at(.atSign, .poundIf) && loopProgress.evaluate(currentToken)
26+
} while self.at(.atSign, .poundIf) && self.hasProgressed(&loopProgress)
2727
return RawAttributeListSyntax(elements: elements, arena: self.arena)
2828
}
2929
}
@@ -453,7 +453,7 @@ extension Parser {
453453

454454
var elements = [RawDifferentiabilityParamSyntax]()
455455
var loopProgress = LoopProgressCondition()
456-
while !self.at(.endOfFile, .rightParen) && loopProgress.evaluate(currentToken) {
456+
while !self.at(.endOfFile, .rightParen) && self.hasProgressed(&loopProgress) {
457457
guard let param = self.parseDifferentiabilityParameter() else {
458458
break
459459
}
@@ -594,7 +594,7 @@ extension Parser {
594594
mutating func parseObjectiveCSelector() -> RawObjCSelectorSyntax {
595595
var elements = [RawObjCSelectorPieceSyntax]()
596596
var loopProgress = LoopProgressCondition()
597-
while loopProgress.evaluate(currentToken) {
597+
while self.hasProgressed(&loopProgress) {
598598
// Empty selector piece.
599599
if let colon = self.consume(if: .colon) {
600600
elements.append(
@@ -676,7 +676,7 @@ extension Parser {
676676
var elements = [RawSpecializeAttributeSpecListSyntax.Element]()
677677
// Parse optional "exported" and "kind" labeled parameters.
678678
var loopProgress = LoopProgressCondition()
679-
while !self.at(.endOfFile, .rightParen, .keyword(.where)) && loopProgress.evaluate(currentToken) {
679+
while !self.at(.endOfFile, .rightParen, .keyword(.where)) && self.hasProgressed(&loopProgress) {
680680
switch self.at(anyIn: SpecializeParameter.self) {
681681
case (.target, let handle)?:
682682
let ident = self.eat(handle)
@@ -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/Availability.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extension Parser {
4848
arena: self.arena
4949
)
5050
)
51-
} while keepGoing != nil && availabilityArgumentProgress.evaluate(currentToken)
51+
} while keepGoing != nil && self.hasProgressed(&availabilityArgumentProgress)
5252
}
5353

5454
return RawAvailabilitySpecListSyntax(elements: elements, arena: self.arena)
@@ -99,7 +99,7 @@ extension Parser {
9999
var elements = [RawAvailabilityArgumentSyntax]()
100100
var keepGoing: RawTokenSyntax? = nil
101101

102-
var loopProgressCondition = LoopProgressCondition()
102+
var loopProgress = LoopProgressCondition()
103103
LOOP: repeat {
104104
let entry: RawAvailabilityArgumentSyntax.Entry
105105
switch self.at(anyIn: AvailabilityArgumentKind.self) {
@@ -176,7 +176,7 @@ extension Parser {
176176
arena: self.arena
177177
)
178178
)
179-
} while keepGoing != nil && loopProgressCondition.evaluate(currentToken)
179+
} while keepGoing != nil && self.hasProgressed(&loopProgress)
180180
return RawAvailabilitySpecListSyntax(elements: elements, arena: self.arena)
181181
}
182182

@@ -266,7 +266,7 @@ extension Parser {
266266
unexpectedTokens.append(unexpectedVersion)
267267
}
268268
keepGoing = self.consume(if: .period)
269-
} while keepGoing != nil && loopProgress.evaluate(currentToken)
269+
} while keepGoing != nil && self.hasProgressed(&loopProgress)
270270
return RawUnexpectedNodesSyntax(unexpectedTokens, arena: self.arena)
271271
}
272272

Sources/SwiftParser/Declarations.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extension TokenConsumer {
6161

6262
var hasAttribute = false
6363
var attributeProgress = LoopProgressCondition()
64-
while attributeProgress.evaluate(subparser.currentToken) && subparser.at(.atSign) {
64+
while subparser.hasProgressed(&attributeProgress) && subparser.at(.atSign) {
6565
hasAttribute = true
6666
_ = subparser.consumeAttributeList()
6767
}
@@ -71,7 +71,7 @@ extension TokenConsumer {
7171
var modifierProgress = LoopProgressCondition()
7272
while let (modifierKind, handle) = subparser.at(anyIn: DeclarationModifier.self),
7373
modifierKind != .class,
74-
modifierProgress.evaluate(subparser.currentToken)
74+
subparser.hasProgressed(&modifierProgress)
7575
{
7676
hasModifier = true
7777
subparser.eat(handle)
@@ -356,7 +356,7 @@ extension Parser {
356356
arena: self.arena
357357
)
358358
)
359-
} while keepGoing != nil && loopProgress.evaluate(currentToken)
359+
} while keepGoing != nil && self.hasProgressed(&loopProgress)
360360
return RawImportPathSyntax(elements: elements, arena: self.arena)
361361
}
362362
}
@@ -486,7 +486,7 @@ extension Parser {
486486
arena: self.arena
487487
)
488488
)
489-
} while keepGoing != nil && loopProgress.evaluate(currentToken)
489+
} while keepGoing != nil && self.hasProgressed(&loopProgress)
490490
}
491491

492492
let whereClause: RawGenericWhereClauseSyntax?
@@ -692,7 +692,7 @@ extension Parser {
692692
arena: self.arena
693693
)
694694
)
695-
} while keepGoing != nil && loopProgress.evaluate(currentToken)
695+
} while keepGoing != nil && self.hasProgressed(&loopProgress)
696696
}
697697

698698
return RawGenericWhereClauseSyntax(
@@ -758,8 +758,8 @@ extension Parser {
758758
let (unexpectedBeforeLBrace, lbrace) = self.expect(.leftBrace)
759759
do {
760760
var loopProgress = LoopProgressCondition()
761-
while !self.at(.endOfFile, .rightBrace) && loopProgress.evaluate(currentToken) {
762-
let newItemAtStartOfLine = self.currentToken.isAtStartOfLine
761+
while !self.at(.endOfFile, .rightBrace) && self.hasProgressed(&loopProgress) {
762+
let newItemAtStartOfLine = self.atStartOfLine
763763
guard let newElement = self.parseMemberDeclListItem() else {
764764
break
765765
}
@@ -858,7 +858,7 @@ extension Parser {
858858
arena: self.arena
859859
)
860860
)
861-
} while keepGoing != nil && loopProgress.evaluate(currentToken)
861+
} while keepGoing != nil && self.hasProgressed(&loopProgress)
862862
}
863863

864864
return RawEnumCaseDeclSyntax(
@@ -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(
@@ -1376,7 +1376,7 @@ extension Parser {
13761376
arena: self.arena
13771377
)
13781378
)
1379-
} while keepGoing != nil && loopProgress.evaluate(currentToken)
1379+
} while keepGoing != nil && self.hasProgressed(&loopProgress)
13801380
}
13811381

13821382
return RawVariableDeclSyntax(
@@ -1521,7 +1521,7 @@ extension Parser {
15211521
var elements = [RawAccessorDeclSyntax]()
15221522
do {
15231523
var loopProgress = LoopProgressCondition()
1524-
while !self.at(.endOfFile, .rightBrace) && loopProgress.evaluate(currentToken) {
1524+
while !self.at(.endOfFile, .rightBrace) && self.hasProgressed(&loopProgress) {
15251525
guard let introducer = self.parseAccessorIntroducer() else {
15261526
// There can only be an implicit getter if no other accessors were
15271527
// seen before this one.
@@ -1741,7 +1741,7 @@ extension Parser {
17411741
while (identifiersAfterOperatorName.last ?? name).trailingTriviaByteLength == 0,
17421742
self.currentToken.leadingTriviaByteLength == 0,
17431743
!self.at(.colon, .leftBrace, .endOfFile),
1744-
loopProgress.evaluate(self.currentToken)
1744+
self.hasProgressed(&loopProgress)
17451745
{
17461746
identifiersAfterOperatorName.append(consumeAnyToken())
17471747
}
@@ -1888,7 +1888,7 @@ extension Parser {
18881888
var elements = [RawPrecedenceGroupAttributeListSyntax.Element]()
18891889
do {
18901890
var attributesProgress = LoopProgressCondition()
1891-
LOOP: while !self.at(.endOfFile, .rightBrace) && attributesProgress.evaluate(currentToken) {
1891+
LOOP: while !self.at(.endOfFile, .rightBrace) && self.hasProgressed(&attributesProgress) {
18921892
switch self.at(anyIn: LabelText.self) {
18931893
case (.associativity, let handle)?:
18941894
let associativity = self.eat(handle)
@@ -1952,7 +1952,7 @@ extension Parser {
19521952
arena: self.arena
19531953
)
19541954
)
1955-
} while keepGoing != nil && namesProgress.evaluate(currentToken)
1955+
} while keepGoing != nil && self.hasProgressed(&namesProgress)
19561956
}
19571957
elements.append(
19581958
.precedenceGroupRelation(
@@ -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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ extension Parser {
121121

122122
// Proceed to parse #if continuation clauses (#elseif, #else, check #elif typo, #endif)
123123
var loopProgress = LoopProgressCondition()
124-
LOOP: while let (match, handle) = self.canRecoverTo(anyIn: IfConfigContinuationClauseStartKeyword.self), loopProgress.evaluate(self.currentToken) {
124+
LOOP: while let (match, handle) = self.canRecoverTo(anyIn: IfConfigContinuationClauseStartKeyword.self), self.hasProgressed(&loopProgress) {
125125
var unexpectedBeforePound: RawUnexpectedNodesSyntax?
126126
var pound: RawTokenSyntax
127127
let condition: RawExprSyntax?
@@ -207,9 +207,9 @@ extension Parser {
207207
while !self.at(.endOfFile)
208208
&& !self.at(.poundElse, .poundElseif, .poundEndif)
209209
&& !self.atElifTypo()
210-
&& elementsProgress.evaluate(currentToken)
210+
&& self.hasProgressed(&elementsProgress)
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
}

0 commit comments

Comments
 (0)