Skip to content

Commit

Permalink
Add support for T_HASHBANG (HHVM)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Mar 14, 2015
1 parent 68defc2 commit 592836c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/PhpParser/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,20 +258,23 @@ protected function createTokenMap() {
// 256 is the minimum possible token number, as everything below
// it is an ASCII value
for ($i = 256; $i < 1000; ++$i) {
// T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
if (T_DOUBLE_COLON === $i) {
// T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
$tokenMap[$i] = Parser::T_PAAMAYIM_NEKUDOTAYIM;
// T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
} elseif(T_OPEN_TAG_WITH_ECHO === $i) {
// T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
$tokenMap[$i] = Parser::T_ECHO;
// T_CLOSE_TAG is equivalent to ';'
} elseif(T_CLOSE_TAG === $i) {
// T_CLOSE_TAG is equivalent to ';'
$tokenMap[$i] = ord(';');
// and the others can be mapped directly
} elseif ('UNKNOWN' !== ($name = token_name($i))
&& defined($name = 'PhpParser\Parser::' . $name)
) {
$tokenMap[$i] = constant($name);
} elseif ('UNKNOWN' !== $name = token_name($i)) {
if ('T_HASHBANG' === $name) {
// HHVM uses a special token for #! hashbang lines
$tokenMap[$i] = Parser::T_INLINE_HTML;
} else if (defined($name = 'PhpParser\Parser::' . $name)) {
// Other tokens can be mapped directly
$tokenMap[$i] = constant($name);
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions test/code/parser/stmt/hashbang.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Hashbang line
-----
#!/usr/bin/env php
<?php

echo "foobar";

?>
#!/usr/bin/env php
-----
array(
0: Stmt_InlineHTML(
value: #!/usr/bin/env php

)
1: Stmt_Echo(
exprs: array(
0: Scalar_String(
value: foobar
)
)
)
2: Stmt_InlineHTML(
value: #!/usr/bin/env php
)
)

0 comments on commit 592836c

Please sign in to comment.