77
88#if canImport(Foundation)
99import Foundation
10+ import HTMLKitUtilities
1011import SwiftDiagnostics
1112import SwiftSyntaxMacros
1213import SwiftParser
@@ -15,23 +16,23 @@ import SwiftSyntax
1516enum InterpolationLookup {
1617 private static var cached : [ String : CodeBlockItemListSyntax ] = [ : ]
1718
18- static func find( context: some MacroExpansionContext , _ node: some ExprSyntaxProtocol , files: Set < String > ) -> String ? {
19- guard !files. isEmpty, let item: Item = item ( node) else { return nil }
19+ static func find( context: HTMLExpansionContext , _ node: some ExprSyntaxProtocol , files: Set < String > ) -> String ? {
20+ guard !files. isEmpty, let item: Item = item ( context : context , node) else { return nil }
2021 for file in files {
2122 if cached [ file] == nil {
2223 if let string: String = try ? String . init ( contentsOfFile: file, encoding: . utf8) {
2324 let parsed : CodeBlockItemListSyntax = Parser . parse ( source: string) . statements
2425 cached [ file] = parsed
2526 } else {
26- context. diagnose ( Diagnostic ( node: node, message: DiagnosticMsg ( id: " fileNotFound " , message: " Could not find file ( \( file) ) on disk, or was denied disk access (file access is always denied on macOS due to the macro being in a sandbox). " , severity: . warning) ) )
27+ context. context . diagnose ( Diagnostic ( node: node, message: DiagnosticMsg ( id: " fileNotFound " , message: " Could not find file ( \( file) ) on disk, or was denied disk access (file access is always denied on macOS due to the macro being in a sandbox). " , severity: . warning) ) )
2728 }
2829 }
2930 }
3031 //print("InterpolationLookup;find;item=\(item)")
3132 switch item {
3233 case . literal( let tokens) :
3334 for (_, statements) in cached {
34- if let flattened: String = flatten ( tokens, statements: statements) {
35+ if let flattened: String = flatten ( context : context , tokens : tokens, statements: statements) {
3536 return flattened
3637 }
3738 }
@@ -42,15 +43,15 @@ enum InterpolationLookup {
4243 }
4344 }
4445
45- private static func item( _ node: some ExprSyntaxProtocol ) -> Item ? {
46+ private static func item( context : HTMLExpansionContext , _ node: some ExprSyntaxProtocol ) -> Item ? {
4647 if let function: FunctionCallExprSyntax = node. functionCall {
4748 var array : [ String ] = [ ]
4849 if let member: MemberAccessExprSyntax = function. calledExpression. memberAccess {
4950 array. append ( contentsOf: test ( member) )
5051 }
5152 var parameters : [ String ] = [ ]
5253 for argument in function. arguments {
53- if let string: String = argument. expression. stringLiteral? . string {
54+ if let string: String = argument. expression. stringLiteral? . string ( encoding : context . encoding ) {
5455 parameters. append ( string)
5556 }
5657 }
@@ -80,7 +81,7 @@ enum InterpolationLookup {
8081}
8182// MARK: Flatten
8283private extension InterpolationLookup {
83- static func flatten( _ tokens: [ String ] , statements: CodeBlockItemListSyntax ) -> String ? {
84+ static func flatten( context : HTMLExpansionContext , tokens: [ String ] , statements: CodeBlockItemListSyntax ) -> String ? {
8485 for statement in statements {
8586 var index : Int = 0
8687 let item = statement. item
@@ -90,8 +91,8 @@ private extension InterpolationLookup {
9091 }
9192 for member in ext. memberBlock. members {
9293 if let string: String = parse_function ( syntax: member. decl, tokens: tokens, index: index)
93- ?? parse_enumeration ( syntax: member. decl, tokens: tokens, index: index)
94- ?? parse_variable ( syntax: member. decl, tokens: tokens, index: index) {
94+ ?? parse_enumeration ( context : context , syntax: member. decl, tokens: tokens, index: index)
95+ ?? parse_variable ( context : context , syntax: member. decl, tokens: tokens, index: index) {
9596 return string
9697 }
9798 }
@@ -104,9 +105,9 @@ private extension InterpolationLookup {
104105 index -= 1
105106 }
106107 }
107- } else if let enumeration: String = parse_enumeration ( syntax: item, tokens: tokens, index: index) {
108+ } else if let enumeration: String = parse_enumeration ( context : context , syntax: item, tokens: tokens, index: index) {
108109 return enumeration
109- } else if let variable: String = parse_variable ( syntax: item, tokens: tokens, index: index) {
110+ } else if let variable: String = parse_variable ( context : context , syntax: item, tokens: tokens, index: index) {
110111 return variable
111112 }
112113 }
@@ -118,7 +119,7 @@ private extension InterpolationLookup {
118119 return nil
119120 }
120121 // MARK: Parse enumeration
121- static func parse_enumeration( syntax: some SyntaxProtocol , tokens: [ String ] , index: Int ) -> String ? {
122+ static func parse_enumeration( context : HTMLExpansionContext , syntax: some SyntaxProtocol , tokens: [ String ] , index: Int ) -> String ? {
122123 let allowed_inheritances : Set < String ? > = [ " String " , " Int " , " Double " , " Float " ]
123124 guard let enumeration: EnumDeclSyntax = syntax. enumeration,
124125 enumeration. name. text == tokens [ index]
@@ -138,7 +139,7 @@ private extension InterpolationLookup {
138139 return case_name
139140 }
140141 switch value_type {
141- case " String " : return enum_case. rawValue? . value. stringLiteral!. string ?? case_name
142+ case " String " : return enum_case. rawValue? . value. stringLiteral!. string ( encoding : context . encoding ) ?? case_name
142143 case " Int " : return enum_case. rawValue? . value. integerLiteral!. literal. text ?? case_name
143144 case " Double " , " Float " : return enum_case. rawValue? . value. floatLiteral!. literal. text ?? case_name
144145 default :
@@ -152,11 +153,11 @@ private extension InterpolationLookup {
152153 return nil
153154 }
154155 // MARK: Parse variable
155- static func parse_variable( syntax: some SyntaxProtocol , tokens: [ String ] , index: Int ) -> String ? {
156+ static func parse_variable( context : HTMLExpansionContext , syntax: some SyntaxProtocol , tokens: [ String ] , index: Int ) -> String ? {
156157 guard let variable: VariableDeclSyntax = syntax. variableDecl else { return nil }
157158 for binding in variable. bindings {
158159 if binding. pattern. as ( IdentifierPatternSyntax . self) ? . identifier. text == tokens [ index] , let initializer: InitializerClauseSyntax = binding. initializer {
159- return initializer. value. stringLiteral? . string
160+ return initializer. value. stringLiteral? . string ( encoding : context . encoding )
160161 ?? initializer. value. integerLiteral? . literal. text
161162 ?? initializer. value. floatLiteral? . literal. text
162163 }
0 commit comments