Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2.9.1 #17

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

----

## v2.9.1 (2024-01-11)

- Inlined one in CoreParserTest.php because it was used only in one place
- Change IfStatement ast node to a better implementation

----

## v2.9.0 (2023-12-23)

- Simplified Lexer code
Expand Down
29 changes: 29 additions & 0 deletions src/Ast/Statements/ElseIfStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Serhii\GoodbyeHtml\Ast\Statements;

use Serhii\GoodbyeHtml\Ast\Expressions\Expression;
use Serhii\GoodbyeHtml\Token\Token;

readonly class ElseIfStatement implements Statement
{
public function __construct(
public Token $token,
public Expression $condition,
public BlockStatement $block,
) {
}

public function tokenLiteral(): string
{
return $this->token->literal;
}

public function string(): string
{
$result = sprintf("{{ else if %s }}", $this->condition->string());
return $result . $this->block->string();
}
}
7 changes: 3 additions & 4 deletions src/Ast/Statements/IfStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
readonly class IfStatement implements Statement
{
/**
* @param list<IfStatement> $elseIfBlocks
* @param list<ElseIfStatement> $elseIfBlocks
*/
public function __construct(
public Token $token,
Expand All @@ -28,7 +28,7 @@ public function tokenLiteral(): string

public function string(): string
{
$result = sprintf("{{ %s %s }}\n", $this->tokenLiteral(), $this->condition->string());
$result = sprintf("{{ if %s }}", $this->condition->string());

$result .= $this->block->string();

Expand All @@ -37,8 +37,7 @@ public function string(): string
}

if ($this->elseBlock) {
$result .= "{{ else }}\n";

$result .= "{{ else }}";
$result .= $this->elseBlock->string();
}

Expand Down
4 changes: 2 additions & 2 deletions src/CoreParser/CoreParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Serhii\GoodbyeHtml\Ast\Literals\StringLiteral;
use Serhii\GoodbyeHtml\Ast\Statements\AssignStatement;
use Serhii\GoodbyeHtml\Ast\Statements\BlockStatement;
use Serhii\GoodbyeHtml\Ast\Statements\ElseIfStatement;
use Serhii\GoodbyeHtml\Ast\Statements\ExpressionStatement;
use Serhii\GoodbyeHtml\Ast\Statements\HtmlStatement;
use Serhii\GoodbyeHtml\Ast\Statements\IfStatement;
Expand Down Expand Up @@ -381,11 +382,10 @@ private function parseIfStatement(): IfStatement
$this->nextToken(); // skip "{{"
$this->nextToken(); // skip "elseif"

$elseIfBlocks[] = new IfStatement(
$elseIfBlocks[] = new ElseIfStatement(
token: $this->curToken,
condition: $this->parseExpression(Precedence::LOWEST),
block: $this->parseBlockStatement(),
elseBlock: null,
);
}

Expand Down
30 changes: 20 additions & 10 deletions tests/CoreParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Serhii\GoodbyeHtml\Ast\Statements\IfStatement;
use Serhii\GoodbyeHtml\Ast\Statements\LoopStatement;
use Serhii\GoodbyeHtml\Ast\Statements\Program;
use Serhii\GoodbyeHtml\Ast\Statements\Statement;
use Serhii\GoodbyeHtml\CoreParser\CoreParser;
use Serhii\GoodbyeHtml\CoreParser\ParserError;
use Serhii\GoodbyeHtml\Exceptions\CoreParserException;
Expand All @@ -38,19 +37,11 @@ private function createProgram(string $input): Program

$program = $parser->parseProgram();

$this->assertOneStatement($parser, $program->statements);
$this->assertCount(1, $program->statements, 'Program must contain 1 statements');

return $program;
}

/**
* @param Statement[] $stmt
*/
private function assertOneStatement(CoreParser $parser, array $stmt): void
{
$this->assertCount(1, $stmt, "Program must contain 1 statements");
}

private static function testVariable($var, string $val): void
{
self::assertInstanceOf(VariableExpression::class, $var);
Expand Down Expand Up @@ -430,4 +421,23 @@ public function testParsingAssignStatement(): void
$this->testString($stmt->value, 'Anna');
$this->assertSame("{{ \$herName = 'Anna' }}", $stmt->string());
}

public function testIfStatementIsPrintedCorrectly(): void
{
$input = <<<HTML
{{ if true }}
Is true
{{ else if false }}
Is false
{{ else if true }}
Is true again
{{ else }}
Else is here
{{ end }}
HTML;

$expect = $input . "\n";

$this->assertSame($expect, $this->createProgram($input)->string());
}
}