-
Notifications
You must be signed in to change notification settings - Fork 3
/
pattern.go
83 lines (81 loc) · 1.53 KB
/
pattern.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
package lisp
import "github.com/hydra13142/lisp/parser"
func init() {
bnd := func(c byte) bool {
return c == '(' || c == ')' || parser.IsSpace(c)
}
pattern.Add(func(s []byte) (interface{}, int) {
if len(s) > 0 {
switch s[0] {
case '(', ')':
return s[0], 1
case '\'':
if len(s) > 2 && s[1] == '(' && s[2] != '\'' {
return byte('['), 2
}
}
}
return nil, 0
})
pattern.Add(func(s []byte) (interface{}, int) {
a, i := parser.ParseInt(s)
if i > 0 {
if _, j := parser.ParseFloat(s); i == j && (i >= len(s) || bnd(s[i])) {
return a, i
}
}
return nil, 0
})
pattern.Add(func(s []byte) (interface{}, int) {
a, i := parser.ParseFloat(s)
if i > 0 && (i >= len(s) || bnd(s[i])) {
return a, i
}
return nil, 0
})
pattern.Add(func(s []byte) (interface{}, int) {
if len(s) == 0 {
return nil, 0
}
if s[0] == '\'' {
a, i := parser.ParseChar(s[1:])
if i > 0 {
i += 1
if i < len(s) && s[i] == '\'' {
i += 1
if i >= len(s) || bnd(s[i]) {
return int64(a), i
}
}
}
}
return nil, 0
})
pattern.Add(func(s []byte) (interface{}, int) {
i := 1
if len(s) == 0 || s[0] != '"' {
return nil, 0
}
for ; i < len(s) && s[i] != '"'; i++ {
if s[i] == '\\' {
i++
}
}
if i < len(s) {
a := string(s[1:i])
i += 1
if i >= len(s) || bnd(s[i]) {
return a, i
}
}
return nil, 0
})
pattern.Add(func(s []byte) (interface{}, int) {
i := 0
for i < len(s) && !bnd(s[i]) {
i++
}
a := Name(string(s[:i]))
return a, i
})
}