Total Complexity | 8 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Coverage | 95% |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
5 | class StringLexer { |
||
6 | |||
7 | private int $pos = 0; |
||
8 | |||
9 | private string $string; |
||
10 | |||
11 | /** |
||
12 | * StringScanner constructor. |
||
13 | */ |
||
14 | 15 | public function __construct( string $string ) { |
|
15 | 15 | $this->string = $string; |
|
16 | } |
||
17 | |||
18 | 15 | public function peek( int $size = 1 ) : CharData { |
|
19 | 15 | $str = substr($this->string, $this->pos, $size); |
|
20 | |||
21 | 15 | return new CharData($str, strlen($str) < $size); |
|
22 | } |
||
23 | |||
24 | 15 | public function pos() : int { |
|
25 | 15 | return $this->pos; |
|
26 | } |
||
27 | |||
28 | 15 | public function next() : CharData { |
|
29 | 15 | $data = $this->peek(); |
|
30 | |||
31 | 15 | $this->pos += $data->length(); |
|
32 | |||
33 | 15 | return $data; |
|
34 | } |
||
35 | |||
36 | 15 | public function rewind() : void { |
|
37 | 15 | $this->pos--; |
|
38 | 15 | if( $this->pos < 0 ) { |
|
39 | //@todo custom exception |
||
40 | throw new \RuntimeException('cannot rewind'); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | 14 | public function hasPrefix( string $prefix ) : bool { |
|
45 | 14 | $peek = $this->peek(strlen($prefix)); |
|
46 | |||
47 | 14 | return $peek->getString() === $prefix; |
|
48 | } |
||
49 | |||
50 | 14 | public function substr( int $start, int $length ) : string { |
|
52 | } |
||
53 | |||
54 | } |
||
55 |