From 6086b97b958f2073442ac2a1fe964bbc54675e25 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 19 Sep 2023 18:30:31 +0200 Subject: [PATCH] TemplateParser: refactoring, creates lexer in constructor --- src/Latte/Compiler/TemplateParser.php | 19 ++++++++++++------- src/Latte/Engine.php | 3 +-- .../common/TemplateLexer.setContentType.phpt | 3 +-- tests/common/TemplateLexer.tokenize.phpt | 3 +-- tests/common/TemplateParser.errors.phpt | 3 +-- tests/common/TemplateParser.nodes.phpt | 3 +-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Latte/Compiler/TemplateParser.php b/src/Latte/Compiler/TemplateParser.php index b10335b20..78a29a41f 100644 --- a/src/Latte/Compiler/TemplateParser.php +++ b/src/Latte/Compiler/TemplateParser.php @@ -38,24 +38,29 @@ final class TemplateParser private TemplateParserHtml $html; private ?TokenStream $stream = null; - private ?TemplateLexer $lexer = null; + private TemplateLexer $lexer; private ?Policy $policy = null; - private string $contentType = ContentType::Html; + private string $contentType; private int $counter = 0; private ?Tag $tag = null; private $lastResolver; + public function __construct() + { + $this->lexer = new TemplateLexer; + $this->setContentType(ContentType::Html); + } + + /** * Parses tokens to nodes. * @throws CompileException */ - public function parse(string $template, TemplateLexer $lexer): Nodes\TemplateNode + public function parse(string $template): Nodes\TemplateNode { - $this->lexer = $lexer; - $this->setContentType($this->contentType); $this->html = new TemplateParserHtml($this, $this->completeAttrParsers()); - $this->stream = new TokenStream($lexer->tokenize($template)); + $this->stream = new TokenStream($this->lexer->tokenize($template)); $headLength = 0; $findLength = function (FragmentNode $fragment) use (&$headLength) { @@ -391,7 +396,7 @@ public function setPolicy(?Policy $policy): static public function setContentType(string $type): static { $this->contentType = $type; - $this->lexer?->setState($type === ContentType::Html || $type === ContentType::Xml + $this->lexer->setState($type === ContentType::Html || $type === ContentType::Xml ? TemplateLexer::StateHtmlText : TemplateLexer::StatePlain); return $this; diff --git a/src/Latte/Engine.php b/src/Latte/Engine.php index 3d6683911..239001799 100644 --- a/src/Latte/Engine.php +++ b/src/Latte/Engine.php @@ -146,7 +146,6 @@ public function compile(string $name): string */ public function parse(string $source): TemplateNode { - $lexer = new Compiler\TemplateLexer; $parser = new Compiler\TemplateParser; $parser->strict = $this->strictParsing; @@ -158,7 +157,7 @@ public function parse(string $source): TemplateNode return $parser ->setContentType($this->contentType) ->setPolicy($this->getPolicy(effective: true)) - ->parse($source, $lexer); + ->parse($source); } diff --git a/tests/common/TemplateLexer.setContentType.phpt b/tests/common/TemplateLexer.setContentType.phpt index d2d840ca3..92e2497d8 100644 --- a/tests/common/TemplateLexer.setContentType.phpt +++ b/tests/common/TemplateLexer.setContentType.phpt @@ -12,10 +12,9 @@ require __DIR__ . '/../bootstrap.php'; function tokenize($s, $contentType = null) { $parser = new Latte\Compiler\TemplateParser; - $lexer = new Latte\Compiler\TemplateLexer; $parser->addTags((new Latte\Essential\CoreExtension)->getTags()); $parser->setContentType($contentType ?? ContentType::Html); - $parser->parse($s, $lexer); + $parser->parse($s); $tokens = Assert::with($parser->getStream(), fn() => $this->tokens); return array_map( fn(Token $token) => [$token->type, $token->text, $token->position->line . ':' . $token->position->column], diff --git a/tests/common/TemplateLexer.tokenize.phpt b/tests/common/TemplateLexer.tokenize.phpt index c26489980..8ea40e500 100644 --- a/tests/common/TemplateLexer.tokenize.phpt +++ b/tests/common/TemplateLexer.tokenize.phpt @@ -11,9 +11,8 @@ require __DIR__ . '/../bootstrap.php'; function tokenize($s) { $parser = new Latte\Compiler\TemplateParser; - $lexer = new Latte\Compiler\TemplateLexer; $parser->addTags((new Latte\Essential\CoreExtension)->getTags()); - $parser->parse($s, $lexer); + $parser->parse($s); $tokens = Assert::with($parser->getStream(), fn() => $this->tokens); return array_map( fn(Token $token) => [$token->type, $token->text, $token->position->line . ':' . $token->position->column], diff --git a/tests/common/TemplateParser.errors.phpt b/tests/common/TemplateParser.errors.phpt index 96f7d67c4..67756c1c5 100644 --- a/tests/common/TemplateParser.errors.phpt +++ b/tests/common/TemplateParser.errors.phpt @@ -10,10 +10,9 @@ require __DIR__ . '/../bootstrap.php'; function parse($s, $tag) { - $lexer = new Latte\Compiler\TemplateLexer; $parser = new Latte\Compiler\TemplateParser; $parser->addTags(['foo' => $tag]); - $node = $parser->parse($s, $lexer); + $parser->parse($s); } diff --git a/tests/common/TemplateParser.nodes.phpt b/tests/common/TemplateParser.nodes.phpt index 404f4597b..e847f3246 100644 --- a/tests/common/TemplateParser.nodes.phpt +++ b/tests/common/TemplateParser.nodes.phpt @@ -24,7 +24,6 @@ class FooNode extends Latte\Compiler\Nodes\AreaNode function parse($s) { - $lexer = new Latte\Compiler\TemplateLexer; $parser = new Latte\Compiler\TemplateParser; $parser->addTags(['foo' => function () { $node = new FooNode; @@ -32,7 +31,7 @@ function parse($s) return $node; }]); - $node = $parser->parse($s, $lexer); + $node = $parser->parse($s); return exportNode($node); }