Skip to content

Commit 592836c

Browse files
committed
Add support for T_HASHBANG (HHVM)
1 parent 68defc2 commit 592836c

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

lib/PhpParser/Lexer.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,23 @@ protected function createTokenMap() {
258258
// 256 is the minimum possible token number, as everything below
259259
// it is an ASCII value
260260
for ($i = 256; $i < 1000; ++$i) {
261-
// T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
262261
if (T_DOUBLE_COLON === $i) {
262+
// T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
263263
$tokenMap[$i] = Parser::T_PAAMAYIM_NEKUDOTAYIM;
264-
// T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
265264
} elseif(T_OPEN_TAG_WITH_ECHO === $i) {
265+
// T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
266266
$tokenMap[$i] = Parser::T_ECHO;
267-
// T_CLOSE_TAG is equivalent to ';'
268267
} elseif(T_CLOSE_TAG === $i) {
268+
// T_CLOSE_TAG is equivalent to ';'
269269
$tokenMap[$i] = ord(';');
270-
// and the others can be mapped directly
271-
} elseif ('UNKNOWN' !== ($name = token_name($i))
272-
&& defined($name = 'PhpParser\Parser::' . $name)
273-
) {
274-
$tokenMap[$i] = constant($name);
270+
} elseif ('UNKNOWN' !== $name = token_name($i)) {
271+
if ('T_HASHBANG' === $name) {
272+
// HHVM uses a special token for #! hashbang lines
273+
$tokenMap[$i] = Parser::T_INLINE_HTML;
274+
} else if (defined($name = 'PhpParser\Parser::' . $name)) {
275+
// Other tokens can be mapped directly
276+
$tokenMap[$i] = constant($name);
277+
}
275278
}
276279
}
277280

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Hashbang line
2+
-----
3+
#!/usr/bin/env php
4+
<?php
5+
6+
echo "foobar";
7+
8+
?>
9+
#!/usr/bin/env php
10+
-----
11+
array(
12+
0: Stmt_InlineHTML(
13+
value: #!/usr/bin/env php
14+
15+
)
16+
1: Stmt_Echo(
17+
exprs: array(
18+
0: Scalar_String(
19+
value: foobar
20+
)
21+
)
22+
)
23+
2: Stmt_InlineHTML(
24+
value: #!/usr/bin/env php
25+
)
26+
)

0 commit comments

Comments
 (0)