@@ -18,7 +18,8 @@ import {
1818 printErr ,
1919 readFile ,
2020 runInMacroContext ,
21- setCurrentFile ,
21+ pushCurrentFile ,
22+ popCurrentFile ,
2223 warn ,
2324 srcDir ,
2425} from './utility.mjs' ;
@@ -36,10 +37,15 @@ export function processMacros(text, filename) {
3637 // The `?` here in makes the regex non-greedy so it matches with the closest
3738 // set of closing braces.
3839 // `[\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+ }
4349}
4450
4551function findIncludeFile ( filename , currentDir ) {
@@ -86,7 +92,6 @@ export function preprocess(filename) {
8692 const showStack = [ ] ;
8793 const showCurrentLine = ( ) => showStack . every ( ( x ) => x == SHOW ) ;
8894
89- const oldFilename = setCurrentFile ( filename ) ;
9095 const fileExt = filename . split ( '.' ) . pop ( ) . toLowerCase ( ) ;
9196 const isHtml = fileExt === 'html' || fileExt === 'htm' ? true : false ;
9297 let inStyle = false ;
@@ -99,6 +104,7 @@ export function preprocess(filename) {
99104 let ret = '' ;
100105 let emptyLine = false ;
101106
107+ pushCurrentFile ( filename ) ;
102108 try {
103109 for ( let [ i , line ] of lines . entries ( ) ) {
104110 if ( isHtml ) {
@@ -209,7 +215,7 @@ no matching #endif found (${showStack.length$}' unmatched preprocessing directiv
209215 ) ;
210216 return ret ;
211217 } finally {
212- setCurrentFile ( oldFilename ) ;
218+ popCurrentFile ( ) ;
213219 }
214220}
215221
0 commit comments