-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
72 lines (58 loc) · 1.18 KB
/
input.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package parser
import "slices"
type ParserInput struct {
source []rune
position int
line int
column int
}
func NewParserInput(source string) ParserInput {
return ParserInput{
source: []rune(source),
position: 0,
line: 1,
column: 1,
}
}
func (p ParserInput) Equal(other ParserInput) bool {
return slices.Equal(p.source, other.source) && p.position == other.position && p.line == other.line && p.column == other.column
}
func (p ParserInput) Advance() ParserInput {
line := p.line
if p.Current() == '\n' {
line++
}
column := p.column
if p.Current() == '\n' {
column = 1
} else {
column++
}
return ParserInput{
source: p.source,
position: p.position + 1,
line: line,
column: column,
}
}
func (p ParserInput) Source() string {
return string(p.source)
}
func (p ParserInput) Current() rune {
return p.source[p.position]
}
func (p ParserInput) IsEnd() bool {
return p.position >= len(p.source)
}
func (p ParserInput) Position() int {
return p.position
}
func (p ParserInput) Line() int {
return p.line
}
func (p ParserInput) Column() int {
return p.column
}
func (p ParserInput) String() string {
return string(p.source[p.position:])
}