@@ -10,9 +10,81 @@ import SwiftSyntaxMacros
1010import SwiftDiagnostics
1111import HTMLKitUtilities
1212
13+ #if canImport(Foundation)
14+ import struct Foundation. Data
15+ #endif
16+
17+ #if canImport(NIOCore)
18+ import struct NIOCore. ByteBuffer
19+ #endif
20+
1321struct HTMLElement : ExpressionMacro {
1422 static func expansion( of node: some FreestandingMacroExpansionSyntax , in context: some MacroExpansionContext ) throws -> ExprSyntax {
15- return " \" \( raw: parse_macro ( context: context, expression: node. as ( MacroExpansionExprSyntax . self) !) ) \" "
23+ var representation : HTMLDataRepresentation
24+ if let declared: String = node. arguments. children ( viewMode: . all) . first ( where: { $0. labeled? . label? . text == " representation " } ) ? . labeled? . expression. as ( MemberAccessExprSyntax . self) ? . declName. baseName. text {
25+ //context.diagnose(Diagnostic(node: node, message: DiagnosticMsg(id: "test", message: context.lexicalContext.last!.debugDescription)))
26+ representation = HTMLDataRepresentation ( rawValue: declared) ?? . string
27+ } else {
28+ representation = . string
29+ //context.diagnose(Diagnostic(node: node, message: DiagnosticMsg(id: "test", message: context.lexicalContext.last!.debugDescription)))
30+ if let returnClause: ReturnClauseSyntax = context. lexicalContext. first? . as ( FunctionDeclSyntax . self) ? . signature. returnClause {
31+ if let array_type: String = returnClause. type. as ( ArrayTypeSyntax . self) ? . element. as ( IdentifierTypeSyntax . self) ? . name. text {
32+ switch array_type {
33+ case " UInt8 " :
34+ representation = . uint8Array
35+ break
36+ case " UInt16 " :
37+ representation = . uint16Array
38+ break
39+ default :
40+ break
41+ }
42+ } else if let id: String = returnClause. type. as ( IdentifierTypeSyntax . self) ? . name. text {
43+ switch id {
44+ case " Data " :
45+ #if canImport(Foundation)
46+ representation = . data
47+ #endif
48+ break
49+ case " ByteBuffer " :
50+ #if canImport(NIOCore)
51+ representation = . byteBuffer
52+ #endif
53+ break
54+ default :
55+ break
56+ }
57+ }
58+ } else {
59+ //context.diagnose(Diagnostic(node: node, message: DiagnosticMsg(id: "test", message: context.lexicalContext.first!.debugDescription)))
60+ }
61+ }
62+ let string : String = parse_macro ( context: context, expression: node. as ( MacroExpansionExprSyntax . self) !)
63+ // TODO: check for interpolation
64+ func bytes< T: FixedWidthInteger > ( _ bytes: [ T ] ) -> String {
65+ return " [ " + bytes. map ( { " \( $0) " } ) . joined ( separator: " , " ) + " ] "
66+ }
67+ switch representation {
68+ case . uint8Array: return " \( raw: bytes ( [ UInt8] ( string. utf8) ) ) "
69+ case . uint16Array: return " \( raw: bytes ( [ UInt16] ( string. utf16) ) ) "
70+
71+ #if canImport(Foundation)
72+ case . data: return " Data( \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
73+ #endif
74+
75+ #if canImport(NIOCore)
76+ case . byteBuffer: return " ByteBuffer(string: \( raw: string) ) "
77+ #endif
78+
79+ default : return " \" \( raw: string) \" "
80+ }
81+ }
82+ }
83+
84+ extension HTMLDataRepresentation {
85+ enum Result {
86+ case string( String )
87+ case uint8Array( [ UInt8 ] )
1688 }
1789}
1890
@@ -50,7 +122,8 @@ private extension HTMLElement {
50122 for element in children {
51123 if let child: LabeledExprSyntax = element. labeled {
52124 if var key: String = child. label? . text {
53- if key == " attributes " {
125+ if key == " representation " { // we don't care
126+ } else if key == " attributes " {
54127 attributes. append ( contentsOf: parse_global_attributes ( context: context, elementType: elementType, array: child. expression. array!) )
55128 } else {
56129 if key == " acceptCharset " {
0 commit comments