1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Htsl\Parser\Node\Contracts; |
4
|
|
|
|
5
|
|
|
use Htsl\Htsl; |
6
|
|
|
use Htsl\Parser\Document; |
7
|
|
|
use Htsl\Helper\TGetter; |
8
|
|
|
use Htsl\Helper\IConfigProvider; |
9
|
|
|
use Htsl\ReadingBuffer\Line; |
10
|
|
|
|
11
|
|
|
//////////////////////////////////////////////////////////////// |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @property-read string $nodeType Type of this node. |
15
|
|
|
* @property-read string|null $scope Whether this node contains a scope and scope name |
16
|
|
|
*/ |
17
|
|
|
abstract class ANode |
18
|
|
|
{ |
19
|
|
|
use TGetter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Htsl main object. |
23
|
|
|
* |
24
|
|
|
* @access protected |
25
|
|
|
* |
26
|
|
|
* @var \Htsl\Htsl |
27
|
|
|
*/ |
28
|
|
|
protected $htsl; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The document. |
32
|
|
|
* |
33
|
|
|
* @access protected |
34
|
|
|
* |
35
|
|
|
* @var \Htsl\Parser\Document |
36
|
|
|
*/ |
37
|
|
|
protected $document; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The document. |
41
|
|
|
* |
42
|
|
|
* @access protected |
43
|
|
|
* |
44
|
|
|
* @var \Htsl\ReadingBuffer\Line |
45
|
|
|
*/ |
46
|
|
|
protected $line; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The config. |
50
|
|
|
* |
51
|
|
|
* @access protected |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $config; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Fake contructor of Node. |
60
|
|
|
* |
61
|
|
|
* @access public |
62
|
|
|
* |
63
|
|
|
* @param \Htsl\Parser\Document $document |
64
|
|
|
* @param \Htsl\ReadingBuffer\Line $line |
65
|
|
|
*/ |
66
|
|
|
final public function __construct( Document$document, Line$line ) |
67
|
|
|
{ |
68
|
|
|
$this->htsl= $document->htsl; |
|
|
|
|
69
|
|
|
$this->document= $document; |
|
|
|
|
70
|
|
|
$this->line= $line; |
|
|
|
|
71
|
|
|
|
72
|
|
|
$this->construct(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Real contructor to be rewrite. |
77
|
|
|
* |
78
|
|
|
* @access protected |
79
|
|
|
* |
80
|
|
|
* @return \Htsl\Parser\Node\Contracts\ANode |
|
|
|
|
81
|
|
|
*/ |
82
|
|
|
abstract protected function construct():self; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Opening this node, and returning node opener. |
86
|
|
|
* |
87
|
|
|
* @access public |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
abstract public function open():string; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Close this node, and returning node closer. |
95
|
|
|
* |
96
|
|
|
* @access public |
97
|
|
|
* |
98
|
|
|
* @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
abstract public function close( Line$closerLine ):string; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Getting whether this node contains a scope and scope name. |
106
|
|
|
* |
107
|
|
|
* @access public |
108
|
|
|
* |
109
|
|
|
* @return string | null |
|
|
|
|
110
|
|
|
*/ |
111
|
|
|
public function getScope() |
112
|
|
|
{ |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Getting the type of this node. |
118
|
|
|
* |
119
|
|
|
* @access public |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getNodeType() |
124
|
|
|
{ |
125
|
|
|
static $nodeType; |
126
|
|
|
return $nodeType??$nodeType= $this->nodeType??(static function($className){return strtolower(preg_replace('/(?<=\\w)([A-Z])/','_$1',preg_replace('/^(?:\\w+\\\\)*(\\w+)Node$/','$1',$className)));})(get_class($this)); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Loading node configuration. |
131
|
|
|
* |
132
|
|
|
* @access protected |
133
|
|
|
* |
134
|
|
|
* @param string $name |
135
|
|
|
* @param \Htsl\Helper\IConfigProvider $configProvider |
136
|
|
|
* |
137
|
|
|
* @return \Htsl\Parser\Node\Contracts\ANode |
138
|
|
|
*/ |
139
|
|
|
protected function loadConfig( string$name, IConfigProvider$configProvider ) |
140
|
|
|
{ |
141
|
|
|
$config= $configProvider->getConfig($this->nodeType.'_nodes',$name) ?: $configProvider->getConfig($this->nodeType.'_nodes','*'); |
|
|
|
|
142
|
|
|
|
143
|
|
|
if( isset($config['multiple']) ){ |
144
|
|
|
foreach( $config['multiple'] as $value ){ |
145
|
|
|
if( $this->line->pregGet($value['pattern']) ){ |
146
|
|
|
$config= $value; |
|
|
|
|
147
|
|
|
break; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if( isset($config['in']) ){ |
153
|
|
|
$config=(function( array$config )use($name){ |
|
|
|
|
154
|
|
|
foreach( $config['in'] as $key=>$value ){ |
155
|
|
|
if( $this->document->scope && $this->document->scope->scope===$key ){ |
156
|
|
|
$value['in_scope']= $key; |
|
|
|
|
157
|
|
|
return $value; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
if( !isset($config['out']) ){ |
161
|
|
|
$this->document->throw("The $this->nodeType node $name only use in scope ".implode(',',array_keys($config['in']))); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
return $config['out']; |
164
|
|
|
})($config); |
165
|
|
|
}elseif( isset($config['only_in']) && (!$this->document->scope || !in_array($this->document->scope->scope,$config['only_in'])) ){ |
166
|
|
|
$this->document->throw("The $this->nodeType node $name only use in scope ".implode(',',$config['only_in'])); |
|
|
|
|
167
|
|
|
}elseif( isset($config['not_in']) && (!$this->document->scope || !in_array($this->document->scope->scope,$config['not_in'])) ){ |
168
|
|
|
$this->document->throw("The $this->nodeType node $name not use in scope ".implode(',',$config['not_in'])); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if( !is_array($config) ){$this->document->throw("The $this->nodeType node $name is not supported.");} |
|
|
|
|
172
|
|
|
|
173
|
|
|
$this->config= $config; |
|
|
|
|
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
} |
|
|
|
|
178
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.