This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
280 lines (224 loc) · 5.41 KB
/
context.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package bpl
import (
"bufio"
"bytes"
"encoding/hex"
"errors"
"fmt"
"reflect"
"runtime/debug"
"qiniupkg.com/x/bufiox.v7"
"qlang.io/exec.v2"
)
// -----------------------------------------------------------------------------
// A Globals represents global variables.
//
type Globals struct {
Impl map[string]interface{}
}
// NewGlobals returns a `Globals` instance.
//
func NewGlobals() Globals {
return Globals{
Impl: make(map[string]interface{}),
}
}
// GetAndSetVar gets old value of a global variable and sets new value to it.
//
func (p Globals) GetAndSetVar(name string, v interface{}) (old interface{}, ok bool) {
old, ok = p.Impl[name]
p.Impl[name] = v
return
}
// SetVar sets a global variable to new value.
//
func (p Globals) SetVar(name string, v interface{}) {
p.Impl[name] = v
}
// Var returns value of a global variable.
//
func (p Globals) Var(name string) (v interface{}, ok bool) {
v, ok = p.Impl[name]
return
}
// -----------------------------------------------------------------------------
// A Context represents the matching context of bpl.
//
type Context struct {
dom interface{}
Stack *exec.Stack
Parent *Context
Globals Globals
}
// NewContext returns a new matching Context.
//
func NewContext() *Context {
gbl := NewGlobals()
stk := exec.NewStack()
return &Context{Globals: gbl, Stack: stk}
}
// NewSub returns a new sub Context.
//
func (p *Context) NewSub() *Context {
return &Context{Parent: p, Globals: p.Globals, Stack: p.Stack}
}
func (p *Context) requireVarSlice() []interface{} {
var vars []interface{}
if p.dom == nil {
vars = make([]interface{}, 0, 4)
} else if domv, ok := p.dom.([]interface{}); ok {
vars = domv
} else {
panic("dom type isn't []interface{}")
}
return vars
}
// SetVar sets a new variable to matching context.
//
func (p *Context) SetVar(name string, v interface{}) {
if _, ok := p.Globals.Var(name); ok {
panic(fmt.Errorf("variable `%s` exists globally", name))
}
var vars map[string]interface{}
if p.dom == nil {
vars = make(map[string]interface{})
p.dom = vars
} else if domv, ok := p.dom.(map[string]interface{}); ok {
if _, ok = domv[name]; ok {
panic(fmt.Errorf("variable `%s` exists in dom", name))
}
vars = domv
} else {
panic("dom type isn't map[string]interface{}")
}
vars[name] = v
}
// LetVar sets a variable to matching context.
//
func (p *Context) LetVar(name string, v interface{}) {
if _, ok := p.Globals.Var(name); ok {
p.Globals.SetVar(name, v)
return
}
var vars map[string]interface{}
if p.dom == nil {
vars = make(map[string]interface{})
p.dom = vars
} else if domv, ok := p.dom.(map[string]interface{}); ok {
vars = domv
} else {
panic("dom type isn't map[string]interface{}")
}
vars[name] = v
}
// Var gets a variable from matching context.
//
func (p *Context) Var(name string) (v interface{}, ok bool) {
vars, ok := p.dom.(map[string]interface{})
if ok {
v, ok = vars[name]
} else {
panic("dom type isn't map[string]interface{}")
}
return
}
// SetDom set matching result of matching result.
//
func (p *Context) SetDom(v interface{}) {
if p.dom == nil {
p.dom = v
} else {
panic("dom was assigned already")
}
}
// Dom returns matching result.
//
func (p *Context) Dom() interface{} {
return p.dom
}
// -----------------------------------------------------------------------------
// A Ruler interface is required to a matching unit.
//
type Ruler interface {
// Match matches input stream `in`, and returns matching result.
Match(in *bufio.Reader, ctx *Context) (v interface{}, err error)
// RetType returns matching result type.
RetType() reflect.Type
// SizeOf returns expected length of result. If length is variadic, it returns -1.
SizeOf() int
}
// MatchStream matches a stream.
//
func MatchStream(r Ruler, in *bufio.Reader, ctx *Context) (v interface{}, err error) {
glbs := ctx.Globals
old, ok := glbs.GetAndSetVar("BPL_IN", in)
v, err = r.Match(in, ctx)
if ok {
glbs.SetVar("BPL_IN", old)
}
return
}
// -----------------------------------------------------------------------------
type fileLine struct {
r Ruler
file string
line int
}
type errorAt struct {
Err error
Buf []byte
}
func (p *errorAt) Error() string {
b := make([]byte, 0, 32)
b = append(b, p.Err.Error()...)
b = append(b, '\n')
w := bytes.NewBuffer(b)
d := hex.Dumper(w)
d.Write(p.Buf)
d.Close()
return string(w.Bytes())
}
func (p *fileLine) Match(in *bufio.Reader, ctx *Context) (v interface{}, err error) {
v, err = doMatch(p.r, in, ctx)
if err != nil {
if _, ok := err.(*exec.Error); !ok {
err = &exec.Error{
Err: &errorAt{Err: err, Buf: bufiox.Buffer(in)},
File: p.file,
Line: p.line,
Stack: debug.Stack(),
}
}
}
return
}
func (p *fileLine) RetType() reflect.Type {
return p.r.RetType()
}
func (p *fileLine) SizeOf() int {
return p.r.SizeOf()
}
func doMatch(R Ruler, in *bufio.Reader, ctx *Context) (v interface{}, err error) {
defer func() {
if e := recover(); e != nil {
switch v := e.(type) {
case string:
err = errors.New(v)
case error:
err = v
default:
panic(e)
}
}
}()
return R.Match(in, ctx)
}
// FileLine is a matching rule that reports error file line when error occurs.
//
func FileLine(file string, line int, R Ruler) Ruler {
if _, ok := R.(*fileLine); ok {
return R
}
return &fileLine{r: R, file: file, line: line}
}
// -----------------------------------------------------------------------------