Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Oct 28, 2024
2 parents fd7c109 + cd2ec3b commit 0e8e811
Show file tree
Hide file tree
Showing 7 changed files with 694 additions and 638 deletions.
1,077 changes: 576 additions & 501 deletions files/parser.php

Large diffs are not rendered by default.

18 changes: 6 additions & 12 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class Compiler
public const PHVOLT_MODE_RAW = 0;
public const PHVOLT_PARSING_FAILED = 0;
public const PHVOLT_PARSING_OK = 1;
public const PHVOLT_RAW_BUFFER_SIZE = 256;
public const PHVOLT_SCANNER_RETCODE_EOF = -1;
public const PHVOLT_SCANNER_RETCODE_ERR = -2;
public const PHVOLT_SCANNER_RETCODE_IMPOSSIBLE = -3;
Expand Down Expand Up @@ -1387,7 +1386,7 @@ public function compileSet(array $statement): string
*
* @throws Exception
*/
public function compileSource(string $viewCode, bool $extendsMode = false): string
public function compileSource(string $viewCode, bool $extendsMode = false): string|array
{
/**
* Enable autoescape globally
Expand Down Expand Up @@ -2808,7 +2807,7 @@ final protected function statementList(array $statements, bool $extendsMode = fa
* Block statement
*/
$blockName = $statement['name'];
$blockStatements = $statement["block_statements"];
$blockStatements = $statement["block_statements"] ?? [];
$blocks = $this->blocks;

if (true === $blockMode) {
Expand All @@ -2817,7 +2816,7 @@ final protected function statementList(array $statements, bool $extendsMode = fa
}

/**
* Create a unamed block
* Create an unnamed block.
*/
if ($compilation !== null) {
$blocks[] = $compilation;
Expand Down Expand Up @@ -2956,7 +2955,7 @@ final protected function statementList(array $statements, bool $extendsMode = fa

$this->level--;

return $compilation;
return $compilation === null ? '' : $compilation;
}

/**
Expand Down Expand Up @@ -3098,13 +3097,8 @@ private function uniquePathKey(?string $path = null): ?string

/**
* @see https://github.com/php/php-src/blob/81623d3a60599d05c83987dec111bf56809f901d/Zend/zend_hash.h#L263
*
* @param array $arKey
* @param int $nKeyLength
*
* @return int
*/
private function zendInlineHashFunc(array $arKey, int $nKeyLength): int
private function zendInlineHashFunc(string $arKey, int $nKeyLength): int
{
$hash = 5381;
$i = 0;
Expand Down Expand Up @@ -3135,7 +3129,7 @@ private function zendInlineHashFunc(array $arKey, int $nKeyLength): int
case 2:
$hash = (($hash << 5) + $hash) + ord($arKey[$i++]); /* fallthrough... */
case 1:
$hash = (($hash << 5) + $hash) + ord($arKey[$i++]);
$hash = (($hash << 5) + $hash) + ord($arKey[$i] ?? '');
break;
case 0:
break;
Expand Down
40 changes: 23 additions & 17 deletions src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Phalcon\Volt\Parser;

use Phalcon\Volt\Compiler;
use Phalcon\Volt\Exception;
use Phalcon\Volt\Scanner\Opcode;
use Phalcon\Volt\Scanner\Scanner;
use Phalcon\Volt\Scanner\State;
Expand All @@ -37,6 +38,7 @@ public function __construct(private string $code)
* @param string $templatePath
*
* @return array
* @throws Exception
*/
public function parseView(string $templatePath): array
{
Expand All @@ -47,6 +49,7 @@ public function parseView(string $templatePath): array
$debug = fopen('log.txt', 'w+');

$state = new State($this->code);
$state->setActiveFile($templatePath);
$parserStatus = new Status($state);
$scanner = new Scanner($parserStatus->getState());

Expand Down Expand Up @@ -385,24 +388,27 @@ public function parseView(string $templatePath): array
break;

case Compiler::PHVOLT_T_RAW_FRAGMENT:
if ($state->extendsMode === 1 && $state->blockLevel === 0) {
$this->createErrorMessage(
$parserStatus,
'Child templates only may contain blocks'
);
$parserStatus->setStatus(Status::PHVOLT_PARSING_FAILED);
break;
}
if ($this->token->getLength() > 0) {
$value = trim($this->token->getValue());
if ($value !== '' && $state->extendsMode === 1 && $state->blockLevel === 0) {
$this->createErrorMessage(
$parserStatus,
'Child templates only may contain blocks'
);
$parserStatus->setStatus(Status::PHVOLT_PARSING_FAILED);
break;
}

if (!$this->phvoltIsBlankString($this->token)) {
$state->statementPosition++;
}

if (!$this->phvoltIsBlankString($this->token)) {
$state->statementPosition++;
$this->phvoltParseWithToken(
$parser,
Compiler::PHVOLT_T_RAW_FRAGMENT,
Opcode::PHVOLT_RAW_FRAGMENT
);
}

$this->phvoltParseWithToken(
$parser,
Compiler::PHVOLT_T_RAW_FRAGMENT,
Opcode::PHVOLT_RAW_FRAGMENT
);
break;

case Compiler::PHVOLT_T_SET:
Expand Down Expand Up @@ -594,7 +600,7 @@ public function parseView(string $templatePath): array
}

if ($parserStatus->getStatus() !== Status::PHVOLT_PARSING_OK) {
break;
throw new Exception($parserStatus->getSyntaxError());
}

$state->setEnd($state->getStart());
Expand Down
9 changes: 9 additions & 0 deletions src/Scanner/Opcode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Volt\Scanner;
Expand Down
Loading

0 comments on commit 0e8e811

Please sign in to comment.