2022-12-08 11:40:55 +03:00
|
|
|
<?php
|
|
|
|
namespace metagen_go;
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
function get_twig(array $inc_path = [])
|
|
|
|
{
|
|
|
|
array_unshift($inc_path, __DIR__ . "/../tpl/");
|
|
|
|
$loader = new \Twig\Loader\FilesystemLoader($inc_path);
|
|
|
|
|
|
|
|
$twig = new \Twig\Environment($loader, [
|
|
|
|
'debug' => true,
|
|
|
|
'autoescape' => false,
|
|
|
|
'strict_variables' => true]
|
|
|
|
);
|
|
|
|
$twig->addExtension(new \Twig\Extension\DebugExtension());
|
|
|
|
|
|
|
|
_add_twig_support($twig);
|
|
|
|
|
|
|
|
return $twig;
|
|
|
|
}
|
|
|
|
|
|
|
|
function supported_tokens()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'POD',
|
|
|
|
'default',
|
|
|
|
'optional',
|
|
|
|
'bitfields',
|
|
|
|
'cloneable',
|
|
|
|
'virtual',
|
|
|
|
|
|
|
|
'table',
|
|
|
|
'id',
|
|
|
|
'owner',
|
|
|
|
'pkey',
|
|
|
|
|
|
|
|
'statist',
|
|
|
|
'statist_skip',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function _add_twig_support(\Twig\Environment $twig)
|
|
|
|
{
|
|
|
|
$twig->addTest(new \Twig\TwigTest('instanceof',
|
|
|
|
function($obj, $class)
|
|
|
|
{
|
|
|
|
return (new \ReflectionClass($class))->isInstance($obj);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
$twig->addFunction(new \Twig\TwigFunction('Error',
|
|
|
|
function($e)
|
|
|
|
{
|
|
|
|
throw new Exception($e);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
$twig->addFunction(new \Twig\TwigFunction('has_token',
|
|
|
|
function($o, $token)
|
|
|
|
{
|
|
|
|
return $o->hasToken($token);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
$twig->addFunction(new \Twig\TwigFunction('has_token_in_parent',
|
|
|
|
function($o, $token)
|
|
|
|
{
|
|
|
|
return $o->hasTokenInParent($token);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
$twig->addFunction(new \Twig\TwigFunction('token',
|
|
|
|
function($o, $name)
|
|
|
|
{
|
|
|
|
return $o->getToken($name);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
$twig->addFunction(new \Twig\TwigFunction('token_or',
|
|
|
|
function($o, $name, $v)
|
|
|
|
{
|
|
|
|
if($o->hasToken($name))
|
|
|
|
return $o->getToken($name);
|
|
|
|
else
|
|
|
|
return $v;
|
|
|
|
}
|
|
|
|
));
|
2022-12-08 14:23:55 +03:00
|
|
|
$twig->addFunction(new \Twig\TwigFunction('get_all_fields',
|
|
|
|
function($meta, $o)
|
|
|
|
{
|
|
|
|
return \mtg_get_all_fields($meta, $o);
|
|
|
|
}
|
|
|
|
));
|
2022-12-08 14:50:32 +03:00
|
|
|
$twig->addFunction(new \Twig\TwigFunction('count_optional',
|
|
|
|
function($units)
|
|
|
|
{
|
|
|
|
$opts = 0;
|
|
|
|
foreach($units as $u)
|
|
|
|
if($u->hasToken('optional'))
|
|
|
|
++$opts;
|
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
));
|
2022-12-08 13:29:16 +03:00
|
|
|
$twig->addFilter(new \Twig\TwigFilter('go_type',
|
|
|
|
function($type, $tokens)
|
|
|
|
{
|
|
|
|
return go_type($type, $tokens);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
$twig->addFilter(new \Twig\TwigFilter('ucfirst',
|
|
|
|
function($str)
|
|
|
|
{
|
|
|
|
return ucfirst($str);
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
function go_type(\mtgType $type, array $tokens = array())
|
|
|
|
{
|
|
|
|
if($type instanceof \mtgArrType)
|
|
|
|
{
|
|
|
|
$vtype = $type->getValue();
|
|
|
|
|
|
|
|
$native = go_type($vtype);
|
|
|
|
|
|
|
|
$str = "[]";
|
|
|
|
if(array_key_exists("virtual", $tokens))
|
|
|
|
$str .= "I";
|
|
|
|
else
|
|
|
|
$str .= $vtype instanceof mtgMetaStruct ? "*" : "";
|
|
|
|
|
|
|
|
$str .= $native;
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
else if($type instanceof \mtgBuiltinType)
|
|
|
|
{
|
|
|
|
if($type->isFloat())
|
|
|
|
return "float32";
|
|
|
|
else if($type->isDouble())
|
|
|
|
return "float64";
|
|
|
|
else if($type->isBlob())
|
|
|
|
return "[]byte";
|
|
|
|
else
|
|
|
|
return $type->getName();
|
|
|
|
}
|
|
|
|
else if($type instanceof \mtgMetaEnum)
|
|
|
|
return $type->getName();
|
|
|
|
else if($type instanceof \mtgMetaStruct)
|
|
|
|
{
|
|
|
|
if(array_key_exists("virtual", $tokens))
|
|
|
|
return "I{$type->getName()}";
|
|
|
|
else
|
|
|
|
return $type->getName();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new Exception("Unknown type '$type'");
|
2022-12-08 11:40:55 +03:00
|
|
|
}
|
2022-12-08 13:29:16 +03:00
|
|
|
|