@@ -18,7 +18,8 @@ import {
18
18
printErr ,
19
19
readFile ,
20
20
runInMacroContext ,
21
- setCurrentFile ,
21
+ pushCurrentFile ,
22
+ popCurrentFile ,
22
23
warn ,
23
24
srcDir ,
24
25
} from './utility.mjs' ;
@@ -36,10 +37,15 @@ export function processMacros(text, filename) {
36
37
// The `?` here in makes the regex non-greedy so it matches with the closest
37
38
// set of closing braces.
38
39
// `[\s\S]` works like `.` but include newline.
39
- return text . replace ( / { { { ( [ \s \S ] + ?) } } } / g, ( _ , str ) => {
40
- const ret = runInMacroContext ( str , { filename : filename } ) ;
41
- return ret !== null ? ret . toString ( ) : '' ;
42
- } ) ;
40
+ pushCurrentFile ( filename ) ;
41
+ try {
42
+ return text . replace ( / { { { ( [ \s \S ] + ?) } } } / g, ( _ , str ) => {
43
+ const ret = runInMacroContext ( str , { filename : filename } ) ;
44
+ return ret !== null ? ret . toString ( ) : '' ;
45
+ } ) ;
46
+ } finally {
47
+ popCurrentFile ( ) ;
48
+ }
43
49
}
44
50
45
51
function findIncludeFile ( filename , currentDir ) {
@@ -86,7 +92,6 @@ export function preprocess(filename) {
86
92
const showStack = [ ] ;
87
93
const showCurrentLine = ( ) => showStack . every ( ( x ) => x == SHOW ) ;
88
94
89
- const oldFilename = setCurrentFile ( filename ) ;
90
95
const fileExt = filename . split ( '.' ) . pop ( ) . toLowerCase ( ) ;
91
96
const isHtml = fileExt === 'html' || fileExt === 'htm' ? true : false ;
92
97
let inStyle = false ;
@@ -99,6 +104,7 @@ export function preprocess(filename) {
99
104
let ret = '' ;
100
105
let emptyLine = false ;
101
106
107
+ pushCurrentFile ( filename ) ;
102
108
try {
103
109
for ( let [ i , line ] of lines . entries ( ) ) {
104
110
if ( isHtml ) {
@@ -209,7 +215,7 @@ no matching #endif found (${showStack.length$}' unmatched preprocessing directiv
209
215
) ;
210
216
return ret ;
211
217
} finally {
212
- setCurrentFile ( oldFilename ) ;
218
+ popCurrentFile ( ) ;
213
219
}
214
220
}
215
221
0 commit comments