/
script.go
336 lines (267 loc) · 7.05 KB
/
script.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package scripting
import (
"errors"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"github.com/fatih/structs"
"github.com/ghetzel/go-stockutil/maputil"
"github.com/ghetzel/go-stockutil/mathutil"
"github.com/ghetzel/go-stockutil/rxutil"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/typeutil"
)
var rxPegContext = regexp.MustCompile(`(?P<message>.*) \(line (?P<line>\d+) symbol (?P<symbol>\d+)(?: - line (?P<eline>\d+) symbol (?P<esymbol>\d+))?`)
var errContextLinesBefore = 3
var errContextLinesAfter = 3
type mappable interface {
ToMap() map[string]interface{}
}
//go:generate peg -inline friendscript.peg
type runtime struct {
scope *Scope
filename string
}
type nodeFunc func(node *node32, depth int)
func Parse(input string) (*Friendscript, error) {
structs.DefaultTagName = `json`
fs := &Friendscript{
Buffer: input,
Pretty: true,
runtime: runtime{
scope: NewScope(nil),
},
}
fs.Init()
if err := fs.Parse(); err == nil {
return fs, nil
} else if strings.HasPrefix(strings.TrimSpace(err.Error()), `parse error near`) {
return nil, fs.errorWithContext(err)
} else {
return nil, err
}
}
func LoadFromFile(filename string) (*Friendscript, error) {
if file, err := os.Open(filename); err == nil {
defer file.Close()
if data, err := ioutil.ReadAll(file); err == nil {
if fs, err := Parse(string(data)); err == nil {
fs.filename = filename
return fs, nil
} else {
return nil, err
}
} else {
return nil, err
}
} else {
return nil, err
}
}
func (self *Friendscript) SetScope(scope *Scope) {
self.runtime.scope = scope
}
func (self *Friendscript) Filename() string {
return self.runtime.filename
}
func (self *Friendscript) Scope() *Scope {
return self.runtime.scope
}
func (self *Friendscript) lcp() string {
return stringutil.LongestCommonPrefix(strings.Split(self.Buffer, "\n"))
}
func (self *Friendscript) errorWithContext(err error) error {
raw := strings.TrimSpace(err.Error())
if match := rxutil.Match(rxPegContext, raw); match != nil {
line := int(stringutil.MustInteger(match.Group(`line`)))
symbol := int(stringutil.MustInteger(match.Group(`symbol`)))
lines := strings.Split(self.Buffer, "\n")
lbound := int(mathutil.ClampLower(float64(line-errContextLinesBefore), 0))
ubound := int(mathutil.ClampUpper(float64(line+errContextLinesAfter), float64(len(lines))))
message := fmt.Sprintf("Syntax error on line %d: %v\n", line, match.Group(`message`))
message += "\n"
lcp := stringutil.LongestCommonPrefix(lines)
for i := lbound; i < ubound; i++ {
message += fmt.Sprintf("%- 4d | %v\n", i, strings.TrimPrefix(lines[i], lcp))
if i == (line - 1) {
sl := (symbol - 1)
if sl < 0 {
sl = 0
}
message += fmt.Sprintf(" | %s^\n", strings.Repeat(`-`, sl))
}
}
return errors.New(message)
} else {
return err
}
}
// Return all top-level blocks in the current script.
func (self *Friendscript) Blocks() []*Block {
blocks := make([]*Block, 0)
root := self.AST()
root.traverse(func(node *node32, depth int) {
// fmt.Printf("[% 2d] %v%v %q\n", depth, strings.Repeat(` `, depth), node, self.s(node))
switch node.rule() {
case ruleStatementBlock:
blocks = append(blocks, &Block{
friendscript: self,
node: node,
})
}
}, 1)
return blocks
}
func (self *Friendscript) s(node *node32) string {
if node != nil {
begin := int(node.token32.begin)
end := int(node.token32.end)
return self.Buffer[begin:end]
} else {
return ``
}
}
func (self *node32) rule() pegRule {
return self.token32.pegRule
}
func (self *node32) first(anyOf ...pegRule) *node32 {
return self.firstN(-1, anyOf...)
}
func (self *node32) firstN(maxdepth int, anyOf ...pegRule) *node32 {
results := self.findN(maxdepth, anyOf...)
if len(results) > 0 {
return results[0]
} else {
return nil
}
}
func (self *node32) firstChild(anyOf ...pegRule) *node32 {
return self.firstN(0, anyOf...)
}
func (self *node32) find(anyOf ...pegRule) []*node32 {
return self.findN(-1, anyOf...)
}
func (self *node32) children(anyOf ...pegRule) []*node32 {
return self.findN(0, anyOf...)
}
func (self *node32) findN(maxdepth int, anyOf ...pegRule) []*node32 {
results := make([]*node32, 0)
self.traverse(func(node *node32, _ int) {
if node == self {
return
}
switch node.rule() {
case rule_, rule__:
return
}
if len(anyOf) == 0 || sliceutil.Contains(anyOf, node.rule()) {
results = append(results, node)
}
}, maxdepth)
return results
}
func (self *node32) findUntil(maxdepth int, stopRule pegRule, anyOf ...pegRule) []*node32 {
results := make([]*node32, 0)
hitStopRule := false
self.traverse(func(node *node32, _ int) {
if !hitStopRule {
if node == self {
return
}
switch node.rule() {
case rule_, rule__:
return
}
if node.rule() == stopRule {
hitStopRule = true
return
}
if len(anyOf) == 0 || sliceutil.Contains(anyOf, node.rule()) {
results = append(results, node)
}
}
}, maxdepth)
return results
}
func (self *node32) findAfter(maxdepth int, startRule pegRule, anyOf ...pegRule) []*node32 {
results := make([]*node32, 0)
hitStartRule := false
self.traverse(func(node *node32, _ int) {
if node == self {
return
}
switch node.rule() {
case rule_, rule__:
return
}
if node.rule() == startRule {
hitStartRule = true
return
}
if hitStartRule {
if len(anyOf) == 0 || sliceutil.Contains(anyOf, node.rule()) {
results = append(results, node)
}
}
}, maxdepth)
return results
}
func (self *node32) traverse(nodeFn nodeFunc, maxdepth int) {
self.traverseNode(self, nodeFn, 0, maxdepth)
}
func (self *node32) traverseNode(start *node32, nodeFn nodeFunc, depth int, maxdepth int) {
node := start
for node != nil {
nodeFn(node, depth)
if node.up != nil {
if maxdepth < 0 || depth <= maxdepth {
self.traverseNode(node.up, nodeFn, depth+1, maxdepth)
}
}
node = node.next
}
}
func debugNode(friendscript *Friendscript, node *node32) {
if node != nil {
node.traverse(func(node *node32, depth int) {
fmt.Printf("[% 2d] %v%v %q\n", depth, strings.Repeat(` `, depth), node, friendscript.s(node))
}, -1)
} else {
fmt.Printf("empty node\n")
}
}
// return int or int64 when appropriate
func intIfYouCan(in interface{}) interface{} {
if oF, ok := in.(float64); ok {
if oF == float64(int(oF)) {
return int(oF)
} else if oF == float64(int64(oF)) {
return int64(oF)
}
}
return in
}
// if the input is a struct, convert it into a map
func mapifyStruct(in interface{}) interface{} {
maputil.UnmarshalStructTag = `json`
if m, ok := in.(mappable); ok {
return m.ToMap()
} else if b, ok := in.([]byte); ok {
return b
} else if typeutil.IsArray(in) {
var elems = make([]interface{}, sliceutil.Len(in))
// work a little to get structs/mappable objects turned into maps
sliceutil.Each(in, func(i int, elem interface{}) error {
elems[i] = mapifyStruct(elem)
return nil
})
return elems
} else if typeutil.IsStruct(in) {
return maputil.M(in).MapNative()
} else {
return in
}
}