|
| 1 | +/* |
| 2 | + * Copyright © 2025 Dustin Collins (Strega's Gate) |
| 3 | + * All Rights Reserved. |
| 4 | + * |
| 5 | + * http://stregasgate.com |
| 6 | + */ |
| 7 | + |
| 8 | +import SwiftSyntax |
| 9 | +import SwiftSyntaxBuilder |
| 10 | +import SwiftSyntaxMacros |
| 11 | + |
| 12 | +public enum RenderingSystemMacroError: Swift.Error, CustomStringConvertible { |
| 13 | + case notClass |
| 14 | + case invalidPhase |
| 15 | + |
| 16 | + public var description: String { |
| 17 | + switch self { |
| 18 | + case .notClass: |
| 19 | + "@RenderingSystem(phase) can only be attached to a class." |
| 20 | + case .invalidPhase: |
| 21 | + "The provided phase is not a valid RenderingSystem.Phase." |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +public struct ECSRenderingSystemMacro: MemberMacro, ExtensionMacro { |
| 27 | + public static func expansion( |
| 28 | + of node: AttributeSyntax, |
| 29 | + providingMembersOf declaration: some DeclGroupSyntax, |
| 30 | + conformingTo protocols: [TypeSyntax], |
| 31 | + in context: some MacroExpansionContext |
| 32 | + ) throws -> [DeclSyntax] { |
| 33 | + |
| 34 | + guard let classDecl = declaration.as(ClassDeclSyntax.self) else { |
| 35 | + throw RenderingSystemMacroError.notClass |
| 36 | + } |
| 37 | + |
| 38 | + func getRenderingSystemPhase() throws -> TokenSyntax { |
| 39 | + switch node.arguments { |
| 40 | + case .argumentList(let labeledExprListSyntax): |
| 41 | + if let token = labeledExprListSyntax.lastToken(viewMode: .sourceAccurate) { |
| 42 | + return "\(token.formatted())" |
| 43 | + } |
| 44 | + default: |
| 45 | + break |
| 46 | + } |
| 47 | + throw RenderingSystemMacroError.invalidPhase |
| 48 | + } |
| 49 | + |
| 50 | + let argument: String = try getRenderingSystemPhase().formatted().description |
| 51 | + |
| 52 | + var syntax = "override class var phase: RenderingSystem.Phase { .\(argument)}" |
| 53 | + |
| 54 | + if let access = classDecl.modifiers.first(where: { |
| 55 | + let syntax = $0.trimmed.description |
| 56 | + return syntax == "public" || syntax == "open" || syntax == "package" |
| 57 | + }) { |
| 58 | + syntax = access.trimmedDescription + " " + syntax |
| 59 | + } |
| 60 | + |
| 61 | + return [DeclSyntax(stringLiteral: syntax)] |
| 62 | + } |
| 63 | + |
| 64 | + public static func expansion( |
| 65 | + of node: SwiftSyntax.AttributeSyntax, |
| 66 | + attachedTo declaration: some SwiftSyntax.DeclGroupSyntax, |
| 67 | + providingExtensionsOf type: some SwiftSyntax.TypeSyntaxProtocol, |
| 68 | + conformingTo protocols: [SwiftSyntax.TypeSyntax], |
| 69 | + in context: some SwiftSyntaxMacros.MacroExpansionContext |
| 70 | + ) throws -> [SwiftSyntax.ExtensionDeclSyntax] { |
| 71 | + guard protocols.contains(where: {$0 == "GateEngine.RenderingSystem"}) == false else { return [] } |
| 72 | + |
| 73 | + guard let /*classDecl*/_ = declaration.as(ClassDeclSyntax.self) else { |
| 74 | + throw SystemMacroError.notClass |
| 75 | + } |
| 76 | + |
| 77 | +// let className = classDecl.name.trimmed |
| 78 | + |
| 79 | +// var protocols: [ExtensionDeclSyntax] = [try ProtocolDeclSyntax("GateEngine.RenderingSystem")] + protocols |
| 80 | +// return protocols |
| 81 | + return [ |
| 82 | + try ExtensionDeclSyntax(SyntaxNodeString("GateEngine.RenderingSystem")) |
| 83 | + ] |
| 84 | + } |
| 85 | + |
| 86 | +// public static func expansion( |
| 87 | +// of node: SwiftSyntax.AttributeSyntax, |
| 88 | +// providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, |
| 89 | +// in context: some SwiftSyntaxMacros.MacroExpansionContext |
| 90 | +// ) throws -> [SwiftSyntax.DeclSyntax] { |
| 91 | +// guard let classDecl = declaration.as(ClassDeclSyntax.self) else { |
| 92 | +// throw SystemMacroError.notClass |
| 93 | +// } |
| 94 | +// let className = classDecl.name.trimmed |
| 95 | +// return [DeclSyntax( |
| 96 | +//""" |
| 97 | +//@_cdecl(\"eventHandler\") |
| 98 | +//fileprivate func _eventHandler(pointer: UnsafeMutableRawPointer!, event: System.Event, arg: CUnsignedInt) -> CInt { |
| 99 | +// return \(className)._eventHandler(pointer: pointer, event: event, arg: arg) |
| 100 | +//} |
| 101 | +//""" |
| 102 | +// )] |
| 103 | +// } |
| 104 | +} |
0 commit comments