Skip to content

Commit

Permalink
Add multiline_interpolation option
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed May 16, 2020
1 parent e6cdbe9 commit 3b3df3c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/Phug/Lexer/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function __construct($options = null)
'indent_style' => null,
'indent_width' => null,
'allow_mixed_indent' => true,
'multiline_interpolation' => true,
'multiline_markup_enabled' => true,
'encoding' => null,
'lexer_modules' => [],
Expand Down Expand Up @@ -216,6 +217,7 @@ public function lex($input, $path = null)
'indent_style' => $this->getOption('indent_style'),
'indent_width' => $this->getOption('indent_width'),
'allow_mixed_indent' => $this->getOption('allow_mixed_indent'),
'multiline_interpolation' => $this->getOption('multiline_interpolation'),
'multiline_markup_enabled' => $this->getOption('multiline_markup_enabled'),
'level' => $this->getOption('level'),
'mixin_keyword' => $this->getRegExpOption('mixin_keyword'),
Expand Down
18 changes: 12 additions & 6 deletions src/Phug/Lexer/Lexer/Scanner/InterpolationScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@

class InterpolationScanner implements ScannerInterface
{
protected function throwEndOfLineExceptionIf(State $state, $condition)
{
if ($condition) {
$state->throwException('End of line was reached with no closing bracket for interpolation.');
}
}

protected function scanTagInterpolation(State $state, $tagInterpolation)
{
/** @var TagInterpolationStartToken $start */
Expand All @@ -33,9 +40,7 @@ protected function scanTagInterpolation(State $state, $tagInterpolation)
yield $start;

foreach ($lexer->lex($tagInterpolation) as $token) {
if ($token instanceof NewLineToken) {
$state->throwException('End of line was reached with no closing bracket for interpolation.');
}
$this->throwEndOfLineExceptionIf($state, $token instanceof NewLineToken);

yield $token;
}
Expand Down Expand Up @@ -68,9 +73,10 @@ protected function scanExpressionInterpolation(State $state, $interpolation, $es

protected function scanInterpolation(State $state, $tagInterpolation, $interpolation, $escape)
{
if (strpos($interpolation, "\n") !== false) {
$state->throwException('End of line was reached with no closing bracket for interpolation.');
}
$this->throwEndOfLineExceptionIf(
$state,
!$state->getOption('multiline_interpolation') && strpos($interpolation, "\n") !== false
);

if ($tagInterpolation) {
return $this->scanTagInterpolation($state, $tagInterpolation);
Expand Down
16 changes: 10 additions & 6 deletions tests/Phug/Lexer/Scanner/CommentScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class CommentScannerTest extends AbstractLexerTest
* @covers \Phug\Lexer\Scanner\CommentScanner
* @covers \Phug\Lexer\Scanner\CommentScanner::scan
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::hasChunksUntil
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::setNewLevel
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testVisibleSingleLineComment()
{
Expand All @@ -42,9 +44,11 @@ public function testVisibleSingleLineComment()
* @covers \Phug\Lexer\Scanner\CommentScanner
* @covers \Phug\Lexer\Scanner\CommentScanner::scan
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::hasChunksUntil
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::setNewLevel
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testInvisibleSingleLineComment()
{
Expand All @@ -67,7 +71,7 @@ public function testInvisibleSingleLineComment()
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testVisibleMultiLineComment()
{
Expand All @@ -93,7 +97,7 @@ public function testVisibleMultiLineComment()
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testInvisibleMultiLineComment()
{
Expand All @@ -119,7 +123,7 @@ public function testInvisibleMultiLineComment()
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testCommentInIndent()
{
Expand Down Expand Up @@ -148,7 +152,7 @@ public function testCommentInIndent()
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testCommentQuit()
{
Expand Down
6 changes: 4 additions & 2 deletions tests/Phug/Lexer/Scanner/FilterScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ class FilterScannerTest extends AbstractLexerTest
* @covers \Phug\Lexer\Scanner\FilterScanner
* @covers \Phug\Lexer\Scanner\FilterScanner::scan
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::hasChunksUntil
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::setNewLevel
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testFilter()
{
Expand Down Expand Up @@ -106,7 +108,7 @@ public function testFilter()
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testStylusFilter()
{
Expand Down
5 changes: 5 additions & 0 deletions tests/Phug/Lexer/Scanner/InterpolationScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public function testTokenInLineAnalyzer()
}

/**
* @covers \Phug\Lexer\Scanner\InterpolationScanner::scanTagInterpolation
* @covers \Phug\Lexer\Scanner\InterpolationScanner::throwEndOfLineExceptionIf
* @covers \Phug\Lexer\Scanner\InterpolationScanner::scanInterpolation
*/
public function testNewLineInTagInterpolation()
Expand All @@ -194,12 +196,15 @@ public function testNewLineInTagInterpolation()
}

/**
* @covers \Phug\Lexer\Scanner\InterpolationScanner::scanExpressionInterpolation
* @covers \Phug\Lexer\Scanner\InterpolationScanner::throwEndOfLineExceptionIf
* @covers \Phug\Lexer\Scanner\InterpolationScanner::scanInterpolation
*/
public function testNewLineInInterpolation()
{
$this->expectMessageToBeThrown('End of line was reached with no closing bracket for interpolation.');

$this->lexer->setOption('multiline_interpolation', false);
$input = "p #{em\n}";
iterator_to_array($this->lexer->lex($input));
}
Expand Down
8 changes: 5 additions & 3 deletions tests/Phug/Lexer/Scanner/MarkupScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class MarkupScannerTest extends AbstractLexerTest
* @covers \Phug\Lexer\Scanner\MarkupScanner
* @covers \Phug\Lexer\Scanner\MarkupScanner::scan
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::hasChunksUntil
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::setNewLevel
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testMarkupInCondition()
{
Expand Down Expand Up @@ -127,7 +129,7 @@ public function testMarkupInCondition()
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testRawMarkup()
{
Expand Down Expand Up @@ -275,7 +277,7 @@ public function testRawMarkup()
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testRawMarkupQuit()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Phug/Lexer/Scanner/TextBlockScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TextBlockScannerTest extends AbstractLexerTest
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
* @covers \Phug\Lexer\Scanner\InterpolationScanner
* @covers \Phug\Lexer\Scanner\InterpolationScanner::scanInterpolation
* @covers \Phug\Lexer\Scanner\InterpolationScanner::scan
Expand Down Expand Up @@ -176,7 +176,7 @@ public function testScan()
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::recordLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLine
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::getLineChunks
*/
public function testScanWhiteSpaces()
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Phug/Lexer/Scanner/TextScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public function testTextQuit()

/**
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::<public>
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::hasChunksUntil
* @covers \Phug\Lexer\Analyzer\LineAnalyzer::setNewLevel
*/
public function testStartingWhitespace()
{
Expand Down

0 comments on commit 3b3df3c

Please sign in to comment.