From dbff0efab550a20c35abd3c69fb6808d2fce664e Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 16 Apr 2026 07:24:40 -0500 Subject: [PATCH 1/4] reorganizing constants/enums --- src/Compiler.php | 153 +++++++++++++-------------- src/Compiler/Opcode.php | 136 ++++++++++++++++++++++++ src/Parser/Parser.php | 183 +++++++++++++++++---------------- src/Scanner/Mode.php | 21 ++++ src/Scanner/Scanner.php | 222 ++++++++++++++++++++-------------------- src/Scanner/State.php | 5 +- 6 files changed, 441 insertions(+), 279 deletions(-) create mode 100644 src/Compiler/Opcode.php create mode 100644 src/Scanner/Mode.php diff --git a/src/Compiler.php b/src/Compiler.php index 2faedb1..c54948c 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -17,6 +17,7 @@ use Exception as BaseException; use Phalcon\Di\DiInterface; use Phalcon\Mvc\ViewBaseInterface; +use Phalcon\Volt\Compiler\Opcode; use Phalcon\Volt\Parser\Parser; use function addslashes; @@ -375,7 +376,7 @@ public function attributeReader(array $expr): string $exprCode = ''; $left = $expr['left']; - if ($left['type'] == static::PHVOLT_T_IDENTIFIER) { + if ($left['type'] == Opcode::IDENTIFIER->value) { $variable = $left['value']; /** @@ -403,7 +404,7 @@ public function attributeReader(array $expr): string $exprCode .= '->'; $right = $expr['right']; - if ($right['type'] == static::PHVOLT_T_IDENTIFIER) { + if ($right['type'] == Opcode::IDENTIFIER->value) { $exprCode .= $right['value']; } else { $exprCode .= $this->expression($right); @@ -695,7 +696,7 @@ public function compileCache(array $statement, bool $extendsMode = false): strin if ($lifetime !== null) { $compilation .= '$_cacheKey[' . $exprCode . ']'; - if ($lifetime['type'] == static::PHVOLT_T_IDENTIFIER) { + if ($lifetime['type'] == Opcode::IDENTIFIER->value) { $compilation .= '$_cache[' . $exprCode . ']->start(' . $exprCode . ', $' . $lifetime['value'] . '); '; } else { $compilation .= '$_cache[' . $exprCode . ']->start(' . $exprCode . ', ' . $lifetime['value'] . '); '; @@ -715,7 +716,7 @@ public function compileCache(array $statement, bool $extendsMode = false): strin * Check if the cache has a lifetime */ if ($lifetime !== null) { - if ($lifetime['type'] == static::PHVOLT_T_IDENTIFIER) { + if ($lifetime['type'] == Opcode::IDENTIFIER->value) { $compilation .= 'save(' . $exprCode . ', null, $' . $lifetime['value'] . '); '; } else { @@ -810,13 +811,13 @@ public function compileEcho(array $statement): string $expr = $statement['expr']; $exprCode = $this->expression($expr); - if ($expr == static::PHVOLT_T_FCALL) { + if ($expr == Opcode::FCALL->value) { if ($this->isTagFactory($expr)) { $exprCode = $this->expression($expr, true); } $name = $expr['name']; - if ($name == static::PHVOLT_T_IDENTIFIER) { + if ($name == Opcode::IDENTIFIER->value) { /** * super() is a function however the return of this function * must be output as it is @@ -993,7 +994,7 @@ public function compileForeach(array $statement, bool $extendsMode = false): str break; } - if ($blockStatement['type'] == static::PHVOLT_T_ELSEFOR) { + if ($blockStatement['type'] == Opcode::ELSEFOR->value) { $compilation .= ''; $forElse = $prefixLevel; $this->forElsePointers[$level] = $forElse; @@ -1340,19 +1341,19 @@ public function compileSet(array $statement): string * Generate the right operator */ switch ($assignment['op']) { - case static::PHVOLT_T_ADD_ASSIGN: + case Opcode::ADD_ASSIGN->value: $compilation .= ' ' . $target . ' += ' . $exprCode . ';'; break; - case static::PHVOLT_T_SUB_ASSIGN: + case Opcode::SUB_ASSIGN->value: $compilation .= ' ' . $target . ' -= ' . $exprCode . ';'; break; - case static::PHVOLT_T_MUL_ASSIGN: + case Opcode::MUL_ASSIGN->value: $compilation .= ' ' . $target . ' *= ' . $exprCode . ';'; break; - case static::PHVOLT_T_DIV_ASSIGN: + case Opcode::DIV_ASSIGN->value: $compilation .= ' ' . $target . ' /= ' . $exprCode . ';'; break; @@ -1584,7 +1585,7 @@ final public function expression(array $expr, bool $doubleQuotes = false): strin /** * Attribute reading needs special handling */ - if ($type == static::PHVOLT_T_DOT) { + if ($type == Opcode::DOT->value) { $exprCode = $this->attributeReader($expr); break; @@ -1600,7 +1601,7 @@ final public function expression(array $expr, bool $doubleQuotes = false): strin /** * Operator "is" also needs special handling */ - if ($type == static::PHVOLT_T_IS) { + if ($type == Opcode::IS->value) { $exprCode = $this->resolveTest($expr['right'], $leftCode); break; @@ -1609,7 +1610,7 @@ final public function expression(array $expr, bool $doubleQuotes = false): strin /** * We don't resolve the right expression for filters */ - if ($type == self::PHVOLT_T_PIPE) { + if ($type == Opcode::PIPE->value) { $exprCode = $this->resolveFilter($expr['right'], $leftCode); break; @@ -1622,23 +1623,23 @@ final public function expression(array $expr, bool $doubleQuotes = false): strin $rightCode = isset($expr['right']) ? $this->expression($expr['right'], $doubleQuotes) : ''; switch ($type) { - case static::PHVOLT_T_NOT: + case Opcode::NOT->value: $exprCode = '!' . $rightCode; break; - case static::PHVOLT_T_MUL: + case Opcode::MUL->value: $exprCode = $leftCode . ' * ' . $rightCode; break; - case static::PHVOLT_T_ADD: + case Opcode::ADD->value: $exprCode = $leftCode . ' + ' . $rightCode; break; - case static::PHVOLT_T_SUB: + case Opcode::SUB->value: $exprCode = $leftCode . ' - ' . $rightCode; break; - case static::PHVOLT_T_DIV: + case Opcode::DIV->value: $exprCode = $leftCode . ' / ' . $rightCode; break; @@ -1646,7 +1647,7 @@ final public function expression(array $expr, bool $doubleQuotes = false): strin $exprCode = $leftCode . ' % ' . $rightCode; break; - case static::PHVOLT_T_LESS: + case Opcode::LESS->value: $exprCode = $leftCode . ' < ' . $rightCode; break; @@ -1663,17 +1664,17 @@ final public function expression(array $expr, bool $doubleQuotes = false): strin $exprCode = 'pow(' . $leftCode . ', ' . $rightCode . ')'; break; - case static::PHVOLT_T_ARRAY: + case Opcode::ARRAY->value: $exprCode = isset($expr['left']) ? '[' . $leftCode . ']' : []; break; case 258: case 259: - case static::PHVOLT_T_RESOLVED_EXPR: + case Opcode::RESOLVED_EXPR->value: $exprCode = $expr['value']; break; - case static::PHVOLT_T_STRING: + case Opcode::STRING->value: if ($doubleQuotes === false) { $exprCode = '\'' . str_replace('\'', '\\\'', $expr['value']) . '\''; } else { @@ -1681,23 +1682,23 @@ final public function expression(array $expr, bool $doubleQuotes = false): strin } break; - case static::PHVOLT_T_NULL: + case Opcode::NULL->value: $exprCode = 'null'; break; - case static::PHVOLT_T_FALSE: + case Opcode::FALSE->value: $exprCode = 'false'; break; - case static::PHVOLT_T_TRUE: + case Opcode::TRUE->value: $exprCode = 'true'; break; - case static::PHVOLT_T_IDENTIFIER: + case Opcode::IDENTIFIER->value: $exprCode = '$' . $expr['value']; break; - case static::PHVOLT_T_AND: + case Opcode::AND->value: $exprCode = $leftCode . ' && ' . $rightCode; break; @@ -1705,7 +1706,7 @@ final public function expression(array $expr, bool $doubleQuotes = false): strin $exprCode = $leftCode . ' || ' . $rightCode; break; - case static::PHVOLT_T_LESSEQUAL: + case Opcode::LESSEQUAL->value: $exprCode = $leftCode . ' <= ' . $rightCode; break; @@ -1729,23 +1730,23 @@ final public function expression(array $expr, bool $doubleQuotes = false): strin $exprCode = $leftCode .= ' !== ' . $rightCode; break; - case static::PHVOLT_T_RANGE: + case Opcode::RANGE->value: $exprCode = 'range(' . $leftCode . ', ' . $rightCode . ')'; break; - case static::PHVOLT_T_FCALL: + case Opcode::FCALL->value: $exprCode = $this->functionCall($expr, $doubleQuotes); break; - case static::PHVOLT_T_ENCLOSED: + case Opcode::ENCLOSED->value: $exprCode = '(' . $leftCode . ')'; break; - case static::PHVOLT_T_ARRAYACCESS: + case Opcode::ARRAYACCESS->value: $exprCode = $leftCode . "[" . $rightCode . "]"; break; - case static::PHVOLT_T_SLICE: + case Opcode::SLICE->value: /** * Evaluate the start part of the slice */ @@ -1759,80 +1760,80 @@ final public function expression(array $expr, bool $doubleQuotes = false): strin $exprCode = '$this->slice(' . $leftCode . ', ' . $startCode . ', ' . $endCode . ')'; break; - case static::PHVOLT_T_NOT_ISSET: + case Opcode::NOT_ISSET->value: $exprCode = '!isset(' . $leftCode . ')'; break; - case static::PHVOLT_T_ISSET: + case Opcode::ISSET->value: $exprCode = 'isset(' . $leftCode . ')'; break; - case static::PHVOLT_T_NOT_ISEMPTY: + case Opcode::NOT_ISEMPTY->value: $exprCode = '!empty(' . $leftCode . ')'; break; - case static::PHVOLT_T_ISEMPTY: + case Opcode::ISEMPTY->value: $exprCode = 'empty(' . $leftCode . ')'; break; - case static::PHVOLT_T_NOT_ISEVEN: + case Opcode::NOT_ISEVEN->value: $exprCode = '!(((' . $leftCode . ') % 2) == 0)'; break; - case static::PHVOLT_T_ISEVEN: + case Opcode::ISEVEN->value: $exprCode = '(((' . $leftCode . ') % 2) == 0)'; break; - case static::PHVOLT_T_NOT_ISODD: + case Opcode::NOT_ISODD->value: $exprCode = '!(((' . $leftCode . ') % 2) != 0)'; break; - case static::PHVOLT_T_ISODD: + case Opcode::ISODD->value: $exprCode = '(((' . $leftCode . ') % 2) != 0)'; break; - case static::PHVOLT_T_NOT_ISNUMERIC: + case Opcode::NOT_ISNUMERIC->value: $exprCode = '!is_numeric(' . $leftCode . ')'; break; - case static::PHVOLT_T_ISNUMERIC: + case Opcode::ISNUMERIC->value: $exprCode = 'is_numeric(' . $leftCode . ')'; break; - case static::PHVOLT_T_NOT_ISSCALAR: + case Opcode::NOT_ISSCALAR->value: $exprCode = '!is_scalar(' . $leftCode . ')'; break; - case static::PHVOLT_T_ISSCALAR: + case Opcode::ISSCALAR->value: $exprCode = 'is_scalar(' . $leftCode . ')'; break; - case static::PHVOLT_T_NOT_ISITERABLE: + case Opcode::NOT_ISITERABLE->value: $exprCode = '!(is_array(' . $leftCode . ') || (' . $leftCode . ') instanceof Traversable)'; break; - case static::PHVOLT_T_ISITERABLE: + case Opcode::ISITERABLE->value: $exprCode = '(is_array(' . $leftCode . ') || (' . $leftCode . ') instanceof Traversable)'; break; - case static::PHVOLT_T_IN: + case Opcode::IN->value: $exprCode = '$this->isIncluded(' . $leftCode . ', ' . $rightCode . ')'; break; - case static::PHVOLT_T_NOT_IN: + case Opcode::NOT_IN->value: $exprCode = '!$this->isIncluded(' . $leftCode . ', ' . $rightCode . ')'; break; - case static::PHVOLT_T_TERNARY: + case Opcode::TERNARY->value: $exprCode = '(' . $this->expression($expr['ternary'], $doubleQuotes) . ' ? ' . $leftCode . ' : ' . $rightCode . ')'; break; - case static::PHVOLT_T_MINUS: + case Opcode::MINUS->value: $exprCode = '-' . $rightCode; break; - case static::PHVOLT_T_PLUS: + case Opcode::PLUS->value: $exprCode = '+' . $rightCode; break; @@ -1909,7 +1910,7 @@ public function functionCall(array $expr, bool $doubleQuotes = false): string /** * Check if it's a single function */ - if ($nameType == static::PHVOLT_T_IDENTIFIER) { + if ($nameType == Opcode::IDENTIFIER->value) { $name = $nameExpr['value']; /** @@ -2225,7 +2226,7 @@ public function resolveTest(array $test, string $left): string { $type = $test['type']; - if ($type === self::PHVOLT_T_IDENTIFIER) { + if ($type === Opcode::IDENTIFIER->value) { $name = $test['value']; /** @@ -2274,7 +2275,7 @@ public function resolveTest(array $test, string $left): string /** * Check if right part is a function call */ - if ($type === self::PHVOLT_T_FCALL) { + if ($type === Opcode::FCALL->value) { $testName = $test['name']; if (isset($testName['value'])) { @@ -2373,10 +2374,10 @@ final protected function resolveFilter(array $filter, string $left): string /** * Check if the filter is a single identifier */ - if ($type === self::PHVOLT_T_IDENTIFIER) { + if ($type === Opcode::IDENTIFIER->value) { $name = $filter['value']; } else { - if ($type !== self::PHVOLT_T_FCALL) { + if ($type !== Opcode::FCALL->value) { /** * Unknown filter throw an exception */ @@ -2748,19 +2749,19 @@ final protected function statementList(array $statements, bool $extendsMode = fa * Compile the statement according to the statement's type */ switch ($type) { - case self::PHVOLT_T_RAW_FRAGMENT: + case Opcode::RAW_FRAGMENT->value: $compilation .= $statement["value"]; break; - case self::PHVOLT_T_IF: + case Opcode::IF->value: $compilation .= $this->compileIf($statement, $extendsMode); break; - case self::PHVOLT_T_ELSEIF: + case Opcode::ELSEIF->value: $compilation .= $this->compileElseIf($statement); break; - case self::PHVOLT_T_SWITCH: + case Opcode::SWITCH->value: $compilation .= $this->compileSwitch( $statement, $extendsMode @@ -2768,15 +2769,15 @@ final protected function statementList(array $statements, bool $extendsMode = fa break; - case self::PHVOLT_T_CASE: + case Opcode::CASE->value: $compilation .= $this->compileCase($statement); break; - case self::PHVOLT_T_DEFAULT: + case Opcode::DEFAULT->value: $compilation .= $this->compileCase($statement, false); break; - case self::PHVOLT_T_FOR: + case Opcode::FOR->value: $compilation .= $this->compileForeach( $statement, $extendsMode @@ -2784,15 +2785,15 @@ final protected function statementList(array $statements, bool $extendsMode = fa break; - case self::PHVOLT_T_SET: + case Opcode::SET->value: $compilation .= $this->compileSet($statement); break; - case self::PHVOLT_T_ECHO: + case Opcode::ECHO->value: $compilation .= $this->compileEcho($statement); break; - case self::PHVOLT_T_BLOCK: + case Opcode::BLOCK->value: /** * Block statement */ @@ -2825,7 +2826,7 @@ final protected function statementList(array $statements, bool $extendsMode = fa break; - case self::PHVOLT_T_EXTENDS: + case Opcode::EXTENDS->value: /** * Extends statement */ @@ -2853,20 +2854,20 @@ final protected function statementList(array $statements, bool $extendsMode = fa break; - case self::PHVOLT_T_INCLUDE: + case Opcode::INCLUDE->value: $compilation .= $this->compileInclude($statement); break; - case self::PHVOLT_T_DO: + case Opcode::DO->value: $compilation .= $this->compileDo($statement); break; - case self::PHVOLT_T_RETURN: + case Opcode::RETURN->value: $compilation .= $this->compileReturn($statement); break; - case self::PHVOLT_T_AUTOESCAPE: + case Opcode::AUTOESCAPE->value: $compilation .= $this->compileAutoEscape( $statement, $extendsMode @@ -2874,14 +2875,14 @@ final protected function statementList(array $statements, bool $extendsMode = fa break; - case self::PHVOLT_T_CONTINUE: + case Opcode::CONTINUE->value: /** * "Continue" statement */ $compilation .= ""; break; - case self::PHVOLT_T_BREAK: + case Opcode::BREAK->value: /** * "Break" statement */ @@ -2895,7 +2896,7 @@ final protected function statementList(array $statements, bool $extendsMode = fa $compilation .= $this->compileForElse(); break; - case self::PHVOLT_T_MACRO: + case Opcode::MACRO->value: /** * Define a macro */ diff --git a/src/Compiler/Opcode.php b/src/Compiler/Opcode.php new file mode 100644 index 0000000..d2bdcc5 --- /dev/null +++ b/src/Compiler/Opcode.php @@ -0,0 +1,136 @@ + + * + * 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\Compiler; + +enum Opcode: int +{ + case ADD = 43; + case ADD_ASSIGN = 281; + case AND = 266; + case ARRAY = 360; + case ARRAYACCESS = 361; + case ASSIGN = 61; + case AUTOESCAPE = 317; + case BLOCK = 307; + case BREAK = 320; + case CACHE = 314; + case CALL = 325; + case CASE = 412; + case CBRACKET_CLOSE = 125; + case CBRACKET_OPEN = 123; + case CLOSE_DELIMITER = 331; + case CLOSE_EDELIMITER = 333; + case COLON = 277; + case COMMA = 269; + case CONCAT = 126; + case CONTINUE = 319; + case DECR = 280; + case DEFAULT = 413; + case DEFINED = 312; + case DIV = 47; + case DIV_ASSIGN = 284; + case DO = 316; + case DOT = 46; + case DOUBLE = 259; + case ECHO = 359; + case ELSE = 301; + case ELSEFOR = 321; + case ELSEIF = 302; + case EMPTY = 380; + case EMPTY_STATEMENT = 358; + case ENCLOSED = 356; + case ENDAUTOESCAPE = 318; + case ENDBLOCK = 308; + case ENDCACHE = 315; + case ENDCALL = 326; + case ENDFOR = 305; + case ENDIF = 303; + case ENDMACRO = 323; + case ENDRAW = 401; + case ENDSWITCH = 414; + case EQUALS = 272; + case EVEN = 381; + case EXPR = 354; + case EXTENDS = 310; + case FALSE = 262; + case FCALL = 350; + case FOR = 304; + case GREATER = 62; + case GREATEREQUAL = 271; + case IDENTICAL = 274; + case IDENTIFIER = 265; + case IF = 300; + case IGNORE = 257; + case IN = 309; + case INCLUDE = 313; + case INCR = 279; + case INTEGER = 258; + case IS = 311; + case ISEMPTY = 386; + case ISEVEN = 387; + case ISITERABLE = 391; + case ISNUMERIC = 389; + case ISODD = 388; + case ISSCALAR = 390; + case ISSET = 363; + case ITERABLE = 385; + case LESS = 60; + case LESSEQUAL = 270; + case MACRO = 322; + case MINUS = 368; + case MOD = 37; + case MUL = 42; + case MUL_ASSIGN = 283; + case NOT = 33; + case NOTEQUALS = 273; + case NOTIDENTICAL = 275; + case NOT_IN = 367; + case NOT_ISEMPTY = 392; + case NOT_ISEVEN = 393; + case NOT_ISITERABLE = 397; + case NOT_ISNUMERIC = 395; + case NOT_ISODD = 394; + case NOT_ISSCALAR = 396; + case NOT_ISSET = 362; + case NULL = 261; + case NUMERIC = 383; + case ODD = 382; + case OPEN_DELIMITER = 330; + case OPEN_EDELIMITER = 332; + case OR = 267; + case PARENTHESES_CLOSE = 41; + case PARENTHESES_OPEN = 40; + case PIPE = 124; + case PLUS = 369; + case POW = 278; + case QUALIFIED = 355; + case QUESTION = 63; + case RANGE = 276; + case RAW = 400; + case RAW_FRAGMENT = 357; + case RESOLVED_EXPR = 364; + case RETURN = 327; + case SBRACKET_CLOSE = 93; + case SBRACKET_OPEN = 91; + case SCALAR = 384; + case SET = 306; + case SLICE = 365; + case STRING = 260; + case SUB = 45; + case SUB_ASSIGN = 282; + case SWITCH = 411; + case TERNARY = 366; + case TRUE = 263; + case WITH = 324; +} \ No newline at end of file diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php index 4df2ccb..addf51e 100644 --- a/src/Parser/Parser.php +++ b/src/Parser/Parser.php @@ -14,6 +14,7 @@ namespace Phalcon\Volt\Parser; use Phalcon\Volt\Compiler; +use Phalcon\Volt\Compiler\Opcode as CompilerOpcode; use Phalcon\Volt\Exception; use Phalcon\Volt\Scanner\Opcode; use Phalcon\Volt\Scanner\Scanner; @@ -72,103 +73,103 @@ public function parse(string $code, string $templatePath = 'eval code'): array $state->setActiveToken($opcode); match ($opcode) { - Compiler::PHVOLT_T_IGNORE => null, - Compiler::PHVOLT_T_ADD => $parser->phvolt_(Opcode::PLUS->value), - Compiler::PHVOLT_T_SUB => $parser->phvolt_(Opcode::MINUS->value), - Compiler::PHVOLT_T_MUL => $parser->phvolt_(Opcode::TIMES->value), - Compiler::PHVOLT_T_DIV => $parser->phvolt_(Opcode::DIVIDE->value), - Compiler::PHVOLT_T_MOD => $parser->phvolt_(Opcode::MOD->value), - Compiler::PHVOLT_T_AND => $parser->phvolt_(Opcode::AND->value), - Compiler::PHVOLT_T_OR => $parser->phvolt_(Opcode::OR->value), - Compiler::PHVOLT_T_IS => $parser->phvolt_(Opcode::IS->value), - Compiler::PHVOLT_T_EQUALS => $parser->phvolt_(Opcode::EQUALS->value), - Compiler::PHVOLT_T_NOTEQUALS => $parser->phvolt_(Opcode::NOTEQUALS->value), - Compiler::PHVOLT_T_LESS => $parser->phvolt_(Opcode::LESS->value), - Compiler::PHVOLT_T_GREATER => $parser->phvolt_(Opcode::GREATER->value), - Compiler::PHVOLT_T_GREATEREQUAL => $parser->phvolt_(Opcode::GREATEREQUAL->value), - Compiler::PHVOLT_T_LESSEQUAL => $parser->phvolt_(Opcode::LESSEQUAL->value), - Compiler::PHVOLT_T_IDENTICAL => $parser->phvolt_(Opcode::IDENTICAL->value), - Compiler::PHVOLT_T_NOTIDENTICAL => $parser->phvolt_(Opcode::NOTIDENTICAL->value), - Compiler::PHVOLT_T_NOT => $parser->phvolt_(Opcode::NOT->value), - Compiler::PHVOLT_T_DOT => $parser->phvolt_(Opcode::DOT->value), - Compiler::PHVOLT_T_CONCAT => $parser->phvolt_(Opcode::CONCAT->value), - Compiler::PHVOLT_T_RANGE => $parser->phvolt_(Opcode::RANGE->value), - Compiler::PHVOLT_T_PIPE => $parser->phvolt_(Opcode::PIPE->value), - Compiler::PHVOLT_T_COMMA => $parser->phvolt_(Opcode::COMMA->value), - Compiler::PHVOLT_T_COLON => $parser->phvolt_(Opcode::COLON->value), - Compiler::PHVOLT_T_QUESTION => $parser->phvolt_(Opcode::QUESTION->value), - Compiler::PHVOLT_T_PARENTHESES_OPEN => $parser->phvolt_(Opcode::PARENTHESES_OPEN->value), - Compiler::PHVOLT_T_PARENTHESES_CLOSE => $parser->phvolt_(Opcode::PARENTHESES_CLOSE->value), - Compiler::PHVOLT_T_SBRACKET_OPEN => $parser->phvolt_(Opcode::SBRACKET_OPEN->value), - Compiler::PHVOLT_T_SBRACKET_CLOSE => $parser->phvolt_(Opcode::SBRACKET_CLOSE->value), - Compiler::PHVOLT_T_CBRACKET_OPEN => $parser->phvolt_(Opcode::CBRACKET_OPEN->value), - Compiler::PHVOLT_T_CBRACKET_CLOSE => $parser->phvolt_(Opcode::CBRACKET_CLOSE->value), - Compiler::PHVOLT_T_OPEN_DELIMITER => $parser->phvolt_(Opcode::OPEN_DELIMITER->value), - Compiler::PHVOLT_T_CLOSE_DELIMITER => $parser->phvolt_(Opcode::CLOSE_DELIMITER->value), - Compiler::PHVOLT_T_OPEN_EDELIMITER => $this->handleOpenEdelimiter($parser, $parserStatus, $state), - Compiler::PHVOLT_T_CLOSE_EDELIMITER => $parser->phvolt_(Opcode::CLOSE_EDELIMITER->value), - Compiler::PHVOLT_T_NULL => $parser->phvolt_(Opcode::NULL->value), - Compiler::PHVOLT_T_TRUE => $parser->phvolt_(Opcode::TRUE->value), - Compiler::PHVOLT_T_FALSE => $parser->phvolt_(Opcode::FALSE->value), - Compiler::PHVOLT_T_INTEGER => $this->parseWithToken($parser, $token, Opcode::INTEGER), - Compiler::PHVOLT_T_DOUBLE => $this->parseWithToken($parser, $token, Opcode::DOUBLE), - Compiler::PHVOLT_T_STRING => $this->parseWithToken($parser, $token, Opcode::STRING), - Compiler::PHVOLT_T_IDENTIFIER => $this->parseWithToken($parser, $token, Opcode::IDENTIFIER), - Compiler::PHVOLT_T_IF => $this->handleIf($parser, $parserStatus, $state), - Compiler::PHVOLT_T_ELSE => $state->getIfLevel() === 0 && $state->getForLevel() > 0 + CompilerOpcode::IGNORE->value => null, + CompilerOpcode::ADD->value => $parser->phvolt_(Opcode::PLUS->value), + CompilerOpcode::SUB->value => $parser->phvolt_(Opcode::MINUS->value), + CompilerOpcode::MUL->value => $parser->phvolt_(Opcode::TIMES->value), + CompilerOpcode::DIV->value => $parser->phvolt_(Opcode::DIVIDE->value), + CompilerOpcode::MOD->value => $parser->phvolt_(Opcode::MOD->value), + CompilerOpcode::AND->value => $parser->phvolt_(Opcode::AND->value), + CompilerOpcode::OR->value => $parser->phvolt_(Opcode::OR->value), + CompilerOpcode::IS->value => $parser->phvolt_(Opcode::IS->value), + CompilerOpcode::EQUALS->value => $parser->phvolt_(Opcode::EQUALS->value), + CompilerOpcode::NOTEQUALS->value => $parser->phvolt_(Opcode::NOTEQUALS->value), + CompilerOpcode::LESS->value => $parser->phvolt_(Opcode::LESS->value), + CompilerOpcode::GREATER->value => $parser->phvolt_(Opcode::GREATER->value), + CompilerOpcode::GREATEREQUAL->value => $parser->phvolt_(Opcode::GREATEREQUAL->value), + CompilerOpcode::LESSEQUAL->value => $parser->phvolt_(Opcode::LESSEQUAL->value), + CompilerOpcode::IDENTICAL->value => $parser->phvolt_(Opcode::IDENTICAL->value), + CompilerOpcode::NOTIDENTICAL->value => $parser->phvolt_(Opcode::NOTIDENTICAL->value), + CompilerOpcode::NOT->value => $parser->phvolt_(Opcode::NOT->value), + CompilerOpcode::DOT->value => $parser->phvolt_(Opcode::DOT->value), + CompilerOpcode::CONCAT->value => $parser->phvolt_(Opcode::CONCAT->value), + CompilerOpcode::RANGE->value => $parser->phvolt_(Opcode::RANGE->value), + CompilerOpcode::PIPE->value => $parser->phvolt_(Opcode::PIPE->value), + CompilerOpcode::COMMA->value => $parser->phvolt_(Opcode::COMMA->value), + CompilerOpcode::COLON->value => $parser->phvolt_(Opcode::COLON->value), + CompilerOpcode::QUESTION->value => $parser->phvolt_(Opcode::QUESTION->value), + CompilerOpcode::PARENTHESES_OPEN->value => $parser->phvolt_(Opcode::PARENTHESES_OPEN->value), + CompilerOpcode::PARENTHESES_CLOSE->value => $parser->phvolt_(Opcode::PARENTHESES_CLOSE->value), + CompilerOpcode::SBRACKET_OPEN->value => $parser->phvolt_(Opcode::SBRACKET_OPEN->value), + CompilerOpcode::SBRACKET_CLOSE->value => $parser->phvolt_(Opcode::SBRACKET_CLOSE->value), + CompilerOpcode::CBRACKET_OPEN->value => $parser->phvolt_(Opcode::CBRACKET_OPEN->value), + CompilerOpcode::CBRACKET_CLOSE->value => $parser->phvolt_(Opcode::CBRACKET_CLOSE->value), + CompilerOpcode::OPEN_DELIMITER->value => $parser->phvolt_(Opcode::OPEN_DELIMITER->value), + CompilerOpcode::CLOSE_DELIMITER->value => $parser->phvolt_(Opcode::CLOSE_DELIMITER->value), + CompilerOpcode::OPEN_EDELIMITER->value => $this->handleOpenEdelimiter($parser, $parserStatus, $state), + CompilerOpcode::CLOSE_EDELIMITER->value => $parser->phvolt_(Opcode::CLOSE_EDELIMITER->value), + CompilerOpcode::NULL->value => $parser->phvolt_(Opcode::NULL->value), + CompilerOpcode::TRUE->value => $parser->phvolt_(Opcode::TRUE->value), + CompilerOpcode::FALSE->value => $parser->phvolt_(Opcode::FALSE->value), + CompilerOpcode::INTEGER->value => $this->parseWithToken($parser, $token, Opcode::INTEGER), + CompilerOpcode::DOUBLE->value => $this->parseWithToken($parser, $token, Opcode::DOUBLE), + CompilerOpcode::STRING->value => $this->parseWithToken($parser, $token, Opcode::STRING), + CompilerOpcode::IDENTIFIER->value => $this->parseWithToken($parser, $token, Opcode::IDENTIFIER), + CompilerOpcode::IF->value => $this->handleIf($parser, $parserStatus, $state), + CompilerOpcode::ELSE->value => $state->getIfLevel() === 0 && $state->getForLevel() > 0 ? $parser->phvolt_(Opcode::ELSEFOR->value) : $parser->phvolt_(Opcode::ELSE->value), - Compiler::PHVOLT_T_ELSEFOR => $parser->phvolt_(Opcode::ELSEFOR->value), - Compiler::PHVOLT_T_ELSEIF => $this->handleElseif($parser, $parserStatus, $state), - Compiler::PHVOLT_T_ENDIF => $this->handleEndif($parser, $state), - Compiler::PHVOLT_T_FOR => $this->handleFor($parser, $parserStatus, $state), - Compiler::PHVOLT_T_IN => $parser->phvolt_(Opcode::IN->value), - Compiler::PHVOLT_T_ENDFOR => $this->handleEndfor($parser, $state), - Compiler::PHVOLT_T_SWITCH => $this->handleSwitch($parser, $parserStatus, $state), - Compiler::PHVOLT_T_CASE => $this->handleCase($parser, $parserStatus), - Compiler::PHVOLT_T_DEFAULT => $this->handleDefault($parser, $parserStatus, $token, $state), - Compiler::PHVOLT_T_ENDSWITCH => $this->handleEndswitch($parser, $parserStatus, $state), - Compiler::PHVOLT_T_RAW_FRAGMENT => $this->handleRawFragment( + CompilerOpcode::ELSEFOR->value => $parser->phvolt_(Opcode::ELSEFOR->value), + CompilerOpcode::ELSEIF->value => $this->handleElseif($parser, $parserStatus, $state), + CompilerOpcode::ENDIF->value => $this->handleEndif($parser, $state), + CompilerOpcode::FOR->value => $this->handleFor($parser, $parserStatus, $state), + CompilerOpcode::IN->value => $parser->phvolt_(Opcode::IN->value), + CompilerOpcode::ENDFOR->value => $this->handleEndfor($parser, $state), + CompilerOpcode::SWITCH->value => $this->handleSwitch($parser, $parserStatus, $state), + CompilerOpcode::CASE->value => $this->handleCase($parser, $parserStatus), + CompilerOpcode::DEFAULT->value => $this->handleDefault($parser, $parserStatus, $token, $state), + CompilerOpcode::ENDSWITCH->value => $this->handleEndswitch($parser, $parserStatus, $state), + CompilerOpcode::RAW_FRAGMENT->value => $this->handleRawFragment( $parser, $parserStatus, $token, $state ), - Compiler::PHVOLT_T_SET => $this->handleSet($parser, $parserStatus, $state), - Compiler::PHVOLT_T_ASSIGN => $parser->phvolt_(Opcode::ASSIGN->value), - Compiler::PHVOLT_T_ADD_ASSIGN => $parser->phvolt_(Opcode::ADD_ASSIGN->value), - Compiler::PHVOLT_T_SUB_ASSIGN => $parser->phvolt_(Opcode::SUB_ASSIGN->value), - Compiler::PHVOLT_T_MUL_ASSIGN => $parser->phvolt_(Opcode::MUL_ASSIGN->value), - Compiler::PHVOLT_T_DIV_ASSIGN => $parser->phvolt_(Opcode::DIV_ASSIGN->value), - Compiler::PHVOLT_T_INCR => $parser->phvolt_(Opcode::INCR->value), - Compiler::PHVOLT_T_DECR => $parser->phvolt_(Opcode::DECR->value), - Compiler::PHVOLT_T_BLOCK => $this->handleBlock($parser, $parserStatus, $state), - Compiler::PHVOLT_T_ENDBLOCK => $this->handleEndblock($parser, $state), - Compiler::PHVOLT_T_MACRO => $this->handleMacro($parser, $parserStatus, $state), - Compiler::PHVOLT_T_ENDMACRO => $this->handleEndmacro($parser, $state), - Compiler::PHVOLT_T_CALL => $parser->phvolt_(Opcode::CALL->value), - Compiler::PHVOLT_T_ENDCALL => $parser->phvolt_(Opcode::ENDCALL->value), - Compiler::PHVOLT_T_CACHE => $parser->phvolt_(Opcode::CACHE->value), - Compiler::PHVOLT_T_ENDCACHE => $parser->phvolt_(Opcode::ENDCACHE->value), - Compiler::PHVOLT_T_RAW => $this->handleRaw($parser, $state), - Compiler::PHVOLT_T_ENDRAW => $this->handleEndraw($parser, $state), - Compiler::PHVOLT_T_INCLUDE => $parser->phvolt_(Opcode::INCLUDE->value), - Compiler::PHVOLT_T_WITH => $parser->phvolt_(Opcode::WITH->value), - Compiler::PHVOLT_T_DEFINED => $parser->phvolt_(Opcode::DEFINED->value), - Compiler::PHVOLT_T_EMPTY => $parser->phvolt_(Opcode::EMPTY->value), - Compiler::PHVOLT_T_EVEN => $parser->phvolt_(Opcode::EVEN->value), - Compiler::PHVOLT_T_ODD => $parser->phvolt_(Opcode::ODD->value), - Compiler::PHVOLT_T_NUMERIC => $parser->phvolt_(Opcode::NUMERIC->value), - Compiler::PHVOLT_T_SCALAR => $parser->phvolt_(Opcode::SCALAR->value), - Compiler::PHVOLT_T_ITERABLE => $parser->phvolt_(Opcode::ITERABLE->value), - Compiler::PHVOLT_T_DO => $parser->phvolt_(Opcode::DO->value), - Compiler::PHVOLT_T_RETURN => $parser->phvolt_(Opcode::RETURN->value), - Compiler::PHVOLT_T_AUTOESCAPE => $parser->phvolt_(Opcode::AUTOESCAPE->value), - Compiler::PHVOLT_T_ENDAUTOESCAPE => $parser->phvolt_(Opcode::ENDAUTOESCAPE->value), - Compiler::PHVOLT_T_BREAK => $parser->phvolt_(Opcode::BREAK->value), - Compiler::PHVOLT_T_CONTINUE => $parser->phvolt_(Opcode::CONTINUE->value), - Compiler::PHVOLT_T_EXTENDS => $this->handleExtends($parser, $parserStatus, $state), - default => $this->handleUnknownOpcode($parserStatus, $opcode), + CompilerOpcode::SET->value => $this->handleSet($parser, $parserStatus, $state), + CompilerOpcode::ASSIGN->value => $parser->phvolt_(Opcode::ASSIGN->value), + CompilerOpcode::ADD_ASSIGN->value => $parser->phvolt_(Opcode::ADD_ASSIGN->value), + CompilerOpcode::SUB_ASSIGN->value => $parser->phvolt_(Opcode::SUB_ASSIGN->value), + CompilerOpcode::MUL_ASSIGN->value => $parser->phvolt_(Opcode::MUL_ASSIGN->value), + CompilerOpcode::DIV_ASSIGN->value => $parser->phvolt_(Opcode::DIV_ASSIGN->value), + CompilerOpcode::INCR->value => $parser->phvolt_(Opcode::INCR->value), + CompilerOpcode::DECR->value => $parser->phvolt_(Opcode::DECR->value), + CompilerOpcode::BLOCK->value => $this->handleBlock($parser, $parserStatus, $state), + CompilerOpcode::ENDBLOCK->value => $this->handleEndblock($parser, $state), + CompilerOpcode::MACRO->value => $this->handleMacro($parser, $parserStatus, $state), + CompilerOpcode::ENDMACRO->value => $this->handleEndmacro($parser, $state), + CompilerOpcode::CALL->value => $parser->phvolt_(Opcode::CALL->value), + CompilerOpcode::ENDCALL->value => $parser->phvolt_(Opcode::ENDCALL->value), + CompilerOpcode::CACHE->value => $parser->phvolt_(Opcode::CACHE->value), + CompilerOpcode::ENDCACHE->value => $parser->phvolt_(Opcode::ENDCACHE->value), + CompilerOpcode::RAW->value => $this->handleRaw($parser, $state), + CompilerOpcode::ENDRAW->value => $this->handleEndraw($parser, $state), + CompilerOpcode::INCLUDE->value => $parser->phvolt_(Opcode::INCLUDE->value), + CompilerOpcode::WITH->value => $parser->phvolt_(Opcode::WITH->value), + CompilerOpcode::DEFINED->value => $parser->phvolt_(Opcode::DEFINED->value), + CompilerOpcode::EMPTY->value => $parser->phvolt_(Opcode::EMPTY->value), + CompilerOpcode::EVEN->value => $parser->phvolt_(Opcode::EVEN->value), + CompilerOpcode::ODD->value => $parser->phvolt_(Opcode::ODD->value), + CompilerOpcode::NUMERIC->value => $parser->phvolt_(Opcode::NUMERIC->value), + CompilerOpcode::SCALAR->value => $parser->phvolt_(Opcode::SCALAR->value), + CompilerOpcode::ITERABLE->value => $parser->phvolt_(Opcode::ITERABLE->value), + CompilerOpcode::DO->value => $parser->phvolt_(Opcode::DO->value), + CompilerOpcode::RETURN->value => $parser->phvolt_(Opcode::RETURN->value), + CompilerOpcode::AUTOESCAPE->value => $parser->phvolt_(Opcode::AUTOESCAPE->value), + CompilerOpcode::ENDAUTOESCAPE->value => $parser->phvolt_(Opcode::ENDAUTOESCAPE->value), + CompilerOpcode::BREAK->value => $parser->phvolt_(Opcode::BREAK->value), + CompilerOpcode::CONTINUE->value => $parser->phvolt_(Opcode::CONTINUE->value), + CompilerOpcode::EXTENDS->value => $this->handleExtends($parser, $parserStatus, $state), + default => $this->handleUnknownOpcode($parserStatus, $opcode), }; if ($parserStatus->getStatus() !== Status::PHVOLT_PARSING_OK) { @@ -291,7 +292,7 @@ private function handleDefault( return; } - $newToken = new Token(Compiler::PHVOLT_T_IDENTIFIER, $token->value); + $newToken = new Token(CompilerOpcode::IDENTIFIER->value, $token->value); $parser->phvolt_(Opcode::IDENTIFIER->value, $newToken); } diff --git a/src/Scanner/Mode.php b/src/Scanner/Mode.php new file mode 100644 index 0000000..632122a --- /dev/null +++ b/src/Scanner/Mode.php @@ -0,0 +1,21 @@ + + * + * 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; + +enum Mode: int +{ + case CODE = 1; + case COMMENT = 2; + case RAW = 0; +} diff --git a/src/Scanner/Scanner.php b/src/Scanner/Scanner.php index 025b0c7..163e35e 100644 --- a/src/Scanner/Scanner.php +++ b/src/Scanner/Scanner.php @@ -14,6 +14,8 @@ namespace Phalcon\Volt\Scanner; use Phalcon\Volt\Compiler; +use Phalcon\Volt\Compiler\Opcode; +use Phalcon\Volt\Scanner\Mode; use Phalcon\Volt\Scanner\ScannerStatus; class Scanner @@ -38,7 +40,7 @@ public function scanForToken(): ScannerStatus while (ScannerStatus::IMPOSSIBLE === $status) { $cursor = $this->state->getStart(); $mode = $this->state->getMode(); - if ($mode === Compiler::PHVOLT_MODE_RAW || $mode === Compiler::PHVOLT_MODE_COMMENT) { + if ($mode === Mode::RAW->value || $mode === Mode::COMMENT->value) { $next = $this->state->getNext(); $doubleNext = $this->state->getNext(2); @@ -48,7 +50,7 @@ public function scanForToken(): ScannerStatus if ($cursor === null || ($cursor === '{' && ($next === '%' || $next === '{' || $next === '#'))) { if ($next !== '#') { - $this->state->setMode(Compiler::PHVOLT_MODE_CODE); + $this->state->setMode(Mode::CODE->value); if ($this->state->getRawFragment() !== '') { if ($this->state->getWhitespaceControl()) { @@ -60,18 +62,18 @@ public function scanForToken(): ScannerStatus $this->state->setRawFragment(rtrim($this->state->getRawFragment())); } - $this->token = new Token(Compiler::PHVOLT_T_RAW_FRAGMENT, $this->state->getRawFragment()); + $this->token = new Token(Opcode::RAW_FRAGMENT->value, $this->state->getRawFragment()); $this->state->setRawFragment(''); } else { - $this->token = new Token(Compiler::PHVOLT_T_IGNORE); + $this->token = new Token(Opcode::IGNORE->value); } } else { while ($next = $this->state->incrementStart()->getStart()) { $doubleNext = $this->state->getNext(); if ($next === '#' && $doubleNext === '}') { $this->state->incrementStart(2); - $this->token = new Token(Compiler::PHVOLT_T_IGNORE); + $this->token = new Token(Opcode::IGNORE->value); return ScannerStatus::OK; } elseif ($next === "\n") { $this->state->incrementActiveLine(); @@ -250,12 +252,12 @@ public function scanForToken(): ScannerStatus goto vv8; } vv8: - $this->token = new Token(Compiler::PHVOLT_T_IGNORE); + $this->token = new Token(Opcode::IGNORE->value); return ScannerStatus::OK; vv9: $this->state->incrementStart(); $this->state->incrementActiveLine(); - $this->token = new Token(Compiler::PHVOLT_T_IGNORE); + $this->token = new Token(Opcode::IGNORE->value); return ScannerStatus::OK; vv11: $vvch = $this->state->incrementStart()->getStart(); @@ -266,7 +268,7 @@ public function scanForToken(): ScannerStatus goto vv12; } vv12: - $this->token = new Token(Compiler::PHVOLT_T_NOT); + $this->token = new Token(Opcode::NOT->value); return ScannerStatus::OK; vv13: @@ -287,7 +289,7 @@ public function scanForToken(): ScannerStatus goto vv15; } vv15: - $this->token = new Token(Compiler::PHVOLT_T_MOD); + $this->token = new Token(Opcode::MOD->value); return ScannerStatus::OK; vv16: @@ -301,12 +303,12 @@ public function scanForToken(): ScannerStatus vv17: $this->state->incrementStart(); - $this->token = new Token(Compiler::PHVOLT_T_PARENTHESES_OPEN); + $this->token = new Token(Opcode::PARENTHESES_OPEN->value); return ScannerStatus::OK; vv19: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_PARENTHESES_CLOSE); + $this->token = new Token(Opcode::PARENTHESES_CLOSE->value); return ScannerStatus::OK; } vv21: @@ -319,7 +321,7 @@ public function scanForToken(): ScannerStatus } vv22: { - $this->token = new Token(Compiler::PHVOLT_T_MUL); + $this->token = new Token(Opcode::MUL->value); return ScannerStatus::OK; } vv23: @@ -334,12 +336,12 @@ public function scanForToken(): ScannerStatus } vv24: - $this->token = new Token(Compiler::PHVOLT_T_ADD); + $this->token = new Token(Opcode::ADD->value); return ScannerStatus::OK; vv25: $this->state->incrementStart(); - $this->token = new Token(Compiler::PHVOLT_T_COMMA); + $this->token = new Token(Opcode::COMMA->value); return ScannerStatus::OK; vv27: @@ -360,7 +362,7 @@ public function scanForToken(): ScannerStatus } vv28: - $this->token = new Token(Compiler::PHVOLT_T_SUB); + $this->token = new Token(Opcode::SUB->value); return ScannerStatus::OK; vv29: @@ -373,7 +375,7 @@ public function scanForToken(): ScannerStatus } vv30: - $this->token = new Token(Compiler::PHVOLT_T_DOT); + $this->token = new Token(Opcode::DOT->value); return ScannerStatus::OK; vv31: @@ -386,7 +388,7 @@ public function scanForToken(): ScannerStatus } vv32: - $this->token = new Token(Compiler::PHVOLT_T_DIV); + $this->token = new Token(Opcode::DIV->value); return ScannerStatus::OK; vv33: @@ -413,14 +415,14 @@ public function scanForToken(): ScannerStatus vv35: $this->token = new Token( - Compiler::PHVOLT_T_INTEGER, + Opcode::INTEGER->value, substr($this->state->getRawBuffer(), $start, $this->state->getCursor() - $start) ); return ScannerStatus::OK; vv36: $this->state->incrementStart(); - $this->token = new Token(Compiler::PHVOLT_T_COLON); + $this->token = new Token(Opcode::COLON->value); return ScannerStatus::OK; vv38: $vvch = $this->state->incrementStart()->getStart(); @@ -433,7 +435,7 @@ public function scanForToken(): ScannerStatus goto vv39; } vv39: - $this->token = new Token(Compiler::PHVOLT_T_LESS); + $this->token = new Token(Opcode::LESS->value); return ScannerStatus::OK; vv40: $vvch = $this->state->incrementStart()->getStart(); @@ -445,7 +447,7 @@ public function scanForToken(): ScannerStatus } vv41: { - $this->token = new Token(Compiler::PHVOLT_T_ASSIGN); + $this->token = new Token(Opcode::ASSIGN->value); return ScannerStatus::OK; } vv42: @@ -458,13 +460,13 @@ public function scanForToken(): ScannerStatus } vv43: { - $this->token = new Token(Compiler::PHVOLT_T_GREATER); + $this->token = new Token(Opcode::GREATER->value); return ScannerStatus::OK; } vv44: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_QUESTION); + $this->token = new Token(Opcode::QUESTION->value); return ScannerStatus::OK; } vv46: @@ -482,7 +484,7 @@ public function scanForToken(): ScannerStatus vv47: $this->token = new Token( - Compiler::PHVOLT_T_IDENTIFIER, + Opcode::IDENTIFIER->value, substr($this->state->getRawBuffer(), $start, $this->state->getCursor() - $start) ); return ScannerStatus::OK; @@ -729,7 +731,7 @@ public function scanForToken(): ScannerStatus vv63: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_SBRACKET_OPEN); + $this->token = new Token(Opcode::SBRACKET_OPEN->value); return ScannerStatus::OK; } vv65: @@ -795,7 +797,7 @@ public function scanForToken(): ScannerStatus vv66: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_SBRACKET_CLOSE); + $this->token = new Token(Opcode::SBRACKET_CLOSE->value); return ScannerStatus::OK; } vv68: @@ -829,12 +831,12 @@ public function scanForToken(): ScannerStatus } vv70: { - $this->token = new Token(Compiler::PHVOLT_T_CBRACKET_OPEN); + $this->token = new Token(Opcode::CBRACKET_OPEN->value); return ScannerStatus::OK; } vv71: $this->state->incrementStart(); - $this->token = new Token(Compiler::PHVOLT_T_PIPE); + $this->token = new Token(Opcode::PIPE->value); return ScannerStatus::OK; vv73: $vvch = $this->state->incrementStart()->getStart(); @@ -846,13 +848,13 @@ public function scanForToken(): ScannerStatus } vv74: { - $this->token = new Token(Compiler::PHVOLT_T_CBRACKET_CLOSE); + $this->token = new Token(Opcode::CBRACKET_CLOSE->value); return ScannerStatus::OK; } vv75: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_CONCAT); + $this->token = new Token(Opcode::CONCAT->value); return ScannerStatus::OK; } vv77: @@ -865,7 +867,7 @@ public function scanForToken(): ScannerStatus } vv78: { - $this->token = new Token(Compiler::PHVOLT_T_NOTEQUALS); + $this->token = new Token(Opcode::NOTEQUALS->value); return ScannerStatus::OK; } vv79: @@ -899,7 +901,7 @@ public function scanForToken(): ScannerStatus $this->state->incrementStart(); $start++; $this->token = new Token( - Compiler::PHVOLT_T_STRING, + Opcode::STRING->value, substr($this->state->getRawBuffer(), $start, $this->state->getCursor() - $start - 1) ); return ScannerStatus::OK; @@ -913,8 +915,8 @@ public function scanForToken(): ScannerStatus } vv85: $this->state->incrementStart(); - $this->state->setMode(Compiler::PHVOLT_MODE_RAW); - $this->token = new Token(Compiler::PHVOLT_T_CLOSE_DELIMITER); + $this->state->setMode(Mode::RAW->value); + $this->token = new Token(Opcode::CLOSE_DELIMITER->value); return ScannerStatus::OK; vv87: $vvch = $this->state->incrementStart()->getStart(); @@ -940,19 +942,19 @@ public function scanForToken(): ScannerStatus vv90: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_MUL_ASSIGN); + $this->token = new Token(Opcode::MUL_ASSIGN->value); return ScannerStatus::OK; } vv92: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_INCR); + $this->token = new Token(Opcode::INCR->value); return ScannerStatus::OK; } vv94: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_ADD_ASSIGN); + $this->token = new Token(Opcode::ADD_ASSIGN->value); return ScannerStatus::OK; } vv96: @@ -966,12 +968,12 @@ public function scanForToken(): ScannerStatus vv97: $this->state->incrementStart(); - $this->token = new Token(Compiler::PHVOLT_T_DECR); + $this->token = new Token(Opcode::DECR->value); return ScannerStatus::OK; vv99: $this->state->incrementStart(); - $this->token = new Token(Compiler::PHVOLT_T_SUB_ASSIGN); + $this->token = new Token(Opcode::SUB_ASSIGN->value); return ScannerStatus::OK; vv101: @@ -985,12 +987,12 @@ public function scanForToken(): ScannerStatus vv102: $this->state->incrementStart(); - $this->token = new Token(Compiler::PHVOLT_T_RANGE); + $this->token = new Token(Opcode::RANGE->value); return ScannerStatus::OK; vv104: $this->state->incrementStart(); - $this->token = new Token(Compiler::PHVOLT_T_DIV_ASSIGN); + $this->token = new Token(Opcode::DIV_ASSIGN->value); return ScannerStatus::OK; vv106: @@ -1013,13 +1015,13 @@ public function scanForToken(): ScannerStatus vv107: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_LESSEQUAL); + $this->token = new Token(Opcode::LESSEQUAL->value); return ScannerStatus::OK; } vv109: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_NOTEQUALS); + $this->token = new Token(Opcode::NOTEQUALS->value); return ScannerStatus::OK; } vv111: @@ -1032,13 +1034,13 @@ public function scanForToken(): ScannerStatus } vv112: { - $this->token = new Token(Compiler::PHVOLT_T_EQUALS); + $this->token = new Token(Opcode::EQUALS->value); return ScannerStatus::OK; } vv113: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_GREATEREQUAL); + $this->token = new Token(Opcode::GREATEREQUAL->value); return ScannerStatus::OK; } vv115: @@ -1184,7 +1186,7 @@ public function scanForToken(): ScannerStatus vv123: $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_DO); + $this->token = new Token(Opcode::DO->value); return ScannerStatus::OK; vv124: @@ -1324,7 +1326,7 @@ public function scanForToken(): ScannerStatus vv132: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_IF); + $this->token = new Token(Opcode::IF->value); return ScannerStatus::OK; } vv133: @@ -1401,7 +1403,7 @@ public function scanForToken(): ScannerStatus } vv134: - $this->token = new Token(Compiler::PHVOLT_T_IN); + $this->token = new Token(Opcode::IN->value); return ScannerStatus::OK; vv135: @@ -1481,14 +1483,14 @@ public function scanForToken(): ScannerStatus } vv136: - if ($this->state->getActiveToken() === Compiler::PHVOLT_T_DOT) { + if ($this->state->getActiveToken() === Opcode::DOT->value) { $this->token = new Token( - Compiler::PHVOLT_T_IDENTIFIER, + Opcode::IDENTIFIER->value, substr($this->state->getRawBuffer(), $start, $this->state->getCursor() - $start) ); } else { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_IS); + $this->token = new Token(Opcode::IS->value); } return ScannerStatus::OK; @@ -1614,7 +1616,7 @@ public function scanForToken(): ScannerStatus } vv143: { - $this->token = new Token(Compiler::PHVOLT_T_OR); + $this->token = new Token(Opcode::OR->value); return ScannerStatus::OK; } vv144: @@ -1766,7 +1768,7 @@ public function scanForToken(): ScannerStatus vv153: $this->state->setWhitespaceControl(false); - $this->token = new Token(Compiler::PHVOLT_T_OPEN_DELIMITER); + $this->token = new Token(Opcode::OPEN_DELIMITER->value); return ScannerStatus::OK; vv154: @@ -1781,31 +1783,31 @@ public function scanForToken(): ScannerStatus vv155: $this->state->setWhitespaceControl(false); $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_OPEN_EDELIMITER); + $this->token = new Token(Opcode::OPEN_EDELIMITER->value); return ScannerStatus::OK; vv156: $this->state->incrementStart(); - $this->state->setMode(Compiler::PHVOLT_MODE_RAW); - $this->token = new Token(Compiler::PHVOLT_T_CLOSE_EDELIMITER); + $this->state->setMode(Mode::RAW->value); + $this->token = new Token(Opcode::CLOSE_EDELIMITER->value); return ScannerStatus::OK; vv158: $this->state->incrementStart(); - $this->token = new Token(Compiler::PHVOLT_T_NOTIDENTICAL); + $this->token = new Token(Opcode::NOTIDENTICAL->value); return ScannerStatus::OK; vv160: $this->state->incrementStart(); - $this->state->setMode(Compiler::PHVOLT_MODE_RAW); + $this->state->setMode(Mode::RAW->value); $this->state->setWhitespaceControl(true); - $this->token = new Token(Compiler::PHVOLT_T_CLOSE_DELIMITER); + $this->token = new Token(Opcode::CLOSE_DELIMITER->value); return ScannerStatus::OK; vv162: $this->state->incrementStart(); - $this->state->setMode(Compiler::PHVOLT_MODE_RAW); + $this->state->setMode(Mode::RAW->value); $this->state->setWhitespaceControl(true); - $this->token = new Token(Compiler::PHVOLT_T_CLOSE_EDELIMITER); + $this->token = new Token(Opcode::CLOSE_EDELIMITER->value); return ScannerStatus::OK; vv164: $vvch = $this->state->incrementStart()->getStart(); @@ -1827,7 +1829,7 @@ public function scanForToken(): ScannerStatus vv166: { $this->token = new Token( - Compiler::PHVOLT_T_DOUBLE, + Opcode::DOUBLE->value, substr($this->state->getRawBuffer(), $start, $this->state->getCursor() - $start) ); return ScannerStatus::OK; @@ -1835,7 +1837,7 @@ public function scanForToken(): ScannerStatus vv167: $this->state->incrementStart(); { - $this->token = new Token(Compiler::PHVOLT_T_IDENTICAL); + $this->token = new Token(Opcode::IDENTICAL->value); return ScannerStatus::OK; } vv169: @@ -1911,7 +1913,7 @@ public function scanForToken(): ScannerStatus } vv170: { - $this->token = new Token(Compiler::PHVOLT_T_AND); + $this->token = new Token(Opcode::AND->value); return ScannerStatus::OK; } vv171: @@ -2138,7 +2140,7 @@ public function scanForToken(): ScannerStatus vv186: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_FOR); + $this->token = new Token(Opcode::FOR->value); return ScannerStatus::OK; } vv187: @@ -2251,7 +2253,7 @@ public function scanForToken(): ScannerStatus vv192: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_NOT); + $this->token = new Token(Opcode::NOT->value); return ScannerStatus::OK; } vv193: @@ -2346,7 +2348,7 @@ public function scanForToken(): ScannerStatus vv196: $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_ODD); + $this->token = new Token(Opcode::ODD->value); return ScannerStatus::OK; vv197: @@ -2423,7 +2425,7 @@ public function scanForToken(): ScannerStatus vv198: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_RAW); + $this->token = new Token(Opcode::RAW->value); return ScannerStatus::OK; } vv199: @@ -2517,13 +2519,13 @@ public function scanForToken(): ScannerStatus } vv202: { - if ($this->state->getActiveToken() === Compiler::PHVOLT_T_DOT) { + if ($this->state->getActiveToken() === Opcode::DOT->value) { $this->token = new Token( - Compiler::PHVOLT_T_IDENTIFIER, + Opcode::IDENTIFIER->value, substr($this->state->getRawBuffer(), $start, $this->state->getCursor() - $start) ); } else { - $this->token = new Token(Compiler::PHVOLT_T_SET); + $this->token = new Token(Opcode::SET->value); } return ScannerStatus::OK; @@ -2569,14 +2571,14 @@ public function scanForToken(): ScannerStatus vv207: $this->state->incrementStart(); $this->state->setWhitespaceControl(false); - $this->token = new Token(Compiler::PHVOLT_T_OPEN_DELIMITER); + $this->token = new Token(Opcode::OPEN_DELIMITER->value); return ScannerStatus::OK; vv209: $this->state->incrementStart(); $this->state->setWhitespaceControl(false); $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_OPEN_EDELIMITER); + $this->token = new Token(Opcode::OPEN_EDELIMITER->value); return ScannerStatus::OK; vv211: @@ -2689,7 +2691,7 @@ public function scanForToken(): ScannerStatus vv216: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_CALL); + $this->token = new Token(Opcode::CALL->value); return ScannerStatus::OK; } vv217: @@ -2765,7 +2767,7 @@ public function scanForToken(): ScannerStatus } vv218: { - $this->token = new Token(Compiler::PHVOLT_T_CASE); + $this->token = new Token(Opcode::CASE->value); return ScannerStatus::OK; } vv219: @@ -2870,7 +2872,7 @@ public function scanForToken(): ScannerStatus } vv223: { - $this->token = new Token(Compiler::PHVOLT_T_ELSE); + $this->token = new Token(Opcode::ELSE->value); return ScannerStatus::OK; } vv224: @@ -3028,7 +3030,7 @@ public function scanForToken(): ScannerStatus vv234: $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_EVEN); + $this->token = new Token(Opcode::EVEN->value); return ScannerStatus::OK; vv235: @@ -3158,7 +3160,7 @@ public function scanForToken(): ScannerStatus } vv242: { - $this->token = new Token(Compiler::PHVOLT_T_NULL); + $this->token = new Token(Opcode::NULL->value); return ScannerStatus::OK; } vv243: @@ -3270,7 +3272,7 @@ public function scanForToken(): ScannerStatus } vv248: { - $this->token = new Token(Compiler::PHVOLT_T_TRUE); + $this->token = new Token(Opcode::TRUE->value); return ScannerStatus::OK; } vv249: @@ -3346,7 +3348,7 @@ public function scanForToken(): ScannerStatus } vv250: { - $this->token = new Token(Compiler::PHVOLT_T_WITH); + $this->token = new Token(Opcode::WITH->value); return ScannerStatus::OK; } vv251: @@ -3443,7 +3445,7 @@ public function scanForToken(): ScannerStatus vv255: $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_BLOCK); + $this->token = new Token(Opcode::BLOCK->value); return ScannerStatus::OK; vv256: @@ -3520,7 +3522,7 @@ public function scanForToken(): ScannerStatus vv257: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_BREAK); + $this->token = new Token(Opcode::BREAK->value); return ScannerStatus::OK; } vv258: @@ -3597,7 +3599,7 @@ public function scanForToken(): ScannerStatus vv259: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_CACHE); + $this->token = new Token(Opcode::CACHE->value); return ScannerStatus::OK; } vv260: @@ -3719,7 +3721,7 @@ public function scanForToken(): ScannerStatus vv266: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_EMPTY); + $this->token = new Token(Opcode::EMPTY->value); return ScannerStatus::OK; } vv267: @@ -3834,7 +3836,7 @@ public function scanForToken(): ScannerStatus } vv272: { - $this->token = new Token(Compiler::PHVOLT_T_ENDIF); + $this->token = new Token(Opcode::ENDIF->value); return ScannerStatus::OK; } vv273: @@ -3946,7 +3948,7 @@ public function scanForToken(): ScannerStatus } vv278: { - $this->token = new Token(Compiler::PHVOLT_T_FALSE); + $this->token = new Token(Opcode::FALSE->value); return ScannerStatus::OK; } vv279: @@ -4050,7 +4052,7 @@ public function scanForToken(): ScannerStatus vv283: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_MACRO); + $this->token = new Token(Opcode::MACRO->value); return ScannerStatus::OK; } vv284: @@ -4215,7 +4217,7 @@ public function scanForToken(): ScannerStatus } vv295: { - $this->token = new Token(Compiler::PHVOLT_T_ELSEIF); + $this->token = new Token(Opcode::ELSEIF->value); return ScannerStatus::OK; } vv296: @@ -4327,7 +4329,7 @@ public function scanForToken(): ScannerStatus } vv301: { - $this->token = new Token(Compiler::PHVOLT_T_ENDFOR); + $this->token = new Token(Opcode::ENDFOR->value); return ScannerStatus::OK; } vv302: @@ -4413,7 +4415,7 @@ public function scanForToken(): ScannerStatus vv304: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_ENDRAW); + $this->token = new Token(Opcode::ENDRAW->value); return ScannerStatus::OK; } vv305: @@ -4447,7 +4449,7 @@ public function scanForToken(): ScannerStatus $this->state->incrementStart(); { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_NOTEQUALS); + $this->token = new Token(Opcode::NOTEQUALS->value); return ScannerStatus::OK; } vv310: @@ -4541,7 +4543,7 @@ public function scanForToken(): ScannerStatus } vv313: { - $this->token = new Token(Compiler::PHVOLT_T_RETURN); + $this->token = new Token(Opcode::RETURN->value); return ScannerStatus::OK; } vv314: @@ -4618,7 +4620,7 @@ public function scanForToken(): ScannerStatus vv315: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_SCALAR); + $this->token = new Token(Opcode::SCALAR->value); return ScannerStatus::OK; } vv316: @@ -4695,7 +4697,7 @@ public function scanForToken(): ScannerStatus vv317: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_SWITCH); + $this->token = new Token(Opcode::SWITCH->value); return ScannerStatus::OK; } vv318: @@ -4797,7 +4799,7 @@ public function scanForToken(): ScannerStatus } vv322: $this->token = new Token( - Compiler::PHVOLT_T_DEFAULT, + Opcode::DEFAULT->value, substr($this->state->getRawBuffer(), $start, $this->state->getCursor() - $start) ); return ScannerStatus::OK; @@ -4875,7 +4877,7 @@ public function scanForToken(): ScannerStatus vv324: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_DEFINED); + $this->token = new Token(Opcode::DEFINED->value); return ScannerStatus::OK; } vv325: @@ -4951,7 +4953,7 @@ public function scanForToken(): ScannerStatus } vv326: { - $this->token = new Token(Compiler::PHVOLT_T_ELSEFOR); + $this->token = new Token(Opcode::ELSEFOR->value); return ScannerStatus::OK; } vv327: @@ -5054,7 +5056,7 @@ public function scanForToken(): ScannerStatus } vv331: { - $this->token = new Token(Compiler::PHVOLT_T_ENDCALL); + $this->token = new Token(Opcode::ENDCALL->value); return ScannerStatus::OK; } vv332: @@ -5148,7 +5150,7 @@ public function scanForToken(): ScannerStatus } vv335: $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_EXTENDS); + $this->token = new Token(Opcode::EXTENDS->value); return ScannerStatus::OK; vv336: @@ -5225,7 +5227,7 @@ public function scanForToken(): ScannerStatus vv337: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_INCLUDE); + $this->token = new Token(Opcode::INCLUDE->value); return ScannerStatus::OK; } vv338: @@ -5311,14 +5313,14 @@ public function scanForToken(): ScannerStatus vv340: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_NUMERIC); + $this->token = new Token(Opcode::NUMERIC->value); return ScannerStatus::OK; } vv341: $this->state->incrementStart(); { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_NOTEQUALS); + $this->token = new Token(Opcode::NOTEQUALS->value); return ScannerStatus::OK; } vv343: @@ -5404,7 +5406,7 @@ public function scanForToken(): ScannerStatus vv345: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_CONTINUE); + $this->token = new Token(Opcode::CONTINUE->value); return ScannerStatus::OK; } vv346: @@ -5489,7 +5491,7 @@ public function scanForToken(): ScannerStatus } vv348: { - $this->token = new Token(Compiler::PHVOLT_T_ENDBLOCK); + $this->token = new Token(Opcode::ENDBLOCK->value); return ScannerStatus::OK; } vv349: @@ -5565,7 +5567,7 @@ public function scanForToken(): ScannerStatus } vv350: { - $this->token = new Token(Compiler::PHVOLT_T_ENDCACHE); + $this->token = new Token(Opcode::ENDCACHE->value); return ScannerStatus::OK; } vv351: @@ -5641,7 +5643,7 @@ public function scanForToken(): ScannerStatus } vv352: { - $this->token = new Token(Compiler::PHVOLT_T_ENDMACRO); + $this->token = new Token(Opcode::ENDMACRO->value); return ScannerStatus::OK; } vv353: @@ -5727,7 +5729,7 @@ public function scanForToken(): ScannerStatus vv355: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_ITERABLE); + $this->token = new Token(Opcode::ITERABLE->value); return ScannerStatus::OK; } vv356: @@ -5821,7 +5823,7 @@ public function scanForToken(): ScannerStatus } vv359: { - $this->token = new Token(Compiler::PHVOLT_T_ENDSWITCH); + $this->token = new Token(Opcode::ENDSWITCH->value); return ScannerStatus::OK; } vv360: @@ -5898,7 +5900,7 @@ public function scanForToken(): ScannerStatus vv361: { $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_AUTOESCAPE); + $this->token = new Token(Opcode::AUTOESCAPE->value); return ScannerStatus::OK; } vv362: @@ -6001,7 +6003,7 @@ public function scanForToken(): ScannerStatus } vv366: $this->state->incrementStatementPosition(); - $this->token = new Token(Compiler::PHVOLT_T_ENDAUTOESCAPE); + $this->token = new Token(Opcode::ENDAUTOESCAPE->value); return ScannerStatus::OK; } } diff --git a/src/Scanner/State.php b/src/Scanner/State.php index 661b46e..35e2399 100644 --- a/src/Scanner/State.php +++ b/src/Scanner/State.php @@ -13,7 +13,7 @@ namespace Phalcon\Volt\Scanner; -use Phalcon\Volt\Compiler; +use Phalcon\Volt\Scanner\Mode; class State { @@ -37,13 +37,14 @@ class State protected int $activeLine = 1; protected int $cursor = 0; protected ?string $end = null; - protected int $mode = Compiler::PHVOLT_MODE_RAW; + protected int $mode; protected ?string $start = null; public function __construct(string $buffer) { $this->rawBuffer = $buffer; $this->startLength = mb_strlen($buffer); + $this->mode = Mode::RAW->value; if ($this->startLength > 0) { $this->setStart($buffer[0]); $this->setEnd($buffer[0]); From df3e8028c04d519e858556a6b49dcb191f80b754 Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 16 Apr 2026 07:24:47 -0500 Subject: [PATCH 2/4] added tests --- tests/unit/Compiler/OpcodeTest.php | 145 +++++++++++++++++++++++++++++ tests/unit/Scanner/ModeTest.php | 27 ++++++ 2 files changed, 172 insertions(+) create mode 100644 tests/unit/Compiler/OpcodeTest.php create mode 100644 tests/unit/Scanner/ModeTest.php diff --git a/tests/unit/Compiler/OpcodeTest.php b/tests/unit/Compiler/OpcodeTest.php new file mode 100644 index 0000000..00a6afb --- /dev/null +++ b/tests/unit/Compiler/OpcodeTest.php @@ -0,0 +1,145 @@ + + * + * 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\Tests\Unit\Compiler; + +use Phalcon\Volt\Compiler\Opcode; +use PHPUnit\Framework\TestCase; + +final class OpcodeTest extends TestCase +{ + public function testOpcodeValues(): void + { + // ASCII-value tokens + $this->assertSame(43, Opcode::ADD->value); + $this->assertSame(61, Opcode::ASSIGN->value); + $this->assertSame(125, Opcode::CBRACKET_CLOSE->value); + $this->assertSame(123, Opcode::CBRACKET_OPEN->value); + $this->assertSame(126, Opcode::CONCAT->value); + $this->assertSame(47, Opcode::DIV->value); + $this->assertSame(46, Opcode::DOT->value); + $this->assertSame(62, Opcode::GREATER->value); + $this->assertSame(60, Opcode::LESS->value); + $this->assertSame(37, Opcode::MOD->value); + $this->assertSame(42, Opcode::MUL->value); + $this->assertSame(33, Opcode::NOT->value); + $this->assertSame(41, Opcode::PARENTHESES_CLOSE->value); + $this->assertSame(40, Opcode::PARENTHESES_OPEN->value); + $this->assertSame(124, Opcode::PIPE->value); + $this->assertSame(63, Opcode::QUESTION->value); + $this->assertSame(93, Opcode::SBRACKET_CLOSE->value); + $this->assertSame(91, Opcode::SBRACKET_OPEN->value); + $this->assertSame(45, Opcode::SUB->value); + + // Numeric-range tokens + $this->assertSame(257, Opcode::IGNORE->value); + $this->assertSame(258, Opcode::INTEGER->value); + $this->assertSame(259, Opcode::DOUBLE->value); + $this->assertSame(260, Opcode::STRING->value); + $this->assertSame(261, Opcode::NULL->value); + $this->assertSame(262, Opcode::FALSE->value); + $this->assertSame(263, Opcode::TRUE->value); + $this->assertSame(265, Opcode::IDENTIFIER->value); + $this->assertSame(266, Opcode::AND->value); + $this->assertSame(267, Opcode::OR->value); + $this->assertSame(269, Opcode::COMMA->value); + $this->assertSame(270, Opcode::LESSEQUAL->value); + $this->assertSame(271, Opcode::GREATEREQUAL->value); + $this->assertSame(272, Opcode::EQUALS->value); + $this->assertSame(273, Opcode::NOTEQUALS->value); + $this->assertSame(274, Opcode::IDENTICAL->value); + $this->assertSame(275, Opcode::NOTIDENTICAL->value); + $this->assertSame(276, Opcode::RANGE->value); + $this->assertSame(277, Opcode::COLON->value); + $this->assertSame(278, Opcode::POW->value); + $this->assertSame(279, Opcode::INCR->value); + $this->assertSame(280, Opcode::DECR->value); + $this->assertSame(281, Opcode::ADD_ASSIGN->value); + $this->assertSame(282, Opcode::SUB_ASSIGN->value); + $this->assertSame(283, Opcode::MUL_ASSIGN->value); + $this->assertSame(284, Opcode::DIV_ASSIGN->value); + $this->assertSame(300, Opcode::IF->value); + $this->assertSame(301, Opcode::ELSE->value); + $this->assertSame(302, Opcode::ELSEIF->value); + $this->assertSame(303, Opcode::ENDIF->value); + $this->assertSame(304, Opcode::FOR->value); + $this->assertSame(305, Opcode::ENDFOR->value); + $this->assertSame(306, Opcode::SET->value); + $this->assertSame(307, Opcode::BLOCK->value); + $this->assertSame(308, Opcode::ENDBLOCK->value); + $this->assertSame(309, Opcode::IN->value); + $this->assertSame(310, Opcode::EXTENDS->value); + $this->assertSame(311, Opcode::IS->value); + $this->assertSame(312, Opcode::DEFINED->value); + $this->assertSame(313, Opcode::INCLUDE->value); + $this->assertSame(314, Opcode::CACHE->value); + $this->assertSame(315, Opcode::ENDCACHE->value); + $this->assertSame(316, Opcode::DO->value); + $this->assertSame(317, Opcode::AUTOESCAPE->value); + $this->assertSame(318, Opcode::ENDAUTOESCAPE->value); + $this->assertSame(319, Opcode::CONTINUE->value); + $this->assertSame(320, Opcode::BREAK->value); + $this->assertSame(321, Opcode::ELSEFOR->value); + $this->assertSame(322, Opcode::MACRO->value); + $this->assertSame(323, Opcode::ENDMACRO->value); + $this->assertSame(324, Opcode::WITH->value); + $this->assertSame(325, Opcode::CALL->value); + $this->assertSame(326, Opcode::ENDCALL->value); + $this->assertSame(327, Opcode::RETURN->value); + $this->assertSame(330, Opcode::OPEN_DELIMITER->value); + $this->assertSame(331, Opcode::CLOSE_DELIMITER->value); + $this->assertSame(332, Opcode::OPEN_EDELIMITER->value); + $this->assertSame(333, Opcode::CLOSE_EDELIMITER->value); + $this->assertSame(350, Opcode::FCALL->value); + $this->assertSame(354, Opcode::EXPR->value); + $this->assertSame(355, Opcode::QUALIFIED->value); + $this->assertSame(356, Opcode::ENCLOSED->value); + $this->assertSame(357, Opcode::RAW_FRAGMENT->value); + $this->assertSame(358, Opcode::EMPTY_STATEMENT->value); + $this->assertSame(359, Opcode::ECHO->value); + $this->assertSame(360, Opcode::ARRAY->value); + $this->assertSame(361, Opcode::ARRAYACCESS->value); + $this->assertSame(362, Opcode::NOT_ISSET->value); + $this->assertSame(363, Opcode::ISSET->value); + $this->assertSame(364, Opcode::RESOLVED_EXPR->value); + $this->assertSame(365, Opcode::SLICE->value); + $this->assertSame(366, Opcode::TERNARY->value); + $this->assertSame(367, Opcode::NOT_IN->value); + $this->assertSame(368, Opcode::MINUS->value); + $this->assertSame(369, Opcode::PLUS->value); + $this->assertSame(380, Opcode::EMPTY->value); + $this->assertSame(381, Opcode::EVEN->value); + $this->assertSame(382, Opcode::ODD->value); + $this->assertSame(383, Opcode::NUMERIC->value); + $this->assertSame(384, Opcode::SCALAR->value); + $this->assertSame(385, Opcode::ITERABLE->value); + $this->assertSame(386, Opcode::ISEMPTY->value); + $this->assertSame(387, Opcode::ISEVEN->value); + $this->assertSame(388, Opcode::ISODD->value); + $this->assertSame(389, Opcode::ISNUMERIC->value); + $this->assertSame(390, Opcode::ISSCALAR->value); + $this->assertSame(391, Opcode::ISITERABLE->value); + $this->assertSame(392, Opcode::NOT_ISEMPTY->value); + $this->assertSame(393, Opcode::NOT_ISEVEN->value); + $this->assertSame(394, Opcode::NOT_ISODD->value); + $this->assertSame(395, Opcode::NOT_ISNUMERIC->value); + $this->assertSame(396, Opcode::NOT_ISSCALAR->value); + $this->assertSame(397, Opcode::NOT_ISITERABLE->value); + $this->assertSame(400, Opcode::RAW->value); + $this->assertSame(401, Opcode::ENDRAW->value); + $this->assertSame(411, Opcode::SWITCH->value); + $this->assertSame(412, Opcode::CASE->value); + $this->assertSame(413, Opcode::DEFAULT->value); + $this->assertSame(414, Opcode::ENDSWITCH->value); + } +} \ No newline at end of file diff --git a/tests/unit/Scanner/ModeTest.php b/tests/unit/Scanner/ModeTest.php new file mode 100644 index 0000000..5429c22 --- /dev/null +++ b/tests/unit/Scanner/ModeTest.php @@ -0,0 +1,27 @@ + + * + * 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\Tests\Unit\Scanner; + +use Phalcon\Volt\Scanner\Mode; +use PHPUnit\Framework\TestCase; + +final class ModeTest extends TestCase +{ + public function testModeValues(): void + { + $this->assertSame(1, Mode::CODE->value); + $this->assertSame(2, Mode::COMMENT->value); + $this->assertSame(0, Mode::RAW->value); + } +} \ No newline at end of file From b4104bc965f2ae8005d26c5a4931138951d7259c Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 16 Apr 2026 07:26:24 -0500 Subject: [PATCH 3/4] phpcs --- composer.lock | 8 ++++---- src/Compiler/Opcode.php | 2 +- tests/unit/Compiler/OpcodeTest.php | 32 +++++++++++++++--------------- tests/unit/Scanner/ModeTest.php | 2 +- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/composer.lock b/composer.lock index 3320ed8..88c261f 100644 --- a/composer.lock +++ b/composer.lock @@ -347,11 +347,11 @@ }, { "name": "phpstan/phpstan", - "version": "2.1.47", + "version": "2.1.48", "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/79015445d8bd79e62b29140f12e5bfced1dcca65", - "reference": "79015445d8bd79e62b29140f12e5bfced1dcca65", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/231397213efb7c0a066ee024b5c3c87f2d3adfa0", + "reference": "231397213efb7c0a066ee024b5c3c87f2d3adfa0", "shasum": "" }, "require": { @@ -396,7 +396,7 @@ "type": "github" } ], - "time": "2026-04-13T15:49:08+00:00" + "time": "2026-04-15T20:24:19+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Compiler/Opcode.php b/src/Compiler/Opcode.php index d2bdcc5..51e079a 100644 --- a/src/Compiler/Opcode.php +++ b/src/Compiler/Opcode.php @@ -133,4 +133,4 @@ enum Opcode: int case TERNARY = 366; case TRUE = 263; case WITH = 324; -} \ No newline at end of file +} diff --git a/tests/unit/Compiler/OpcodeTest.php b/tests/unit/Compiler/OpcodeTest.php index 00a6afb..7402a6e 100644 --- a/tests/unit/Compiler/OpcodeTest.php +++ b/tests/unit/Compiler/OpcodeTest.php @@ -21,25 +21,25 @@ final class OpcodeTest extends TestCase public function testOpcodeValues(): void { // ASCII-value tokens - $this->assertSame(43, Opcode::ADD->value); - $this->assertSame(61, Opcode::ASSIGN->value); + $this->assertSame(43, Opcode::ADD->value); + $this->assertSame(61, Opcode::ASSIGN->value); $this->assertSame(125, Opcode::CBRACKET_CLOSE->value); $this->assertSame(123, Opcode::CBRACKET_OPEN->value); $this->assertSame(126, Opcode::CONCAT->value); - $this->assertSame(47, Opcode::DIV->value); - $this->assertSame(46, Opcode::DOT->value); - $this->assertSame(62, Opcode::GREATER->value); - $this->assertSame(60, Opcode::LESS->value); - $this->assertSame(37, Opcode::MOD->value); - $this->assertSame(42, Opcode::MUL->value); - $this->assertSame(33, Opcode::NOT->value); - $this->assertSame(41, Opcode::PARENTHESES_CLOSE->value); - $this->assertSame(40, Opcode::PARENTHESES_OPEN->value); + $this->assertSame(47, Opcode::DIV->value); + $this->assertSame(46, Opcode::DOT->value); + $this->assertSame(62, Opcode::GREATER->value); + $this->assertSame(60, Opcode::LESS->value); + $this->assertSame(37, Opcode::MOD->value); + $this->assertSame(42, Opcode::MUL->value); + $this->assertSame(33, Opcode::NOT->value); + $this->assertSame(41, Opcode::PARENTHESES_CLOSE->value); + $this->assertSame(40, Opcode::PARENTHESES_OPEN->value); $this->assertSame(124, Opcode::PIPE->value); - $this->assertSame(63, Opcode::QUESTION->value); - $this->assertSame(93, Opcode::SBRACKET_CLOSE->value); - $this->assertSame(91, Opcode::SBRACKET_OPEN->value); - $this->assertSame(45, Opcode::SUB->value); + $this->assertSame(63, Opcode::QUESTION->value); + $this->assertSame(93, Opcode::SBRACKET_CLOSE->value); + $this->assertSame(91, Opcode::SBRACKET_OPEN->value); + $this->assertSame(45, Opcode::SUB->value); // Numeric-range tokens $this->assertSame(257, Opcode::IGNORE->value); @@ -142,4 +142,4 @@ public function testOpcodeValues(): void $this->assertSame(413, Opcode::DEFAULT->value); $this->assertSame(414, Opcode::ENDSWITCH->value); } -} \ No newline at end of file +} diff --git a/tests/unit/Scanner/ModeTest.php b/tests/unit/Scanner/ModeTest.php index 5429c22..87a63be 100644 --- a/tests/unit/Scanner/ModeTest.php +++ b/tests/unit/Scanner/ModeTest.php @@ -24,4 +24,4 @@ public function testModeValues(): void $this->assertSame(2, Mode::COMMENT->value); $this->assertSame(0, Mode::RAW->value); } -} \ No newline at end of file +} From d7a08d163d65b4e50114af9275c790defa85baef Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Thu, 16 Apr 2026 07:28:04 -0500 Subject: [PATCH 4/4] phpcs --- src/Parser/Parser.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php index addf51e..188a912 100644 --- a/src/Parser/Parser.php +++ b/src/Parser/Parser.php @@ -127,7 +127,12 @@ public function parse(string $code, string $templatePath = 'eval code'): array CompilerOpcode::ENDFOR->value => $this->handleEndfor($parser, $state), CompilerOpcode::SWITCH->value => $this->handleSwitch($parser, $parserStatus, $state), CompilerOpcode::CASE->value => $this->handleCase($parser, $parserStatus), - CompilerOpcode::DEFAULT->value => $this->handleDefault($parser, $parserStatus, $token, $state), + CompilerOpcode::DEFAULT->value => $this->handleDefault( + $parser, + $parserStatus, + $token, + $state + ), CompilerOpcode::ENDSWITCH->value => $this->handleEndswitch($parser, $parserStatus, $state), CompilerOpcode::RAW_FRAGMENT->value => $this->handleRawFragment( $parser,