Adding experimental support for @shared_tokens which are applied to all metagen units declared in a file

This commit is contained in:
Pavel Shevaev 2023-07-18 15:27:49 +03:00
parent 259efc20ab
commit 4939f05cfc
1 changed files with 28 additions and 6 deletions

View File

@ -14,6 +14,7 @@ class mtgMetaInfoParser
private $attribute = "";
private $idltypes = array();
private $token_strs = array();
private $shared_tokens = array();
const T_EOF = 1001;
const T_StringConstant = 1002;
@ -89,6 +90,7 @@ class mtgMetaInfoParser
if(!isset($config['valid_tokens']))
$config['valid_tokens'] = array();
$config['valid_tokens'][] = 'shared_tokens';
$config['valid_tokens'][] = 'enum_override';
}
@ -144,6 +146,7 @@ class mtgMetaInfoParser
$this->source = $source;
$this->cursor = 0;
$this->line = 1;
$this->shared_tokens = array();
try
{
@ -152,7 +155,9 @@ class mtgMetaInfoParser
{
//echo "TOKEN : " . $this->token . " " . $this->attribute . " " . $this->line . "\n";
if($this->token == self::T_Enum)
if($this->token == self::T_Prop)
$this->_parseSharedTokens($this->_parsePropTokens());
else if($this->token == self::T_Enum)
$this->_parseEnum();
else if($this->token == self::T_Struct)
$this->_parseStruct();
@ -172,6 +177,16 @@ class mtgMetaInfoParser
}
}
private function _parseSharedTokens(array $tokens)
{
if(!isset($tokens['shared_tokens']))
return;
$this->shared_tokens = json_decode($tokens['shared_tokens'], true);
if(!is_array($this->shared_tokens))
$this->_error("Invalid 'shared_tokens' formant, invalid JSON");
}
private function _parseType($can_be_multi = false)
{
$types = array();
@ -316,8 +331,10 @@ class mtgMetaInfoParser
$name = $this->_parseDotName();
$enum = new mtgMetaEnum($name);
$tokens = $this->shared_tokens;
if($this->token == self::T_Prop)
$enum->setTokens($this->_parsePropTokens());
$tokens = array_merge($tokens, $this->_parsePropTokens());
$enum->setTokens($tokens);
$or_values = array();
while(true)
@ -470,6 +487,7 @@ class mtgMetaInfoParser
{
$this->_next();
$fn = $this->_parseFunc();
$fn->setTokens(array_merge($this->shared_tokens, $fn->getTokens()));
$this->current_meta->addUnit(new mtgMetaInfoUnit($this->file, $fn));
}
@ -500,8 +518,10 @@ class mtgMetaInfoParser
$s = new mtgMetaStruct($name, array(), $parent, array(), $implements);
$this->current_meta->addUnit(new mtgMetaInfoUnit($this->file, $s));
$tokens = $this->shared_tokens;
if($this->token == self::T_Prop)
$s->setTokens($this->_parsePropTokens());
$tokens = array_merge($tokens, $this->_parsePropTokens());
$s->setTokens($tokens);
$seen_funcs = false;
$flds = $this->_parseFields(
@ -535,8 +555,10 @@ class mtgMetaInfoParser
$s = new mtgMetaInterface($name);
$this->current_meta->addUnit(new mtgMetaInfoUnit($this->file, $s));
$tokens = $this->shared_tokens;
if($this->token == self::T_Prop)
$s->setTokens($this->_parsePropTokens());
$tokens = array_merge($tokens, $this->_parsePropTokens());
$s->setTokens($tokens);
$this->_next();
$funcs = $this->_parseFuncs();
@ -551,9 +573,9 @@ class mtgMetaInfoParser
$name = $this->_checkThenNext(self::T_Identifier);
$this->_checkThenNext('(');
$tokens = array();
$tokens = $this->shared_tokens;
if($this->token == self::T_Prop)
$tokens = $this->_parsePropTokens();
$tokens = array_merge($tokens, $this->_parsePropTokens());
$req_fields = $this->_parseFields(function()
{ return $this->_nextIf(')'); }