Skip to content

Support __halt_compiler statement #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ matrix:
- php: 7.2
env: STATIC_ANALYSIS=true
fast_finish: true
allow_failures:
- env: VALIDATION=true

cache:
directories:
Expand Down
36 changes: 36 additions & 0 deletions src/Node/Statement/HaltCompilerStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

namespace Microsoft\PhpParser\Node\Statement;

use Microsoft\PhpParser\Node\Expression;
use Microsoft\PhpParser\Token;

class HaltCompilerStatement extends Expression {

/** @var Token */
public $haltCompilerKeyword;

/** @var Token */
public $openParen;

/** @var Token */
public $closeParen;

/** @var Token */
public $semicolon;

/** @var Token */
public $data;

const CHILD_NAMES = [
'haltCompilerKeyword',
'openParen',
'closeParen',
'semicolon',
'data',
];
}
25 changes: 25 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
ForStatement,
FunctionDeclaration,
GotoStatement,
HaltCompilerStatement,
IfStatementNode,
InlineHtml,
InterfaceDeclaration,
Expand Down Expand Up @@ -615,6 +616,15 @@ private function parseStatementFn() {

case TokenKind::UnsetKeyword:
return $this->parseUnsetStatement($parentNode);

case TokenKind::HaltCompilerKeyword:
if ($parentNode instanceof SourceFileNode) {
return $this->parseHaltCompilerStatement($parentNode);
}
// __halt_compiler is a fatal compile error anywhere other than the top level.
// It won't be seen elsewhere in other programs - warn about the token being unexpected.
$this->advanceToken();
return new SkippedToken($token);
}

$expressionStatement = new ExpressionStatement();
Expand Down Expand Up @@ -1141,6 +1151,9 @@ private function isStatementStart(Token $token) {

// attributes
case TokenKind::AttributeToken:

// __halt_compiler
case TokenKind::HaltCompilerKeyword:
return true;

default:
Expand Down Expand Up @@ -2844,6 +2857,18 @@ private function parseUnsetStatement($parentNode) {
return $unsetStatement;
}

private function parseHaltCompilerStatement($parentNode) {
$unsetStatement = new HaltCompilerStatement();
$unsetStatement->parent = $parentNode;

$unsetStatement->haltCompilerKeyword = $this->eat1(TokenKind::HaltCompilerKeyword);
$unsetStatement->openParen = $this->eat1(TokenKind::OpenParenToken);
$unsetStatement->closeParen = $this->eat1(TokenKind::CloseParenToken);
$unsetStatement->semicolon = $this->eatSemicolonOrAbortStatement();
$unsetStatement->data = $this->eatOptional1(TokenKind::InlineHtml);
return $unsetStatement;
}

private function parseArrayCreationExpression($parentNode) {
$arrayExpression = new ArrayCreationExpression();
$arrayExpression->parent = $parentNode;
Expand Down
2 changes: 1 addition & 1 deletion src/PhpTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ protected static function tokenGetAll(string $content, $parseContext): array
T_DIR => TokenKind::Name,
T_FILE => TokenKind::Name,
T_FUNC_C => TokenKind::Name,
T_HALT_COMPILER => TokenKind::Name,
T_METHOD_C => TokenKind::Name,
T_NS_C => TokenKind::Name,
T_TRAIT_C => TokenKind::Name,
Expand Down Expand Up @@ -274,6 +273,7 @@ protected static function tokenGetAll(string $content, $parseContext): array
T_FUNCTION => TokenKind::FunctionKeyword,
T_GLOBAL => TokenKind::GlobalKeyword,
T_GOTO => TokenKind::GotoKeyword,
T_HALT_COMPILER => TokenKind::HaltCompilerKeyword,
T_IF => TokenKind::IfKeyword,
T_IMPLEMENTS => TokenKind::ImplementsKeyword,
T_INCLUDE => TokenKind::IncludeKeyword,
Expand Down
1 change: 1 addition & 0 deletions src/TokenKind.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class TokenKind {
const IterableKeyword = self::IterableReservedWord;
const EnumKeyword = 171;
const ReadonlyKeyword = 172;
const HaltCompilerKeyword = 173;

const OpenBracketToken = 201;
const CloseBracketToken = 202;
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/haltCompiler1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
__halt_compiler();
1 change: 1 addition & 0 deletions tests/cases/parser/haltCompiler1.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
44 changes: 44 additions & 0 deletions tests/cases/parser/haltCompiler1.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"HaltCompilerStatement": {
"haltCompilerKeyword": {
"kind": "HaltCompilerKeyword",
"textLength": 15
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
},
"data": {
"kind": "InlineHtml",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
4 changes: 4 additions & 0 deletions tests/cases/parser/haltCompiler2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
if (true) {
__halt_compiler();
}
26 changes: 26 additions & 0 deletions tests/cases/parser/haltCompiler2.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"kind": 0,
"message": "Unexpected 'HaltCompilerKeyword'",
"start": 22,
"length": 15
},
{
"kind": 0,
"message": "'Expression' expected.",
"start": 38,
"length": 0
},
{
"kind": 0,
"message": "Unexpected 'InlineHtml'",
"start": 40,
"length": 3
},
{
"kind": 0,
"message": "'}' expected.",
"start": 43,
"length": 0
}
]
99 changes: 99 additions & 0 deletions tests/cases/parser/haltCompiler2.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"IfStatementNode": {
"ifKeyword": {
"kind": "IfKeyword",
"textLength": 2
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"expression": {
"ReservedWord": {
"children": {
"kind": "TrueReservedWord",
"textLength": 4
}
}
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
},
"colon": null,
"statements": {
"CompoundStatementNode": {
"openBrace": {
"kind": "OpenBraceToken",
"textLength": 1
},
"statements": [
{
"error": "SkippedToken",
"kind": "HaltCompilerKeyword",
"textLength": 15
},
{
"ExpressionStatement": {
"expression": {
"ParenthesizedExpression": {
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"expression": {
"error": "MissingToken",
"kind": "Expression",
"textLength": 0
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
},
{
"error": "SkippedToken",
"kind": "InlineHtml",
"textLength": 3
}
],
"closeBrace": {
"error": "MissingToken",
"kind": "CloseBraceToken",
"textLength": 0
}
}
},
"elseIfClauses": [],
"elseClause": null,
"endifKeyword": null,
"semicolon": null
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
3 changes: 3 additions & 0 deletions tests/cases/parser/haltCompiler3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// Only allowed in compound statements.
if (true) __halt_compiler();
14 changes: 14 additions & 0 deletions tests/cases/parser/haltCompiler3.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"kind": 0,
"message": "Unexpected 'HaltCompilerKeyword'",
"start": 56,
"length": 15
},
{
"kind": 0,
"message": "'Expression' expected.",
"start": 72,
"length": 0
}
]
Loading