Skip to content

Commit 8d527d2

Browse files
committed
Use a switch over SyntaxEnum to clean up the semantic check
1 parent 16ca7ed commit 8d527d2

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

Sources/SwiftOperatorPrecedence/OperatorPrecedence+Semantics.swift

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ extension PrecedenceGroup {
2020
self.syntax = syntax
2121

2222
for attr in syntax.groupAttributes {
23+
switch attr.as(SyntaxEnum.self) {
2324
// Relation (lowerThan, higherThan)
24-
if let relation = attr.as(PrecedenceGroupRelationSyntax.self) {
25+
case .precedenceGroupRelation(let relation):
2526
let isLowerThan = relation.higherThanOrLowerThan.text == "lowerThan"
2627
for otherGroup in relation.otherNames {
2728
let otherGroupName = otherGroup.name.text
@@ -32,17 +33,12 @@ extension PrecedenceGroup {
3233
self.relations.append(relation)
3334
}
3435

35-
continue
36-
}
37-
3836
// Assignment
39-
if let assignment = attr.as(PrecedenceGroupAssignmentSyntax.self) {
37+
case .precedenceGroupAssignment(let assignment):
4038
self.assignment = assignment.flag.text == "true"
41-
continue
42-
}
4339

4440
// Associativity
45-
if let associativity = attr.as(PrecedenceGroupAssociativitySyntax.self) {
41+
case .precedenceGroupAssociativity(let associativity):
4642
switch associativity.value.text {
4743
case "left":
4844
self.associativity = .left
@@ -56,6 +52,9 @@ extension PrecedenceGroup {
5652
default:
5753
break
5854
}
55+
56+
default:
57+
break
5958
}
6059
}
6160
}
@@ -68,15 +67,9 @@ extension Operator {
6867
init(from syntax: OperatorDeclSyntax) {
6968
self.syntax = syntax
7069

71-
let kindModifier = syntax.modifiers?.first { modifier in
72-
OperatorKind(rawValue: modifier.name.text) != nil
73-
}
74-
75-
if let kindModifier = kindModifier {
76-
kind = OperatorKind(rawValue: kindModifier.name.text)!
77-
} else {
78-
kind = .infix
79-
}
70+
kind = syntax.modifiers?.compactMap {
71+
OperatorKind(rawValue: $0.name.text)
72+
}.first ?? .infix
8073

8174
name = syntax.identifier.text
8275

0 commit comments

Comments
 (0)