Skip to content

Commit

Permalink
misc: simplify symbol parsing algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Aug 18, 2023
1 parent ae83032 commit f260cfb
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions src/Type/Parser/ParserSymbols.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,35 @@ final class ParserSymbols
{
private const OPERATORS = [' ', '|', '&', '<', '>', '[', ']', '{', '}', ':', '?', ',', "'", '"'];

private ?string $current = null;

/** @var list<string> */
private array $symbols = [];

public function __construct(string $string)
{
$current = null;
$quote = null;

foreach (str_split($string) as $char) {
if ($quote !== null) {
if ($char === $quote) {
$this->addCurrent();
$this->symbols[] = $char;

$quote = null;
} else {
$this->current .= $char;
}
} elseif (in_array($char, self::OPERATORS, true)) {
$this->addCurrent();
$this->symbols[] = $char;

if ($char === '"' || $char === "'") {
$quote = $char;
}
} else {
$this->current .= $char;
if ($char === $quote) {
$quote = null;
} elseif ($char === '"' || $char === "'") {
$quote = $char;
} elseif ($quote !== null || ! in_array($char, self::OPERATORS, true)) {
$current .= $char;
continue;
}

if ($current !== null) {
$this->symbols[] = $current;
$current = null;
}

$this->symbols[] = $char;
}

$this->addCurrent();
if ($current !== null) {
$this->symbols[] = $current;
}

$this->symbols = array_map('trim', $this->symbols);
$this->symbols = array_filter($this->symbols, static fn ($value) => $value !== '');
Expand All @@ -55,14 +52,6 @@ public function all(): array
return $this->symbols;
}

private function addCurrent(): void
{
if ($this->current !== null) {
$this->symbols[] = $this->current;
$this->current = null;
}
}

private function mergeDoubleColons(): void
{
foreach ($this->symbols as $key => $symbol) {
Expand Down

0 comments on commit f260cfb

Please sign in to comment.