-
Notifications
You must be signed in to change notification settings - Fork 0
Add Accessor control #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0ade190
Add ACL
FlickerSoul 1b98661
Add capability to specify accessor
FlickerSoul 6bffa64
Remove unused imports
FlickerSoul 92ba2b6
Add tests
FlickerSoul 9450e3d
Refactor
FlickerSoul 143f045
Fix review
FlickerSoul 6c16eab
Assert only access modifier count
FlickerSoul 3988e2e
Fix typo
FlickerSoul e11cf20
Multi target doc build
FlickerSoul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // | ||
| // ExtensionAccessor.swift | ||
| // BinaryParseKit | ||
| // | ||
| // Created by Larry Zeng on 11/26/25. | ||
| // | ||
|
|
||
| public enum ExtensionAccessor: ExpressibleByUnicodeScalarLiteral, Sendable, Codable { | ||
| public typealias UnicodeScalarLiteralType = String | ||
|
|
||
| case `public` | ||
| case package | ||
| case `internal` | ||
| case `fileprivate` | ||
| case `private` | ||
| case follow | ||
| case unknown(String) | ||
|
|
||
| public static var allowedCases: [ExtensionAccessor] { | ||
| [.public, .package, .internal, .fileprivate, .private, .follow] | ||
| } | ||
|
|
||
| public var description: String { | ||
| switch self { | ||
| case .public: "public" | ||
| case .package: "package" | ||
| case .internal: "internal" | ||
| case .fileprivate: "fileprivate" | ||
| case .private: "private" | ||
| case .follow: "follow" | ||
| case let .unknown(value): "unknown(\(value))" | ||
| } | ||
| } | ||
|
|
||
| public init(unicodeScalarLiteral value: String) { | ||
| self = ExtensionAccessor | ||
| .allowedCases | ||
| .first { access in | ||
| access.description == value | ||
| } ?? .unknown(value) | ||
| } | ||
| } | ||
FlickerSoul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
23 changes: 23 additions & 0 deletions
23
Sources/BinaryParseKitMacros/Macros/Extensions/ExtensionAccessor+.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // | ||
| // ExtensionAccessor+.swift | ||
| // BinaryParseKit | ||
| // | ||
| // Created by Larry Zeng on 11/26/25. | ||
| // | ||
|
|
||
| import BinaryParseKitCommons | ||
| import SwiftSyntax | ||
|
|
||
| extension ExtensionAccessor { | ||
| func getAccessorToken(defaultAccessor: TokenKind) -> TokenKind? { | ||
| switch self { | ||
| case .public: .keyword(.public) | ||
| case .package: .keyword(.package) | ||
| case .internal: .keyword(.internal) | ||
| case .fileprivate: .keyword(.fileprivate) | ||
| case .private: .keyword(.private) | ||
| case .follow: defaultAccessor | ||
| case .unknown: nil | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
Sources/BinaryParseKitMacros/Macros/Supports/MacroAccessorVisitor.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // | ||
| // MacroAccessorVisitor.swift | ||
| // BinaryParseKit | ||
| // | ||
| // Created by Larry Zeng on 11/26/25. | ||
| // | ||
|
|
||
| import BinaryParseKitCommons | ||
| import SwiftDiagnostics | ||
| import SwiftSyntax | ||
| import SwiftSyntaxMacros | ||
|
|
||
| enum MacroAccessorError: DiagnosticMessage, Error { | ||
| case invalidAccessor(String) | ||
| case moreThanOneModifier(modifiers: String) | ||
| case unknownAccessor | ||
|
|
||
| var message: String { | ||
| switch self { | ||
| case let .invalidAccessor(accessor): | ||
| #"Invalid ACL value: \#(accessor); Please use one of \#(ExtensionAccessor.allowedCases.map(\.description).joined(separator: ", ")); use it in string literal "public" or enum member access .public."# | ||
| case let .moreThanOneModifier(modifiers: modifiers): | ||
| "More than one modifier found: \(modifiers). Only one modifier is allowed." | ||
| case .unknownAccessor: | ||
| "You have used unknown accessor in `@ParseStruct` or `@ParseEnum`." | ||
| } | ||
| } | ||
|
|
||
| var diagnosticID: SwiftDiagnostics.MessageID { | ||
| .init( | ||
| domain: "BinaryParseKit.MacroACLError", | ||
| id: "\(self)", | ||
| ) | ||
| } | ||
|
|
||
| var severity: SwiftDiagnostics.DiagnosticSeverity { | ||
| switch self { | ||
| case .invalidAccessor, .moreThanOneModifier, .unknownAccessor: .error | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class MacroAccessorVisitor: SyntaxVisitor { | ||
| private static let defaultAccessor = ExtensionAccessor.follow | ||
|
|
||
| private(set) var printingAccessor: ExtensionAccessor = MacroAccessorVisitor.defaultAccessor | ||
| private(set) var parsingAccessor: ExtensionAccessor = MacroAccessorVisitor.defaultAccessor | ||
|
|
||
| private let context: any MacroExpansionContext | ||
|
|
||
| init(context: any MacroExpansionContext) { | ||
| self.context = context | ||
| super.init(viewMode: .sourceAccurate) | ||
| } | ||
|
|
||
| override func visit(_ node: LabeledExprSyntax) -> SyntaxVisitorContinueKind { | ||
| let labelText = node.label?.text | ||
| switch labelText { | ||
| case "printingAccessor": | ||
| setACL(to: \.printingAccessor, with: node) | ||
| case "parsingAccessor": | ||
| setACL(to: \.parsingAccessor, with: node) | ||
| default: | ||
| break | ||
| } | ||
| return .skipChildren | ||
| } | ||
|
|
||
| private func setACL( | ||
| to keypath: ReferenceWritableKeyPath<MacroAccessorVisitor, ExtensionAccessor>, | ||
| with node: LabeledExprSyntax, | ||
| ) { | ||
| let acl = parseACL(from: node) | ||
| self[keyPath: keypath] = acl | ||
| if case let .unknown(value) = acl { | ||
| context.diagnose( | ||
| .init( | ||
| node: node, | ||
| message: MacroAccessorError.invalidAccessor(value), | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private func parseACL(from node: LabeledExprSyntax) -> ExtensionAccessor { | ||
| let expression = node.expression | ||
|
|
||
| // FIXME: use macro toolkit | ||
FlickerSoul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if let stringLiteralSyntax = expression.as(StringLiteralExprSyntax.self), | ||
| let stringLiteral = stringLiteralSyntax.segments.first?.as(StringSegmentSyntax.self)?.content.text { | ||
| return .init(unicodeScalarLiteral: stringLiteral) | ||
| } else if let memberAccessSyntax = expression.as(MemberAccessExprSyntax.self) { | ||
| let element = memberAccessSyntax.declName.baseName.text | ||
| return .init(unicodeScalarLiteral: element) | ||
| } | ||
|
|
||
| return .unknown(expression.description) | ||
| } | ||
| } | ||
|
|
||
| struct AccessorInfo { | ||
| let parsingAccessor: DeclModifierSyntax | ||
| let printingAccessor: DeclModifierSyntax | ||
| } | ||
|
|
||
| func extractAccessor( | ||
| from attributeNode: AttributeSyntax, | ||
| attachedTo declaration: some DeclGroupSyntax, | ||
| in context: some MacroExpansionContext, | ||
| ) throws(MacroAccessorError) -> AccessorInfo { | ||
| let modifiers = declaration.modifiers | ||
| guard modifiers.count < 2 else { | ||
| throw MacroAccessorError.moreThanOneModifier(modifiers: modifiers.map(\.name.text).joined(separator: ", ")) | ||
| } | ||
| let modifierToken = modifiers.first?.name.tokenKind ?? .keyword(.internal) | ||
FlickerSoul marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let accessorVisitor = MacroAccessorVisitor(context: context) | ||
| accessorVisitor.walk(attributeNode) | ||
|
|
||
| guard let parsingAccessor = accessorVisitor.parsingAccessor.getAccessorToken(defaultAccessor: modifierToken), | ||
| let printingAccessor = accessorVisitor.printingAccessor.getAccessorToken(defaultAccessor: modifierToken) | ||
| else { | ||
| throw MacroAccessorError.unknownAccessor | ||
| } | ||
|
|
||
| return .init( | ||
| parsingAccessor: .init( | ||
| name: TokenSyntax(parsingAccessor, presence: .present), | ||
| ), | ||
| printingAccessor: .init( | ||
| name: TokenSyntax(printingAccessor, presence: .present), | ||
| ), | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.