-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecuter.go
More file actions
181 lines (160 loc) · 3.98 KB
/
Copy pathexecuter.go
File metadata and controls
181 lines (160 loc) · 3.98 KB
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
// Copyright (c) 2015, Ben Morgan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package twikutil
import (
"errors"
"io/ioutil"
"strconv"
"strings"
"github.com/goulash/pre"
past "github.com/goulash/pre/ast"
"gopkg.in/twik.v1"
"gopkg.in/twik.v1/ast"
)
var ErrFuncExists = errors.New("cannot set variable with name of existing function")
type LoaderFunc func(*twik.Scope) FuncMap
type Executer struct {
PreProcessor *pre.Processor
fset *ast.FileSet
scope *twik.Scope
funcs map[string]bool
}
func New(loader LoaderFunc) *Executer {
fset := twik.NewFileSet()
s := twik.NewScope(fset)
fns := loader(s)
keys := make(map[string]bool)
for k := range fns {
keys[k] = true
}
fns.Export(s)
return &Executer{
fset: fset,
scope: s,
funcs: keys,
}
}
func (e *Executer) Scope() *twik.Scope { return e.scope }
// It is an error to use a key that has already been used as a function.
func (e *Executer) Set(key string, value interface{}) error {
if e.funcs[key] {
return errors.New("function with that name already exists")
}
_, err := e.scope.Get(key)
if err == nil {
return e.scope.Set(key, value)
}
return e.scope.Create(key, value)
}
// It is an error to get a key that has already been used as a function.
func (e *Executer) Get(key string) (interface{}, error) {
if e.funcs[key] {
return nil, errors.New("functions cannot be gotten")
}
return e.scope.Get(key)
}
func (e *Executer) Has(key string) bool {
if e.funcs[key] {
return false
}
v, err := e.scope.Get(key)
return err == nil && v != nil
}
func (e *Executer) Create(key string, fn interface{}) error {
err := e.scope.Create(key, Func(key, fn))
if err != nil {
return err
}
e.funcs[key] = true
return nil
}
func (e *Executer) Override(key string, fn interface{}) error {
if !e.funcs[key] {
return errors.New("no function by that name exists")
}
return e.scope.Set(key, Func(key, fn))
}
func (e *Executer) Exec(file string) (s *twik.Scope, err error) {
bs, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
return e.ExecString(file, string(bs))
}
func (e *Executer) ExecString(name, code string) (s *twik.Scope, err error) {
// When the preprocessor is active, we need to convert any error messages
// we get from twik so that they correspond to the correct file name, line
// and column. This we do in the deferred function.
var root past.Node
if e.PreProcessor != nil {
root, err = e.PreProcessor.ParseString(name, code)
code = root.String()
if err != nil {
return nil, err
}
defer func() {
if err != nil {
err = replaceError(name, root, err)
}
}()
}
node, err := twik.ParseString(e.fset, name, code)
if err != nil {
return nil, err
}
_, err = e.scope.Eval(node)
return e.scope, err
}
func replaceError(name string, root past.Node, err error) error {
if e, ok := err.(*twik.Error); ok {
epi := e.PosInfo
pi := root.OffsetLC(epi.Line, epi.Column)
if pi != nil {
epi.Name = pi.Name
epi.Line = pi.Line
epi.Column = pi.Column
}
return e
}
// Some parse errors aren't returned as *twik.Error,
// so we have to figure it out ourselves. They tend
// to all have the format:
//
// name:line:col: error msg
//
// So we can work with that. If anywhere along the
// way we run into an error, we just return the
// original error.
s := err.Error()
if !strings.HasPrefix(s, name) {
return err
}
s = strings.TrimPrefix(s, name)
xs := strings.Split(s, ":")
if len(xs) < 4 {
return err
}
if xs[0] != "" {
return err
}
line, e := strconv.ParseInt(xs[1], 0, 0)
if e != nil {
return err
}
col, e := strconv.ParseInt(xs[2], 0, 0)
if e != nil {
return err
}
pi := root.OffsetLC(int(line), int(col))
if pi != nil {
xs[0] = pi.Name
xs[1] = strconv.FormatInt(int64(pi.Line), 10)
xs[2] = strconv.FormatInt(int64(pi.Column), 10)
} else {
xs[0] = name
xs[1] = strconv.FormatInt(line, 10)
xs[2] = strconv.FormatInt(col, 10)
}
return errors.New(strings.Join(xs, ":"))
}