From 3b3df3ce1bee4caa29d91e9862bd565e151b9ee5 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sat, 16 May 2020 12:48:46 +0200 Subject: [PATCH] Add multiline_interpolation option --- src/Phug/Lexer/Lexer.php | 2 ++ .../Lexer/Scanner/InterpolationScanner.php | 18 ++++++++++++------ .../Phug/Lexer/Scanner/CommentScannerTest.php | 16 ++++++++++------ tests/Phug/Lexer/Scanner/FilterScannerTest.php | 6 ++++-- .../Lexer/Scanner/InterpolationScannerTest.php | 5 +++++ tests/Phug/Lexer/Scanner/MarkupScannerTest.php | 8 +++++--- .../Lexer/Scanner/TextBlockScannerTest.php | 4 ++-- tests/Phug/Lexer/Scanner/TextScannerTest.php | 2 ++ 8 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/Phug/Lexer/Lexer.php b/src/Phug/Lexer/Lexer.php index a603b296..dd32a569 100644 --- a/src/Phug/Lexer/Lexer.php +++ b/src/Phug/Lexer/Lexer.php @@ -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' => [], @@ -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'), diff --git a/src/Phug/Lexer/Lexer/Scanner/InterpolationScanner.php b/src/Phug/Lexer/Lexer/Scanner/InterpolationScanner.php index c57c9adc..0bc737c2 100644 --- a/src/Phug/Lexer/Lexer/Scanner/InterpolationScanner.php +++ b/src/Phug/Lexer/Lexer/Scanner/InterpolationScanner.php @@ -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 */ @@ -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; } @@ -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); diff --git a/tests/Phug/Lexer/Scanner/CommentScannerTest.php b/tests/Phug/Lexer/Scanner/CommentScannerTest.php index 7851e04f..5153b635 100644 --- a/tests/Phug/Lexer/Scanner/CommentScannerTest.php +++ b/tests/Phug/Lexer/Scanner/CommentScannerTest.php @@ -19,9 +19,11 @@ class CommentScannerTest extends AbstractLexerTest * @covers \Phug\Lexer\Scanner\CommentScanner * @covers \Phug\Lexer\Scanner\CommentScanner::scan * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: + * @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() { @@ -42,9 +44,11 @@ public function testVisibleSingleLineComment() * @covers \Phug\Lexer\Scanner\CommentScanner * @covers \Phug\Lexer\Scanner\CommentScanner::scan * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: + * @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() { @@ -67,7 +71,7 @@ public function testInvisibleSingleLineComment() * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: * @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() { @@ -93,7 +97,7 @@ public function testVisibleMultiLineComment() * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: * @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() { @@ -119,7 +123,7 @@ public function testInvisibleMultiLineComment() * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: * @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() { @@ -148,7 +152,7 @@ public function testCommentInIndent() * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: * @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() { diff --git a/tests/Phug/Lexer/Scanner/FilterScannerTest.php b/tests/Phug/Lexer/Scanner/FilterScannerTest.php index 8a60c6a6..eca6134c 100644 --- a/tests/Phug/Lexer/Scanner/FilterScannerTest.php +++ b/tests/Phug/Lexer/Scanner/FilterScannerTest.php @@ -22,9 +22,11 @@ class FilterScannerTest extends AbstractLexerTest * @covers \Phug\Lexer\Scanner\FilterScanner * @covers \Phug\Lexer\Scanner\FilterScanner::scan * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: + * @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() { @@ -106,7 +108,7 @@ public function testFilter() * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: * @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() { diff --git a/tests/Phug/Lexer/Scanner/InterpolationScannerTest.php b/tests/Phug/Lexer/Scanner/InterpolationScannerTest.php index fbe39d01..2a27d0a9 100644 --- a/tests/Phug/Lexer/Scanner/InterpolationScannerTest.php +++ b/tests/Phug/Lexer/Scanner/InterpolationScannerTest.php @@ -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() @@ -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)); } diff --git a/tests/Phug/Lexer/Scanner/MarkupScannerTest.php b/tests/Phug/Lexer/Scanner/MarkupScannerTest.php index 17dc476d..e235c9e1 100644 --- a/tests/Phug/Lexer/Scanner/MarkupScannerTest.php +++ b/tests/Phug/Lexer/Scanner/MarkupScannerTest.php @@ -24,9 +24,11 @@ class MarkupScannerTest extends AbstractLexerTest * @covers \Phug\Lexer\Scanner\MarkupScanner * @covers \Phug\Lexer\Scanner\MarkupScanner::scan * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: + * @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() { @@ -127,7 +129,7 @@ public function testMarkupInCondition() * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: * @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() { @@ -275,7 +277,7 @@ public function testRawMarkup() * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: * @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() { diff --git a/tests/Phug/Lexer/Scanner/TextBlockScannerTest.php b/tests/Phug/Lexer/Scanner/TextBlockScannerTest.php index 5322daf7..1a3e583e 100644 --- a/tests/Phug/Lexer/Scanner/TextBlockScannerTest.php +++ b/tests/Phug/Lexer/Scanner/TextBlockScannerTest.php @@ -33,7 +33,7 @@ class TextBlockScannerTest extends AbstractLexerTest * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: * @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 @@ -176,7 +176,7 @@ public function testScan() * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: * @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() { diff --git a/tests/Phug/Lexer/Scanner/TextScannerTest.php b/tests/Phug/Lexer/Scanner/TextScannerTest.php index b6f56b15..bac5ee35 100644 --- a/tests/Phug/Lexer/Scanner/TextScannerTest.php +++ b/tests/Phug/Lexer/Scanner/TextScannerTest.php @@ -71,6 +71,8 @@ public function testTextQuit() /** * @covers \Phug\Lexer\Analyzer\LineAnalyzer:: + * @covers \Phug\Lexer\Analyzer\LineAnalyzer::hasChunksUntil + * @covers \Phug\Lexer\Analyzer\LineAnalyzer::setNewLevel */ public function testStartingWhitespace() {