Skip to content

Commit

Permalink
[jscompiler] Simply pushing and popping the current file being proces…
Browse files Browse the repository at this point in the history
…sed. NFC
  • Loading branch information
sbc100 committed Feb 22, 2025
1 parent fc15522 commit c8835d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
error,
readFile,
warn,
setCurrentFile,
pushCurrentFile,
popCurrentFile,
printErr,
addToCompileTimeContext,
runInMacroContext,
Expand Down Expand Up @@ -275,7 +276,7 @@ export const LibraryManager = {
origLibrary = this.library;
this.library = userLibraryProxy;
}
const oldFile = setCurrentFile(filename);
pushCurrentFile(filename);
try {
processed = processMacros(preprocess(filename), filename);
runInMacroContext(processed, {filename: filename.replace(/\.\w+$/, '.preprocessed$&')});
Expand All @@ -295,7 +296,7 @@ export const LibraryManager = {
}
throw e;
} finally {
setCurrentFile(oldFile);
popCurrentFile();
if (origLibrary) {
this.library = origLibrary;
}
Expand Down
20 changes: 13 additions & 7 deletions src/parseTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
printErr,
readFile,
runInMacroContext,
setCurrentFile,
pushCurrentFile,
popCurrentFile,
warn,
srcDir,
} from './utility.mjs';
Expand All @@ -36,10 +37,15 @@ export function processMacros(text, filename) {
// The `?` here in makes the regex non-greedy so it matches with the closest
// set of closing braces.
// `[\s\S]` works like `.` but include newline.
return text.replace(/{{{([\s\S]+?)}}}/g, (_, str) => {
const ret = runInMacroContext(str, {filename: filename});
return ret !== null ? ret.toString() : '';
});
pushCurrentFile(filename);
try {
return text.replace(/{{{([\s\S]+?)}}}/g, (_, str) => {
const ret = runInMacroContext(str, {filename: filename});
return ret !== null ? ret.toString() : '';
});
} finally {
popCurrentFile();
}
}

function findIncludeFile(filename, currentDir) {
Expand Down Expand Up @@ -86,7 +92,6 @@ export function preprocess(filename) {
const showStack = [];
const showCurrentLine = () => showStack.every((x) => x == SHOW);

const oldFilename = setCurrentFile(filename);
const fileExt = filename.split('.').pop().toLowerCase();
const isHtml = fileExt === 'html' || fileExt === 'htm' ? true : false;
let inStyle = false;
Expand All @@ -99,6 +104,7 @@ export function preprocess(filename) {
let ret = '';
let emptyLine = false;

pushCurrentFile(filename);
try {
for (let [i, line] of lines.entries()) {
if (isHtml) {
Expand Down Expand Up @@ -209,7 +215,7 @@ no matching #endif found (${showStack.length$}' unmatched preprocessing directiv
);
return ret;
} finally {
setCurrentFile(oldFilename);
popCurrentFile();
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/utility.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ export function warningOccured() {
return warnings;
}

let currentFile = null;
let currentFile = [];

export function setCurrentFile(f) {
let rtn = currentFile;
currentFile = f;
return rtn;
export function pushCurrentFile(f) {
currentFile.push(f);
}

export function popCurrentFile(f) {
currentFile.pop(f);
}

function errorPrefix() {
if (currentFile) {
return currentFile + ': ';
if (currentFile.length > 0) {
return currentFile[currentFile.length - 1] + ': ';
} else {
return '';
}
Expand Down

0 comments on commit c8835d0

Please sign in to comment.