43 lines
818 B
PHP
43 lines
818 B
PHP
<?php
|
|
|
|
namespace NoccyLabs\PhpCalc;
|
|
/**
|
|
* Helper class to iterate over a list of tokens
|
|
*
|
|
* @license GPL V3 or later
|
|
* @copyright (c) 2022, NoccyLabs
|
|
*/
|
|
class TokenList
|
|
{
|
|
|
|
private array $toks;
|
|
|
|
private int $ptr = 0;
|
|
|
|
public function __construct(array $toks)
|
|
{
|
|
$this->toks = $toks;
|
|
}
|
|
|
|
public function current(): ?string
|
|
{
|
|
return ($this->ptr >= count($this->toks) ? null : $this->toks[$this->ptr]);
|
|
}
|
|
|
|
public function read(): ?string
|
|
{
|
|
$v = $this->current();
|
|
$this->next();
|
|
return $v;
|
|
}
|
|
|
|
public function next(): ?string
|
|
{
|
|
if ($this->ptr < count($this->toks)) {
|
|
$this->ptr++;
|
|
return ($this->ptr < count($this->toks) ? $this->toks[$this->ptr] : null);
|
|
}
|
|
return null;
|
|
}
|
|
}
|