TextMate-style syntax highlighting in PHP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

87 lines
2.1 KiB

<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace dW\Highlighter\Scope;
class Data {
protected string $data;
protected int $_position = 0;
protected int $endPosition;
public function __construct(string $data) {
$this->data = $data;
$this->endPosition = strlen($data);
}
public function consume(int $length = 1): string|bool {
if ($this->_position === $this->endPosition) {
return false;
}
$stop = $this->_position + $length - 1;
if ($stop > $this->endPosition) {
$stop = $this->endPosition;
}
$result = '';
while ($this->_position <= $stop) {
$result .= $this->data[$this->_position++];
}
return $result;
}
public function consumeIf(string $match): string|bool {
return $this->consumeWhile($match, 1);
}
public function consumeWhile(string $match, $limit = null): string|bool {
if ($this->_position === $this->endPosition) {
return false;
}
$length = strspn($this->data, $match, $this->_position, $limit);
if ($length === 0) {
return '';
}
return $this->consume($length);
}
public function peek(int $length = 1): string|bool {
if ($this->_position === $this->endPosition) {
return false;
}
$stop = $this->_position + $length - 1;
if ($stop >= $this->endPosition) {
$stop = $this->endPosition;
}
$output = '';
for ($i = $this->_position; $i <= $stop; $i++) {
$output .= $this->data[$i];
}
return $output;
}
public function unconsumeTo(int $position = 1): bool {
if ($position < 0 || $position > $this->endPosition) {
return false;
}
$this->_position = $position;
return true;
}
public function __get(string $name) {
if ($name === 'position') {
return $this->_position;
}
}
}