Skip to content

Commit f241f3a

Browse files
committed
Migrate a few uses of consumeAnyToken to higher level parser constructs
1 parent 9dabfc7 commit f241f3a

File tree

6 files changed

+59
-63
lines changed

6 files changed

+59
-63
lines changed

Sources/SwiftParser/Attributes.swift

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ extension Parser {
512512
arena: self.arena)
513513
}
514514

515-
enum SpecializeParameter: SyntaxText {
515+
enum SpecializeParameter: SyntaxText, ContextualKeywords {
516516
case target
517517
case availability
518518
case exported
@@ -526,12 +526,10 @@ extension Parser {
526526
// Parse optional "exported" and "kind" labeled parameters.
527527
var loopProgress = LoopProgressCondition()
528528
while !self.at(any: [.eof, .rightParen, .whereKeyword]) && loopProgress.evaluate(currentToken) {
529-
let ident = self.parseAnyIdentifier()
530-
let knownParameter = SpecializeParameter(rawValue: ident.tokenText)
531-
let (unexpectedBeforeColon, colon) = self.expect(.colon)
532-
533-
switch knownParameter {
534-
case .target:
529+
switch self.at(anyIn: SpecializeParameter.self) {
530+
case (.target, let handle)?:
531+
let ident = self.eat(handle)
532+
let (unexpectedBeforeColon, colon) = self.expect(.colon)
535533
let (targetFunction, args) = self.parseDeclNameRef([ .zeroArgCompoundNames, .keywordsUsingSpecialNames, .operators ])
536534
let declName = RawDeclNameSyntax(
537535
declBaseName: RawSyntax(targetFunction),
@@ -546,7 +544,9 @@ extension Parser {
546544
trailingComma: comma,
547545
arena: self.arena
548546
)))
549-
case .availability:
547+
case (.availability, let handle)?:
548+
let ident = self.eat(handle)
549+
let (unexpectedBeforeColon, colon) = self.expect(.colon)
550550
let availability = self.parseAvailabilitySpecList(from: .available)
551551
// FIXME: This is modeled incorrectly in libSyntax.
552552
let semi = RawTokenSyntax(missing: .semicolon, arena: self.arena)
@@ -558,7 +558,9 @@ extension Parser {
558558
semicolon: semi,
559559
arena: self.arena
560560
)))
561-
case .available:
561+
case (.available, let handle)?:
562+
let ident = self.eat(handle)
563+
let (unexpectedBeforeColon, colon) = self.expect(.colon)
562564
// FIXME: I have no idea what this is supposed to be, but the Syntax
563565
// tree only allows us to insert a token so we'll take anything.
564566
let available = self.consumeAnyToken()
@@ -571,7 +573,9 @@ extension Parser {
571573
trailingComma: comma,
572574
arena: self.arena
573575
)))
574-
case .exported:
576+
case (.exported, let handle)?:
577+
let ident = self.eat(handle)
578+
let (unexpectedBeforeColon, colon) = self.expect(.colon)
575579
let (unexpectedBeforeValue, value) = self.expectAny([.trueKeyword, .falseKeyword], default: .falseKeyword)
576580
let comma = self.consume(if: .comma)
577581
elements.append(RawSyntax(RawLabeledSpecializeEntrySyntax(
@@ -583,7 +587,9 @@ extension Parser {
583587
trailingComma: comma,
584588
arena: self.arena
585589
)))
586-
case .kind:
590+
case (.kind, let handle)?:
591+
let ident = self.eat(handle)
592+
let (unexpectedBeforeColon, colon) = self.expect(.colon)
587593
let valueLabel = self.parseAnyIdentifier()
588594
let comma = self.consume(if: .comma)
589595
elements.append(RawSyntax(RawLabeledSpecializeEntrySyntax(
@@ -594,7 +600,10 @@ extension Parser {
594600
trailingComma: comma,
595601
arena: self.arena
596602
)))
597-
case .spiModule, .spi:
603+
case (.spiModule, let handle)?,
604+
(.spi, let handle)?:
605+
let ident = self.eat(handle)
606+
let (unexpectedBeforeColon, colon) = self.expect(.colon)
598607
let valueLabel = self.consumeAnyToken()
599608
let comma = self.consume(if: .comma)
600609
elements.append(RawSyntax(RawLabeledSpecializeEntrySyntax(
@@ -606,6 +615,8 @@ extension Parser {
606615
arena: self.arena
607616
)))
608617
case nil:
618+
let ident = self.consumeAnyToken()
619+
let (unexpectedBeforeColon, colon) = self.expect(.colon)
609620
let valueLabel = self.consumeAnyToken()
610621
let comma = self.consume(if: .comma)
611622
elements.append(RawSyntax(RawLabeledSpecializeEntrySyntax(

Sources/SwiftParser/Declarations.swift

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,11 @@ extension Parser {
457457
rightTypeIdentifier: secondType,
458458
arena: self.arena))
459459
}
460-
case (.spacedBinaryOperator, _)?,
461-
(.unspacedBinaryOperator, _)?,
462-
(.postfixOperator, _)?,
463-
(.prefixOperator, _)?:
464-
let equal = self.consumeAnyToken()
460+
case (.spacedBinaryOperator, let handle)?,
461+
(.unspacedBinaryOperator, let handle)?,
462+
(.postfixOperator, let handle)?,
463+
(.prefixOperator, let handle)?:
464+
let equal = self.eat(handle)
465465
let secondType = self.parseType()
466466
requirement = RawSyntax(RawSameTypeRequirementSyntax(
467467
leftTypeIdentifier: firstType,
@@ -1330,12 +1330,7 @@ extension Parser {
13301330
public mutating func parseFunctionSignature() -> RawFunctionSignatureSyntax {
13311331
let input = self.parseParameterClause()
13321332

1333-
let async: RawTokenSyntax?
1334-
if self.atContextualKeyword("async") {
1335-
async = self.consumeAnyToken(remapping: .contextualKeyword)
1336-
} else {
1337-
async = nil
1338-
}
1333+
let async = self.consumeIfContextualKeyword("async")
13391334

13401335
var throwsKeyword = self.consume(ifAny: [.throwsKeyword, .rethrowsKeyword])
13411336

@@ -1512,12 +1507,12 @@ extension Parser {
15121507
// Parse the contextual keywords for 'mutating' and 'nonmutating' before
15131508
// get and set.
15141509
let modifier: RawDeclModifierSyntax?
1515-
if self.atContextualKeyword("mutating") ||
1516-
self.atContextualKeyword("nonmutating") ||
1517-
self.atContextualKeyword("__consuming") {
1510+
if let name = self.consume(ifAny: [], contextualKeywords: ["mutating", "nonmutating", "__consuming"]) {
15181511
modifier = RawDeclModifierSyntax(
1519-
name: self.consumeAnyToken(), detail: nil,
1520-
arena: self.arena)
1512+
name: name,
1513+
detail: nil,
1514+
arena: self.arena
1515+
)
15211516
} else {
15221517
modifier = nil
15231518
}
@@ -1535,13 +1530,13 @@ extension Parser {
15351530
@_spi(RawSyntax)
15361531
public mutating func parseEffectsSpecifier() -> RawTokenSyntax? {
15371532
// 'async'
1538-
if self.atContextualKeyword("async") {
1539-
return self.consumeAnyToken(remapping: .contextualKeyword)
1533+
if let async = self.consumeIfContextualKeyword("async") {
1534+
return async
15401535
}
15411536

15421537
// 'reasync'
1543-
if self.atContextualKeyword("reasync") {
1544-
return self.consumeAnyToken(remapping: .contextualKeyword)
1538+
if let reasync = self.consumeIfContextualKeyword("reasync") {
1539+
return reasync
15451540
}
15461541

15471542
// 'throws'/'rethrows'

Sources/SwiftParser/Directives.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ extension Parser {
7373
repeat {
7474
// Parse the condition.
7575
let condition: RawExprSyntax?
76-
if self.at(.poundElseKeyword) {
76+
if let parsedElse = self.consume(if: .poundElseKeyword) {
7777
unexpectedBeforePoundIf = nil
78-
poundIf = self.consumeAnyToken()
78+
poundIf = parsedElse
7979
condition = nil
80-
} else if self.at(.poundElseifKeyword) {
80+
} else if let poundElseif = self.consume(if: .poundElseifKeyword) {
8181
unexpectedBeforePoundIf = nil
82-
poundIf = self.consumeAnyToken()
82+
poundIf = poundElseif
8383
condition = RawExprSyntax(self.parseSequenceExpression(.basic, forDirective: true))
8484
} else {
8585
assert(poundIf.tokenKind == .poundIfKeyword)

Sources/SwiftParser/Expressions.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,7 @@ extension Parser {
259259
return nil
260260
}
261261
case (.arrow, _)?, (.throwsKeyword, _)?:
262-
let asyncKeyword: RawTokenSyntax?
263-
if self.atContextualKeyword("async") {
264-
asyncKeyword = self.consumeAnyToken(remapping: .contextualKeyword)
265-
} else {
266-
asyncKeyword = nil
267-
}
262+
let asyncKeyword = self.consumeIfContextualKeyword("async")
268263

269264
let throwsKeyword = self.consume(if: .throwsKeyword)
270265
let (unexpectedBeforeArrow, arrow) = self.expect(.arrow)
@@ -297,8 +292,7 @@ extension Parser {
297292
forDirective: Bool = false,
298293
inVarOrLet: Bool = false
299294
) -> RawExprSyntax {
300-
if self.atContextualKeyword("await") {
301-
let awaitTok = self.consumeAnyToken()
295+
if let awaitTok = self.consumeIfContextualKeyword("await") {
302296
let sub = self.parseSequenceExpressionElement(flavor,
303297
inVarOrLet: inVarOrLet)
304298
return RawExprSyntax(RawAwaitExprSyntax(
@@ -832,6 +826,13 @@ extension Parser {
832826
case .leftSquareBracket: return .leftSquareBracket
833827
}
834828
}
829+
830+
var remappedKind: RawTokenKind? {
831+
switch self {
832+
case .period: return .prefixPeriod
833+
default: return nil
834+
}
835+
}
835836
}
836837

837838
switch self.at(anyIn: ExpectedTokenKind.self) {
@@ -982,9 +983,9 @@ extension Parser {
982983

983984
case (.leftBrace, _)?: // expr-closure
984985
return RawExprSyntax(self.parseClosureExpression())
985-
case (.period, _)?, //=.foo
986-
(.prefixPeriod, _)?: // .foo
987-
let dot = self.consumeAnyToken(remapping: .prefixPeriod)
986+
case (.period, let handle)?, //=.foo
987+
(.prefixPeriod, let handle)?: // .foo
988+
let dot = self.eat(handle)
988989
let (name, args) = self.parseDeclNameRef([ .keywords, .compoundNames ])
989990
return RawExprSyntax(RawMemberAccessExprSyntax(
990991
base: nil, dot: dot, name: name, declNameArguments: args,

Sources/SwiftParser/Statements.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,7 @@ extension Parser {
517517
let (unexpectedBeforeForKeyword, forKeyword) = self.expect(.forKeyword)
518518
let tryKeyword = self.consume(if: .tryKeyword)
519519

520-
let awaitKeyword: RawTokenSyntax?
521-
if self.atContextualKeyword("await") {
522-
awaitKeyword = self.consumeAnyToken()
523-
} else {
524-
awaitKeyword = nil
525-
}
520+
let awaitKeyword = self.consumeIfContextualKeyword("await")
526521

527522
// Parse the pattern. This is either 'case <refutable pattern>' or just a
528523
// normal pattern.

Sources/SwiftParser/Types.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,7 @@ extension Parser {
104104
/// protocol-composition-continuation → type-identifier | protocol-composition-type
105105
@_spi(RawSyntax)
106106
public mutating func parseSimpleOrCompositionType() -> RawTypeSyntax {
107-
let someOrAny: RawTokenSyntax?
108-
if self.atContextualKeyword("some") || self.atContextualKeyword("any") {
109-
someOrAny = self.consumeAnyToken()
110-
} else {
111-
someOrAny = nil
112-
}
107+
let someOrAny = self.consume(ifAny: [], contextualKeywords: ["some", "any"])
113108

114109
var base = self.parseSimpleType()
115110
guard self.atContextualPunctuator("&") else {
@@ -477,10 +472,9 @@ extension Parser.Lookahead {
477472
// Accept 'inout' at for better recovery.
478473
_ = self.consume(if: .inoutKeyword)
479474

480-
if self.atContextualKeyword("some") {
481-
self.consumeAnyToken()
482-
} else if self.atContextualKeyword("any") {
483-
self.consumeAnyToken()
475+
if self.consumeIfContextualKeyword("some") != nil {
476+
} else {
477+
self.consumeIfContextualKeyword("any")
484478
}
485479

486480
switch self.currentToken.tokenKind {

0 commit comments

Comments
 (0)