-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.go
105 lines (89 loc) · 1.61 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package script
type Rune struct {
Value rune
Position
}
func (r *Rune) Rune() rune {
if r == nil {
return 0
}
return r.Value
}
type RuneBuffer struct {
buffer Buffer[rune]
pos Position
max Position
}
func NewRuneBuffer(data string) *RuneBuffer {
r := &RuneBuffer{
buffer: NewBuffer[rune](len(data)),
}
r.buffer.Set([]rune(data))
return r
}
func (b *RuneBuffer) Set(data string) {
b.buffer.Set([]rune(data))
b.pos = Position{}
b.max = Position{}
}
func (b RuneBuffer) Len() int {
return b.buffer.n
}
func (b *RuneBuffer) Read() (r *Rune) {
r = b.Peek()
if r != nil {
b.buffer.i++
b.pos.Index = b.buffer.i
if r.Value == '\n' {
b.pos.Column = 0
b.pos.Line++
} else {
b.pos.Column++
}
if b.pos.Index > b.max.Index {
b.max = b.pos
}
}
return
}
func (b RuneBuffer) Peek() (r *Rune) {
p := b.buffer.Peek()
if p != nil {
r = &Rune{
Value: *p,
Position: b.pos,
}
}
return
}
func (b RuneBuffer) Pos() Position {
return b.pos
}
func (b RuneBuffer) Max() Position {
return b.max
}
func (b RuneBuffer) Get(start, end int) string {
return string(b.GetRunes(start, end))
}
func (b RuneBuffer) GetRunes(start, end int) []rune {
return b.buffer.data[start:end]
}
func (b *RuneBuffer) Reset(pos Position) {
b.buffer.i = pos.Index
b.pos = pos
}
func (b *RuneBuffer) Ended() bool {
return b.buffer.Ended()
}
// The parsed output
type Input struct {
Matchers []MatcherMatch
Runes *RuneBuffer
cycles Set[cycleState]
}
// A state that should not repeat itself, this is to avoid infinite loops
type cycleState struct {
matcher int
reset int
greediness int
}