Skip to content

Commit c34bbb6

Browse files
committed
Add enums for the element choices of syntax collections
1 parent 59b5efe commit c34bbb6

File tree

14 files changed

+290
-76
lines changed

14 files changed

+290
-76
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/AttributeNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public let ATTRIBUTE_NODES: [Node] = [
137137
kind: "SyntaxCollection",
138138
element: "Syntax",
139139
elementName: "Attribute",
140-
elementChoices: ["Attribute", "CustomAttribute"],
140+
elementChoices: ["Attribute", "CustomAttribute", "IfConfigDecl"],
141141
omitWhenEmpty: true),
142142

143143
Node(name: "SpecializeAttributeSpecList",

Sources/SwiftParser/Attributes.swift

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension Parser {
1818
return nil
1919
}
2020

21-
var elements = [RawSyntax]()
21+
var elements = [RawAttributeListSyntax.Element]()
2222
var loopProgress = LoopProgressCondition()
2323
repeat {
2424
let attribute = self.parseAttribute()
@@ -29,44 +29,44 @@ extension Parser {
2929
}
3030

3131
extension Parser {
32-
mutating func parseAttribute() -> RawSyntax {
32+
mutating func parseAttribute() -> RawAttributeListSyntax.Element {
3333
if self.at(.poundIfKeyword) {
34-
return RawSyntax(self.parsePoundIfDirective { parser -> RawSyntax in
34+
return .ifConfigDecl(self.parsePoundIfDirective { parser -> RawAttributeListSyntax.Element in
3535
return parser.parseAttribute()
3636
} syntax: { parser, attributes in
3737
return .attributes(RawAttributeListSyntax(elements: attributes, arena: parser.arena))
3838
})
3939
}
4040

4141
guard let declAttr = DeclarationAttribute(rawValue: self.peek().tokenText) else {
42-
return RawSyntax(self.parseCustomAttribute())
42+
return .customAttribute(self.parseCustomAttribute())
4343
}
4444

4545
switch declAttr {
4646
case .available:
47-
return RawSyntax(self.parseAvailabilityAttribute())
47+
return .attribute(self.parseAvailabilityAttribute())
4848
case ._spi_available:
49-
return RawSyntax(self.parseSPIAvailableAttribute())
49+
return .attribute(self.parseSPIAvailableAttribute())
5050
case .differentiable:
51-
return RawSyntax(self.parseDifferentiableAttribute())
51+
return .attribute(self.parseDifferentiableAttribute())
5252
case .derivative:
53-
return RawSyntax(self.parseDerivativeAttribute())
53+
return .attribute(self.parseDerivativeAttribute())
5454
case .transpose:
55-
return RawSyntax(self.parseTransposeAttribute())
55+
return .attribute(self.parseTransposeAttribute())
5656
case .objc:
57-
return RawSyntax(self.parseObjectiveCAttribute())
57+
return .attribute(self.parseObjectiveCAttribute())
5858
case ._specialize:
59-
return RawSyntax(self.parseSpecializeAttribute())
59+
return .attribute(self.parseSpecializeAttribute())
6060
case ._private:
61-
return RawSyntax(self.parsePrivateImportAttribute())
61+
return .attribute(self.parsePrivateImportAttribute())
6262
case ._dynamicReplacement:
63-
return RawSyntax(self.parseDynamicReplacementAttribute())
63+
return .attribute(self.parseDynamicReplacementAttribute())
6464
case ._spi:
65-
return RawSyntax(self.parseSPIAttribute())
65+
return .attribute(self.parseSPIAttribute())
6666
case ._implements:
67-
return RawSyntax(self.parseImplementsAttribute())
67+
return .attribute(self.parseImplementsAttribute())
6868
case ._semantics:
69-
return RawSyntax(self.parseSemanticsAttribute())
69+
return .attribute(self.parseSemanticsAttribute())
7070
default:
7171
break
7272
}
@@ -91,7 +91,7 @@ extension Parser {
9191
unexpectedBeforeRightParen = nil
9292
rightParen = nil
9393
}
94-
return RawSyntax(RawAttributeSyntax(
94+
return .attribute(RawAttributeSyntax(
9595
unexpectedBeforeAtSign,
9696
atSignToken: atSign,
9797
unexpectedBeforeIdent,
@@ -536,7 +536,7 @@ extension Parser {
536536
case available
537537
}
538538
mutating func parseSpecializeAttributeSpecList() -> RawSpecializeAttributeSpecListSyntax {
539-
var elements = [RawSyntax]()
539+
var elements = [RawSpecializeAttributeSpecListSyntax.Element]()
540540
// Parse optional "exported" and "kind" labeled parameters.
541541
var loopProgress = LoopProgressCondition()
542542
while !self.at(any: [.eof, .rightParen, .whereKeyword]) && loopProgress.evaluate(currentToken) {
@@ -550,7 +550,7 @@ extension Parser {
550550
declNameArguments: args,
551551
arena: self.arena)
552552
let comma = self.consume(if: .comma)
553-
elements.append(RawSyntax(RawTargetFunctionEntrySyntax(
553+
elements.append(.targetFunctionEntry(RawTargetFunctionEntrySyntax(
554554
label: ident,
555555
unexpectedBeforeColon,
556556
colon: colon,
@@ -563,7 +563,7 @@ extension Parser {
563563
let (unexpectedBeforeColon, colon) = self.expect(.colon)
564564
let availability = self.parseAvailabilitySpecList(from: .available)
565565
let (unexpectedBeforeSemi, semi) = self.expect(.semicolon)
566-
elements.append(RawSyntax(RawAvailabilityEntrySyntax(
566+
elements.append(.availabilityEntry(RawAvailabilityEntrySyntax(
567567
label: ident,
568568
unexpectedBeforeColon,
569569
colon: colon,
@@ -579,7 +579,7 @@ extension Parser {
579579
// tree only allows us to insert a token so we'll take anything.
580580
let available = self.consumeAnyToken()
581581
let comma = self.consume(if: .comma)
582-
elements.append(RawSyntax(RawLabeledSpecializeEntrySyntax(
582+
elements.append(.labeledSpecializeEntry(RawLabeledSpecializeEntrySyntax(
583583
label: ident,
584584
unexpectedBeforeColon,
585585
colon: colon,
@@ -592,7 +592,7 @@ extension Parser {
592592
let (unexpectedBeforeColon, colon) = self.expect(.colon)
593593
let (unexpectedBeforeValue, value) = self.expectAny([.trueKeyword, .falseKeyword], default: .falseKeyword)
594594
let comma = self.consume(if: .comma)
595-
elements.append(RawSyntax(RawLabeledSpecializeEntrySyntax(
595+
elements.append(.labeledSpecializeEntry(RawLabeledSpecializeEntrySyntax(
596596
label: ident,
597597
unexpectedBeforeColon,
598598
colon: colon,
@@ -606,7 +606,7 @@ extension Parser {
606606
let (unexpectedBeforeColon, colon) = self.expect(.colon)
607607
let valueLabel = self.parseAnyIdentifier()
608608
let comma = self.consume(if: .comma)
609-
elements.append(RawSyntax(RawLabeledSpecializeEntrySyntax(
609+
elements.append(.labeledSpecializeEntry(RawLabeledSpecializeEntrySyntax(
610610
label: ident,
611611
unexpectedBeforeColon,
612612
colon: colon,
@@ -620,7 +620,7 @@ extension Parser {
620620
let (unexpectedBeforeColon, colon) = self.expect(.colon)
621621
let valueLabel = self.consumeAnyToken()
622622
let comma = self.consume(if: .comma)
623-
elements.append(RawSyntax(RawLabeledSpecializeEntrySyntax(
623+
elements.append(.labeledSpecializeEntry(RawLabeledSpecializeEntrySyntax(
624624
label: ident,
625625
unexpectedBeforeColon,
626626
colon: colon,
@@ -633,7 +633,7 @@ extension Parser {
633633
let (unexpectedBeforeColon, colon) = self.expect(.colon)
634634
let valueLabel = self.consumeAnyToken()
635635
let comma = self.consume(if: .comma)
636-
elements.append(RawSyntax(RawLabeledSpecializeEntrySyntax(
636+
elements.append(.labeledSpecializeEntry(RawLabeledSpecializeEntrySyntax(
637637
label: ident,
638638
unexpectedBeforeColon,
639639
colon: colon,
@@ -647,7 +647,7 @@ extension Parser {
647647
// Parse the where clause.
648648
if self.at(.whereKeyword) {
649649
let whereClause = self.parseGenericWhereClause()
650-
elements.append(RawSyntax(whereClause))
650+
elements.append(.genericWhereClause(whereClause))
651651
}
652652
return RawSpecializeAttributeSpecListSyntax(elements: elements, arena: self.arena)
653653
}

Sources/SwiftParser/Declarations.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ extension Parser {
18251825
case lowerThan = "lowerThan"
18261826
}
18271827

1828-
var elements = [RawSyntax]()
1828+
var elements = [RawPrecedenceGroupAttributeListSyntax.Element]()
18291829
do {
18301830
var attributesProgress = LoopProgressCondition()
18311831
LOOP: while !self.at(any: [.eof, .rightBrace]) && attributesProgress.evaluate(currentToken) {
@@ -1834,7 +1834,7 @@ extension Parser {
18341834
let associativity = self.eat(handle)
18351835
let (unexpectedBeforeColon, colon) = self.expect(.colon)
18361836
let (unexpectedBeforeValue, value) = self.expectIdentifier()
1837-
elements.append(RawSyntax(RawPrecedenceGroupAssociativitySyntax(
1837+
elements.append(.precedenceGroupAssociativity(RawPrecedenceGroupAssociativitySyntax(
18381838
associativityKeyword: associativity,
18391839
unexpectedBeforeColon,
18401840
colon: colon,
@@ -1852,7 +1852,7 @@ extension Parser {
18521852
} else {
18531853
unexpectedAfterFlag = nil
18541854
}
1855-
elements.append(RawSyntax(RawPrecedenceGroupAssignmentSyntax(
1855+
elements.append(.precedenceGroupAssignment(RawPrecedenceGroupAssignmentSyntax(
18561856
assignmentKeyword: assignmentKeyword,
18571857
unexpectedBeforeColon,
18581858
colon: colon,
@@ -1881,7 +1881,7 @@ extension Parser {
18811881
))
18821882
} while keepGoing != nil && namesProgress.evaluate(currentToken)
18831883
}
1884-
elements.append(RawSyntax(RawPrecedenceGroupRelationSyntax(
1884+
elements.append(.precedenceGroupRelation(RawPrecedenceGroupRelationSyntax(
18851885
higherThanOrLowerThan: level,
18861886
unexpectedBeforeColon,
18871887
colon: colon,

Sources/SwiftParser/Expressions.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ extension Parser {
14801480
) -> (RawStringLiteralSegmentsSyntax, SyntaxText.Index) {
14811481
let allowsMultiline = closer.tokenKind == .multilineStringQuote
14821482

1483-
var segments = [RawSyntax]()
1483+
var segments = [RawStringLiteralSegmentsSyntax.Element]()
14841484
var segment = text
14851485
var stringLiteralSegmentStart = segment.startIndex
14861486
while let slashIndex = segment.firstIndex(of: UInt8(ascii: "\\")), stringLiteralSegmentStart < segment.endIndex {
@@ -1508,7 +1508,7 @@ extension Parser {
15081508
text: SyntaxText(rebasing: text[stringLiteralSegmentStart..<slashIndex]),
15091509
presence: .present,
15101510
arena: self.arena)
1511-
segments.append(RawSyntax(RawStringSegmentSyntax(content: segmentToken, arena: self.arena)))
1511+
segments.append(.stringSegment(RawStringSegmentSyntax(content: segmentToken, arena: self.arena)))
15121512

15131513
let content = SyntaxText(rebasing: text[contentStart...])
15141514
let contentSize = content.withBuffer { buf in
@@ -1557,7 +1557,7 @@ extension Parser {
15571557
}
15581558
let rparen = subparser.expectWithoutRecovery(.rightParen)
15591559

1560-
segments.append(RawSyntax(RawExpressionSegmentSyntax(
1560+
segments.append(.expressionSegment(RawExpressionSegmentSyntax(
15611561
backslash: slashToken,
15621562
delimiter: delim,
15631563
lunexpected,
@@ -1598,7 +1598,7 @@ extension Parser {
15981598
text: SyntaxText(rebasing: segment),
15991599
presence: .present,
16001600
arena: self.arena)
1601-
segments.append(RawSyntax(RawStringSegmentSyntax(content: segmentToken,
1601+
segments.append(.stringSegment(RawStringSegmentSyntax(content: segmentToken,
16021602
arena: self.arena)))
16031603

16041604
return (RawStringLiteralSegmentsSyntax(elements: segments, arena: arena), segment.endIndex)

Sources/SwiftParser/Statements.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -675,16 +675,16 @@ extension Parser {
675675
/// this recovery is disabled.
676676
@_spi(RawSyntax)
677677
public mutating func parseSwitchCases(allowStandaloneStmtRecovery: Bool) -> RawSwitchCaseListSyntax {
678-
var elements = [RawSyntax]()
678+
var elements = [RawSwitchCaseListSyntax.Element]()
679679
var elementsProgress = LoopProgressCondition()
680680
while !self.at(any: [.eof, .rightBrace, .poundEndifKeyword, .poundElseifKeyword, .poundElseKeyword])
681681
&& elementsProgress.evaluate(currentToken) {
682682
if self.lookahead().isAtStartOfSwitchCase(allowRecovery: false) {
683-
elements.append(RawSyntax(self.parseSwitchCase()))
683+
elements.append(.switchCase(self.parseSwitchCase()))
684684
} else if self.canRecoverTo(.poundIfKeyword) != nil {
685685
// '#if' in 'case' position can enclose zero or more 'case' or 'default'
686686
// clauses.
687-
elements.append(RawSyntax(self.parsePoundIfDirective(
687+
elements.append(.ifConfigDecl(self.parsePoundIfDirective(
688688
{ $0.parseSwitchCases(allowStandaloneStmtRecovery: allowStandaloneStmtRecovery) },
689689
syntax: { parser, cases in
690690
guard cases.count == 1, let firstCase = cases.first else {
@@ -696,7 +696,7 @@ extension Parser {
696696
} else if allowStandaloneStmtRecovery && (self.atStartOfExpression() || self.atStartOfStatement() || self.atStartOfDeclaration()) {
697697
// Synthesize a label for the stamenent or declaration that isn't coverd by a case right now.
698698
let statements = parseSwitchCaseBody()
699-
elements.append(RawSyntax(RawSwitchCaseSyntax(
699+
elements.append(.switchCase(RawSwitchCaseSyntax(
700700
unknownAttr: nil,
701701
label: .case(RawSwitchCaseLabelSyntax(
702702
caseKeyword: missingToken(.caseKeyword, text: nil),
@@ -718,7 +718,7 @@ extension Parser {
718718
arena: self.arena
719719
)))
720720
} else if self.lookahead().isAtStartOfSwitchCase(allowRecovery: true) {
721-
elements.append(RawSyntax(self.parseSwitchCase()))
721+
elements.append(.switchCase(self.parseSwitchCase()))
722722
} else {
723723
break
724724
}

Sources/SwiftParser/Types.swift

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ extension Parser {
4848
}
4949

5050
mutating func parseTypeScalar(misplacedSpecifiers: [RawTokenSyntax] = []) -> RawTypeSyntax {
51-
let (specifier, attrList) = self.parseTypeAttributeList(misplacedSpecifiers: misplacedSpecifiers)
51+
let (specifier, unexpectedBeforeAttrList, attrList) = self.parseTypeAttributeList(misplacedSpecifiers: misplacedSpecifiers)
5252
var base = RawTypeSyntax(self.parseSimpleOrCompositionType())
5353
if self.lookahead().isAtFunctionTypeArrow() {
5454
let firstEffect = self.parseEffectsSpecifier()
@@ -97,9 +97,10 @@ extension Parser {
9797
arena: self.arena))
9898
}
9999

100-
if attrList != nil || specifier != nil {
100+
if unexpectedBeforeAttrList != nil || specifier != nil || attrList != nil {
101101
return RawTypeSyntax(RawAttributedTypeSyntax(
102102
specifier: specifier,
103+
unexpectedBeforeAttrList,
103104
attributes: attrList,
104105
baseType: base, arena: self.arena))
105106
} else {
@@ -821,34 +822,34 @@ extension Parser.Lookahead {
821822

822823
extension Parser {
823824
@_spi(RawSyntax)
824-
public mutating func parseTypeAttributeList(misplacedSpecifiers: [RawTokenSyntax] = []) -> (RawTokenSyntax?, RawAttributeListSyntax?) {
825+
public mutating func parseTypeAttributeList(misplacedSpecifiers: [RawTokenSyntax] = []) -> (
826+
specifier: RawTokenSyntax?, unexpectedBeforeAttributes: RawUnexpectedNodesSyntax?, attributes: RawAttributeListSyntax?
827+
) {
825828
var specifier: RawTokenSyntax? = self.consume(ifAnyIn: TypeSpecifier.self)
826829
// We can only stick one specifier on this type. Let's pick the first one
827830
if specifier == nil, let misplacedSpecifier = misplacedSpecifiers.first {
828831
specifier = missingToken(misplacedSpecifier.tokenKind, text: misplacedSpecifier.tokenText)
829832
}
833+
var extraneousSpecifiers: [RawTokenSyntax] = []
834+
while let extraSpecifier = self.consume(ifAny: [.inoutKeyword], contextualKeywords: ["__shared", "__owned", "isolated", "_const"]) {
835+
if specifier == nil {
836+
specifier = extraSpecifier
837+
} else {
838+
extraneousSpecifiers.append(extraSpecifier)
839+
}
840+
}
841+
let unexpectedBeforeAttributeList = RawUnexpectedNodesSyntax(extraneousSpecifiers, arena: self.arena)
830842

831843
if self.at(any: [.atSign, .inoutKeyword]) {
832-
return (specifier, self.parseTypeAttributeListPresent())
844+
return (specifier, unexpectedBeforeAttributeList, self.parseTypeAttributeListPresent())
833845
}
834846

835-
if self.at(.identifier) {
836-
if self.at(any: [], contextualKeywords: ["__shared", "__owned", "isolated", "_const"]) {
837-
return (specifier, self.parseTypeAttributeListPresent())
838-
839-
}
840-
}
841-
return (specifier, nil)
847+
return (specifier, unexpectedBeforeAttributeList, nil)
842848
}
843849

844850
@_spi(RawSyntax)
845851
public mutating func parseTypeAttributeListPresent() -> RawAttributeListSyntax {
846-
var elements = [RawSyntax]()
847-
var modifiersProgress = LoopProgressCondition()
848-
while let token = self.consume(ifAny: [.inoutKeyword], contextualKeywords: ["__shared", "__owned", "isolated", "_const"]), modifiersProgress.evaluate(currentToken) {
849-
elements.append(RawSyntax(token))
850-
}
851-
852+
var elements = [RawAttributeListSyntax.Element]()
852853
var attributeProgress = LoopProgressCondition()
853854
while self.at(.atSign) && attributeProgress.evaluate(currentToken) {
854855
elements.append(self.parseTypeAttribute())
@@ -857,22 +858,22 @@ extension Parser {
857858
}
858859

859860
@_spi(RawSyntax)
860-
public mutating func parseTypeAttribute() -> RawSyntax {
861+
public mutating func parseTypeAttribute() -> RawAttributeListSyntax.Element {
861862
guard let typeAttr = Parser.TypeAttribute(rawValue: self.peek().tokenText) else {
862-
return RawSyntax(self.parseCustomAttribute())
863+
return .customAttribute(self.parseCustomAttribute())
863864
}
864865

865866
switch typeAttr {
866867
case .differentiable:
867-
return RawSyntax(self.parseDifferentiableAttribute())
868+
return .attribute(self.parseDifferentiableAttribute())
868869

869870
case .convention:
870871
let (unexpectedBeforeAt, at) = self.expect(.atSign)
871872
let (unexpectedBeforeIdent, ident) = self.expectIdentifier()
872873
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
873874
let arguments = self.parseConventionArguments()
874875
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
875-
return RawSyntax(
876+
return .attribute(
876877
RawAttributeSyntax(
877878
unexpectedBeforeAt,
878879
atSignToken: at,
@@ -893,7 +894,7 @@ extension Parser {
893894
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
894895
let argument = self.parseOpaqueReturnTypeOfAttributeArguments()
895896
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
896-
return RawSyntax(
897+
return .attribute(
897898
RawAttributeSyntax(
898899
unexpectedBeforeAt,
899900
atSignToken: at,
@@ -911,7 +912,7 @@ extension Parser {
911912
default:
912913
let (unexpectedBeforeAt, at) = self.expect(.atSign)
913914
let ident = self.expectIdentifierWithoutRecovery()
914-
return RawSyntax(
915+
return .attribute(
915916
RawAttributeSyntax(
916917
unexpectedBeforeAt,
917918
atSignToken: at,

0 commit comments

Comments
 (0)