@@ -18,62 +18,29 @@ import struct Foundation.Data
1818import struct NIOCore. ByteBuffer
1919#endif
2020
21- struct HTMLElement : ExpressionMacro {
21+ enum HTMLElement : ExpressionMacro {
2222 static func expansion( of node: some FreestandingMacroExpansionSyntax , in context: some MacroExpansionContext ) throws -> ExprSyntax {
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- }
6223 let string : String = parse_macro ( context: context, expression: node. as ( MacroExpansionExprSyntax . self) !)
6324 // TODO: check for interpolation
6425 func bytes< T: FixedWidthInteger > ( _ bytes: [ T ] ) -> String {
6526 return " [ " + bytes. map ( { " \( $0) " } ) . joined ( separator: " , " ) + " ] "
6627 }
67- switch representation {
68- case . uint8Array: return " \( raw: bytes ( [ UInt8] ( string. utf8) ) ) "
69- case . uint16Array: return " \( raw: bytes ( [ UInt16] ( string. utf16) ) ) "
28+ switch HTMLElementType ( rawValue: node. macroName. text) {
29+ case . htmlUTF8Bytes:
30+ return " \( raw: bytes ( [ UInt8] ( string. utf8) ) ) "
31+ case . htmlUTF16Bytes:
32+ return " \( raw: bytes ( [ UInt16] ( string. utf16) ) ) "
33+ case . htmlUTF8CString:
34+ return " \( raw: string. utf8CString) "
7035
7136 #if canImport(Foundation)
72- case . data: return " Data( \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
37+ case . htmlData:
38+ return " Data( \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
7339 #endif
7440
7541 #if canImport(NIOCore)
76- case . byteBuffer: return " ByteBuffer(string: \" \( raw: string) \" ) "
42+ case . htmlByteBuffer:
43+ return " ByteBuffer(bytes: \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
7744 #endif
7845
7946 default : return " \" \( raw: string) \" "
@@ -99,12 +66,12 @@ private extension HTMLElement {
9966 children = childs. dropFirst ( ) // tag
10067 children. removeFirst ( ) // isVoid
10168 } else {
102- tag = elementType. rawValue
69+ tag = elementType. rawValue. starts ( with : " html " ) ? " html " : elementType . rawValue
10370 isVoid = elementType. isVoid
10471 children = childs. prefix ( childs. count)
10572 }
10673 let data : ElementData = parse_arguments ( context: context, elementType: elementType, children: children)
107- var string : String = ( elementType == . html ? " <!DOCTYPE html> " : " " ) + " < " + tag + data. attributes + " > " + data. innerHTML
74+ var string : String = ( tag == " html " ? " <!DOCTYPE html> " : " " ) + " < " + tag + data. attributes + " > " + data. innerHTML
10875 if !isVoid {
10976 string += " </ " + tag + " > "
11077 }
@@ -115,7 +82,7 @@ private extension HTMLElement {
11582 for element in children {
11683 if let child: LabeledExprSyntax = element. labeled {
11784 if var key: String = child. label? . text {
118- if key == " representation " { // we don't care
85+ if key == " dataType " { // HTMLDataRepresentation; we don't care
11986 } else if key == " attributes " {
12087 attributes. append ( contentsOf: parse_global_attributes ( context: context, elementType: elementType, array: child. expression. array!) )
12188 } else {
@@ -387,8 +354,17 @@ enum LiteralReturnType {
387354// MARK: HTMLElementType
388355enum HTMLElementType : String , CaseIterable {
389356 case escapeHTML
390- case html
391357 case custom
358+
359+ case html, htmlUTF8Bytes, htmlUTF16Bytes, htmlUTF8CString
360+
361+ #if canImport(Foundation)
362+ case htmlData
363+ #endif
364+
365+ #if canImport(NIOCore)
366+ case htmlByteBuffer
367+ #endif
392368
393369 case a
394370 case abbr
0 commit comments