Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ struct EnumCaseParameterParseInfo {
let parseInfo: ParseMacroInfo
let firstName: TokenSyntax?
let type: TypeSyntax

init(parseInfo: ParseMacroInfo, firstName: TokenSyntax?, type: TypeSyntax) {
self.parseInfo = parseInfo
self.firstName = firstName?.trimmed
self.type = type.trimmed
}
}

enum EnumParseAction {
Expand Down Expand Up @@ -111,6 +117,12 @@ struct EnumCaseParseInfo {
let matchAction: EnumCaseMatchAction
let parseActions: [EnumParseAction]
let caseElementName: TokenSyntax

init(matchAction: EnumCaseMatchAction, parseActions: [EnumParseAction], caseElementName: TokenSyntax) {
self.matchAction = matchAction
self.parseActions = parseActions
self.caseElementName = caseElementName.trimmed
}
}

struct EnumParseInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class ParseStructField<C: MacroExpansionContext>: SyntaxVisitor {
struct VariableInfo {
let type: TypeSyntax
let parseActions: [StructParseAction]

init(type: TypeSyntax, parseActions: [StructParseAction]) {
self.type = type.trimmed
self.parseActions = parseActions
}
}

typealias ParseVariableMapping = OrderedDictionary<TokenSyntax, VariableInfo>
Expand Down Expand Up @@ -102,7 +107,7 @@ class ParseStructField<C: MacroExpansionContext>: SyntaxVisitor {
throw .invalidTypeAnnotation
}

variables[variableName] = .init(type: typeName, parseActions: structFieldVisitor.parseActions)
variables[variableName.trimmed] = .init(type: typeName, parseActions: structFieldVisitor.parseActions)
}

func validate(for node: some SwiftSyntax.SyntaxProtocol) throws(ParseStructMacroError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private class ParseMacroArgVisitor<C: MacroExpansionContext>: SyntaxVisitor {

let selfAccessExpr = ExprSyntax("self\(keyPath.components)")

byteCount = .ofVariable(selfAccessExpr)
byteCount = .ofVariable(selfAccessExpr.trimmed)
} else {
byteCount = .unspecified
}
Expand Down Expand Up @@ -119,7 +119,7 @@ struct ParseMacroInfo {

init(byteCount: Count, endianness: ExprSyntax? = nil, source: Syntax) {
self.byteCount = byteCount
self.endianness = endianness
self.endianness = endianness?.trimmed
self.source = source
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/BinaryParseKitMacros/Macros/Supports/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func generateParseBlock(
case let (endianness?, size?):
#"""
// Parse `\#(variableName)` of type \#(variableType) with endianness and byte count
\#(raw: Constants.UtilityFunctions.assertEndianSizedParsable)(\#(variableType).self)
\#(raw: Constants.UtilityFunctions.assertEndianSizedParsable)((\#(variableType)).self)
"""#

let assigned: ExprSyntax = #"try \#(variableType)(parsing: &span, endianness: \#(endianness), byteCount: \#(size))"#
Expand All @@ -43,7 +43,7 @@ func generateParseBlock(
case (let endianness?, nil):
#"""
// Parse `\#(variableName)` of type \#(variableType) with endianness
\#(raw: Constants.UtilityFunctions.assertEndianParsable)(\#(variableType).self)
\#(raw: Constants.UtilityFunctions.assertEndianParsable)((\#(variableType)).self)
"""#

let assigned: ExprSyntax = #"try \#(variableType)(parsing: &span, endianness: \#(endianness))"#
Expand All @@ -56,7 +56,7 @@ func generateParseBlock(
case (nil, let size?):
#"""
// Parse `\#(variableName)` of type \#(variableType) with byte count
\#(raw: Constants.UtilityFunctions.assertSizedParsable)(\#(variableType).self)
\#(raw: Constants.UtilityFunctions.assertSizedParsable)((\#(variableType)).self)
"""#

let assigned: ExprSyntax = #"try \#(variableType)(parsing: &span, byteCount: \#(size))"#
Expand All @@ -69,7 +69,7 @@ func generateParseBlock(
case (nil, nil):
#"""
// Parse `\#(variableName)` of type \#(variableType)
\#(raw: Constants.UtilityFunctions.assertParsable)(\#(variableType).self)
\#(raw: Constants.UtilityFunctions.assertParsable)((\#(variableType)).self)
"""#

let assigned: ExprSyntax = #"try \#(variableType)(parsing: &span)"#
Expand Down
88 changes: 83 additions & 5 deletions Tests/BinaryParseKitMacroTests/BinaryParseKitEnumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,29 +152,29 @@ extension BinaryParseKitMacroTests {
init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
if BinaryParseKit.__match([0x08], in: &span) {
// Parse `__macro_local_12TestEnum_a_0fMu_` of type SomeType with byte count
BinaryParseKit.__assertSizedParsable(SomeType.self)
BinaryParseKit.__assertSizedParsable((SomeType).self)
let __macro_local_12TestEnum_a_0fMu_ = try SomeType(parsing: &span, byteCount: 1)
// construct `a` with above associated values
self = .a(__macro_local_12TestEnum_a_0fMu_)
return
}
if BinaryParseKit.__match([0x01, 0x02], in: &span) {
// Parse `__macro_local_12TestEnum_b_0fMu_` of type Int
BinaryParseKit.__assertParsable(Int.self)
BinaryParseKit.__assertParsable((Int).self)
let __macro_local_12TestEnum_b_0fMu_ = try Int(parsing: &span)
// Parse `value` of type SomeType with endianness
BinaryParseKit.__assertEndianParsable(SomeType.self)
BinaryParseKit.__assertEndianParsable((SomeType).self)
let value = try SomeType(parsing: &span, endianness: .big)
// construct `b` with above associated values
self = .b(__macro_local_12TestEnum_b_0fMu_, value: value)
return
}
if BinaryParseKit.__match([0x09], in: &span) {
// Parse `code` of type UInt8 with endianness
BinaryParseKit.__assertEndianParsable(UInt8.self)
BinaryParseKit.__assertEndianParsable((UInt8).self)
let code = try UInt8(parsing: &span, endianness: .little)
// Parse `value` of type SomeType with endianness
BinaryParseKit.__assertEndianParsable(SomeType.self)
BinaryParseKit.__assertEndianParsable((SomeType).self)
let value = try SomeType(parsing: &span, endianness: .little)
// construct `c` with above associated values
self = .c(code: code, value: value)
Expand Down Expand Up @@ -592,6 +592,84 @@ extension BinaryParseKitMacroTests {
],
)
}

@Test
func `no comments in code generation`() {
assertMacroExpansion("""
@ParseEnum
enum TestEnum {
@match
@parse
case a(
value: Int // some value
)

@match
@parse
case b(
value: // some value
Int // some value
)

@match
@parse
@parse
case c(
Int, // some value
value: // some value
Int // some value
)
}
""", expandedSource: #"""
enum TestEnum {
case a(
value: Int // some value
)
case b(
value: // some value
Int // some value
)
case c(
Int, // some value
value: // some value
Int // some value
)
}

extension TestEnum: BinaryParseKit.Parsable {
init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
if BinaryParseKit.__match((TestEnum.a as any MatchableRawRepresentable) .bytesToMatch(), in: &span) {
// Parse `value` of type Int
BinaryParseKit.__assertParsable((Int).self)
let value = try Int(parsing: &span)
// construct `a` with above associated values
self = .a(value: value)
return
}
if BinaryParseKit.__match((TestEnum.b as any MatchableRawRepresentable) .bytesToMatch(), in: &span) {
// Parse `value` of type Int
BinaryParseKit.__assertParsable((Int).self)
let value = try Int(parsing: &span)
// construct `b` with above associated values
self = .b(value: value)
return
}
if BinaryParseKit.__match((TestEnum.c as any MatchableRawRepresentable) .bytesToMatch(), in: &span) {
// Parse `__macro_local_12TestEnum_c_0fMu_` of type Int
BinaryParseKit.__assertParsable((Int).self)
let __macro_local_12TestEnum_c_0fMu_ = try Int(parsing: &span)
// Parse `value` of type Int
BinaryParseKit.__assertParsable((Int).self)
let value = try Int(parsing: &span)
// construct `c` with above associated values
self = .c(__macro_local_12TestEnum_c_0fMu_, value: value)
return
}
throw BinaryParseKit.BinaryParserKitError.failedToParse("Failed to find a match for TestEnum, at \(span.startPosition)")
}
}
"""#)
}
}
}

Expand Down
57 changes: 50 additions & 7 deletions Tests/BinaryParseKitMacroTests/BinaryParseKitStructTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,33 @@ extension BinaryParseKitMacroTests {
extension Header: BinaryParseKit.Parsable {
public init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
// Parse `a` of type Int with endianness and byte count
BinaryParseKit.__assertEndianSizedParsable(Int.self)
BinaryParseKit.__assertEndianSizedParsable((Int).self)
self.a = try Int(parsing: &span, endianness: .big, byteCount: 1)
// Parse `b` of type Int32 with endianness
BinaryParseKit.__assertEndianParsable(Int32.self)
BinaryParseKit.__assertEndianParsable((Int32).self)
self.b = try Int32(parsing: &span, endianness: .little)
// Skip 2 because of "not needed", before parsing `d`
try span.seek(toRelativeOffset: 2)
// Skip 4 because of "also not needed", before parsing `d`
try span.seek(toRelativeOffset: 4)
// Parse `d` of type Float16 with endianness
BinaryParseKit.__assertEndianParsable(Float16.self)
BinaryParseKit.__assertEndianParsable((Float16).self)
self.d = try Float16(parsing: &span, endianness: .big)
// Parse `c` of type CustomValue
BinaryParseKit.__assertParsable(CustomValue.self)
BinaryParseKit.__assertParsable((CustomValue).self)
self.c = try CustomValue(parsing: &span)
// Skip 6 because of "again, not needed", before parsing `e`
try span.seek(toRelativeOffset: 6)
// Parse `e` of type CustomValue
BinaryParseKit.__assertParsable(CustomValue.self)
BinaryParseKit.__assertParsable((CustomValue).self)
self.e = try CustomValue(parsing: &span)
// Parse `g` of type CustomValue with byte count
BinaryParseKit.__assertSizedParsable(CustomValue.self)
BinaryParseKit.__assertSizedParsable((CustomValue).self)
self.g = try CustomValue(parsing: &span, byteCount: Int(self.b))
// Skip 7 because of "last one skip", before parsing `f`
try span.seek(toRelativeOffset: 7)
// Parse `f` of type CustomValue with endianness and byte count
BinaryParseKit.__assertEndianSizedParsable(CustomValue.self)
BinaryParseKit.__assertEndianSizedParsable((CustomValue).self)
self.f = try CustomValue(parsing: &span, endianness: .little, byteCount: span.endPosition - span.startPosition)
}
}
Expand Down Expand Up @@ -611,6 +611,49 @@ extension BinaryParseKitMacroTests {
],
)
}

@Test
func `no comments and spaces in code generation`() {
assertMacroExpansion("""
@ParseStruct
struct Header {
@parse
var a: Int // some comments

@parse
var b: // some comments
Int // some comments

@parse
var // some comments
c: // some comments
Int // some comments
}
""", expandedSource: """
struct Header {
var a: Int // some comments
var b: // some comments
Int // some comments
var // some comments
c: // some comments
Int // some comments
}

extension Header: BinaryParseKit.Parsable {
init(parsing span: inout BinaryParsing.ParserSpan) throws(BinaryParsing.ThrownParsingError) {
// Parse `a` of type Int
BinaryParseKit.__assertParsable((Int).self)
self.a = try Int(parsing: &span)
// Parse `b` of type Int
BinaryParseKit.__assertParsable((Int).self)
self.b = try Int(parsing: &span)
// Parse `c` of type Int
BinaryParseKit.__assertParsable((Int).self)
self.c = try Int(parsing: &span)
}
}
""")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/BinaryParseKitMacroTests/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Testing
#if canImport(BinaryParseKitMacros)
import BinaryParseKitMacros

private nonisolated(unsafe) let testMacros: [String: Macro.Type] = [
private let testMacros: [String: Macro.Type] = [
"ParseStruct": ConstructStructParseMacro.self,
"parse": EmptyPeerMacro.self,
"skip": EmptyPeerMacro.self,
Expand Down