Skip to content

Commit 7be7bee

Browse files
committed
Distinguish default match action
1 parent 73119c3 commit 7be7bee

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

Sources/BinaryParseKitMacros/Macros/ParseEnum/ConstructParseEnumMacro.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public struct ConstructEnumParseMacro: ExtensionMacro {
5353
} else if let toBeMatched = caseParseInfo.bytesToMatch(of: type) {
5454
// Byte-array-based matching: use __matchBytes with inout span
5555
ExprSyntax("\(raw: Constants.UtilityFunctions.matchBytes)(\(toBeMatched), in: &span)")
56+
} else if caseParseInfo.matchAction.target.isDefaultMatch {
57+
// Default matching: always true
58+
ExprSyntax("true")
5659
} else {
5760
// Otherwise, it's a failure on our side
5861
throw ParseEnumMacroError.unexpectedError(
@@ -61,9 +64,7 @@ public struct ConstructEnumParseMacro: ExtensionMacro {
6164
}
6265
}
6366

64-
try IfExprSyntax(
65-
"if \(matchCondition)",
66-
) {
67+
try IfExprSyntax("if \(matchCondition)") {
6768
if caseParseInfo.matchAction.matchPolicy == .matchAndTake {
6869
if let toBeMatched = caseParseInfo.bytesToMatch(of: type) {
6970
"try span.seek(toRelativeOffset: \(toBeMatched).count)"
@@ -204,7 +205,8 @@ public struct ConstructEnumParseMacro: ExtensionMacro {
204205
let caseCodeBlock = try CodeBlockItemListSyntax {
205206
let bytesTakenInMatching = context.makeUniqueName("bytesTakenInMatching")
206207

207-
if caseParseInfo.matchAction.isLengthMatch {
208+
if caseParseInfo.matchAction.target.isLengthMatch
209+
|| caseParseInfo.matchAction.target.isDefaultMatch {
208210
// For length-based matching, use empty bytes array (similar to matchDefault)
209211
"let \(bytesTakenInMatching): [UInt8] = []"
210212
} else if let bytesToMatch = caseParseInfo.bytesToMatch(of: type) {

Sources/BinaryParseKitMacros/Macros/ParseEnum/EnumCaseParseInfo.swift

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,47 +52,56 @@ enum EnumMatchTarget {
5252
// @matchAndTake() -> .bytes(nil)
5353
// @matchAndTake(byte: byte) -> .byte([byte])
5454
// @matchAndTake(bytes: bytes) -> .bytes(bytes)
55-
// @matchDefault() -> .bytes([])
5655
case bytes(ExprSyntax?)
5756
// @match(length: length) -> .length(length)
5857
case length(ExprSyntax)
59-
}
60-
61-
struct EnumCaseMatchAction {
62-
let target: EnumMatchTarget
63-
let matchPolicy: EnumCaseMatchPolicy
58+
// @matchDefault
59+
case `default`
6460

6561
var matchBytes: ExprSyntax?? {
66-
if case let .bytes(bytes) = target {
62+
if case let .bytes(bytes) = self {
6763
bytes
6864
} else {
6965
nil
7066
}
7167
}
7268

7369
var matchLength: ExprSyntax? {
74-
if case let .length(length) = target {
70+
if case let .length(length) = self {
7571
length
7672
} else {
7773
nil
7874
}
7975
}
8076

8177
var isLengthMatch: Bool {
82-
if case .length = target {
78+
if case .length = self {
8379
true
8480
} else {
8581
false
8682
}
8783
}
8884

8985
var isByteMatch: Bool {
90-
if case .bytes = target {
86+
if case .bytes = self {
87+
true
88+
} else {
89+
false
90+
}
91+
}
92+
93+
var isDefaultMatch: Bool {
94+
if case .default = self {
9195
true
9296
} else {
9397
false
9498
}
9599
}
100+
}
101+
102+
struct EnumCaseMatchAction {
103+
let target: EnumMatchTarget
104+
let matchPolicy: EnumCaseMatchPolicy
96105

97106
static func match(bytes: ExprSyntax?) -> EnumCaseMatchAction {
98107
EnumCaseMatchAction(target: .bytes(bytes), matchPolicy: .match)
@@ -103,7 +112,7 @@ struct EnumCaseMatchAction {
103112
}
104113

105114
static func matchDefault() -> EnumCaseMatchAction {
106-
EnumCaseMatchAction(target: .bytes("[]"), matchPolicy: .matchDefault)
115+
EnumCaseMatchAction(target: .default, matchPolicy: .matchDefault)
107116
}
108117

109118
static func matchAndTake(bytes: ExprSyntax?) -> EnumCaseMatchAction {
@@ -189,7 +198,7 @@ struct EnumCaseParseInfo {
189198
}
190199

191200
func bytesToMatch(of type: some TypeSyntaxProtocol) -> ExprSyntax? {
192-
matchAction.matchBytes.map { matchBytes in
201+
matchAction.target.matchBytes.map { matchBytes in
193202
if let matchBytes {
194203
matchBytes
195204
} else {
@@ -201,7 +210,7 @@ struct EnumCaseParseInfo {
201210
}
202211

203212
func lengthToMatch() -> ExprSyntax? {
204-
matchAction.matchLength
213+
matchAction.target.matchLength
205214
}
206215
}
207216

Sources/BinaryParseKitMacros/Macros/ParseEnum/ParseEnumCase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class ParseEnumCase: SyntaxVisitor {
111111
}
112112

113113
// Track matching strategy for mutual exclusivity validation
114-
if matchAction.isLengthMatch {
114+
if matchAction.target.isLengthMatch {
115115
if hasByteMatch {
116116
errors.append(
117117
.init(
@@ -121,7 +121,7 @@ class ParseEnumCase: SyntaxVisitor {
121121
)
122122
}
123123
hasLengthMatch = true
124-
} else if matchAction.isByteMatch {
124+
} else if matchAction.target.isByteMatch {
125125
if hasLengthMatch {
126126
errors.append(
127127
.init(

0 commit comments

Comments
 (0)