Skip to content

Commit

Permalink
Merge pull request #4882 from mhsdesign/bugfix/fusionParserCommentLexing
Browse files Browse the repository at this point in the history
BUGFIX: Fusion parser fix multi line comment
  • Loading branch information
mhsdesign authored Feb 11, 2024
2 parents 4b137ed + 196e600 commit 92bb4e2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public function linePrint(int $offset = 0): string

public function char(int $index = 0): string
{
if ($index < 0) {
return mb_substr($this->linePart, $index, 1);
if ($index < 0 && mb_strlen($this->linePart) < abs($index)) {
// prevent mb_substr returning first char if out of bounds
return '';
}
return mb_substr($this->linePart, $index, $index + 1);
return mb_substr($this->linePart, $index, 1);
}

public function charPrint(int $index = 0): string
Expand Down
10 changes: 6 additions & 4 deletions Neos.Fusion/Classes/Core/ObjectTreeParser/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ class Lexer
Token::MULTILINE_COMMENT => <<<'REGEX'
`^
/\* # start of a comment '/*'
[^*]* # match everything until special case '*'
[^*]* # consume until special case '*'
\*+ # consume all '*'
(?:
\*[^/] # if after the '*' there is a '/' break, else continue
[^*]* # until the special case '*' is encountered - unrolled loop following Jeffrey Friedl
[^/] # break if its the end: '/'
[^*]* # unrolled loop following Jeffrey E.F. Friedl
\*+
)*
\*/ # the end of a comment.
/ # the end of a comment.
`x
REGEX,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ comment with // ane more comment
Here comes some comment with # and /* and // in it
*/

/**
* php doc style comment
*/

/***
comment with multiple stars uneven
***/

// another edge-case mentioned in NEOS-864
/**
comment with multiple stars even
**/

// another edge-case mentioned in NEOS-864 (no new line at the end)
#include: Pages/**/*.fusion
28 changes: 28 additions & 0 deletions Neos.Fusion/Tests/Unit/Core/Parser/ParserExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* source code.
*/

use Neos\Fusion\Core\ObjectTreeParser\ExceptionMessage\MessageLinePart;
use Neos\Fusion\Core\Parser;
use Neos\Fusion\Core\Cache\ParserCache;
use Neos\Fusion\Core\ObjectTreeParser\Exception\ParserException;
Expand Down Expand Up @@ -195,6 +196,11 @@ public function unclosedStatements(): \Generator
'Unclosed comment.'
];

yield 'unclosed multiline comment with multiple stars' => [
'/***',
'Unclosed comment.'
];

yield 'unclosed eel expression' => [
'a = ${',
'Unclosed eel expression.'
Expand Down Expand Up @@ -286,4 +292,26 @@ public function itMatchesThePartialExceptionMessage($fusion, $expectedMessage):
self::assertSame($expectedMessage, $e->getHelperMessagePart());
}
}

/**
* @test
*/
public function messageLinePartWorks()
{
$part = new MessageLinePart('abcd');

self::assertSame('', $part->char(-5));
self::assertSame('a', $part->char(-4));
self::assertSame('b', $part->char(-3));
self::assertSame('c', $part->char(-2));
self::assertSame('d', $part->char(-1));
self::assertSame('a', $part->char());
self::assertSame('a', $part->char(0));
self::assertSame('b', $part->char(1));
self::assertSame('c', $part->char(2));
self::assertSame('d', $part->char(3));
self::assertSame('', $part->char(4));
self::assertSame('abcd', $part->line());
self::assertSame('bcd', $part->line(1));
}
}

0 comments on commit 92bb4e2

Please sign in to comment.