From 6f64be06fe780000c500b98143aab328f374c15e Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Tue, 30 May 2023 14:29:23 +0300 Subject: [PATCH] Adding experimental support for prefixing of included macros via <%INC("path/to", "prefix")%> --- jsm.inc.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/jsm.inc.php b/jsm.inc.php index 43a5c74..c9e9e75 100644 --- a/jsm.inc.php +++ b/jsm.inc.php @@ -305,7 +305,7 @@ class JSM return $txt; } - /*private */function _includesCallback($inc_path, $curr_file) + /*private */function _includesCallback($inc_path, $prefix, $curr_file) { $file = jsm_resolve_inc_path($this->base_dirs, $curr_file, $inc_path); @@ -337,8 +337,11 @@ class JSM { foreach($m->includes as $f => $_) $cm->includes[$f] = 1; - foreach($m->defs as $n => $d) + + foreach($m->defs as $_n => $d) { + $n = $prefix.$_n; + if(isset($cm->defs[$n]) && $cm->defs[$n] !== $d) throw new Exception("Def '$n' is already defined in '{$cm->file}' (check {$cm->defs[$n]->file})"); $cm->defs[$n] = $d; @@ -356,8 +359,8 @@ class JSM { $self = $this; $txt = preg_replace_callback( - '~<%\s*INC\s*\(\s*"([^"]+)"\s*\)\s*%>~', - function($m) use($self, $file) { return $self->_includesCallback($m[1], $file); }, + '~<%\s*INC\s*\(\s*"([^"]+)"\s*(?:,\s*"([^"]+)")?\s*\)\s*%>~', + function($m) use($self, $file) { return $self->_includesCallback($m[1], isset($m[2]) ? $m[2] : '', $file); }, $txt); } @@ -385,7 +388,7 @@ class JSM static private function _extractDeps($txt) { $dep_files = array(); - if(preg_match_all('~<%\s*(?:INC)\s*\(\s*"([^\n]+)~', $txt, $ms)) + if(preg_match_all('~<%\s*INC\s*\(\s*"([^\n]+)~', $txt, $ms)) { foreach($ms[1] as $raw_dep) { @@ -1794,8 +1797,12 @@ class JSM_ArgsParser /** * @global @raw_args */ -function macro_INC($jsm, $file) +function macro_INC($jsm, $file_and_prefix) { + $items = explode(',', $file_and_prefix); + //NOTE: we don't care about prefix here, it's handled + // during includes processing + $file = trim($items[0]); $m = $jsm->getModule($file); $m->node->call($jsm); }