Skip to content

Commit

Permalink
Implement missing exceptions during parse syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Nov 2, 2024
1 parent c3988f2 commit 4a84c9b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
5 changes: 1 addition & 4 deletions files/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ class phvolt_yyStackEntry

class phvolt_Parser
{
public Status $status;

protected array $output = [];

public function __construct(Status $status)
public function __construct(private Status $status)
{
$this->status = $status;
}

public function getOutput(): array
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2221,11 +2221,11 @@ public function getUniquePrefix(): string
* $compiler->parse("{% raw %}{{ 3 + 2 }}{% endraw %}")
* );
*```
* @throws Exception
*/
public function parse(string $viewCode): array
{
// TODO: rewrite from C
return phvolt_parse_view($viewCode, 'eval code');
return (new Parser($viewCode))->parseView("eval code");
}

/**
Expand Down
21 changes: 17 additions & 4 deletions src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ public function parseView(string $templatePath): array

$debug = fopen('log.txt', 'w+');

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

$parser = new phvolt_Parser($parserStatus);
$parser->phvolt_Trace($debug);

while (0 <= $scannerStatus = $scanner->scanForToken()) {
$state = $parserStatus->getState();
$this->token = $scanner->getToken();
$parserStatus->setToken($this->token);
$state->setStartLength($codeLength - $state->getStartLength());

$opcode = $this->token->getOpcode();
$state->setActiveToken($opcode);
Expand Down Expand Up @@ -600,16 +603,26 @@ public function parseView(string $templatePath): array
}

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

$state->setEnd($state->getStart());
}

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

$parserStatus->getState()->setStartLength(0);

if ($scannerStatus === Scanner::PHVOLT_SCANNER_RETCODE_EOF) {
$parser->phvolt_(0);
}

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

return $parser->getOutput();
}

Expand Down

0 comments on commit 4a84c9b

Please sign in to comment.