Skip to content

Commit f6ba4aa

Browse files
authored
Merge pull request #1314 from ahoppen/ahoppen/no-arrays
Don’t use arrays in `consume` functions
2 parents a19ab8b + 2036cab commit f6ba4aa

17 files changed

+418
-177
lines changed

Sources/SwiftParser/Attributes.swift

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
extension Parser {
1616
mutating func parseAttributeList() -> RawAttributeListSyntax? {
17-
guard self.at(any: [.atSign, .poundIfKeyword]) else {
17+
guard self.at(.atSign, .poundIfKeyword) else {
1818
return nil
1919
}
2020

@@ -23,7 +23,7 @@ extension Parser {
2323
repeat {
2424
let attribute = self.parseAttribute()
2525
elements.append(attribute)
26-
} while self.at(any: [.atSign, .poundIfKeyword]) && loopProgress.evaluate(currentToken)
26+
} while self.at(.atSign, .poundIfKeyword) && loopProgress.evaluate(currentToken)
2727
return RawAttributeListSyntax(elements: elements, arena: self.arena)
2828
}
2929
}
@@ -282,7 +282,7 @@ extension Parser {
282282
// The contents of the @_effects attribute are parsed in SIL, we just
283283
// represent the contents as a list of tokens in SwiftSyntax.
284284
var tokens: [RawTokenSyntax] = []
285-
while !parser.at(any: [.rightParen, .eof]) {
285+
while !parser.at(.rightParen, .eof) {
286286
tokens.append(parser.consumeAnyToken())
287287
}
288288
return .effectsArguments(RawEffectsArgumentsSyntax(elements: tokens, arena: parser.arena))
@@ -443,7 +443,7 @@ extension Parser {
443443

444444
var elements = [RawDifferentiabilityParamSyntax]()
445445
var loopProgress = LoopProgressCondition()
446-
while !self.at(any: [.eof, .rightParen]) && loopProgress.evaluate(currentToken) {
446+
while !self.at(.eof, .rightParen) && loopProgress.evaluate(currentToken) {
447447
guard let param = self.parseDifferentiabilityParameter() else {
448448
break
449449
}
@@ -605,7 +605,7 @@ extension Parser {
605605
mutating func parseObjectiveCSelector() -> RawObjCSelectorSyntax {
606606
var elements = [RawObjCSelectorPieceSyntax]()
607607
var loopProgress = LoopProgressCondition()
608-
while !self.at(any: [.eof, .rightParen]) && loopProgress.evaluate(currentToken) {
608+
while !self.at(.eof, .rightParen) && loopProgress.evaluate(currentToken) {
609609
// Empty selector piece.
610610
if let colon = self.consume(if: .colon) {
611611
elements.append(
@@ -687,7 +687,7 @@ extension Parser {
687687
var elements = [RawSpecializeAttributeSpecListSyntax.Element]()
688688
// Parse optional "exported" and "kind" labeled parameters.
689689
var loopProgress = LoopProgressCondition()
690-
while !self.at(any: [.eof, .rightParen, .keyword(.where)]) && loopProgress.evaluate(currentToken) {
690+
while !self.at(.eof, .rightParen, .keyword(.where)) && loopProgress.evaluate(currentToken) {
691691
switch self.at(anyIn: SpecializeParameter.self) {
692692
case (.target, let handle)?:
693693
let ident = self.eat(handle)
@@ -751,7 +751,7 @@ extension Parser {
751751
case (.exported, let handle)?:
752752
let ident = self.eat(handle)
753753
let (unexpectedBeforeColon, colon) = self.expect(.colon)
754-
let (unexpectedBeforeValue, value) = self.expectAny([.keyword(.true), .keyword(.false)], default: .keyword(.false))
754+
let (unexpectedBeforeValue, value) = self.expect(.keyword(.true), .keyword(.false), default: .keyword(.false))
755755
let comma = self.consume(if: .comma)
756756
elements.append(
757757
.labeledSpecializeEntry(
@@ -951,7 +951,7 @@ extension Parser {
951951
extension Parser {
952952
mutating func parseExposeArguments() -> RawExposeAttributeArgumentsSyntax {
953953
let language: RawTokenSyntax
954-
if !self.at(any: [.rightParen, .comma]) {
954+
if !self.at(.rightParen, .comma) {
955955
language = self.consumeAnyToken()
956956
} else {
957957
language = missingToken(.identifier)
@@ -1076,13 +1076,42 @@ extension Parser {
10761076

10771077
var keepGoing: RawTokenSyntax? = nil
10781078
repeat {
1079-
let (unexpectedBeforeLabel, label) = self.expectAny([.keyword(.visibility), .keyword(.metadata)], default: .keyword(.visibility))
1079+
let (unexpectedBeforeLabel, label) = self.expect(.keyword(.visibility), .keyword(.metadata), default: .keyword(.visibility))
10801080
let (unexpectedBeforeColon, colon) = self.expect(.colon)
10811081
let unexpectedBeforeValue: RawUnexpectedNodesSyntax?
10821082
let value: RawDocumentationAttributeArgumentSyntax.Value
10831083
switch label.tokenText {
10841084
case "visibility":
1085-
let (unexpected, token) = self.expectAny([.keyword(.open), .keyword(.public), .keyword(.internal), .keyword(.fileprivate), .keyword(.private)], default: .keyword(.internal))
1085+
enum AccessLevelModifier: RawTokenKindSubset {
1086+
case `private`
1087+
case `fileprivate`
1088+
case `internal`
1089+
case `public`
1090+
case `open`
1091+
1092+
var rawTokenKind: RawTokenKind {
1093+
switch self {
1094+
case .private: return .keyword(.private)
1095+
case .fileprivate: return .keyword(.fileprivate)
1096+
case .internal: return .keyword(.internal)
1097+
case .public: return .keyword(.public)
1098+
case .open: return .keyword(.open)
1099+
}
1100+
}
1101+
1102+
init?(lexeme: Lexer.Lexeme) {
1103+
switch lexeme {
1104+
case RawTokenKindMatch(.private): self = .private
1105+
case RawTokenKindMatch(.fileprivate): self = .fileprivate
1106+
case RawTokenKindMatch(.internal): self = .internal
1107+
case RawTokenKindMatch(.public): self = .public
1108+
case RawTokenKindMatch(.open): self = .open
1109+
default: return nil
1110+
}
1111+
}
1112+
}
1113+
1114+
let (unexpected, token) = self.expect(anyIn: AccessLevelModifier.self, default: .internal)
10861115
unexpectedBeforeValue = unexpected
10871116
value = .token(token)
10881117
case "metadata":

Sources/SwiftParser/Availability.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ extension Parser {
219219
}
220220

221221
let version: RawVersionTupleSyntax?
222-
if self.at(any: [.integerLiteral, .floatingLiteral]) {
222+
if self.at(.integerLiteral, .floatingLiteral) {
223223
version = self.parseVersionTuple()
224224
} else {
225225
version = nil
@@ -242,7 +242,7 @@ extension Parser {
242242
/// platform-version → decimal-digits '.' decimal-digits
243243
/// platform-version → decimal-digits '.' decimal-digits '.' decimal-digits
244244
mutating func parseVersionTuple() -> RawVersionTupleSyntax {
245-
let (unexpectedBeforeMajorMinor, majorMinor) = self.expectAny([.integerLiteral, .floatingLiteral], default: .integerLiteral)
245+
let (unexpectedBeforeMajorMinor, majorMinor) = self.expect(.integerLiteral, .floatingLiteral, default: .integerLiteral)
246246
let patchPeriod: RawTokenSyntax?
247247
let unexpectedBeforePatch: RawUnexpectedNodesSyntax?
248248
let patch: RawTokenSyntax?

Sources/SwiftParser/Declarations.swift

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ extension Parser {
257257
return RawDeclSyntax(self.parseMacroDeclaration(attrs: attrs, introducerHandle: handle))
258258
case nil:
259259
if inMemberDeclList {
260-
let isProbablyVarDecl = self.at(any: [.identifier, .wildcard]) && self.peek().rawTokenKind.is(any: [.colon, .equal, .comma])
261-
let isProbablyTupleDecl = self.at(.leftParen) && self.peek().rawTokenKind.is(any: [.identifier, .wildcard])
260+
let isProbablyVarDecl = self.at(.identifier, .wildcard) && self.peek().rawTokenKind.is(.colon, .equal, .comma)
261+
let isProbablyTupleDecl = self.at(.leftParen) && self.peek().rawTokenKind.is(.identifier, .wildcard)
262262

263263
if isProbablyVarDecl || isProbablyTupleDecl {
264264
return RawDeclSyntax(self.parseLetOrVarDeclaration(attrs, .missing(.keyword(.var))))
@@ -274,7 +274,7 @@ extension Parser {
274274
)
275275
}
276276

277-
let isProbablyFuncDecl = self.at(any: [.identifier, .wildcard]) || self.at(anyIn: Operator.self) != nil
277+
let isProbablyFuncDecl = self.at(.identifier, .wildcard) || self.at(anyIn: Operator.self) != nil
278278

279279
if isProbablyFuncDecl {
280280
return RawDeclSyntax(self.parseFuncDeclaration(attrs, .missing(.keyword(.func))))
@@ -321,7 +321,45 @@ extension Parser {
321321

322322
@_spi(RawSyntax)
323323
public mutating func parseImportKind() -> RawTokenSyntax? {
324-
return self.consume(ifAny: [.keyword(.typealias), .keyword(.struct), .keyword(.class), .keyword(.enum), .keyword(.protocol), .keyword(.var), .keyword(.let), .keyword(.func)])
324+
enum ImportKind: RawTokenKindSubset {
325+
case `typealias`
326+
case `struct`
327+
case `class`
328+
case `enum`
329+
case `protocol`
330+
case `var`
331+
case `let`
332+
case `func`
333+
334+
var rawTokenKind: RawTokenKind {
335+
switch self {
336+
case .typealias: return .keyword(.typealias)
337+
case .struct: return .keyword(.struct)
338+
case .class: return .keyword(.class)
339+
case .enum: return .keyword(.enum)
340+
case .protocol: return .keyword(.protocol)
341+
case .var: return .keyword(.var)
342+
case .let: return .keyword(.let)
343+
case .func: return .keyword(.func)
344+
}
345+
}
346+
347+
init?(lexeme: Lexer.Lexeme) {
348+
switch lexeme {
349+
case RawTokenKindMatch(.typealias): self = .typealias
350+
case RawTokenKindMatch(.struct): self = .struct
351+
case RawTokenKindMatch(.class): self = .class
352+
case RawTokenKindMatch(.enum): self = .enum
353+
case RawTokenKindMatch(.protocol): self = .protocol
354+
case RawTokenKindMatch(.var): self = .var
355+
case RawTokenKindMatch(.let): self = .let
356+
case RawTokenKindMatch(.func): self = .func
357+
default: return nil
358+
}
359+
}
360+
}
361+
362+
return self.consume(ifAnyIn: ImportKind.self)
325363
}
326364

327365
@_spi(RawSyntax)
@@ -442,7 +480,7 @@ extension Parser {
442480
let unexpectedBeforeInherited: RawUnexpectedNodesSyntax?
443481
let inherited: RawTypeSyntax?
444482
if colon != nil {
445-
if self.at(any: [.identifier, .keyword(.protocol), .keyword(.Any)]) {
483+
if self.at(.identifier, .keyword(.protocol), .keyword(.Any)) {
446484
unexpectedBeforeInherited = nil
447485
inherited = self.parseType()
448486
} else if let classKeyword = self.consume(if: .keyword(.class)) {
@@ -769,7 +807,7 @@ extension Parser {
769807
let (unexpectedBeforeLBrace, lbrace) = self.expect(.leftBrace)
770808
do {
771809
var loopProgress = LoopProgressCondition()
772-
while !self.at(any: [.eof, .rightBrace]) && loopProgress.evaluate(currentToken) {
810+
while !self.at(.eof, .rightBrace) && loopProgress.evaluate(currentToken) {
773811
let newItemAtStartOfLine = self.currentToken.isAtStartOfLine
774812
guard let newElement = self.parseMemberDeclListItem() else {
775813
break
@@ -979,7 +1017,7 @@ extension Parser {
9791017

9801018
// Parse the '!' or '?' for a failable initializer.
9811019
let failable: RawTokenSyntax?
982-
if let parsedFailable = self.consume(ifAny: [.exclamationMark, .postfixQuestionMark, .infixQuestionMark]) {
1020+
if let parsedFailable = self.consume(if: .exclamationMark, .postfixQuestionMark, .infixQuestionMark) {
9831021
failable = parsedFailable
9841022
} else if let parsedFailable = self.consumeIfContextualPunctuator("!", remapping: .exclamationMark) {
9851023
failable = parsedFailable
@@ -1195,7 +1233,7 @@ extension Parser {
11951233
if !shouldSkipParameterParsing {
11961234
var keepGoing = true
11971235
var loopProgress = LoopProgressCondition()
1198-
while !self.at(any: [.eof, .rightParen])
1236+
while !self.at(.eof, .rightParen)
11991237
&& keepGoing
12001238
&& loopProgress.evaluate(currentToken)
12011239
{
@@ -1277,7 +1315,7 @@ extension Parser {
12771315
let (unexpectedBeforeFuncKeyword, funcKeyword) = self.eat(handle)
12781316
let unexpectedBeforeIdentifier: RawUnexpectedNodesSyntax?
12791317
let identifier: RawTokenSyntax
1280-
if self.at(anyIn: Operator.self) != nil || self.at(any: [.exclamationMark, .prefixAmpersand]) || self.atRegexLiteralThatCouldBeAnOperator() {
1318+
if self.at(anyIn: Operator.self) != nil || self.at(.exclamationMark, .prefixAmpersand) || self.atRegexLiteralThatCouldBeAnOperator() {
12811319
var name = self.currentToken.tokenText
12821320
if name.count > 1 && name.hasSuffix("<") && self.peek().rawTokenKind == .identifier {
12831321
name = SyntaxText(rebasing: name.dropLast())
@@ -1552,7 +1590,7 @@ extension Parser {
15521590
// Check there is an identifier before consuming
15531591
var look = self.lookahead()
15541592
let _ = look.consumeAttributeList()
1555-
let hasModifier = look.consume(ifAny: [.keyword(.mutating), .keyword(.nonmutating), .keyword(.__consuming)]) != nil
1593+
let hasModifier = look.consume(if: .keyword(.mutating), .keyword(.nonmutating), .keyword(.__consuming)) != nil
15561594
guard let (kind, handle) = look.at(anyIn: AccessorKind.self) ?? forcedKind else {
15571595
return nil
15581596
}
@@ -1563,7 +1601,7 @@ extension Parser {
15631601
// get and set.
15641602
let modifier: RawDeclModifierSyntax?
15651603
if hasModifier {
1566-
let (unexpectedBeforeName, name) = self.expectAny([.keyword(.mutating), .keyword(.nonmutating), .keyword(.__consuming)], default: .keyword(.mutating))
1604+
let (unexpectedBeforeName, name) = self.expect(.keyword(.mutating), .keyword(.nonmutating), .keyword(.__consuming), default: .keyword(.mutating))
15671605
modifier = RawDeclModifierSyntax(
15681606
unexpectedBeforeName,
15691607
name: name,
@@ -1667,7 +1705,7 @@ extension Parser {
16671705
var elements = [RawAccessorDeclSyntax]()
16681706
do {
16691707
var loopProgress = LoopProgressCondition()
1670-
while !self.at(any: [.eof, .rightBrace]) && loopProgress.evaluate(currentToken) {
1708+
while !self.at(.eof, .rightBrace) && loopProgress.evaluate(currentToken) {
16711709
guard let introducer = self.parseAccessorIntroducer() else {
16721710
// There can only be an implicit getter if no other accessors were
16731711
// seen before this one.
@@ -1812,7 +1850,7 @@ extension Parser {
18121850
case (_, let handle)?:
18131851
(unexpectedBeforeName, name) = self.eat(handle)
18141852
default:
1815-
if let identifier = self.consume(ifAny: [.identifier, .dollarIdentifier], allowTokenAtStartOfLine: false) {
1853+
if let identifier = self.consume(if: .identifier, .dollarIdentifier, allowTokenAtStartOfLine: false) {
18161854
// Recover if the developer tried to use an identifier as the operator name
18171855
unexpectedBeforeName = RawUnexpectedNodesSyntax([identifier], arena: self.arena)
18181856
} else {
@@ -1827,7 +1865,7 @@ extension Parser {
18271865
var loopProgress = LoopProgressCondition()
18281866
while (identifiersAfterOperatorName.last ?? name).trailingTriviaByteLength == 0,
18291867
self.currentToken.leadingTriviaByteLength == 0,
1830-
!self.at(any: [.colon, .leftBrace, .eof]),
1868+
!self.at(.colon, .leftBrace, .eof),
18311869
loopProgress.evaluate(self.currentToken)
18321870
{
18331871
identifiersAfterOperatorName.append(consumeAnyToken())
@@ -1977,12 +2015,12 @@ extension Parser {
19772015
var elements = [RawPrecedenceGroupAttributeListSyntax.Element]()
19782016
do {
19792017
var attributesProgress = LoopProgressCondition()
1980-
LOOP: while !self.at(any: [.eof, .rightBrace]) && attributesProgress.evaluate(currentToken) {
2018+
LOOP: while !self.at(.eof, .rightBrace) && attributesProgress.evaluate(currentToken) {
19812019
switch self.at(anyIn: LabelText.self) {
19822020
case (.associativity, let handle)?:
19832021
let associativity = self.eat(handle)
19842022
let (unexpectedBeforeColon, colon) = self.expect(.colon)
1985-
var (unexpectedBeforeValue, value) = self.expectAny([.keyword(.left), .keyword(.right), .keyword(.none)], default: .keyword(.none))
2023+
var (unexpectedBeforeValue, value) = self.expect(.keyword(.left), .keyword(.right), .keyword(.none), default: .keyword(.none))
19862024
if value.isMissing, let identifier = self.consume(if: .identifier) {
19872025
unexpectedBeforeValue = RawUnexpectedNodesSyntax(combining: unexpectedBeforeValue, identifier, arena: self.arena)
19882026
}
@@ -2001,7 +2039,7 @@ extension Parser {
20012039
case (.assignment, let handle)?:
20022040
let assignmentKeyword = self.eat(handle)
20032041
let (unexpectedBeforeColon, colon) = self.expect(.colon)
2004-
let (unexpectedBeforeFlag, flag) = self.expectAny([.keyword(.true), .keyword(.false)], default: .keyword(.true))
2042+
let (unexpectedBeforeFlag, flag) = self.expect(.keyword(.true), .keyword(.false), default: .keyword(.true))
20052043
let unexpectedAfterFlag: RawUnexpectedNodesSyntax?
20062044
if flag.isMissing, let unexpectedIdentifier = self.consume(if: .identifier, allowTokenAtStartOfLine: false) {
20072045
unexpectedAfterFlag = RawUnexpectedNodesSyntax([unexpectedIdentifier], arena: self.arena)

Sources/SwiftParser/Directives.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,29 @@
1313
@_spi(RawSyntax) import SwiftSyntax
1414

1515
extension Parser {
16+
private enum IfConfigClauseStartKeyword: RawTokenKindSubset {
17+
case poundIfKeyword
18+
case poundElseifKeyword
19+
case poundElseKeyword
20+
21+
var rawTokenKind: RawTokenKind {
22+
switch self {
23+
case .poundIfKeyword: return .poundIfKeyword
24+
case .poundElseifKeyword: return .poundElseifKeyword
25+
case .poundElseKeyword: return .poundElseKeyword
26+
}
27+
}
28+
29+
init?(lexeme: Lexer.Lexeme) {
30+
switch lexeme {
31+
case RawTokenKindMatch(.poundIfKeyword): self = .poundIfKeyword
32+
case RawTokenKindMatch(.poundElseifKeyword): self = .poundElseifKeyword
33+
case RawTokenKindMatch(.poundElseKeyword): self = .poundElseKeyword
34+
default: return nil
35+
}
36+
}
37+
}
38+
1639
/// Parse a conditional compilation block.
1740
///
1841
/// This function should be used to parse conditional compilation statements,
@@ -83,7 +106,7 @@ extension Parser {
83106
do {
84107
var firstIteration = true
85108
var loopProgress = LoopProgressCondition()
86-
while let poundIfHandle = self.canRecoverTo(any: firstIteration ? [.poundIfKeyword] : [.poundIfKeyword, .poundElseifKeyword, .poundElseKeyword]),
109+
while let poundIfHandle = firstIteration ? self.canRecoverTo(.poundIfKeyword) : self.canRecoverTo(anyIn: IfConfigClauseStartKeyword.self)?.handle,
87110
loopProgress.evaluate(self.currentToken)
88111
{
89112
let (unexpectedBeforePoundIf, poundIf) = self.eat(poundIfHandle)
@@ -102,7 +125,7 @@ extension Parser {
102125
var elements = [Element]()
103126
do {
104127
var elementsProgress = LoopProgressCondition()
105-
while !self.at(any: [.eof, .poundElseKeyword, .poundElseifKeyword, .poundEndifKeyword]) && elementsProgress.evaluate(currentToken) {
128+
while !self.at(.eof) && !self.at(.poundElseKeyword, .poundElseifKeyword, .poundEndifKeyword) && elementsProgress.evaluate(currentToken) {
106129
let newItemAtStartOfLine = self.currentToken.isAtStartOfLine
107130
guard let element = parseElement(&self), !element.isEmpty else {
108131
break

0 commit comments

Comments
 (0)