-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp.go
111 lines (93 loc) · 2.62 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
101
102
103
104
105
106
107
108
109
110
111
package vm
import (
"regexp"
"github.com/robotii/lito/vm/classes"
"github.com/robotii/lito/vm/errors"
)
// Regexp type alias for regexp
type Regexp = regexp.Regexp
// RegexpObject represents regexp instances, which of the type is actually string.
type RegexpObject struct {
BaseObj
regexp *Regexp
}
var regexpClassMethods = []*BuiltinMethodObject{
{
Name: "new",
Fn: func(receiver Object, t *Thread, args []Object) Object {
if len(args) != 1 {
return t.vm.InitErrorObject(t, errors.ArgumentError, errors.WrongNumberOfArgument, 1, len(args))
}
arg, ok := args[0].(StringObject)
if !ok {
return t.vm.InitErrorObject(t, errors.TypeError, errors.WrongArgumentTypeFormat, classes.StringClass, arg.Class().Name)
}
r := initRegexpObject(t.vm, args[0].ToString(t))
if r == nil {
return t.vm.InitErrorObject(t, errors.ArgumentError, "Invalid regexp: %v", args[0].ToString(t))
}
return r
},
},
}
var regexpInstanceMethods = []*BuiltinMethodObject{
{
Name: "match?",
Fn: func(receiver Object, t *Thread, args []Object) Object {
if len(args) != 1 {
return t.vm.InitErrorObject(t, errors.ArgumentError, errors.WrongNumberOfArgument, 1, len(args))
}
arg := args[0]
input, ok := arg.(StringObject)
if !ok {
return t.vm.InitErrorObject(t, errors.TypeError, errors.WrongArgumentTypeFormat, classes.StringClass, arg.Class().Name)
}
re := receiver.(*RegexpObject).regexp
m := re.MatchString(string(input))
return BooleanObject(m)
},
Primitive: true,
},
}
func initRegexpObject(vm *VM, regexpStr string) *RegexpObject {
r, err := regexp.Compile(regexpStr)
if err != nil {
return nil
}
return &RegexpObject{
BaseObj: BaseObj{class: vm.TopLevelClass(classes.RegexpClass)},
regexp: r,
}
}
func initRegexpClass(vm *VM) *RClass {
return vm.InitClass(classes.RegexpClass).
ClassMethods(regexpClassMethods).
InstanceMethods(regexpInstanceMethods)
}
// Value returns the object
func (r *RegexpObject) Value() interface{} {
return r.regexp.String()
}
// ToString returns the object's name as the string format
func (r *RegexpObject) ToString(t *Thread) string {
return r.regexp.String()
}
// Inspect delegates to ToString
func (r *RegexpObject) Inspect(t *Thread) string {
return r.ToString(t)
}
// ToJSON just delegates to ToString
func (r *RegexpObject) ToJSON(t *Thread) string {
return "\"" + r.ToString(t) + "\""
}
// EqualTo returns true if the RegexpObject is equal to the other object
func (r *RegexpObject) EqualTo(with Object) bool {
right, ok := with.(*RegexpObject)
if !ok {
return false
}
if r.Value() == right.Value() {
return true
}
return false
}