Passed
Push — master ( b8c5dd...52423a )
by Koen
12:09
created

InputStream::next()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.4285
nc 2
cc 2
eloc 8
nop 0
crap 2
1
<?php
2
3
namespace Vlaswinkel\Lua;
4
5
/**
6
 * Class InputStream
7
 *
8
 * @see     http://lisperator.net/pltut/parser/input-stream
9
 *
10
 * @author  Koen Vlaswinkel <[email protected]>
11
 * @package Vlaswinkel\Lua
12
 */
13
class InputStream {
14
    /**
15
     * @var string
16
     */
17
    private $input;
18
19
    /**
20
     * @var int
21
     */
22
    private $position = 0;
23
    /**
24
     * @var int
25
     */
26
    private $line = 1;
27
    /**
28
     * @var int
29
     */
30
    private $column = 0;
31
32
    /**
33
     * InputStream constructor.
34
     *
35
     * @param string $input
36
     */
37 41
    public function __construct($input) {
38 41
        $this->input = $input;
39 41
    }
40
41 40
    public function next() {
42 40
        $char = $this->input[$this->position++];
43 40
        if ($char == "\n") {
44 11
            $this->line++;
45 11
            $this->column = 0;
46 11
        } else {
47 40
            $this->column++;
48
        }
49 40
        return $char;
50
    }
51
52 35
    public function peek($pos = 0) {
53 35
        if ($this->eof($pos)) {
54 1
            $this->error('Unexpected end of input');
55
        }
56 35
        return $this->input[$this->position + $pos];
57
    }
58
59 36
    public function eof($pos = 0) {
60 36
        return $this->position + $pos >= strlen($this->input);
61
    }
62
63 7
    public function error($msg) {
64 7
        throw new ParseException($msg . ' (' . $this->line . ':' . $this->column . ')');
65
    }
66
}