InputStream   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 54
c 0
b 0
f 0
ccs 19
cts 20
cp 0.95
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A next() 0 10 2
A peek() 0 6 2
A eof() 0 3 1
A error() 0 3 1
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 46
    public function __construct($input) {
38 46
        $this->input = $input;
39 46
    }
40
41 45
    public function next() {
42 45
        $char = $this->input[$this->position++];
43 45
        if ($char == "\n") {
44 12
            $this->line++;
45 12
            $this->column = 0;
46 12
        } else {
47 45
            $this->column++;
48
        }
49 45
        return $char;
50
    }
51
52 40
    public function peek($pos = 0) {
53 40
        if ($this->eof($pos)) {
54 1
            $this->error('Unexpected end of input');
55
        }
56 40
        return $this->input[$this->position + $pos];
57
    }
58
59 41
    public function eof($pos = 0) {
60 41
        return $this->position + $pos >= strlen($this->input);
61
    }
62
63 10
    public function error($msg) {
64 10
        throw new ParseException($msg . ' (' . $this->line . ':' . $this->column . ')');
65
    }
66
}