1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Htsl\Parser\Node; |
4
|
|
|
|
5
|
|
|
use Htsl\Htsl; |
6
|
|
|
use Htsl\ReadingBuffer\Line; |
7
|
|
|
use Htsl\Parser\Node\Contracts\ANode; |
8
|
|
|
|
9
|
|
|
//////////////////////////////////////////////////////////////// |
10
|
|
|
|
11
|
|
|
class ControlNode extends ANode |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The name of the Htsl.php control structure. |
15
|
|
|
* |
16
|
|
|
* @access private |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The name of the complied(PHP) control structure. |
24
|
|
|
* |
25
|
|
|
* @access private |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $structureName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Parameters. |
33
|
|
|
* |
34
|
|
|
* @access private |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $param; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Unique id for check whether loop executed. |
42
|
|
|
* |
43
|
|
|
* @access private |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $id; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Real constructor. |
51
|
|
|
* |
52
|
|
|
* @access protected |
53
|
|
|
* |
54
|
|
|
* @return \Htsl\Parser\Node\Contracts\ANode |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
protected function construct():parent |
57
|
|
|
{ |
58
|
|
|
$name= $this->line->pregGet('/(?<=^~)[\w-]*/'); |
|
|
|
|
59
|
|
|
$this->name=$name; |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->loadConfig($name,$this->htsl); |
62
|
|
|
|
63
|
|
|
$this->param= $this->line->pregGet('/^~[\w-]*\( (.*) \)/',1); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->structureName=$this->config['name']??$name; |
|
|
|
|
66
|
|
|
|
67
|
|
|
$this->id=strtoupper(uniqid()); |
|
|
|
|
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Opening this control node, and returning node opener. |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function open():string |
80
|
|
|
{ |
81
|
|
|
return $this->withParam($this->config['opener']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Getting whether this node contains a scope and scope name. |
86
|
|
|
* |
87
|
|
|
* @access public |
88
|
|
|
* |
89
|
|
|
* @return string | null |
90
|
|
|
*/ |
91
|
|
|
public function getScope() |
92
|
|
|
{ |
93
|
|
|
return $this->config['scope']??null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Close this control node, and returning node closer. |
99
|
|
|
* |
100
|
|
|
* @access public |
101
|
|
|
* |
102
|
|
|
* @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function close( Line$closerLine ):string |
107
|
|
|
{ |
108
|
|
|
if( isset($this->config['close_by']) && $closerLine->indentLevel==$this->line->indentLevel ){ |
109
|
|
|
foreach( $this->config['close_by'] as $key=>$value ){ |
110
|
|
|
if( $closerLine->pregMatch($key) ){ |
111
|
|
|
return $this->withParam($value); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if( isset($this->config['closer']) ) |
117
|
|
|
{ return $this->withParam($this->config['closer']); } |
118
|
|
|
|
119
|
|
|
return ''; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Parse opener or closer with parameters. |
124
|
|
|
* |
125
|
|
|
* @access private |
126
|
|
|
* |
127
|
|
|
* @param string $input Opener or Closer |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
private function withParam( string$input ) |
132
|
|
|
{ |
133
|
|
|
return str_replace('$_FLAG_$',"__HTSL_CTRL_FLAG_{$this->id}__",preg_replace_callback('/(?<!%)%s((?:\\/.+?(?<!\\\\)\\/.+?(?<!\\\\)\\/)+)?/',function( array$matches ){ |
|
|
|
|
134
|
|
|
$param= $this->param; |
|
|
|
|
135
|
|
|
|
136
|
|
|
if( isset($matches[1]) ){ |
137
|
|
|
array_map(...[ |
138
|
|
|
function($replacer)use(&$param){ |
139
|
|
|
list($pattern,$replacement,)= preg_split('/(?<!\\\\)\\//',$replacer); |
|
|
|
|
140
|
|
|
$param= preg_replace(...[ |
|
|
|
|
141
|
|
|
"/$pattern/", |
|
|
|
|
142
|
|
|
preg_replace('/^\\\\_$/','',$replacement), |
143
|
|
|
$param, |
144
|
|
|
]); |
145
|
|
|
}, |
146
|
|
|
preg_split( |
147
|
|
|
'/(?<!\\\\)\\/\\//' |
148
|
|
|
, |
149
|
|
|
trim($matches[1],'/') |
150
|
|
|
), |
151
|
|
|
]); |
152
|
|
|
} |
153
|
|
|
return $param; |
154
|
|
|
},$input)); |
155
|
|
|
} |
156
|
|
|
} |
|
|
|
|
157
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.