Skip to content

Commit 12c230d

Browse files
committed
Add sanity check of this library running without php-ast enabled.
ast_shim.php provides class and constant definitions. Users of this library can call methods of ASTConverter to provide substitutes for ast\parse_code() and ast\parse_file().
1 parent bd4fdec commit 12c230d

File tree

6 files changed

+494
-0
lines changed

6 files changed

+494
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ install:
1919
script:
2020
- vendor/bin/phan
2121
- ./test
22+
- cd tests/test_without_ast; ./test.sh

src/ASTConverter/ASTConverter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
use PhpParser;
66
use PhpParser\ParserFactory;
77

8+
// If php-ast isn't loaded already, then load this file to generate equivalent
9+
// class, constant, and function definitions.
10+
if (!class_exists('\ast\Node')) {
11+
require_once __DIR__ . '/../ast_shim.php';
12+
}
13+
814
/**
915
* Source: https://github.com/TysonAndre/php-parser-to-php-ast
1016
* Uses PhpParser to create an instance of ast\Node.

src/ast_shim.php

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
<?php
2+
/**
3+
* Based on PHPDoc stub file for ast extension from
4+
* https://github.com/nikic/php-ast/blob/master/ast_stub.php
5+
*
6+
* @author Bill Schaller <[email protected]>
7+
* @author Nikita Popov <[email protected]>
8+
*
9+
* With modifications to be a functional replacement for the data
10+
* structures and global constants of ext-ast. (for class ast\Node and ast\Node\Decl)
11+
*
12+
* However, this file does not define any global functions such as
13+
* ast\parse_code() and ast\parse_file(). (to avoid confusion)
14+
*
15+
* @author Tyson Andre
16+
*/
17+
18+
// AST KIND CONSTANTS
19+
namespace ast;
20+
21+
const AST_ARG_LIST = 128;
22+
const AST_LIST = 255;
23+
const AST_ARRAY = 129;
24+
const AST_ENCAPS_LIST = 130;
25+
const AST_EXPR_LIST = 131;
26+
const AST_STMT_LIST = 132;
27+
const AST_IF = 133;
28+
const AST_SWITCH_LIST = 134;
29+
const AST_CATCH_LIST = 135;
30+
const AST_PARAM_LIST = 136;
31+
const AST_CLOSURE_USES = 137;
32+
const AST_PROP_DECL = 138;
33+
const AST_CONST_DECL = 139;
34+
const AST_CLASS_CONST_DECL = 140;
35+
const AST_NAME_LIST = 141;
36+
const AST_TRAIT_ADAPTATIONS = 142;
37+
const AST_USE = 143;
38+
const AST_NAME = 2048;
39+
const AST_CLOSURE_VAR = 2049;
40+
const AST_NULLABLE_TYPE = 2050;
41+
const AST_FUNC_DECL = 66;
42+
const AST_CLOSURE = 67;
43+
const AST_METHOD = 68;
44+
const AST_CLASS = 69;
45+
const AST_MAGIC_CONST = 0;
46+
const AST_TYPE = 1;
47+
const AST_VAR = 256;
48+
const AST_CONST = 257;
49+
const AST_UNPACK = 258;
50+
const AST_UNARY_PLUS = 259;
51+
const AST_UNARY_MINUS = 260;
52+
const AST_CAST = 261;
53+
const AST_EMPTY = 262;
54+
const AST_ISSET = 263;
55+
const AST_SILENCE = 264;
56+
const AST_SHELL_EXEC = 265;
57+
const AST_CLONE = 266;
58+
const AST_EXIT = 267;
59+
const AST_PRINT = 268;
60+
const AST_INCLUDE_OR_EVAL = 269;
61+
const AST_UNARY_OP = 270;
62+
const AST_PRE_INC = 271;
63+
const AST_PRE_DEC = 272;
64+
const AST_POST_INC = 273;
65+
const AST_POST_DEC = 274;
66+
const AST_YIELD_FROM = 275;
67+
const AST_GLOBAL = 276;
68+
const AST_UNSET = 277;
69+
const AST_RETURN = 278;
70+
const AST_LABEL = 279;
71+
const AST_REF = 280;
72+
const AST_HALT_COMPILER = 281;
73+
const AST_ECHO = 282;
74+
const AST_THROW = 283;
75+
const AST_GOTO = 284;
76+
const AST_BREAK = 285;
77+
const AST_CONTINUE = 286;
78+
const AST_DIM = 512;
79+
const AST_PROP = 513;
80+
const AST_STATIC_PROP = 514;
81+
const AST_CALL = 515;
82+
const AST_CLASS_CONST = 516;
83+
const AST_ASSIGN = 517;
84+
const AST_ASSIGN_REF = 518;
85+
const AST_ASSIGN_OP = 519;
86+
const AST_BINARY_OP = 520;
87+
const AST_GREATER = 521;
88+
const AST_GREATER_EQUAL = 522;
89+
const AST_AND = 523;
90+
const AST_OR = 524;
91+
const AST_ARRAY_ELEM = 525;
92+
const AST_NEW = 526;
93+
const AST_INSTANCEOF = 527;
94+
const AST_YIELD = 528;
95+
const AST_COALESCE = 529;
96+
const AST_STATIC = 530;
97+
const AST_WHILE = 531;
98+
const AST_DO_WHILE = 532;
99+
const AST_IF_ELEM = 533;
100+
const AST_SWITCH = 534;
101+
const AST_SWITCH_CASE = 535;
102+
const AST_DECLARE = 536;
103+
const AST_PROP_ELEM = 774;
104+
const AST_CONST_ELEM = 775;
105+
const AST_USE_TRAIT = 537;
106+
const AST_TRAIT_PRECEDENCE = 538;
107+
const AST_METHOD_REFERENCE = 539;
108+
const AST_NAMESPACE = 540;
109+
const AST_USE_ELEM = 541;
110+
const AST_TRAIT_ALIAS = 542;
111+
const AST_GROUP_USE = 543;
112+
const AST_METHOD_CALL = 768;
113+
const AST_STATIC_CALL = 769;
114+
const AST_CONDITIONAL = 770;
115+
const AST_TRY = 771;
116+
const AST_CATCH = 772;
117+
const AST_PARAM = 773;
118+
const AST_FOR = 1024;
119+
const AST_FOREACH = 1025;
120+
// END AST KIND CONSTANTS
121+
122+
// AST FLAG CONSTANTS
123+
namespace ast\flags;
124+
const NAME_FQ = 0;
125+
const NAME_NOT_FQ = 1;
126+
const NAME_RELATIVE = 2;
127+
const MODIFIER_PUBLIC = 256;
128+
const MODIFIER_PROTECTED = 512;
129+
const MODIFIER_PRIVATE = 1024;
130+
const MODIFIER_STATIC = 1;
131+
const MODIFIER_ABSTRACT = 2;
132+
const MODIFIER_FINAL = 4;
133+
const RETURNS_REF = 67108864;
134+
const CLASS_ABSTRACT = 32;
135+
const CLASS_FINAL = 4;
136+
const CLASS_TRAIT = 128;
137+
const CLASS_INTERFACE = 64;
138+
const CLASS_ANONYMOUS = 256;
139+
const PARAM_REF = 1;
140+
const PARAM_VARIADIC = 2;
141+
const TYPE_NULL = 1;
142+
const TYPE_BOOL = 13;
143+
const TYPE_LONG = 4;
144+
const TYPE_DOUBLE = 5;
145+
const TYPE_STRING = 6;
146+
const TYPE_ARRAY = 7;
147+
const TYPE_OBJECT = 8;
148+
const TYPE_CALLABLE = 14;
149+
const TYPE_VOID = 18;
150+
const TYPE_ITERABLE = 19;
151+
const UNARY_BOOL_NOT = 13;
152+
const UNARY_BITWISE_NOT = 12;
153+
const UNARY_SILENCE = 260;
154+
const UNARY_PLUS = 261;
155+
const UNARY_MINUS = 262;
156+
const BINARY_BOOL_AND = 259;
157+
const BINARY_BOOL_OR = 258;
158+
const BINARY_BOOL_XOR = 14;
159+
const BINARY_BITWISE_OR = 9;
160+
const BINARY_BITWISE_AND = 10;
161+
const BINARY_BITWISE_XOR = 11;
162+
const BINARY_CONCAT = 8;
163+
const BINARY_ADD = 1;
164+
const BINARY_SUB = 2;
165+
const BINARY_MUL = 3;
166+
const BINARY_DIV = 4;
167+
const BINARY_MOD = 5;
168+
const BINARY_POW = 166;
169+
const BINARY_SHIFT_LEFT = 6;
170+
const BINARY_SHIFT_RIGHT = 7;
171+
const BINARY_IS_IDENTICAL = 15;
172+
const BINARY_IS_NOT_IDENTICAL = 16;
173+
const BINARY_IS_EQUAL = 17;
174+
const BINARY_IS_NOT_EQUAL = 18;
175+
const BINARY_IS_SMALLER = 19;
176+
const BINARY_IS_SMALLER_OR_EQUAL = 20;
177+
const BINARY_IS_GREATER = 256;
178+
const BINARY_IS_GREATER_OR_EQUAL = 257;
179+
const BINARY_SPACESHIP = 170;
180+
const BINARY_COALESCE = 260;
181+
const ASSIGN_BITWISE_OR = 31;
182+
const ASSIGN_BITWISE_AND = 32;
183+
const ASSIGN_BITWISE_XOR = 33;
184+
const ASSIGN_CONCAT = 30;
185+
const ASSIGN_ADD = 23;
186+
const ASSIGN_SUB = 24;
187+
const ASSIGN_MUL = 25;
188+
const ASSIGN_DIV = 26;
189+
const ASSIGN_MOD = 27;
190+
const ASSIGN_POW = 167;
191+
const ASSIGN_SHIFT_LEFT = 28;
192+
const ASSIGN_SHIFT_RIGHT = 29;
193+
const EXEC_EVAL = 1;
194+
const EXEC_INCLUDE = 2;
195+
const EXEC_INCLUDE_ONCE = 4;
196+
const EXEC_REQUIRE = 8;
197+
const EXEC_REQUIRE_ONCE = 16;
198+
const USE_NORMAL = 361;
199+
const USE_FUNCTION = 346;
200+
const USE_CONST = 347;
201+
const MAGIC_LINE = 370;
202+
const MAGIC_FILE = 371;
203+
const MAGIC_DIR = 372;
204+
const MAGIC_NAMESPACE = 389;
205+
const MAGIC_FUNCTION = 376;
206+
const MAGIC_METHOD = 375;
207+
const MAGIC_CLASS = 373;
208+
const MAGIC_TRAIT = 374;
209+
const ARRAY_SYNTAX_LIST = 1;
210+
const ARRAY_SYNTAX_LONG = 2;
211+
const ARRAY_SYNTAX_SHORT = 3;
212+
// END AST FLAG CONSTANTS
213+
214+
namespace ast;
215+
use ASTConverter\ASTConverter;
216+
217+
if (!class_exists('\ast\Node')) {
218+
/**
219+
* This class describes a single node in a PHP AST.
220+
* @suppress PhanRedefineClassInternal
221+
*/
222+
class Node
223+
{
224+
/** @var int AST Node Kind. Values are one of ast\AST_* constants. */
225+
public $kind;
226+
227+
/**
228+
* @var int AST Flags.
229+
* Certain node kinds have flags that can be set.
230+
* These will be a bitfield of ast\flags\* constants.
231+
*/
232+
public $flags;
233+
234+
/** @var int Line the node starts in */
235+
public $lineno;
236+
237+
/** @var array Child nodes (may be empty) */
238+
public $children;
239+
/**
240+
* A constructor which validates data types but not the values themselves.
241+
* For backwards compatibility reasons, all values are optional and properties default to null
242+
*/
243+
public function __construct(int $kind = null, int $flags = null, array $children = null, int $lineno = null) {
244+
$this->kind = $kind;
245+
$this->flags = $flags;
246+
$this->children = $children;
247+
$this->lineno = $lineno;
248+
}
249+
}
250+
}
251+
252+
namespace ast\Node;
253+
254+
if (!class_exists('\ast\Node\Decl')) {
255+
/**
256+
* AST Node type for function and class declarations.
257+
* @suppress PhanRedefineClassInternal
258+
*/
259+
class Decl extends \ast\Node
260+
{
261+
/** @var int End line number of the declaration */
262+
public $endLineno;
263+
264+
/** @var string Name of the function or class (not including the namespace prefix) */
265+
public $name;
266+
267+
/** @var string|null Doc comment preceeding the declaration. null if no doc comment was used. */
268+
public $docComment;
269+
}
270+
}

0 commit comments

Comments
 (0)