|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Richard Klees <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This software is licensed under The MIT License. You should have received |
|
8
|
|
|
* a copy of the license along with the code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Definition; |
|
12
|
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Rules\Ruleset; |
|
14
|
|
|
use Lechimp\Dicto\Variables as V; |
|
15
|
|
|
use Lechimp\Dicto\Rules as R; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Builds Rulesets from strings. |
|
19
|
|
|
*/ |
|
20
|
|
|
class RuleBuilder { |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ASTParser |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $parser; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Compiler |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $compiler; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param V\Variable[] $predefined_variables |
|
33
|
|
|
* @param R\Schema[] $schemas |
|
34
|
|
|
* @param V\Property[] $properties |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( array $predefined_variables |
|
37
|
|
|
, array $schemas |
|
38
|
|
|
, array $properties) { |
|
39
|
|
|
$this->parser = $this->build_parser(); |
|
40
|
|
|
$this->compiler = $this->build_compiler($predefined_variables, $schemas, $properties); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return AST\Factory |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function build_factory() { |
|
47
|
|
|
return new AST\Factory(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return ASTParser |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function build_parser() { |
|
54
|
|
|
return new ASTParser($this->build_factory()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param V\Variable[] $predefined_variables |
|
59
|
|
|
* @param R\Schema[] $schemas |
|
60
|
|
|
* @param V\Property[] $properties |
|
61
|
|
|
* @return Compiler |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function build_compiler( array $predefined_variables |
|
64
|
|
|
, array $schemas |
|
65
|
|
|
, array $properties) { |
|
66
|
|
|
return new Compiler($predefined_variables, $schemas, $properties); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string |
|
71
|
|
|
* @return Ruleset |
|
72
|
|
|
*/ |
|
73
|
|
|
public function parse($source) { |
|
74
|
|
|
assert('is_string($source)'); |
|
75
|
|
|
$ast = $this->parser->parse($source); |
|
76
|
|
|
return $this->compiler->compile($ast); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|