1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VLF\VST; |
4
|
|
|
|
5
|
|
|
use \VLF\{ |
6
|
|
|
AST, |
7
|
|
|
Node |
8
|
|
|
}; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Интерпретатор AST VST разметки |
12
|
|
|
*/ |
13
|
|
|
class Interpreter extends \VLF\Interpreter |
14
|
|
|
{ |
15
|
|
|
static array $styles = []; // Массив созданных стилей (название => стиль) |
16
|
|
|
static array $default_styles = []; // Массив дефолтных стилей |
17
|
|
|
|
18
|
|
|
static bool $throw_errors = true; // Выводить ли ошибки интерпретации |
19
|
|
|
static bool $allow_multimethods_calls = true; // Можно ли использовать многоуровневые вызовы методов (->method1->method2) |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* * Интерпретирование синтаксического дерева |
23
|
|
|
* Выполняет то, что было сгенерировано парсером VST кода |
24
|
|
|
* |
25
|
|
|
* @param AST $tree - Абстрактное Синтаксическое Дерево (АСД), сгенерированное VST Parser'ом |
26
|
|
|
* [@param array $parent = null] - нода-родитель дерева (системная настройка) |
27
|
|
|
* |
28
|
|
|
* @return array - возвращает список созданных стилей |
29
|
|
|
*/ |
30
|
|
|
public static function run (AST $tree, Node $parent = null): array |
31
|
|
|
{ |
32
|
|
|
foreach ($tree->getNodes () as $id => $node) |
33
|
|
|
{ |
34
|
|
|
if ($node->type == \VLF\STYLE_DEFINITION) |
35
|
|
|
{ |
36
|
|
|
$name = $node->args['name']; |
37
|
|
|
$nodes = $node->getNodes (); |
38
|
|
|
|
39
|
|
|
if ($node->args['parents'] !== null) |
40
|
|
|
foreach ($node->args['parents'] as $parent) |
41
|
|
|
if (!isset (self::$styles[$parent]) && !isset (self::$default_styles[$parent])) |
42
|
|
|
{ |
43
|
|
|
if (self::$throw_errors) |
44
|
|
|
throw new \Exception ('Style "'. $parent .'" not founded'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
else $nodes = array_merge (self::$styles[$parent] ?? self::$default_styles[$parent], $nodes); |
48
|
|
|
|
49
|
|
|
if ($node->args['is_default']) |
50
|
|
|
self::$default_styles[$name] = isset (self::$objects[$name]) ? |
51
|
|
|
array_merge (self::$default_styles[$name], $nodes) : $nodes; |
52
|
|
|
|
53
|
|
|
else self::$styles[$name] = isset (self::$objects[$name]) ? |
54
|
|
|
array_merge (self::$styles[$name], $nodes) : $nodes; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
list (self::$styles, self::$default_styles) = self::run (new AST (array_map ( |
58
|
|
|
fn ($node) => $node->export (), $node->getNodes ())), $node); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return [self::$styles, self::$default_styles]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function clear (): void |
65
|
|
|
{ |
66
|
|
|
self::$styles = []; |
67
|
|
|
self::$default_styles = []; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|