-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFromSyntaxTree.class.php
More file actions
executable file
·218 lines (194 loc) · 6.5 KB
/
FromSyntaxTree.class.php
File metadata and controls
executable file
·218 lines (194 loc) · 6.5 KB
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
<?php namespace lang\meta;
use lang\IllegalArgumentException;
use lang\ast\{Language, Tokens, Visitor, Code};
/**
* Parses annotations from AST, using PHP language syntax.
*
* @see https://github.com/xp-framework/ast
*/
class FromSyntaxTree {
const CACHE_SIZE= 16;
private static $lang;
private static $parse= null;
private $cache= [];
static function __static() {
self::$lang= Language::named('PHP');
}
private function tree($name) {
if (!isset($this->cache[$name])) {
$class= strtr($name, '\\', '.');
sscanf(\xp::$cl[$class], '%[^:]://%[^$]', $cl, $argument);
$instanceFor= [literal($cl), 'instanceFor'];
$bytes= $instanceFor($argument)->loadClassBytes($class);
// Limit cache
$this->cache[$name]= new SyntaxTree(self::$lang->parse(new Tokens($bytes, $class))->tree(), $name);
if (sizeof($this->cache) > self::CACHE_SIZE) unset($this->cache[key($this->cache)]);
}
return $this->cache[$name];
}
private function parse($code, $resolver) {
if (null === self::$parse) {
// Parse lambdas and closures into code
self::$parse= clone self::$lang;
self::$parse->prefix('fn', 0, function($parse, $token) {
$signature= $this->signature($parse);
$parse->expecting('=>', 'fn');
// Parse expression
$code= '';
$b= $c= 0;
do {
switch ($parse->token->value) {
case '(': $b++; break;
case ')': $b--; if ($b < 0) break 2;
case '[': $c++; break;
case ']': $c--; if ($c < 0) break 2;
}
$code.= ' '.$parse->token->value;
$parse->forward();
} while (';' !== $parse->token->value);
$params= '';
foreach ($signature->parameters as $param) {
$params.= ', $'.$param->name;
}
if (0 === strncmp($code, ' throw ', 7)) {
return new Code('function('.substr($params, 2).') {'.$code.'; }');
} else {
return new Code('function('.substr($params, 2).') { return'.$code.'; }');
}
});
self::$parse->prefix('function', 0, function($parse, $token) {
$signature= $this->signature($parse);
// Parse body
$code= '';
$b= 0;
do {
if ('{' === $parse->token->value) {
$b++;
} else if ('}' === $parse->token->value) {
if (0 === --$b) break;
}
$code.= ' '.$parse->token->value;
$parse->forward();
} while (null !== $parse->token->value);
$parse->forward();
$params= '';
foreach ($signature->parameters as $param) {
$params.= ', $'.$param->name;
}
return new Code('function('.substr($params, 2).')'.$code.' }');
});
}
return self::$parse->parse(new Tokens($code.';', '(evaluated)'), $resolver);
}
public function evaluate($reflect, $code) {
$tree= $this->tree($reflect->name);
$parsed= self::parse($code, $tree->resolver())->tree()->children();
if (1 === sizeof($parsed)) {
return $parsed[0]->visit($tree);
}
throw new IllegalArgumentException('Given code must be a single expression');
}
/**
* Evaluates annotation values, including special-case handling for the
* named argument `eval`.
*
* @param lang.annotations.SyntaxTree $tree
* @param lang.ast.nodes.Annotated $annotated
* @return [:var]
*/
private function treeAnnotations($tree, $annotated) {
if (null === $annotated->annotations) return [];
$r= [];
foreach ($annotated->annotations as $type => $arguments) {
if ('eval' === key($arguments)) {
$parsed= self::parse($arguments['eval']->visit($tree).';', $tree->resolver());
$r[$type]= [$parsed->tree()->children()[0]->visit($tree)];
} else {
$p= &$r[$type];
$p= [];
foreach ($arguments as $name => $argument) {
$p[$name]= $argument->visit($tree);
}
}
}
return $r;
}
/**
* Constructs annotations from meta information
*
* @param [:var] $meta
* @return [:var]
*/
private function metaAnnotations($meta) {
$r= [];
foreach ($meta[DETAIL_ANNOTATIONS] as $name => $value) {
$r[$meta[DETAIL_TARGET_ANNO][$name] ?? $name]= (array)$value;
}
return $r;
}
public function imports($reflect) {
$resolver= $this->tree($reflect->name)->resolver();
$imports= [];
foreach ($resolver->imports as $alias => $type) {
$imports[$alias]= ltrim($type, '\\');
}
return $imports;
}
/** @return iterable */
public function ofType($reflect) {
if ($meta= \xp::$meta[strtr($reflect->name, '\\', '.')]['class'] ?? null) {
return $this->metaAnnotations($meta);
} else {
$tree= $this->tree($reflect->name);
return $this->treeAnnotations($tree, $tree->type());
}
}
/** @return iterable */
public function ofConstant($reflect) {
$type= $reflect->getDeclaringClass()->name;
if ($meta= \xp::$meta[strtr($type, '\\', '.')][2][$reflect->name] ?? null) {
return $this->metaAnnotations($meta);
} else {
$tree= $this->tree($type);
return $this->treeAnnotations($tree, $tree->type()->constant($reflect->name));
}
}
/** @return iterable */
public function ofProperty($reflect) {
$type= $reflect->getDeclaringClass()->name;
if ($meta= \xp::$meta[strtr($type, '\\', '.')][0][$reflect->name] ?? null) {
return $this->metaAnnotations($meta);
} else {
$tree= $this->tree($type);
return $this->treeAnnotations($tree, $tree->type()->property($reflect->name));
}
}
/** @return iterable */
public function ofMethod($reflect) {
$type= $reflect->getDeclaringClass()->name;
if ($meta= \xp::$meta[strtr($type, '\\', '.')][1][$reflect->name] ?? null) {
return $this->metaAnnotations($meta);
} else {
$tree= $this->tree($type);
return $this->treeAnnotations($tree, $tree->type()->method($reflect->name));
}
}
/** @return iterable */
public function ofParameter($method, $reflect) {
$type= $reflect->getDeclaringClass()->name;
if ($target= \xp::$meta[strtr($type, '\\', '.')][1][$method->name][DETAIL_TARGET_ANNO] ?? null) {
$r= [];
foreach ($target['$'.$reflect->name] ?? [] as $name => $value) {
$r[$target[$name] ?? $name]= (array)$value;
}
return $r;
} else {
$tree= $this->tree($type);
return $this->treeAnnotations($tree, $tree->type()
->method($method->name)
->signature
->parameters[$reflect->getPosition()]
);
}
}
}