Adding experimental support for prefixing of included macros via <%INC("path/to", "prefix")%>

This commit is contained in:
Pavel Shevaev 2023-05-30 14:29:23 +03:00
parent 5681e61fdf
commit 6f64be06fe
1 changed files with 13 additions and 6 deletions

View File

@ -305,7 +305,7 @@ class JSM
return $txt; 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); $file = jsm_resolve_inc_path($this->base_dirs, $curr_file, $inc_path);
@ -337,8 +337,11 @@ class JSM
{ {
foreach($m->includes as $f => $_) foreach($m->includes as $f => $_)
$cm->includes[$f] = 1; $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) 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})"); throw new Exception("Def '$n' is already defined in '{$cm->file}' (check {$cm->defs[$n]->file})");
$cm->defs[$n] = $d; $cm->defs[$n] = $d;
@ -356,8 +359,8 @@ class JSM
{ {
$self = $this; $self = $this;
$txt = preg_replace_callback( $txt = preg_replace_callback(
'~<%\s*INC\s*\(\s*"([^"]+)"\s*\)\s*%>~', '~<%\s*INC\s*\(\s*"([^"]+)"\s*(?:,\s*"([^"]+)")?\s*\)\s*%>~',
function($m) use($self, $file) { return $self->_includesCallback($m[1], $file); }, function($m) use($self, $file) { return $self->_includesCallback($m[1], isset($m[2]) ? $m[2] : '', $file); },
$txt); $txt);
} }
@ -385,7 +388,7 @@ class JSM
static private function _extractDeps($txt) static private function _extractDeps($txt)
{ {
$dep_files = array(); $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) foreach($ms[1] as $raw_dep)
{ {
@ -1794,8 +1797,12 @@ class JSM_ArgsParser
/** /**
* @global @raw_args * @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 = $jsm->getModule($file);
$m->node->call($jsm); $m->node->call($jsm);
} }