@@ -18,62 +18,29 @@ import struct Foundation.Data
18
18
import struct NIOCore. ByteBuffer
19
19
#endif
20
20
21
- struct HTMLElement : ExpressionMacro {
21
+ enum HTMLElement : ExpressionMacro {
22
22
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
- }
62
23
let string : String = parse_macro ( context: context, expression: node. as ( MacroExpansionExprSyntax . self) !)
63
24
// TODO: check for interpolation
64
25
func bytes< T: FixedWidthInteger > ( _ bytes: [ T ] ) -> String {
65
26
return " [ " + bytes. map ( { " \( $0) " } ) . joined ( separator: " , " ) + " ] "
66
27
}
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) "
70
35
71
36
#if canImport(Foundation)
72
- case . data: return " Data( \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
37
+ case . htmlData:
38
+ return " Data( \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
73
39
#endif
74
40
75
41
#if canImport(NIOCore)
76
- case . byteBuffer: return " ByteBuffer(string: \" \( raw: string) \" ) "
42
+ case . htmlByteBuffer:
43
+ return " ByteBuffer(bytes: \( raw: bytes ( [ UInt8] ( string. utf8) ) ) ) "
77
44
#endif
78
45
79
46
default : return " \" \( raw: string) \" "
@@ -99,12 +66,12 @@ private extension HTMLElement {
99
66
children = childs. dropFirst ( ) // tag
100
67
children. removeFirst ( ) // isVoid
101
68
} else {
102
- tag = elementType. rawValue
69
+ tag = elementType. rawValue. starts ( with : " html " ) ? " html " : elementType . rawValue
103
70
isVoid = elementType. isVoid
104
71
children = childs. prefix ( childs. count)
105
72
}
106
73
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
108
75
if !isVoid {
109
76
string += " </ " + tag + " > "
110
77
}
@@ -115,7 +82,7 @@ private extension HTMLElement {
115
82
for element in children {
116
83
if let child: LabeledExprSyntax = element. labeled {
117
84
if var key: String = child. label? . text {
118
- if key == " representation " { // we don't care
85
+ if key == " dataType " { // HTMLDataRepresentation; we don't care
119
86
} else if key == " attributes " {
120
87
attributes. append ( contentsOf: parse_global_attributes ( context: context, elementType: elementType, array: child. expression. array!) )
121
88
} else {
@@ -387,8 +354,17 @@ enum LiteralReturnType {
387
354
// MARK: HTMLElementType
388
355
enum HTMLElementType : String , CaseIterable {
389
356
case escapeHTML
390
- case html
391
357
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
392
368
393
369
case a
394
370
case abbr
0 commit comments