-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFbpParser.php
361 lines (321 loc) · 10.5 KB
/
FbpParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/*
* This file is part of the phpflo\phpflo-fbp package.
*
* (c) Marc Aschmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace PhpFlo\Fbp;
use PhpFlo\Common\DefinitionInterface;
use PhpFlo\Common\FbpDefinitionsInterface;
use PhpFlo\Common\Exception\ParserDefinitionException;
use PhpFlo\Common\Exception\ParserException;
/**
* Class FbpParser
*
* @package PhpFlo\Fbp
* @author Marc Aschmann <[email protected]>
*/
final class FbpParser implements FbpDefinitionsInterface
{
/**
* @var string
*/
private $source;
/**
* @var array
*/
private $settings;
/**
* @var array
*/
private $schema;
/**
* @var int
*/
private $linecount;
/**
* @var int
*/
private $linecountOverall;
/**
* @var array
*/
private $definition;
/**
* FbpParser constructor.
*
* @param string $source optional for initializing
* @param array $settings optional settings for parser
*/
public function __construct(string $source = '', array $settings = [])
{
$this->source = $source;
$this->settings = array_replace_recursive(
[],
$settings
);
$this->schema = [
self::PROPERTIES_LABEL => [
'name' => '',
],
self::INITIALIZERS_LABEL => [],
self::PROCESSES_LABEL => [],
self::CONNECTIONS_LABEL => [],
];
$this->definition = [];
}
/**
* @param string $source
* @return DefinitionInterface
* @throws ParserException
*/
public function run(string $source = ''): DefinitionInterface
{
if ('' != $source) {
$this->source = $source;
}
if (empty($this->source)) {
throw new ParserException("FbpParser::run(): no source data or empty string given!");
}
$this->definition = $this->schema; // reset
$this->linecount = 1;
$this->linecountOverall = 0;
/*
* split by lines, OS-independent
* work each line and parse for definitions
*/
foreach (preg_split('/' . self::NEWLINES . '/m', $this->source) as $line) {
$this->linecountOverall++;
// skip lines if empty or comments
if ($this->doSkip($line)) {
continue;
}
$subset = $this->examineSubset($line);
$this->validate($subset, $line); // post-parse validation, easier that way
$this->definition[self::CONNECTIONS_LABEL] = array_merge_recursive(
$this->definition[self::CONNECTIONS_LABEL], $subset
);
$this->linecount;
}
return new FbpDefinition($this->definition);
}
/**
* @param string $line
* @return array
* @throws ParserDefinitionException
*/
private function examineSubset(string $line): array
{
$subset = [];
$step = [];
$nextSrc = null;
$hasInitializer = false;
if (1 == $this->linecount && 0 === strpos(trim($line), "'")) {
$hasInitializer = true;
}
// subset
foreach (explode(self::SOURCE_TARGET_SEPARATOR, $line) as $definition) {
$resolved = [];
if (!$hasInitializer) {
$resolved = $this->examineDefinition($definition);
}
$hasInport = $this->hasValue($resolved, self::INPORT_LABEL);
$hasOutport = $this->hasValue($resolved, self::OUTPORT_LABEL);
//define states
switch (true) {
case !empty($step[self::DATA_LABEL]) && ($hasInport && $hasOutport):
// initializer + inport
$nextSrc = $resolved;
$step[self::TARGET_LABEL] = $this->addPort($resolved, self::INPORT_LABEL);
// multi def oneliner initializer resolved
array_push($this->definition[self::INITIALIZERS_LABEL], $step);
$step = [];
break;
case !empty($nextSrc) && ($hasInport && $hasOutport):
// if there was an initializer, we get a full touple with this iteration
$step = [
self::SOURCE_LABEL => $this->addPort($nextSrc, self::OUTPORT_LABEL),
self::TARGET_LABEL => $this->addPort($resolved, self::INPORT_LABEL),
];
$nextSrc = $resolved;
array_push($subset, $step);
$step = [];
break;
case $hasInport && $hasOutport:
// tgt + multi def
$nextSrc = $resolved;
$step[self::TARGET_LABEL] = $this->addPort($resolved, self::INPORT_LABEL);
// check if we've already got the touple ready
if (!empty($step[self::SOURCE_LABEL])) {
array_push($subset, $step);
$step = [];
}
break;
case $hasInport && $nextSrc:
// use previous OUT as src to fill touple
$step[self::SOURCE_LABEL] = $this->addPort($nextSrc, self::OUTPORT_LABEL);
$nextSrc = null;
case $hasInport:
$step[self::TARGET_LABEL] = $this->addPort($resolved, self::INPORT_LABEL);
// resolved touple
if (empty($step[self::DATA_LABEL])) {
array_push($subset, $step);
} else {
array_push($this->definition[self::INITIALIZERS_LABEL], $step);
}
$nextSrc = null;
$step = [];
break;
case $hasOutport:
// simplest case OUT -> IN
$step[self::SOURCE_LABEL] = $this->addPort($resolved, self::OUTPORT_LABEL);
break;
case $hasInitializer:
// initialization value: at the moment we only support one
$step[self::DATA_LABEL] = trim($definition, " '");
$hasInitializer = false; // reset
break;
default:
throw new ParserDefinitionException(
"Line ({$this->linecountOverall}) {$line} does not contain in or out ports!"
);
}
}
return $subset;
}
/**
* Check if array has a specific key and is not empty.
*
* @param array $check
* @param string $value
* @return bool
*/
private function hasValue(array $check, string $value): bool
{
if (empty($check[$value])) {
return false;
}
return true;
}
/**
* @param array $definition
* @param string $label
* @return array
*/
private function addPort(array $definition, string $label): array
{
return [
self::PROCESS_LABEL => $definition[self::PROCESS_LABEL],
self::PORT_LABEL => $definition[$label],
];
}
/**
* @param string $line
* @return array
* @throws ParserDefinitionException
*/
private function examineDefinition(string $line): array
{
preg_match('/' . self::PROCESS_DEFINITION . '/', $line, $matches);
foreach ($matches as $key => $value) {
if (is_numeric($key)) {
unset($matches[$key]);
}
}
if (!empty($matches[self::PROCESS_LABEL])) {
if (empty($matches[self::COMPONENT_LABEL])) {
$matches[self::COMPONENT_LABEL] = $matches[self::PROCESS_LABEL];
}
$this->examineProcess($matches);
} else {
throw new ParserDefinitionException(
"No process definition found in line ({$this->linecountOverall}) {$line}"
);
}
return $matches;
}
/**
* Add entry to processes.
*
* @param array $process
*/
private function examineProcess(array $process)
{
if (!isset($this->definition[self::PROCESSES_LABEL][$process[self::PROCESS_LABEL]])) {
$component = $process[self::COMPONENT_LABEL];
if (empty($component)) {
$component = $process[self::PROCESS_LABEL];
}
$this->definition[self::PROCESSES_LABEL][$process[self::PROCESS_LABEL]] = [
self::COMPONENT_LABEL => $component,
self::METADATA_LABEL => [
'label' => $component,
],
];
}
}
/**
* Add name to definition
*
* @param string $line
*/
private function addName(string $line)
{
$this->definition[self::PROPERTIES_LABEL]['name'] = trim(str_replace('#', '', $line));
}
/**
* Check if line is empty or has comment.
* In case of comments, add name to definition.
*
* @param string $line
* @return bool
*/
private function doSkip(string $line): bool
{
switch (true) {
case (empty(trim($line))):
// empty line
$skip = true;
break;
case (1 == preg_match('/(#[\s\w]+)/', $line)):
if (1 === $this->linecountOverall) {
$this->addName($line);
}
$skip = true;
break;
default:
$skip = false;
}
return $skip;
}
/**
* @param array $subset
* @param string $line
*/
private function validate(array $subset, string $line)
{
foreach ($subset as $touple) {
if (empty($touple[self::SOURCE_LABEL])) {
$this->validationError($line, self::SOURCE_LABEL);
}
if (empty($touple[self::TARGET_LABEL])) {
$this->validationError($line, self::TARGET_LABEL);
}
}
}
/**
* @param string $line
* @param string $port
* @throws ParserException
*/
private function validationError(string $line, string $port)
{
throw new ParserException(
"Error on line ({$this->linecountOverall}) {$line}: There is no {$port} defined. Maybe you forgot an in or out port?"
);
}
}