/
regexp.go
100 lines (82 loc) · 2.13 KB
/
regexp.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
package zygo
import (
"errors"
"fmt"
"regexp"
)
type SexpRegexp regexp.Regexp
func (re *SexpRegexp) SexpString(ps *PrintState) string {
r := (*regexp.Regexp)(re)
return fmt.Sprintf(`(regexpCompile "%v")`, r.String())
}
func (r *SexpRegexp) Type() *RegisteredType {
return nil // TODO what should this be?
}
func regexpFindIndex(env *Zlisp,
needle *regexp.Regexp, haystack string) (Sexp, error) {
loc := needle.FindStringIndex(haystack)
arr := make([]Sexp, len(loc))
for i := range arr {
arr[i] = Sexp(&SexpInt{Val: int64(loc[i])})
}
return &SexpArray{Val: arr, Env: env}, nil
}
func RegexpFind(env *Zlisp, name string,
args []Sexp) (Sexp, error) {
if len(args) != 2 {
return SexpNull, WrongNargs
}
var haystack string
switch t := args[1].(type) {
case *SexpStr:
haystack = t.S
default:
return SexpNull,
errors.New(fmt.Sprintf("2nd argument of %v should be a string", name))
}
var needle *regexp.Regexp
switch t := args[0].(type) {
case *SexpRegexp:
needle = (*regexp.Regexp)(t)
default:
return SexpNull,
errors.New(fmt.Sprintf("1st argument of %v should be a compiled regular expression", name))
}
switch name {
case "regexpFind":
str := needle.FindString(haystack)
return &SexpStr{S: str}, nil
case "regexpFindIndex":
return regexpFindIndex(env, needle, haystack)
case "regexpMatch":
matches := needle.MatchString(haystack)
return &SexpBool{Val: matches}, nil
}
return SexpNull, errors.New("unknown function")
}
func RegexpCompile(env *Zlisp, name string,
args []Sexp) (Sexp, error) {
if len(args) < 1 {
return SexpNull, WrongNargs
}
var re string
switch t := args[0].(type) {
case *SexpStr:
re = t.S
default:
return SexpNull,
errors.New("argument of regexpCompile should be a string")
}
r, err := regexp.Compile(re)
if err != nil {
return SexpNull, errors.New(
fmt.Sprintf("error during regexpCompile: '%v'", err))
}
return Sexp((*SexpRegexp)(r)), nil
}
func (env *Zlisp) ImportRegex() {
env.AddFunction("regexpCompile", RegexpCompile)
env.AddFunction("regexpFindIndex", RegexpFind)
env.AddFunction("regexpFind", RegexpFind)
env.AddFunction("regexpMatch", RegexpFind)
}