2022-12-08 19:14:25 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class mtgMetaInfoParser
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
const T_NonOrdMark = 1000; //marks start of non 'ord(..)' values
|
2022-12-08 19:14:25 +03:00
|
|
|
const T_EOF = 1001;
|
|
|
|
const T_StringConstant = 1002;
|
|
|
|
const T_IntegerConstant = 1003;
|
|
|
|
const T_FloatConstant = 1004;
|
|
|
|
const T_Enum = 1005;
|
|
|
|
const T_RPC = 1006;
|
|
|
|
const T_End = 1007;
|
|
|
|
const T_Identifier = 1008;
|
|
|
|
const T_Struct = 1009;
|
|
|
|
const T_Prop = 1010;
|
|
|
|
const T_Extends = 1011;
|
|
|
|
const T_Func = 1012;
|
|
|
|
const T_RawStringConstant = 1013;
|
|
|
|
const T_Interface = 1014;
|
2023-05-23 13:38:16 +03:00
|
|
|
const T_Implements = 1015;
|
2023-11-12 18:17:32 +03:00
|
|
|
const T_MinType = 1019; //built-in types begin mark
|
2022-12-08 19:14:25 +03:00
|
|
|
const T_string = 1020;
|
|
|
|
const T_uint32 = 1021;
|
|
|
|
const T_int32 = 1022;
|
|
|
|
const T_uint16 = 1023;
|
|
|
|
const T_int16 = 1024;
|
|
|
|
const T_uint8 = 1025;
|
|
|
|
const T_int8 = 1026;
|
|
|
|
const T_float = 1027;
|
|
|
|
const T_uint64 = 1028;
|
|
|
|
const T_int64 = 1029;
|
|
|
|
const T_bool = 1030;
|
|
|
|
const T_blob = 1031;
|
2023-11-12 18:17:32 +03:00
|
|
|
const T_MaxType = 1032; //built-in types end mark
|
|
|
|
|
|
|
|
private array $config = array();
|
|
|
|
private mtgMetaInfo $current_meta;
|
|
|
|
private array $parsed_files = array();
|
|
|
|
private array $file_stack = array();
|
|
|
|
private ?mtgMetaParsedModule $module = null;
|
|
|
|
private string $file = "";
|
|
|
|
private string $source = "";
|
|
|
|
private int $cursor = 0;
|
|
|
|
private int $line = 0;
|
|
|
|
//TODO: setting it an 'int' type makes PHPStan produce many
|
|
|
|
// false positives
|
|
|
|
private $T = 0;
|
|
|
|
private string $T_value = "";
|
|
|
|
/** @var array<string,int>*/
|
|
|
|
private $type2T = array();
|
|
|
|
/** @var array<int,string>*/
|
|
|
|
private $T2descr = array();
|
|
|
|
private array $shared_tokens = array();
|
2022-12-08 19:14:25 +03:00
|
|
|
|
|
|
|
function __construct($config = array())
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_initTables();
|
|
|
|
|
|
|
|
self::_addDefaultTokens($config);
|
2023-07-14 16:21:21 +03:00
|
|
|
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->config = $config;
|
|
|
|
if(!isset($this->config['include_path']))
|
|
|
|
$this->config['include_path'] = array('.');
|
2023-11-12 18:17:32 +03:00
|
|
|
}
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
private function _initTables()
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->type2T = array(
|
2022-12-08 19:14:25 +03:00
|
|
|
"string" => self::T_string,
|
|
|
|
"uint32" => self::T_uint32,
|
|
|
|
"int32" => self::T_int32,
|
|
|
|
"uint16" => self::T_uint16,
|
|
|
|
"int16" => self::T_int16,
|
|
|
|
"uint8" => self::T_uint8,
|
|
|
|
"int8" => self::T_int8,
|
|
|
|
"float" => self::T_float,
|
|
|
|
"double" => self::T_float,
|
|
|
|
"uint64" => self::T_uint64,
|
|
|
|
"int64" => self::T_int64,
|
|
|
|
"bool" => self::T_bool,
|
|
|
|
"blob" => self::T_blob,
|
|
|
|
);
|
2023-11-12 18:17:32 +03:00
|
|
|
|
|
|
|
$this->T2descr = array_flip($this->type2T);
|
|
|
|
$this->T2descr[self::T_EOF] = '<EOF>';
|
|
|
|
$this->T2descr[self::T_StringConstant] = '<StringConstant>';
|
|
|
|
$this->T2descr[self::T_RawStringConstant] = '<RawStringConstant>';
|
|
|
|
$this->T2descr[self::T_IntegerConstant] = '<IntegerConstant>';
|
|
|
|
$this->T2descr[self::T_FloatConstant] = '<FloatConstant>';
|
|
|
|
$this->T2descr[self::T_Enum] = '<enum>';
|
|
|
|
$this->T2descr[self::T_RPC] = '<RPC>';
|
|
|
|
$this->T2descr[self::T_End] = '<end>';
|
|
|
|
$this->T2descr[self::T_Identifier] = '<Identifier>';
|
|
|
|
$this->T2descr[self::T_Struct] = '<struct>';
|
|
|
|
$this->T2descr[self::T_Interface] = '<interface>';
|
|
|
|
$this->T2descr[self::T_Prop] = '<@prop>';
|
|
|
|
$this->T2descr[self::T_Extends] = '<extends>';
|
|
|
|
$this->T2descr[self::T_Implements] = '<implements>';
|
|
|
|
$this->T2descr[self::T_Func] = '<func>';
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
private static function _addDefaultTokens(array &$config)
|
2023-07-14 16:21:21 +03:00
|
|
|
{
|
|
|
|
if(!isset($config['valid_tokens']))
|
|
|
|
$config['valid_tokens'] = array();
|
|
|
|
|
2023-08-16 14:16:13 +03:00
|
|
|
$config['valid_tokens'][] = 'class_id';
|
2023-07-18 15:27:49 +03:00
|
|
|
$config['valid_tokens'][] = 'shared_tokens';
|
2023-07-14 16:21:21 +03:00
|
|
|
$config['valid_tokens'][] = 'enum_override';
|
2023-11-09 17:47:36 +03:00
|
|
|
$config['valid_tokens'][] = 'enum_replace';
|
2023-07-14 16:21:21 +03:00
|
|
|
}
|
|
|
|
|
2022-12-08 19:14:25 +03:00
|
|
|
function parse(mtgMetaInfo $meta, $raw_file)
|
|
|
|
{
|
|
|
|
$this->current_meta = $meta;
|
|
|
|
|
|
|
|
$file = realpath($raw_file);
|
|
|
|
if($file === false)
|
|
|
|
throw new Exception("No such file '$raw_file'");
|
|
|
|
|
|
|
|
$this->_parse($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _parse($file)
|
|
|
|
{
|
|
|
|
if(isset($this->parsed_files[$file]))
|
|
|
|
return;
|
2023-08-03 18:03:37 +03:00
|
|
|
$module = new mtgMetaParsedModule($file);
|
|
|
|
$this->parsed_files[$file] = $module;
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->file_stack[] = $file;
|
|
|
|
$source = file_get_contents($file);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if($source === false)
|
|
|
|
throw new Exception("Could not read file '$file'");
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_resolveIncludes($module, $source);
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
catch(Exception $e)
|
|
|
|
{
|
|
|
|
throw new Exception(end($this->file_stack) . " : " . $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
array_pop($this->file_stack);
|
|
|
|
|
2023-08-03 18:03:37 +03:00
|
|
|
$this->module = $module;
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->file = $file;
|
|
|
|
$this->source = $source;
|
|
|
|
$this->cursor = 0;
|
|
|
|
$this->line = 1;
|
2023-07-18 15:27:49 +03:00
|
|
|
$this->shared_tokens = array();
|
2022-12-08 19:14:25 +03:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$this->_next();
|
2023-11-12 18:17:32 +03:00
|
|
|
while($this->T != self::T_EOF)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
//echo "TOKEN : " . $this->T . " " . $this->T_value . " " . $this->line . "\n";
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Prop)
|
2023-07-18 15:27:49 +03:00
|
|
|
$this->_parseSharedTokens($this->_parsePropTokens());
|
2023-11-12 18:17:32 +03:00
|
|
|
else if($this->T == self::T_Enum)
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_parseEnum();
|
2023-11-12 18:17:32 +03:00
|
|
|
else if($this->T == self::T_Struct)
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_parseStruct();
|
2023-11-12 18:17:32 +03:00
|
|
|
else if($this->T == self::T_Interface)
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_parseInterface();
|
2023-11-12 18:17:32 +03:00
|
|
|
else if($this->T == self::T_Func)
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_parseFreeFunc();
|
2023-11-12 18:17:32 +03:00
|
|
|
else if($this->T == self::T_RPC)
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_parseRPC();
|
|
|
|
else
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_error("Unexpected T ('" . $this->_toStr($this->T) . "' " . $this->T_value . ")");
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception $e)
|
|
|
|
{
|
|
|
|
throw new Exception("$file@{$this->line} : " . $e->getMessage() . " " . $e->getTraceAsString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 18:03:37 +03:00
|
|
|
private function _parseInclude(mtgMetaParsedModule $module, $file)
|
|
|
|
{
|
|
|
|
$this->_parse($file);
|
|
|
|
|
|
|
|
$module->addInclude($this->parsed_files[$file]);
|
|
|
|
}
|
|
|
|
|
2023-07-18 15:27:49 +03:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:14:25 +03:00
|
|
|
private function _parseType($can_be_multi = false)
|
|
|
|
{
|
|
|
|
$types = array();
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
$type = null;
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Func)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-09-27 00:15:44 +03:00
|
|
|
$origin = new mtgOrigin($this->file, $this->line);
|
2022-12-08 19:14:25 +03:00
|
|
|
$func_type = $this->_parseFuncType();
|
2023-09-27 00:15:44 +03:00
|
|
|
$type = new mtgTypeRef($func_type, $this->module, $origin);
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
2023-11-12 18:17:32 +03:00
|
|
|
else if($this->T == self::T_Identifier)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-09-27 00:15:44 +03:00
|
|
|
$origin = new mtgOrigin($this->file, $this->line);
|
2022-12-08 19:14:25 +03:00
|
|
|
$type_name = $this->_parseDotName();
|
2023-09-27 00:15:44 +03:00
|
|
|
$type = new mtgTypeRef($type_name, $this->module, $origin);
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-27 00:15:44 +03:00
|
|
|
$origin = new mtgOrigin($this->file, $this->line);
|
2023-11-12 18:17:32 +03:00
|
|
|
$type_name = $this->T_value;
|
2023-09-27 00:15:44 +03:00
|
|
|
$type = new mtgTypeRef(new mtgBuiltinType($type_name), $this->module, $origin);
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_next();
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == ord('['))
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-09-27 00:15:44 +03:00
|
|
|
$origin = new mtgOrigin($this->file, $this->line);
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_next();
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_checkThenNext(ord(']'));
|
2023-09-27 00:15:44 +03:00
|
|
|
$type = new mtgTypeRef(new mtgArrType($type), $this->module, $origin);
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
$types[] = $type;
|
|
|
|
|
|
|
|
if(!$can_be_multi)
|
|
|
|
break;
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T != ord(','))
|
2022-12-08 19:14:25 +03:00
|
|
|
break;
|
|
|
|
$this->_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sizeof($types) > 1)
|
2023-09-27 00:15:44 +03:00
|
|
|
return new mtgTypeRef(new mtgMultiType($types), $this->module, new mtgOrigin($this->file, $this->line));
|
2022-12-08 19:14:25 +03:00
|
|
|
else
|
|
|
|
return $types[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _parseFuncType()
|
|
|
|
{
|
|
|
|
$ftype = new mtgMetaFunc('');
|
|
|
|
|
|
|
|
$this->_next();
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_checkThenNext(ord('('));
|
2022-12-08 19:14:25 +03:00
|
|
|
|
|
|
|
$c = 0;
|
|
|
|
while(true)
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == ord(')'))
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$this->_next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if($c > 0)
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_checkThenNext(ord(','));
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$arg_type = $this->_parseType();
|
|
|
|
$c++;
|
|
|
|
$arg = new mtgMetaField("_$c", $arg_type);
|
|
|
|
$ftype->addArg($arg);
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == ord(':'))
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$this->_next();
|
|
|
|
$ret_type = $this->_parseType(true/*can be multi-type*/);
|
|
|
|
$ftype->setReturnType($ret_type);
|
|
|
|
}
|
|
|
|
return $ftype;
|
|
|
|
}
|
|
|
|
|
2023-08-03 18:03:37 +03:00
|
|
|
private function _resolveIncludes(mtgMetaParsedModule $module, &$text)
|
|
|
|
{
|
|
|
|
$include_paths = $this->config['include_path'];
|
|
|
|
|
2022-12-08 19:14:25 +03:00
|
|
|
$result = array();
|
|
|
|
$lines = explode("\n", $text);
|
|
|
|
foreach($lines as $line)
|
|
|
|
{
|
|
|
|
if(preg_match('~^#include\s+(\S+)~', $line, $m))
|
|
|
|
{
|
2023-08-03 18:03:37 +03:00
|
|
|
$this->_processInclude($module, $m[1], $include_paths);
|
2022-12-08 19:14:25 +03:00
|
|
|
$result[] = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$result[] = $line;
|
|
|
|
}
|
|
|
|
$text = implode("\n", $result);
|
|
|
|
}
|
|
|
|
|
2023-08-03 18:03:37 +03:00
|
|
|
private function _processInclude(mtgMetaParsedModule $module, $include, array $include_paths)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$file = false;
|
|
|
|
foreach($include_paths as $include_path)
|
|
|
|
{
|
|
|
|
$file = realpath($include_path . "/" . $include);
|
|
|
|
if($file !== false)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($file === false)
|
|
|
|
throw new Exception("#include {$include} can't be resolved(include path is '". implode(':', $include_paths) . "')");
|
|
|
|
|
2023-08-03 18:03:37 +03:00
|
|
|
$this->_parseInclude($module, $file);
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _parseEnumOrValues()
|
|
|
|
{
|
|
|
|
$values = array();
|
|
|
|
while(true)
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Identifier)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$values[] = $this->T_value;
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_next();
|
2023-11-12 18:17:32 +03:00
|
|
|
if(!$this->_nextIf(ord('|')))
|
2022-12-08 19:14:25 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _parseEnum()
|
|
|
|
{
|
|
|
|
$this->_next();
|
|
|
|
|
|
|
|
$name = $this->_parseDotName();
|
|
|
|
|
|
|
|
$enum = new mtgMetaEnum($name);
|
2023-07-18 15:27:49 +03:00
|
|
|
$tokens = $this->shared_tokens;
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Prop)
|
2023-07-18 15:27:49 +03:00
|
|
|
$tokens = array_merge($tokens, $this->_parsePropTokens());
|
|
|
|
$enum->setTokens($tokens);
|
2022-12-08 19:14:25 +03:00
|
|
|
|
|
|
|
$or_values = array();
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
if($this->_nextIf(self::T_End))
|
|
|
|
break;
|
|
|
|
$key = $this->_checkThenNext(self::T_Identifier);
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_checkThenNext(ord('='));
|
|
|
|
if($this->T == self::T_Identifier)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$or_values[$key] = $this->_parseEnumOrValues();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$value = $this->_checkThenNext(self::T_IntegerConstant);
|
|
|
|
$enum->addValue($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$enum->addOrValues($or_values);
|
2023-07-14 16:21:21 +03:00
|
|
|
|
|
|
|
//NOTE: special case for enums when we allow to 'override' the original one,
|
|
|
|
// with additional values
|
|
|
|
if($enum->hasToken('enum_override'))
|
|
|
|
{
|
2023-08-16 14:16:13 +03:00
|
|
|
$existing = $this->current_meta->findUnit($enum->getMetaId());
|
2023-07-14 16:21:21 +03:00
|
|
|
if(!$existing)
|
2023-11-09 17:47:36 +03:00
|
|
|
throw new Exception("Not found '{$name}' enum to override values");
|
2023-07-14 16:21:21 +03:00
|
|
|
if(!($existing->object instanceof mtgMetaEnum))
|
|
|
|
throw new Exception("Not an enum struct '{$name}'");
|
|
|
|
|
|
|
|
$existing->object->override($enum);
|
|
|
|
}
|
2023-11-09 17:47:36 +03:00
|
|
|
//NOTE: special case for enums when we allow to 'replace' the original one,
|
|
|
|
// with additional values
|
|
|
|
else if($enum->hasToken('enum_replace'))
|
|
|
|
{
|
|
|
|
$existing = $this->current_meta->findUnit($enum->getMetaId());
|
|
|
|
if(!$existing)
|
|
|
|
throw new Exception("Not found '{$name}' enum to replace values");
|
|
|
|
if(!($existing->object instanceof mtgMetaEnum))
|
|
|
|
throw new Exception("Not an enum struct '{$name}'");
|
|
|
|
|
|
|
|
$existing->object->replace($enum);
|
|
|
|
}
|
2023-07-14 16:21:21 +03:00
|
|
|
else
|
2023-08-03 18:03:37 +03:00
|
|
|
$this->_addUnit(new mtgMetaInfoUnit($this->file, $enum));
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
static private function _isBuiltinType(int $t) : bool
|
|
|
|
{
|
|
|
|
return $t > self::T_MinType && $t < self::T_MaxType;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _parseFields(callable $next_doer)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$flds = array();
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
if($next_doer())
|
|
|
|
break;
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Identifier)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$name = $this->T_value;
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_next();
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_checkThenNext(ord(':'));
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Identifier ||
|
|
|
|
$this->T == self::T_Func ||
|
|
|
|
self::_isBuiltinType($this->T))
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$type = $this->_parseType();
|
|
|
|
|
|
|
|
$fld = new mtgMetaField($name, $type);
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Prop)
|
2022-12-08 19:14:25 +03:00
|
|
|
$fld->setTokens($this->_parsePropTokens());
|
|
|
|
|
|
|
|
$flds[] = $fld;
|
|
|
|
}
|
|
|
|
else
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_error("Type expected");
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
else
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_error("Unexpected fields T");
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $flds;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _parseFuncs()
|
|
|
|
{
|
|
|
|
$end_token = self::T_End;
|
|
|
|
|
|
|
|
$funcs = array();
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
$fn = $this->_parseFunc();
|
|
|
|
$funcs[] = $fn;
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == $end_token)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$this->_next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $funcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _parseDotName()
|
|
|
|
{
|
|
|
|
$dot_name = '';
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T != self::T_Identifier)
|
|
|
|
$this->_error("Unexpected name T");
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
$dot_name .= $this->T_value;
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_next();
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T != ord('.'))
|
2022-12-08 19:14:25 +03:00
|
|
|
break;
|
|
|
|
$dot_name .= '.';
|
|
|
|
$this->_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dot_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _parseFunc()
|
|
|
|
{
|
|
|
|
$name = $this->_parseDotName();
|
|
|
|
$fn = new mtgMetaFunc($name);
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_checkThenNext(ord('('));
|
|
|
|
if($this->T == self::T_Prop)
|
2022-12-08 19:14:25 +03:00
|
|
|
$fn->setTokens($this->_parsePropTokens());
|
|
|
|
$args = $this->_parseFields(function()
|
2023-11-12 18:17:32 +03:00
|
|
|
{ return $this->_nextIf(ord(')')); }
|
2022-12-08 19:14:25 +03:00
|
|
|
);
|
|
|
|
$fn->setArgs($args);
|
|
|
|
|
|
|
|
$ret_type = null;
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == ord(':'))
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$this->_next();
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Identifier ||
|
|
|
|
$this->T == self::T_Func ||
|
|
|
|
self::_isBuiltinType($this->T))
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$ret_type = $this->_parseType(true/*can be multi-type*/);
|
|
|
|
$fn->setReturnType($ret_type);
|
|
|
|
}
|
|
|
|
else
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_error("Unexpected func type");
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $fn;
|
|
|
|
}
|
|
|
|
|
2023-08-03 18:03:37 +03:00
|
|
|
private function _addUnit(mtgMetaInfoUnit $unit)
|
|
|
|
{
|
|
|
|
$this->current_meta->addUnit($unit);
|
|
|
|
$this->module->addUnit($unit);
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:14:25 +03:00
|
|
|
private function _parseFreeFunc()
|
|
|
|
{
|
|
|
|
$this->_next();
|
|
|
|
$fn = $this->_parseFunc();
|
2023-07-18 15:27:49 +03:00
|
|
|
$fn->setTokens(array_merge($this->shared_tokens, $fn->getTokens()));
|
2023-08-03 18:03:37 +03:00
|
|
|
$this->_addUnit(new mtgMetaInfoUnit($this->file, $fn));
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _parseStruct()
|
|
|
|
{
|
|
|
|
$this->_next();
|
2023-09-27 00:15:44 +03:00
|
|
|
$struct_origin = new mtgOrigin($this->file, $this->line);
|
2022-12-08 19:14:25 +03:00
|
|
|
$name = $this->_parseDotName();
|
|
|
|
|
|
|
|
$parent = null;
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Extends)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$this->_next();
|
2023-09-27 00:15:44 +03:00
|
|
|
$origin = new mtgOrigin($this->file, $this->line);
|
2023-11-10 01:07:04 +03:00
|
|
|
$parent_name = $this->_parseDotName();
|
2023-09-27 00:15:44 +03:00
|
|
|
$parent = new mtgTypeRef($parent_name, $this->module, $origin);
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
2023-05-23 13:38:16 +03:00
|
|
|
$implements = array();
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Implements)
|
2023-05-23 13:38:16 +03:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
$this->_next();
|
2023-09-27 00:15:44 +03:00
|
|
|
$origin = new mtgOrigin($this->file, $this->line);
|
2023-11-10 01:07:04 +03:00
|
|
|
$if_name = $this->_parseDotName();
|
2023-09-27 00:15:44 +03:00
|
|
|
$implements[] = new mtgTypeRef($if_name, $this->module, $origin);
|
2023-11-12 18:17:32 +03:00
|
|
|
} while($this->T == ord(','));
|
2023-05-23 13:38:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$s = new mtgMetaStruct($name, array(), $parent, array(), $implements);
|
2023-09-27 00:15:44 +03:00
|
|
|
$s->setOrigin($struct_origin);
|
2023-08-03 18:03:37 +03:00
|
|
|
$this->_addUnit(new mtgMetaInfoUnit($this->file, $s));
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2023-07-18 15:27:49 +03:00
|
|
|
$tokens = $this->shared_tokens;
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Prop)
|
2023-07-18 15:27:49 +03:00
|
|
|
$tokens = array_merge($tokens, $this->_parsePropTokens());
|
|
|
|
$s->setTokens($tokens);
|
2022-12-08 19:14:25 +03:00
|
|
|
|
|
|
|
$seen_funcs = false;
|
|
|
|
$flds = $this->_parseFields(
|
|
|
|
function() use(&$seen_funcs)
|
|
|
|
{
|
|
|
|
if($this->_nextIf(self::T_End))
|
|
|
|
return true;
|
|
|
|
if($this->_nextIf(self::T_Func))
|
|
|
|
{
|
|
|
|
$seen_funcs = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
foreach($flds as $fld)
|
|
|
|
$s->addField($fld);
|
|
|
|
|
|
|
|
if($seen_funcs)
|
|
|
|
{
|
|
|
|
$funcs = $this->_parseFuncs();
|
|
|
|
foreach($funcs as $fn)
|
|
|
|
$s->addFunc($fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _parseInterface()
|
|
|
|
{
|
|
|
|
$this->_next();
|
|
|
|
$name = $this->_parseDotName();
|
|
|
|
|
|
|
|
$s = new mtgMetaInterface($name);
|
2023-08-03 18:03:37 +03:00
|
|
|
$this->_addUnit(new mtgMetaInfoUnit($this->file, $s));
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2023-07-18 15:27:49 +03:00
|
|
|
$tokens = $this->shared_tokens;
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Prop)
|
2023-07-18 15:27:49 +03:00
|
|
|
$tokens = array_merge($tokens, $this->_parsePropTokens());
|
|
|
|
$s->setTokens($tokens);
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T !== self::T_End)
|
2023-09-27 00:15:44 +03:00
|
|
|
{
|
|
|
|
$this->_next();
|
|
|
|
$funcs = $this->_parseFuncs();
|
|
|
|
foreach($funcs as $fn)
|
|
|
|
$s->addFunc($fn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$this->_next();
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _parseRPC()
|
|
|
|
{
|
|
|
|
$this->_next();
|
|
|
|
$code = $this->_checkThenNext(self::T_IntegerConstant);
|
2023-11-10 01:07:04 +03:00
|
|
|
$name = $this->_parseDotName();
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_checkThenNext(ord('('));
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2023-07-18 15:27:49 +03:00
|
|
|
$tokens = $this->shared_tokens;
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == self::T_Prop)
|
2023-07-18 15:27:49 +03:00
|
|
|
$tokens = array_merge($tokens, $this->_parsePropTokens());
|
2022-12-08 19:14:25 +03:00
|
|
|
|
|
|
|
$req_fields = $this->_parseFields(function()
|
2023-11-12 18:17:32 +03:00
|
|
|
{ return $this->_nextIf(ord(')')); }
|
2022-12-08 19:14:25 +03:00
|
|
|
);
|
|
|
|
$rsp_fields = $this->_parseFields(function()
|
|
|
|
{ return $this->_nextIf(self::T_End); }
|
|
|
|
);
|
|
|
|
|
|
|
|
$req = new mtgMetaPacket($code, "RPC_REQ_$name");
|
|
|
|
$req->setFields($req_fields);
|
|
|
|
$rsp = new mtgMetaPacket($code, "RPC_RSP_$name");
|
|
|
|
$rsp->setFields($rsp_fields);
|
|
|
|
|
|
|
|
$rpc = new mtgMetaRPC("RPC_$name", $code, $req, $rsp, $tokens);
|
2023-08-03 18:03:37 +03:00
|
|
|
$this->_addUnit(new mtgMetaInfoUnit($this->file, $rpc));
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _parsePropTokens()
|
|
|
|
{
|
|
|
|
$new_line = ord("\n");
|
|
|
|
|
|
|
|
$prop_tokens = array();
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T != self::T_Prop)
|
2022-12-08 19:14:25 +03:00
|
|
|
break;
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
$name = ltrim($this->T_value, '@');
|
|
|
|
$this->_validatePropToken($name);
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_next();
|
|
|
|
|
|
|
|
$value = null;
|
2023-11-12 18:17:32 +03:00
|
|
|
$value_start_line = $this->line;
|
|
|
|
if($this->T == ord(':'))
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
$this->_next(false/*don't skip new line*/);
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == $new_line ||
|
|
|
|
$this->T == self::T_Prop)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
//let's skip it
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T == $new_line)
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_next();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$tmp = $this->T_value;
|
|
|
|
if($this->T == self::T_StringConstant)
|
2022-12-08 19:14:25 +03:00
|
|
|
$tmp = "\"$tmp\"";
|
|
|
|
if($value === null)
|
|
|
|
$value = '';
|
|
|
|
$value .= $tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-12 18:17:32 +03:00
|
|
|
|
|
|
|
if($value && substr($value, 0, 1) === '{')
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$json = json_decode($value);
|
|
|
|
if($json === null)
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
//for better line reporting
|
|
|
|
$this->line = $value_start_line;
|
|
|
|
$this->_error("Bad json");
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$prop_tokens[$name] = $value;
|
|
|
|
}
|
|
|
|
return $prop_tokens;
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
private function _validatePropToken(string $name)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-05-17 23:47:45 +03:00
|
|
|
if(!isset($this->config['valid_tokens']) ||
|
|
|
|
!is_array($this->config['valid_tokens']))
|
2022-12-08 19:14:25 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(!in_array($name, $this->config['valid_tokens']))
|
2023-05-17 23:47:45 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
throw new Exception("Unknown T '$name'");
|
2023-05-17 23:47:45 +03:00
|
|
|
}
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
private function _char() : string
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$str = substr($this->source, $this->cursor, 1);
|
|
|
|
if($str === false)
|
|
|
|
$str = '';
|
|
|
|
return $str;
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _next($skip_newlines = true)
|
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$c = $this->_char();
|
|
|
|
if($c == '')
|
|
|
|
{
|
|
|
|
$this->cursor--;
|
|
|
|
$this->T = self::T_EOF;
|
|
|
|
$this->T_value = $c;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->T = ord($c);
|
|
|
|
$this->T_value = $c;
|
2022-12-08 19:14:25 +03:00
|
|
|
|
|
|
|
++$this->cursor;
|
|
|
|
|
|
|
|
switch($c)
|
|
|
|
{
|
|
|
|
case ' ': case "\r": case "\t": break;
|
|
|
|
case "\n": $this->line++; if($skip_newlines) break; else return;
|
|
|
|
case '{': case '}': case '(': case ')': case '[': case ']': case '|': return;
|
|
|
|
case ',': case ':': case ';': case '=': return;
|
|
|
|
case '.':
|
2023-11-12 18:17:32 +03:00
|
|
|
if(!ctype_digit($this->_char())) return;
|
|
|
|
$this->_error("Floating point constant can't start with .");
|
2022-12-08 19:14:25 +03:00
|
|
|
break;
|
|
|
|
case '"':
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T_value = "";
|
|
|
|
while($this->_char() != '"')
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
if(ord($this->_char()) < ord(' '))
|
|
|
|
$this->_error("Illegal character in string constant");
|
|
|
|
if($this->_char() == '\\')
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$this->cursor++;
|
2023-11-12 18:17:32 +03:00
|
|
|
switch($this->_char())
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
case 'n': $this->T_value .= "\n"; $this->cursor++; break;
|
|
|
|
case 't': $this->T_value .= "\t"; $this->cursor++; break;
|
|
|
|
case 'r': $this->T_value .= "\r"; $this->cursor++; break;
|
|
|
|
case '"': $this->T_value .= '"'; $this->cursor++; break;
|
|
|
|
case '\\': $this->T_value .= '\\'; $this->cursor++; break;
|
|
|
|
default: $this->_error("Unknown escape code in string constant"); break;
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else // printable chars + UTF-8 bytes
|
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T_value .= $this->_char();
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->cursor++;
|
|
|
|
}
|
|
|
|
}
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T = self::T_StringConstant;
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->cursor++;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '`':
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T_value = "";
|
|
|
|
while($this->_char() != '`')
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T_value .= $this->_char();
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->cursor++;
|
|
|
|
}
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T = self::T_RawStringConstant;
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->cursor++;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '/':
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->_char() == '/')
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$this->cursor++;
|
2023-11-12 18:17:32 +03:00
|
|
|
while($this->_char() != '' && $this->_char() != "\n") $this->cursor++;
|
2022-12-08 19:14:25 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '#':
|
2023-11-12 18:17:32 +03:00
|
|
|
while($this->_char() != '' && $this->_char() != "\n") $this->cursor++;
|
2022-12-08 19:14:25 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '@':
|
|
|
|
$start = $this->cursor - 1;
|
2023-11-12 18:17:32 +03:00
|
|
|
while(ctype_alnum($this->_char()) || $this->_char() == '_')
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->cursor++;
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T = self::T_Prop;
|
|
|
|
$this->T_value = substr($this->source, $start, $this->cursor - $start);
|
2022-12-08 19:14:25 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
//fall thru
|
|
|
|
default:
|
|
|
|
|
|
|
|
if(ctype_alpha($c))
|
|
|
|
{
|
|
|
|
//collect all chars of an identifier
|
|
|
|
$start = $this->cursor - 1;
|
2023-11-12 18:17:32 +03:00
|
|
|
while(ctype_alnum($this->_char()) || $this->_char() == '_')
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->cursor++;
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T_value = substr($this->source, $start, $this->cursor - $start);
|
2022-12-08 19:14:25 +03:00
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if(isset($this->type2T[$this->T_value]))
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T = $this->type2T[$this->T_value];
|
2022-12-08 19:14:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T_value == "true" || $this->T_value == "false")
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T = self::T_IntegerConstant;
|
2022-12-08 19:14:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//check for declaration keywords:
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->T_value == "struct") { $this->T = self::T_Struct; return; }
|
|
|
|
if($this->T_value == "interface") { $this->T = self::T_Interface; return; }
|
|
|
|
if($this->T_value == "enum") { $this->T = self::T_Enum; return; }
|
|
|
|
if($this->T_value == "RPC") { $this->T = self::T_RPC; return; }
|
|
|
|
if($this->T_value == "end") { $this->T = self::T_End; return; }
|
|
|
|
if($this->T_value == "extends") { $this->T = self::T_Extends; return; }
|
|
|
|
if($this->T_value == "implements") { $this->T = self::T_Implements; return; }
|
|
|
|
if($this->T_value == "func") { $this->T = self::T_Func; return; }
|
2022-12-08 19:14:25 +03:00
|
|
|
|
|
|
|
//if not it's a user defined identifier
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T = self::T_Identifier;
|
2022-12-08 19:14:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(ctype_digit($c) || $c == '-')
|
|
|
|
{
|
|
|
|
$start = $this->cursor - 1;
|
2023-11-12 18:17:32 +03:00
|
|
|
while(ctype_digit($this->_char())) $this->cursor++;
|
|
|
|
if($this->_char() == '.')
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$this->cursor++;
|
2023-11-12 18:17:32 +03:00
|
|
|
while(ctype_digit($this->_char())) $this->cursor++;
|
2022-12-08 19:14:25 +03:00
|
|
|
// see if this float has a scientific notation suffix. Both JSON
|
|
|
|
// and C++ (through strtod() we use) have the same format:
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->_char() == 'e' || $this->_char() == 'E')
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
|
|
|
$this->cursor++;
|
2023-11-12 18:17:32 +03:00
|
|
|
if($this->_char() == '+' || $this->_char() == '-') $this->cursor++;
|
|
|
|
while(ctype_digit($this->_char())) $this->cursor++;
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T = self::T_FloatConstant;
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
else
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->T = self::T_IntegerConstant;
|
|
|
|
$this->T_value = substr($this->source, $start, $this->cursor - $start);
|
2022-12-08 19:14:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_error("Illegal character '$c'");
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
private function _nextIf(int $t) : bool
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$yes = $t === $this->T;
|
2022-12-08 19:14:25 +03:00
|
|
|
if($yes)
|
|
|
|
$this->_next();
|
|
|
|
return $yes;
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
private function _checkThenNext(int $t) : string
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
if($t !== $this->T)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
$this->_error("Expecting '" . $this->_toStr($t) . "' instead got '" . $this->_toStr($this->T) . "'");
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
$attr = $this->T_value;
|
2022-12-08 19:14:25 +03:00
|
|
|
$this->_next();
|
|
|
|
return $attr;
|
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
private function _toStr(int $t) : string
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
if($t < self::T_NonOrdMark)
|
2022-12-08 19:14:25 +03:00
|
|
|
return chr($t);
|
2023-11-12 18:17:32 +03:00
|
|
|
return $this->T2descr[$t];
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
2023-11-12 18:17:32 +03:00
|
|
|
private function _error(string $msg)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2023-11-12 18:17:32 +03:00
|
|
|
throw new Exception($msg . " (T: {$this->T}, attr: {$this->T_value})");
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 18:03:37 +03:00
|
|
|
class mtgMetaParsedModule
|
|
|
|
{
|
|
|
|
public $file;
|
|
|
|
public $units = array();
|
|
|
|
public $includes = array();
|
|
|
|
|
|
|
|
function __construct($file)
|
|
|
|
{
|
|
|
|
$this->file = $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addUnit(mtgMetaInfoUnit $unit)
|
|
|
|
{
|
2023-08-16 14:16:13 +03:00
|
|
|
$this->units[$unit->object->getMetaId()] = $unit;
|
2023-08-03 18:03:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function findUnit($id)
|
|
|
|
{
|
|
|
|
if(isset($this->units[$id]))
|
|
|
|
return $this->units[$id];
|
|
|
|
|
|
|
|
foreach($this->includes as $include)
|
|
|
|
{
|
|
|
|
if(isset($include->units[$id]))
|
|
|
|
return $include->units[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addInclude(mtgMetaParsedModule $include)
|
|
|
|
{
|
|
|
|
$this->includes[] = $include;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 12:37:55 +03:00
|
|
|
function mtg_parse_meta(array $meta_srcs, $valid_tokens = null, $inc_path = null)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2022-12-09 12:37:55 +03:00
|
|
|
if($inc_path === null)
|
2022-12-08 19:14:25 +03:00
|
|
|
{
|
2022-12-09 12:37:55 +03:00
|
|
|
//let's autodetect include path
|
|
|
|
$inc_path = array();
|
|
|
|
foreach($meta_srcs as $src)
|
|
|
|
{
|
|
|
|
if(is_dir($src))
|
|
|
|
$inc_path[] = $src;
|
|
|
|
else if(is_file($src))
|
|
|
|
$inc_path[] = dirname($src);
|
|
|
|
}
|
2022-12-08 19:14:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$meta_parser = new mtgMetaInfoParser(
|
|
|
|
array(
|
2022-12-09 12:37:55 +03:00
|
|
|
'include_path' => $inc_path,
|
2022-12-08 19:14:25 +03:00
|
|
|
'valid_tokens' => $valid_tokens
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$meta = new mtgMetaInfo();
|
|
|
|
foreach($meta_srcs as $src)
|
|
|
|
mtg_load_meta($meta, $meta_parser, $src);
|
2023-09-27 00:15:44 +03:00
|
|
|
|
|
|
|
$meta->validate();
|
|
|
|
|
|
|
|
mtgTypeRef::checkAllResolved();
|
|
|
|
|
2022-12-08 19:14:25 +03:00
|
|
|
return $meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
function mtg_load_meta(mtgMetaInfo $meta, mtgMetaInfoParser $meta_parser, $dir_or_file)
|
|
|
|
{
|
|
|
|
$files = array();
|
|
|
|
if(is_dir($dir_or_file))
|
|
|
|
$files = mtg_find_meta_files($dir_or_file);
|
|
|
|
else if(is_file($dir_or_file))
|
|
|
|
$files[] = $dir_or_file;
|
|
|
|
else
|
|
|
|
throw new Exception("Bad meta source '$dir_or_file'");
|
|
|
|
|
|
|
|
foreach($files as $file)
|
|
|
|
$meta_parser->parse($meta, $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
function mtg_find_meta_files($dir)
|
|
|
|
{
|
|
|
|
$items = scandir($dir);
|
|
|
|
if($items === false)
|
|
|
|
throw new Exception("Directory '$dir' is invalid");
|
|
|
|
|
|
|
|
$files = array();
|
|
|
|
foreach($items as $item)
|
|
|
|
{
|
|
|
|
if($item[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(strpos($item, ".meta") !== (strlen($item)-5))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
$file = $dir . '/' . $item;
|
|
|
|
if(is_file($file) && !is_dir($file))
|
|
|
|
$files[] = $file;
|
|
|
|
}
|
|
|
|
return $files;
|
|
|
|
}
|