-
Notifications
You must be signed in to change notification settings - Fork 6
/
formatter_error.go
238 lines (192 loc) · 4.96 KB
/
formatter_error.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
package formattererror
import (
"fmt"
"strings"
"unicode"
"github.com/dipdup-net/go-lib/tools/formatter"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
)
type code struct {
searchNode int
currentNode int
}
// LocateContractError - returns error position by number of error node
// returned values are: row, startCol, endCol, error
func LocateContractError(n gjson.Result, node int) (int, int, int, error) {
c := &code{
searchNode: node + 1,
}
text, err := c.locateError(n, "", true, false)
if err != nil {
return 0, 0, 0, err
}
row, startCol, endCol := findError(text)
return row, startCol, endCol, nil
}
func (c *code) locateError(node gjson.Result, indent string, isRoot, wrapped bool) (string, error) {
c.currentNode++
if node.IsArray() {
return c.locateInArray(node, indent, isRoot)
}
if node.IsObject() {
return c.locateInObject(node, indent, isRoot, wrapped)
}
return "", errors.Errorf("node is not array or object: %v", node)
}
func (c *code) locateInArray(node gjson.Result, indent string, isRoot bool) (string, error) {
seqIndent := indent
isScriptRoot := isRoot && formatter.IsScript(node)
if !isScriptRoot {
seqIndent = indent + " "
}
items := make([]string, len(node.Array()))
for i, n := range node.Array() {
res, err := c.locateError(n, seqIndent, false, true)
if err != nil {
return "", err
}
items[i] = res
}
if len(items) == 0 {
return "{}", nil
}
length := len(indent) + 4
for _, i := range items {
length += len(i)
}
space := ""
if !isScriptRoot {
space = " "
}
var seq string
if length < formatter.DefLineSize {
seq = strings.Join(items, fmt.Sprintf("%v; ", space))
} else {
seq = strings.Join(items, fmt.Sprintf("%v;\n%v", space, seqIndent))
}
if !isScriptRoot {
return fmt.Sprintf("{ %v }", seq), nil
}
return seq, nil
}
func (c *code) locateInObject(node gjson.Result, indent string, isRoot, wrapped bool) (string, error) {
if node.Get("prim").Exists() {
return c.locatePrimObject(node, indent, isRoot, wrapped)
}
return c.locateNonPrimObject(node)
}
func (c *code) locatePrimObject(node gjson.Result, indent string, isRoot, wrapped bool) (string, error) {
var res []string
prim := node.Get("prim").String()
if c.searchNode == c.currentNode {
res = append(res, unicodeMark(prim))
} else {
res = append(res, prim)
}
if annots := node.Get("annots"); annots.Exists() {
for _, a := range annots.Array() {
res = append(res, a.String())
}
}
expr := strings.Join(res, " ")
var args []gjson.Result
if rawArgs := node.Get("args"); rawArgs.Exists() {
args = rawArgs.Array()
}
switch {
case formatter.IsComplex(node):
argIndent := indent + " "
items := make([]string, len(args))
for i, a := range args {
res, err := c.locateError(a, argIndent, false, false)
if err != nil {
return "", err
}
items[i] = res
}
length := len(indent) + len(expr) + len(items) + 1
for _, item := range items {
length += len(item)
}
if length < formatter.DefLineSize {
expr = fmt.Sprintf("%v %v", expr, strings.Join(items, " "))
} else {
res := []string{expr}
res = append(res, items...)
expr = strings.Join(res, fmt.Sprintf("\n%v", argIndent))
}
case len(args) == 1:
argIndent := indent + strings.Repeat(" ", len(expr)+1)
res, err := c.locateError(args[0], argIndent, false, false)
if err != nil {
return "", err
}
expr = fmt.Sprintf("%v %v", expr, res)
case len(args) > 1:
argIndent := indent + " "
altIndent := indent + strings.Repeat(" ", len(expr)+2)
for _, arg := range args {
item, err := c.locateError(arg, argIndent, false, false)
if err != nil {
return "", err
}
length := len(indent) + len(expr) + len(item) + 1
if formatter.IsInline(node) || length < formatter.DefLineSize {
argIndent = altIndent
expr = fmt.Sprintf("%v %v", expr, item)
} else {
expr = fmt.Sprintf("%v\n%v%v", expr, argIndent, item)
}
}
}
if formatter.IsFramed(node) && !isRoot && !wrapped {
return fmt.Sprintf("(%v)", expr), nil
}
return expr, nil
}
func (c *code) locateNonPrimObject(node gjson.Result) (string, error) {
if len(node.Map()) != 1 {
return "", errors.Errorf("node keys count != 1 %v", node)
}
for coreType, value := range node.Map() {
switch coreType {
case "int":
return value.String(), nil
case "bytes":
return fmt.Sprintf("0x%v", value.String()), nil
case "string":
return value.Raw, nil
}
}
return "", errors.Errorf("invalid coreType %v", node)
}
func unicodeMark(s string) string {
if len(s) < 1 {
return ""
}
return string(rune(s[0])+128) + s[1:]
}
func findError(text string) (int, int, int) {
var found bool
var row, start, end int
rows := strings.Split(text, "\n")
for i, r := range rows {
for idx, c := range r {
if isUnicode(c) {
found = true
row = i
start = idx
continue
}
if found && string(c) == " " {
end = idx - 1
return row, start, end
}
}
}
return 0, 0, 0
}
func isUnicode(r rune) bool {
return r > unicode.MaxASCII
}