Skip to content

Commit

Permalink
TemplateParser: refactoring, creates lexer in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 19, 2023
1 parent 8d1795d commit 6086b97
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
19 changes: 12 additions & 7 deletions src/Latte/Compiler/TemplateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/Latte/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}


Expand Down
3 changes: 1 addition & 2 deletions tests/common/TemplateLexer.setContentType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
3 changes: 1 addition & 2 deletions tests/common/TemplateLexer.tokenize.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
3 changes: 1 addition & 2 deletions tests/common/TemplateParser.errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
3 changes: 1 addition & 2 deletions tests/common/TemplateParser.nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ 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;
yield;
return $node;
}]);

$node = $parser->parse($s, $lexer);
$node = $parser->parse($s);
return exportNode($node);
}

Expand Down

0 comments on commit 6086b97

Please sign in to comment.