@@ -21,7 +21,9 @@ import SwiftSyntax
2121/// syntax tree.
2222public struct OperatorTable {
2323 var precedenceGraph : PrecedenceGraph = . init( )
24- var operators : [ OperatorName : Operator ] = [ : ]
24+ var infixOperators : [ OperatorName : Operator ] = [ : ]
25+ var prefixOperators : [ OperatorName : Operator ] = [ : ]
26+ var postfixOperators : [ OperatorName : Operator ] = [ : ]
2527
2628 public init ( ) { }
2729
@@ -40,19 +42,33 @@ public struct OperatorTable {
4042 }
4143 }
4244
43- /// Record the operator, if it matters .
44- mutating func record(
45+ /// Record the operator in the given operator array .
46+ private func record(
4547 _ op: Operator ,
48+ in table: inout [ OperatorName : Operator ] ,
4649 errorHandler: OperatorPrecedenceErrorHandler = { throw $0 }
4750 ) rethrows {
48- // FIXME: Could do operator-already-exists checking for prefix/postfix
49- // operators as well, since we parse them.
50- if op. kind != . infix { return }
51-
52- if let existing = operators [ op. name] {
51+ if let existing = table [ op. name] {
5352 try errorHandler ( . operatorAlreadyExists( existing: existing, new: op) )
5453 } else {
55- operators [ op. name] = op
54+ table [ op. name] = op
55+ }
56+ }
57+
58+ /// Record the operator.
59+ mutating func record(
60+ _ op: Operator ,
61+ errorHandler: OperatorPrecedenceErrorHandler = { throw $0 }
62+ ) rethrows {
63+ switch op. kind {
64+ case . infix:
65+ return try record ( op, in: & infixOperators, errorHandler: errorHandler)
66+
67+ case . prefix:
68+ return try record ( op, in: & prefixOperators, errorHandler: errorHandler)
69+
70+ case . postfix:
71+ return try record ( op, in: & postfixOperators, errorHandler: errorHandler)
5672 }
5773 }
5874
@@ -72,7 +88,7 @@ extension OperatorTable {
7288 referencedFrom syntax: Syntax ? ,
7389 errorHandler: OperatorPrecedenceErrorHandler = { throw $0 }
7490 ) rethrows -> PrecedenceGroupName ? {
75- guard let op = operators [ operatorName] else {
91+ guard let op = infixOperators [ operatorName] else {
7692 try errorHandler (
7793 . missingOperator( operatorName, referencedFrom: syntax) )
7894 return nil
0 commit comments