Adding @flt_bhl_ref filter

This commit is contained in:
Pavel Shevaev 2023-05-18 15:09:35 +03:00
parent 06e42eb56f
commit 9e3680ff0a
3 changed files with 73 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tags

View File

@ -304,3 +304,51 @@ function bhl_line_row_to_pos($file, $line, $row)
$pos += $row;
return $pos;
}
function bhl_map_module_to_file($module)
{
foreach(bhl_proj()->src_dirs as $dir)
{
$tmp = $dir.'/'.$module.'.bhl';
if(file_exists($tmp))
return $tmp;
}
return null;
}
function bhl_validate_func_ref($module, $func, array $signature)
{
if(sizeof($signature) == 0)
throw new Exception("Signature is invalid");
$module_file = bhl_map_module_to_file($module);
if(!$module_file)
throw new Exception("Module not found '{$module}'");
$module_src = file_get_contents($module_file);
$signature_pattern = '';
$signature_pattern .= '~func\s+';
if($signature[0] !== 'void')
{
$signature_pattern .= preg_quote($signature[0]);
$signature_pattern .= '\s+';
}
$signature_pattern .= $func;
$signature_pattern .= '\s*\(';
array_shift($signature);
foreach($signature as $type)
{
$signature_pattern .= '\s*';
$signature_pattern .= preg_quote($type);
$signature_pattern .= '\s*\S+';
$signature_pattern .= '\s*,';
}
$signature_pattern = rtrim($signature_pattern, '\s*,');
$signature_pattern .= '\s*\)';
$signature_pattern .= '~';
if(!preg_match($signature_pattern, $module_src))
throw new Exception("Func '$func' signature '".implode(',', $signature)."' not found in module '$module'");
}

24
bhl_flt.inc.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace metagen_php;
use Exception;
function flt_bhl_ref($val, $name, $struct, $args)
{
if(!$val)
return $val;
if(!isset($val['module']))
throw new Exception("Missing 'module'");
if(!isset($val['fn']))
throw new Exception("Missing 'fn'");
$signature = explode(",", $args);
if(!$signature)
throw new Exception("Invalid signature: $args");
\taskman\bhl_validate_func_ref($val['module'], $val['fn'], $signature);
return $val;
}