Skip to content
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
46 changes: 46 additions & 0 deletions src/DiagnosticsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,52 @@ public static function checkDiagnostics($node) {
}
}
}
else if ($node instanceof Node\Statement\BreakOrContinueStatement) {
if ($node->breakoutLevel === null) {
return null;
}

$breakoutLevel = $node->breakoutLevel;
while ($breakoutLevel instanceof Node\Expression\ParenthesizedExpression) {
$breakoutLevel = $breakoutLevel->expression;
}

if ($breakoutLevel instanceof Node\Expression\NumericLiteral) {
$literalString = $breakoutLevel->getText();
if (
$breakoutLevel->children->kind === TokenKind::BinaryLiteralToken
&& \bindec(\substr($literalString, 2)) > 0
) {
return null;
}
else if (
\in_array($breakoutLevel->children->kind, [
TokenKind::DecimalLiteralToken,
TokenKind::HexadecimalLiteralToken,
TokenKind::OctalLiteralToken,
TokenKind::IntegerLiteralToken
])
&& \intval($literalString, 0) > 0
) {
return null;
}
}

if ($breakoutLevel instanceof Token) {
$start = $breakoutLevel->getStartPosition();
}
else {
$start = $breakoutLevel->getStart();
}
$end = $breakoutLevel->getEndPosition();

return new Diagnostic(
DiagnosticKind::Error,
"Expected positive integer literal.",
$start,
$end - $start
);
}
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Node/Statement/BreakOrContinueStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class BreakOrContinueStatement extends StatementNode {
/** @var Token */
public $breakOrContinueKeyword;
/** @var Token|null */
/** @var Expression|null */
public $breakoutLevel;
/** @var Token */
public $semicolon;
Expand Down
18 changes: 4 additions & 14 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1865,20 +1865,10 @@ private function parseBreakOrContinueStatement($parentNode) {
$continueStatement->parent = $parentNode;
$continueStatement->breakOrContinueKeyword = $this->eat(TokenKind::ContinueKeyword, TokenKind::BreakKeyword);

// TODO this level of granularity is unnecessary - integer-literal should be sufficient
$continueStatement->breakoutLevel =
$this->eatOptional(
TokenKind::BinaryLiteralToken,
TokenKind::DecimalLiteralToken,
TokenKind::InvalidHexadecimalLiteral,
TokenKind::InvalidBinaryLiteral,
TokenKind::FloatingLiteralToken,
TokenKind::HexadecimalLiteralToken,
TokenKind::OctalLiteralToken,
TokenKind::InvalidOctalLiteralToken,
// TODO the parser should be permissive of floating literals, but rule validation should produce error
TokenKind::IntegerLiteralToken
);
if ($this->isExpressionStart($this->getCurrentToken())) {
$continueStatement->breakoutLevel = $this->parseExpression($continueStatement);
}

$continueStatement->semicolon = $this->eatSemicolonOrAbortStatement();

return $continueStatement;
Expand Down
5 changes: 5 additions & 0 deletions tests/cases/parser/breakStatement10.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

for (;;) {
break (1);
}
96 changes: 96 additions & 0 deletions tests/cases/parser/breakStatement10.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ForStatement": {
"for": {
"kind": "ForKeyword",
"textLength": 3
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"forInitializer": null,
"exprGroupSemicolon1": {
"kind": "SemicolonToken",
"textLength": 1
},
"forControl": null,
"exprGroupSemicolon2": {
"kind": "SemicolonToken",
"textLength": 1
},
"forEndOfLoop": null,
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
},
"colon": null,
"statements": {
"CompoundStatementNode": {
"openBrace": {
"kind": "OpenBraceToken",
"textLength": 1
},
"statements": [
{
"BreakOrContinueStatement": {
"breakOrContinueKeyword": {
"kind": "BreakKeyword",
"textLength": 5
},
"breakoutLevel": {
"ParenthesizedExpression": {
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"expression": {
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"closeBrace": {
"kind": "CloseBraceToken",
"textLength": 1
}
}
},
"endFor": null,
"endForSemicolon": null
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
8 changes: 6 additions & 2 deletions tests/cases/parser/breakStatement3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@
"textLength": 5
},
"breakoutLevel": {
"kind": "IntegerLiteralToken",
"textLength": 4
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 4
}
}
},
"semicolon": {
"kind": "SemicolonToken",
Expand Down
8 changes: 6 additions & 2 deletions tests/cases/parser/breakStatement4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@
"textLength": 5
},
"breakoutLevel": {
"kind": "IntegerLiteralToken",
"textLength": 2
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 2
}
}
},
"semicolon": {
"kind": "SemicolonToken",
Expand Down
8 changes: 6 additions & 2 deletions tests/cases/parser/breakStatement5.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@
"textLength": 5
},
"breakoutLevel": {
"kind": "FloatingLiteralToken",
"textLength": 3
"NumericLiteral": {
"children": {
"kind": "FloatingLiteralToken",
"textLength": 3
}
}
},
"semicolon": {
"kind": "SemicolonToken",
Expand Down
8 changes: 6 additions & 2 deletions tests/cases/parser/breakStatement6.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@
"textLength": 5
},
"breakoutLevel": {
"kind": "IntegerLiteralToken",
"textLength": 1
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
},
"semicolon": {
"kind": "SemicolonToken",
Expand Down
8 changes: 6 additions & 2 deletions tests/cases/parser/breakStatement8.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@
"textLength": 5
},
"breakoutLevel": {
"kind": "IntegerLiteralToken",
"textLength": 3
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 3
}
}
},
"semicolon": {
"kind": "SemicolonToken",
Expand Down
8 changes: 6 additions & 2 deletions tests/cases/parser/breakStatement9.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@
"textLength": 5
},
"breakoutLevel": {
"kind": "IntegerLiteralToken",
"textLength": 3
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 3
}
}
},
"semicolon": {
"error": "MissingToken",
Expand Down
5 changes: 5 additions & 0 deletions tests/cases/parser/continueStatement10.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

for (;;) {
continue (1);
}
96 changes: 96 additions & 0 deletions tests/cases/parser/continueStatement10.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ForStatement": {
"for": {
"kind": "ForKeyword",
"textLength": 3
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"forInitializer": null,
"exprGroupSemicolon1": {
"kind": "SemicolonToken",
"textLength": 1
},
"forControl": null,
"exprGroupSemicolon2": {
"kind": "SemicolonToken",
"textLength": 1
},
"forEndOfLoop": null,
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
},
"colon": null,
"statements": {
"CompoundStatementNode": {
"openBrace": {
"kind": "OpenBraceToken",
"textLength": 1
},
"statements": [
{
"BreakOrContinueStatement": {
"breakOrContinueKeyword": {
"kind": "ContinueKeyword",
"textLength": 8
},
"breakoutLevel": {
"ParenthesizedExpression": {
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"expression": {
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"closeBrace": {
"kind": "CloseBraceToken",
"textLength": 1
}
}
},
"endFor": null,
"endForSemicolon": null
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
Loading