|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of byrokrat\autogiro. |
|
5
|
|
|
* |
|
6
|
|
|
* byrokrat\autogiro is free software: you can redistribute it and/or |
|
7
|
|
|
* modify it under the terms of the GNU General Public License as published |
|
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* byrokrat\autogiro is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License |
|
17
|
|
|
* along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
* |
|
19
|
|
|
* Copyright 2016-21 Hannes Forsgård |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
declare(strict_types=1); |
|
23
|
|
|
|
|
24
|
|
|
namespace byrokrat\autogiro\Parser; |
|
25
|
|
|
|
|
26
|
|
|
use byrokrat\autogiro\Visitor\VisitorInterface; |
|
27
|
|
|
use byrokrat\autogiro\Tree\Node; |
|
28
|
|
|
use byrokrat\autogiro\Exception\ParserException; |
|
29
|
|
|
use ForceUTF8\Encoding; |
|
30
|
|
|
|
|
31
|
|
|
final class Parser implements ParserInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var Grammar |
|
35
|
|
|
*/ |
|
36
|
|
|
private $grammar; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var VisitorInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $visitor; |
|
42
|
2 |
|
|
|
43
|
|
|
public function __construct(Grammar $grammar, VisitorInterface $visitor) |
|
44
|
2 |
|
{ |
|
45
|
2 |
|
$this->grammar = $grammar; |
|
46
|
|
|
$this->visitor = $visitor; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function parse(string $content): Node |
|
50
|
2 |
|
{ |
|
51
|
|
|
try { |
|
52
|
2 |
|
$tree = $this->grammar->parse( |
|
53
|
|
|
Encoding::toUTF8($content) |
|
54
|
|
|
); |
|
55
|
2 |
|
} catch (\InvalidArgumentException $exception) { |
|
56
|
2 |
|
throw new ParserException("Parser: {$exception->getMessage()}"); |
|
57
|
|
|
} |
|
58
|
2 |
|
|
|
59
|
2 |
|
$tree->accept($this->visitor); |
|
60
|
|
|
|
|
61
|
2 |
|
return $tree; |
|
62
|
1 |
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|