Support hashbang before namespace declaration

Fixes issue #243.
This commit is contained in:
Nikita Popov 2016-01-15 22:01:51 +01:00
parent 196f177cfe
commit eb73441032
2 changed files with 37 additions and 3 deletions

View File

@ -444,7 +444,7 @@ abstract class ParserAbstract
private function getNamespacingStyle(array $stmts) {
$style = null;
$hasNotAllowedStmts = false;
foreach ($stmts as $stmt) {
foreach ($stmts as $i => $stmt) {
if ($stmt instanceof Node\Stmt\Namespace_) {
$currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace';
if (null === $style) {
@ -455,9 +455,21 @@ abstract class ParserAbstract
} elseif ($style !== $currentStyle) {
throw new Error('Cannot mix bracketed namespace declarations with unbracketed namespace declarations', $stmt->getLine());
}
} elseif (!$stmt instanceof Node\Stmt\Declare_ && !$stmt instanceof Node\Stmt\HaltCompiler) {
$hasNotAllowedStmts = true;
continue;
}
/* declare() and __halt_compiler() can be used before a namespace declaration */
if ($stmt instanceof Node\Stmt\Declare_ || $stmt instanceof Node\Stmt\HaltCompiler) {
continue;
}
/* There may be a hashbang line at the very start of the file */
if ($i == 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) {
continue;
}
/* Everything else if forbidden before namespace declarations */
$hasNotAllowedStmts = true;
}
return $style;
}

View File

@ -0,0 +1,22 @@
Hashbang followed by namespace declaration
-----
#!/usr/bin/env php
<?php
namespace A;
-----
array(
0: Stmt_InlineHTML(
value: #!/usr/bin/env php
)
1: Stmt_Namespace(
name: Name(
parts: array(
0: A
)
)
stmts: array(
)
)
)