Skip to content
Closed
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
8 changes: 6 additions & 2 deletions src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,16 @@ public function parseCharacter(bool $isForIdentifier): ?string
* This method may change the state of the object by advancing the internal position;
* it does not simply 'get' a value.
*/
public function consumeWhiteSpace(array &$comments = []): string
public function consumeWhiteSpace(array &$comments = [], bool $stopAfterLineFeed = false): string
{
$consumed = '';
do {
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
$consumed .= $this->consume(1);
$characterComsumed = $this->consume(1);
$consumed .= $characterComsumed;
if ($stopAfterLineFeed && $characterComsumed === "\n") {
break 2;
}
}
if ($this->parserSettings->usesLenientParsing()) {
try {
Expand Down
63 changes: 63 additions & 0 deletions tests/Unit/Parsing/ParserStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,67 @@ public function consumeWhiteSpaceStopsAtNonWhitespace(string $nonWhitespace, str

self::assertTrue($subject->comes($nonWhitespace));
}

/**
* @return array<non-empty-string, array{0: string}>
*/
public static function providePossibleWhitespaceWithoutLineFeedPossiblyIncludingComments(): array
{
return [
'nothing' => [''],
'space' => [' '],
'tab' => ["\t"],
'carriage return' => ["\r"], // inclusion of this is debatable, but Mac line endings are not generally seen
'two spaces' => [' '],
'comment' => ['/* Time for bed */'],
'comment with spacing around' => [' /* I am so tired */ '],
];
}

/**
* @return DataProvider<non-empty-string, array{0: string, 1: string}>
*/
public function providePossibleWhitespaceWithoutLineFeedPossiblyIncludingCommentsAndTheSameAgain(): DataProvider
{
return DataProvider::cross(
self::providePossibleWhitespaceWithoutLineFeedPossiblyIncludingComments(),
self::providePossibleWhitespaceWithoutLineFeedPossiblyIncludingComments()
);
}

/**
* @test
*
* @dataProvider providePossibleWhitespaceWithoutLineFeedPossiblyIncludingCommentsAndTheSameAgain
*/
public function consumeWhiteSpaceStopsAfterLineFeedWhenRequested(string $firstLine, string $secondLine): void
{
$subject = new ParserState($firstLine . "\n" . $secondLine, Settings::create());

$comments = [];
$subject->consumeWhiteSpace($comments, true);

if ($secondLine === '') {
self::assertTrue($subject->isEnd());
} else {
self::assertTrue($subject->comes($secondLine));
}
}

/**
* @test
*
* @dataProvider providePossibleWhitespaceWithoutLineFeedPossiblyIncludingCommentsAndTheSameAgain
*/
public function consumeWhiteSpaceDoesNotStopAfterLineFeedWhenNotRequested(
string $firstLine,
string $secondLine
): void {
$subject = new ParserState($firstLine . "\n" . $secondLine, Settings::create());

$comments = [];
$subject->consumeWhiteSpace($comments, false);

self::assertTrue($subject->isEnd());
}
}