-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputstream.go
58 lines (51 loc) · 852 Bytes
/
inputstream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package runevm
import (
"fmt"
"os"
)
type InputStream struct {
filepath string
source string
Pos int
line int
Col int
}
func newInputStream(source string, filepath string) *InputStream {
p := &InputStream{
filepath: filepath,
source: source,
Pos: 0,
line: 1,
Col: 1,
}
return p
}
func (p *InputStream) next() byte {
if p.Pos >= len(p.source) {
return 0
}
ch := p.source[p.Pos]
p.Pos++
if ch == '\n' {
p.line++
p.Col = 1
} else {
p.Col++
}
return ch
}
func (p *InputStream) peek() byte {
if p.Pos >= len(p.source) {
return 0
}
return p.source[p.Pos]
}
func (p *InputStream) eof() bool {
ch := p.peek()
eof := ch == 0
return eof
}
func (p *InputStream) error(tok *Token, msg string) {
fmt.Printf("error [%s:%d:%d]: %s\n", tok.File, tok.Line, tok.Col, msg)
os.Exit(0)
}