|
| 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 SystemMacroError: Error, CustomStringConvertible { |
| 13 | + case notClass |
| 14 | + |
| 15 | + public var description: String { |
| 16 | + switch self { |
| 17 | + case .notClass: |
| 18 | + "@System(phase) can only be attached to a class." |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +public struct ECSSystemMacro: MemberMacro, ExtensionMacro { |
| 24 | + public static func expansion( |
| 25 | + of node: AttributeSyntax, |
| 26 | + providingMembersOf declaration: some DeclGroupSyntax, |
| 27 | + conformingTo protocols: [TypeSyntax], |
| 28 | + in context: some MacroExpansionContext |
| 29 | + ) throws -> [DeclSyntax] { |
| 30 | + |
| 31 | + guard let classDecl = declaration.as(ClassDeclSyntax.self) else { |
| 32 | + throw SystemMacroError.notClass |
| 33 | + } |
| 34 | + |
| 35 | + var syntax = "override class var phase: System.Phase { \(node.arguments!.formatted()) }" |
| 36 | + |
| 37 | + if let access = classDecl.modifiers.first(where: { |
| 38 | + let syntax = $0.trimmed.description |
| 39 | + return syntax == "public" || syntax == "open" || syntax == "package" |
| 40 | + }) { |
| 41 | + syntax = access.trimmedDescription + " " + syntax |
| 42 | + } |
| 43 | + |
| 44 | + return [DeclSyntax(stringLiteral: syntax)] |
| 45 | + } |
| 46 | + |
| 47 | + public static func expansion( |
| 48 | + of node: SwiftSyntax.AttributeSyntax, |
| 49 | + attachedTo declaration: some SwiftSyntax.DeclGroupSyntax, |
| 50 | + providingExtensionsOf type: some SwiftSyntax.TypeSyntaxProtocol, |
| 51 | + conformingTo protocols: [SwiftSyntax.TypeSyntax], |
| 52 | + in context: some SwiftSyntaxMacros.MacroExpansionContext |
| 53 | + ) throws -> [SwiftSyntax.ExtensionDeclSyntax] { |
| 54 | + guard protocols.contains(where: {$0 == "GateEngine.System"}) == false else { return [] } |
| 55 | + |
| 56 | + guard let classDecl = declaration.as(ClassDeclSyntax.self) else { |
| 57 | + throw SystemMacroError.notClass |
| 58 | + } |
| 59 | + |
| 60 | + let className = classDecl.name.trimmed |
| 61 | + |
| 62 | +// var protocols: [ExtensionDeclSyntax] = [try ProtocolDeclSyntax("GateEngine.System")] + protocols |
| 63 | +// return protocols |
| 64 | + return [ |
| 65 | + try ExtensionDeclSyntax(SyntaxNodeString("GateEngine.System")) |
| 66 | + ] |
| 67 | + } |
| 68 | + |
| 69 | +// public static func expansion( |
| 70 | +// of node: SwiftSyntax.AttributeSyntax, |
| 71 | +// providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, |
| 72 | +// in context: some SwiftSyntaxMacros.MacroExpansionContext |
| 73 | +// ) throws -> [SwiftSyntax.DeclSyntax] { |
| 74 | +// guard let classDecl = declaration.as(ClassDeclSyntax.self) else { |
| 75 | +// throw SystemMacroError.notClass |
| 76 | +// } |
| 77 | +// let className = classDecl.name.trimmed |
| 78 | +// return [DeclSyntax( |
| 79 | +//""" |
| 80 | +//@_cdecl(\"eventHandler\") |
| 81 | +//fileprivate func _eventHandler(pointer: UnsafeMutableRawPointer!, event: System.Event, arg: CUnsignedInt) -> CInt { |
| 82 | +// return \(className)._eventHandler(pointer: pointer, event: event, arg: arg) |
| 83 | +//} |
| 84 | +//""" |
| 85 | +// )] |
| 86 | +// } |
| 87 | +} |
0 commit comments