-
Notifications
You must be signed in to change notification settings - Fork 171
/
regexp.go
185 lines (158 loc) · 5.45 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
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
package vm
import (
"github.com/dlclark/regexp2"
"github.com/goby-lang/goby/vm/classes"
"github.com/goby-lang/goby/vm/errors"
)
// RegexpObject represents regexp instances, which of the type is actually string.
// Regexp object holds regexp strings.
// Powered by: github.com/dlclark/regexp2
// The regexp2 package had been ported from .NET Framework's regexp library, which is PCRE-compatible
// and is almost all equivalent to Ruby's Onigmo regexp library.
//
// ```ruby
// a = Regexp.new("orl")
// a.match?("Hello World") #=> true
// a.match?("Hello Regexp") #=> false
//
// b = Regexp.new("😏")
// b.match?("🤡 😏 😐") #=> true
// b.match?("😝 😍 😊") #=> false
//
// c = Regexp.new("居(ら(?=れ)|さ(?=せ)|る|ろ|れ(?=[ばる])|よ|(?=な[いかくけそ]|ま[しすせ]|そう|た|て))")
// c.match?("居られればいいのに") #=> true
// c.match?("居ずまいを正す") #=> false
// ```
//
// **Note:**
//
// - Currently, manipulations are based upon Golang's Unicode manipulations.
// - Currently, UTF-8 encoding is assumed based upon Golang's string manipulation, but the encoding is not actually specified(TBD).
// - `Regexp.new` is exceptionally supported.
//
// **To Goby maintainers**: avoid using Go's standard regexp package (slow and not rich). Consider the faster `Trim` or `Split` etc in Go's "strings" package first, or just use the dlclark/regexp2 instead.
// ToDo: Regexp literals with '/.../'
type Regexp = regexp2.Regexp
type RegexpObject struct {
*baseObj
regexp *Regexp
}
// Class methods --------------------------------------------------------
func builtInRegexpClassMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
Name: "new",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 1 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 1 argument. got=%d", len(args))
}
arg, ok := args[0].(*StringObject)
if !ok {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, classes.StringClass, arg.Class().Name)
}
r := t.vm.initRegexpObject(args[0].toString())
if r == nil {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Invalid regexp: %v", args[0].toString())
}
return r
},
},
}
}
// Instance methods -----------------------------------------------------
func builtinRegexpInstanceMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
// Returns true if the two regexp patterns are exactly the same, or returns false if not.
// If comparing with non Regexp class, just returns false.
//
// ```ruby
// r1 = Regexp.new("goby[0-9]+")
// r2 = Regexp.new("goby[0-9]+")
// r3 = Regexp.new("Goby[0-9]+")
//
// r1 == r2 # => true
// r1 == r2 # => false
// ```
//
// @param regexp [Regexp]
// @return [Boolean]
Name: "==",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 1 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 1 argument. got=%d", len(args))
}
right, ok := args[0].(*RegexpObject)
if !ok {
return FALSE
}
left := receiver.(*RegexpObject)
if left.Value() == right.Value() {
return TRUE
}
return FALSE
},
},
{
// Returns boolean value to indicate the result of regexp match with the string given. The methods evaluates a String object.
//
// ```ruby
// r = Regexp.new("o")
// r.match?("pow") # => true
// r.match?("gee") # => false
// ```
//
// @param string [String]
// @return [Boolean]
Name: "match?",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 1 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 1 argument. got=%d", len(args))
}
arg := args[0]
input, ok := arg.(*StringObject)
if !ok {
return t.vm.InitErrorObject(errors.TypeError, sourceLine, errors.WrongArgumentTypeFormat, classes.StringClass, arg.Class().Name)
}
re := receiver.(*RegexpObject).regexp
m, _ := re.MatchString(input.value)
return toBooleanObject(m)
},
},
}
}
// Internal functions ===================================================
// Functions for initialization -----------------------------------------
func (vm *VM) initRegexpObject(regexp string) *RegexpObject {
r, err := regexp2.Compile(regexp, 0)
if err != nil {
return nil
}
return &RegexpObject{
baseObj: &baseObj{class: vm.topLevelClass(classes.RegexpClass)},
regexp: r,
}
}
func (vm *VM) initRegexpClass() *RClass {
rc := vm.initializeClass(classes.RegexpClass)
rc.setBuiltinMethods(builtinRegexpInstanceMethods(), false)
rc.setBuiltinMethods(builtInRegexpClassMethods(), true)
return rc
}
// Polymorphic helper functions -----------------------------------------
// 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() string {
return r.regexp.String()
}
// toJSON just delegates to toString
func (r *RegexpObject) toJSON(t *Thread) string {
return "\"" + r.toString() + "\""
}
// equal checks if the string values between receiver and argument are equal
func (r *RegexpObject) equal(e *RegexpObject) bool {
return r.toString() == r.toString()
}