-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrepeat.go
159 lines (121 loc) · 2.49 KB
/
repeat.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package bpl
import (
"bufio"
"io"
"reflect"
)
// -----------------------------------------------------------------------------
func directRepeat(R Ruler, in *bufio.Reader, ctx *Context) (v interface{}, err error) {
_, err = R.Match(in, ctx)
if err != nil {
return
}
for {
_, err = in.Peek(1)
if err != nil {
if err == io.EOF {
return nil, nil
}
return
}
_, err = R.Match(in, ctx)
if err != nil {
return
}
}
}
func repeat(R Ruler, in *bufio.Reader, ctx *Context) (v interface{}, err error) {
if _, ok := R.(*seq); ok {
return directRepeat(R, in, ctx)
}
_, err = R.Match(in, ctx.NewSub())
if err != nil {
return
}
for {
_, err = in.Peek(1)
if err != nil {
if err == io.EOF {
return nil, nil
}
return
}
_, err = R.Match(in, ctx.NewSub())
if err != nil {
return
}
}
}
// -----------------------------------------------------------------------------
type repeat0 struct {
r Ruler
}
func (p *repeat0) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
_, err = in.Peek(1)
if err != nil {
if err == io.EOF {
return nil, nil
}
return
}
return repeat(p.r, in, ctx)
}
func (p *repeat0) RetType() reflect.Type {
return TyInterface
}
func (p *repeat0) SizeOf() int {
return -1
}
// Repeat0 returns a matching unit that matches R*
//
func Repeat0(R Ruler) Ruler {
return &repeat0{r: R}
}
// -----------------------------------------------------------------------------
type repeat1 struct {
r Ruler
}
func (p *repeat1) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
_, err = in.Peek(1)
if err != nil {
return
}
return repeat(p.r, in, ctx)
}
func (p *repeat1) RetType() reflect.Type {
return TyInterface
}
func (p *repeat1) SizeOf() int {
return -1
}
// Repeat1 returns a matching unit that matches R+
//
func Repeat1(R Ruler) Ruler {
return &repeat1{r: R}
}
// -----------------------------------------------------------------------------
type repeat01 struct {
r Ruler
}
func (p *repeat01) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
_, err = in.Peek(1)
if err != nil {
if err == io.EOF {
return nil, nil
}
return
}
return p.r.Match(in, ctx)
}
func (p *repeat01) RetType() reflect.Type {
return TyInterface
}
func (p *repeat01) SizeOf() int {
return -1
}
// Repeat01 returns a matching unit that matches R?
//
func Repeat01(R Ruler) Ruler {
return &repeat01{r: R}
}
// -----------------------------------------------------------------------------