-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathref.go
49 lines (46 loc) · 1.22 KB
/
ref.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
package asciiart
import (
"encoding/json"
"regexp"
"strings"
)
var matchReference = regexp.MustCompile(`^\[(.+)\]:([ \t]+)(.*)$`)
func (p *Parser) parseReference(g *Grid, lines [][]byte, startY int) error {
m := matchReference.FindStringSubmatch(string(lines[startY]))
if m == nil {
return &ParseError{X: 0, Y: startY, Err: ErrRefParseError}
}
key := m[1]
mid := m[2]
jsn := m[3]
var ref interface{}
if err := json.Unmarshal([]byte(jsn), &ref); err != nil {
x := len(key) + 2 + len(mid)
se, ok := err.(*json.SyntaxError)
if ok {
x += int(se.Offset)
}
return &ParseError{X: x, Y: startY, Err: ErrRefJSONUnmarshal}
}
refMap, ok := ref.(map[string]interface{})
if !ok {
return &ParseError{X: 0, Y: startY, Err: ErrRefJSONObj}
}
if strings.HasPrefix(key, "_") {
// global reference -> attach to grid
if g.Refs == nil {
g.Refs = make(map[string]map[string]interface{})
}
if g.Refs[key] != nil {
return &ParseError{X: 0, Y: startY, Err: ErrRefTwice}
}
g.Refs[key] = refMap
} else {
// local reference -> store with parser for further processing
if p.refs[key] != nil {
return &ParseError{X: 0, Y: startY, Err: ErrRefTwice}
}
p.refs[key] = &reference{Y: startY, ref: refMap}
}
return nil
}