1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016, 2015 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\Indexer; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Variables\Variable; |
14
|
|
|
use PhpParser\Node as N; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Visitor that wraps an ASTVisitor to make it compatible with PhpParser. |
18
|
|
|
*/ |
19
|
|
|
class AdapterVisitor implements \PhpParser\NodeVisitor { |
20
|
|
|
/** |
21
|
|
|
* @var LocationImpl |
22
|
|
|
*/ |
23
|
|
|
protected $location; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Insert |
27
|
|
|
*/ |
28
|
|
|
protected $insert; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ASTVisitor |
32
|
|
|
*/ |
33
|
|
|
protected $visitor; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This is used to exit early in enterNode and to break enterNode apart |
37
|
|
|
* into many methods. |
38
|
|
|
* |
39
|
|
|
* @var array<string,string> |
40
|
|
|
*/ |
41
|
|
|
protected $jump_labels; |
42
|
|
|
|
43
|
25 |
|
public function __construct(LocationImpl $location, Insert $insert, ASTVisitor $visitor) { |
44
|
25 |
|
$this->location = $location; |
45
|
25 |
|
$this->insert = $insert; |
46
|
25 |
|
$this->visitor = $visitor; |
47
|
25 |
|
$this->jump_labels = $visitor->visitorJumpLabels(); |
48
|
25 |
|
assert('$this->jump_labels_are_correct()'); |
49
|
25 |
|
} |
50
|
|
|
|
51
|
25 |
|
protected function jump_labels_are_correct() { |
52
|
25 |
|
if (!is_array($this->jump_labels)) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
25 |
|
foreach ($this->jump_labels as $label => $method) { |
56
|
25 |
|
if (!is_callable(array($this->visitor, $method))) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
25 |
|
} |
60
|
25 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
66
|
25 |
|
public function beforeTraverse(array $nodes) { |
67
|
25 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
25 |
|
public function afterTraverse(array $nodes) { |
73
|
25 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
25 |
|
public function enterNode(\PhpParser\Node $node) { |
79
|
|
|
|
80
|
25 |
|
$cls = get_class($node); |
81
|
25 |
|
if (isset($this->jump_labels[$cls])) { |
82
|
23 |
|
$this->location->set_current_node($node); |
83
|
23 |
|
$name = $this->jump_labels[$cls]; |
84
|
23 |
|
$this->visitor->$name($this->insert, $this->location, $node); |
85
|
23 |
|
$this->location->flush_current_node(); |
86
|
23 |
|
} |
87
|
25 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
25 |
|
public function leaveNode(\PhpParser\Node $node) { |
93
|
25 |
|
} |
94
|
|
|
} |
95
|
|
|
|