Skip to content

Commit

Permalink
Re-add unclosed ( [ check
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 31, 2025
1 parent 4811d3f commit 29e29ea
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Lexer
private $position;
private $positions;
private $currentVarBlockLine;
private array $openingBrackets = ['{', '(', '['];
private array $closingBrackets = ['}', ')', ']'];

public const STATE_DATA = 0;
public const STATE_BLOCK = 1;
Expand Down Expand Up @@ -345,6 +347,9 @@ private function lexExpression(): void
if (str_contains(self::PUNCTUATION, $operator)) {
$type = Token::PUNCTUATION_TYPE;
}
if (in_array($operator, $this->openingBrackets)) {
$this->checkBrackets($operator);
}
$this->pushToken($type, $operator);
$this->moveCursor($match[0]);
}
Expand All @@ -360,22 +365,7 @@ private function lexExpression(): void
}
// punctuation
elseif (str_contains(self::PUNCTUATION, $this->code[$this->cursor])) {
// opening bracket
if (str_contains('{', $this->code[$this->cursor])) {
$this->brackets[] = [$this->code[$this->cursor], $this->lineno];
}
// closing bracket
elseif (str_contains('}', $this->code[$this->cursor])) {
if (!$this->brackets) {
throw new SyntaxError(\sprintf('Unexpected "%s".', $this->code[$this->cursor]), $this->lineno, $this->source);
}

[$expect, $lineno] = array_pop($this->brackets);
if ($this->code[$this->cursor] != strtr($expect, '{', '}')) {
throw new SyntaxError(\sprintf('Unclosed "%s".', $expect), $lineno, $this->source);
}
}

$this->checkBrackets($this->code[$this->cursor]);
$this->pushToken(Token::PUNCTUATION_TYPE, $this->code[$this->cursor]);
++$this->cursor;
}
Expand Down Expand Up @@ -590,4 +580,22 @@ private function popState(): void

$this->state = array_pop($this->states);
}

private function checkBrackets(string $code): void
{
// opening bracket
if (in_array($code, $this->openingBrackets)) {
$this->brackets[] = [$code, $this->lineno];
} elseif (in_array($code, $this->closingBrackets)) {
// closing bracket
if (!$this->brackets) {
throw new SyntaxError(\sprintf('Unexpected "%s".', $code), $this->lineno, $this->source);
}

[$expect, $lineno] = array_pop($this->brackets);
if ($code !== str_replace($this->openingBrackets, $this->closingBrackets, $expect)) {
throw new SyntaxError(\sprintf('Unclosed "%s".', $expect), $lineno, $this->source);
}
}
}
}

0 comments on commit 29e29ea

Please sign in to comment.