-
Notifications
You must be signed in to change notification settings - Fork 171
/
match_data.go
212 lines (176 loc) · 5.79 KB
/
match_data.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package vm
import (
"fmt"
"github.com/dlclark/regexp2"
"github.com/goby-lang/goby/vm/classes"
"github.com/goby-lang/goby/vm/errors"
)
// MatchDataObject represents the match data returned by a regular expression matching operation.
// You can use named-captures via `(?<name>)`.
//
// ```ruby
// 'abcd'.match(Regexp.new('(b.)'))
// #=> #<MatchData 0:"bc" 1:"bc">
//
// 'abcd'.match(Regexp.new('a(?<first>b)(?<second>c)'))
// #=> #<MatchData 0:"abc" first:"b" second:"c">
// ```
//
// - `MatchData.new` is not supported.
type Match = regexp2.Match
type MatchDataObject struct {
*baseObj
match *Match
}
// Class methods --------------------------------------------------------
func builtInMatchDataClassMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
Name: "new",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
return t.vm.initUnsupportedMethodError(sourceLine, "#new", receiver)
},
},
}
}
// Instance methods -----------------------------------------------------
func builtinMatchDataInstanceMethods() []*BuiltinMethodObject {
return []*BuiltinMethodObject{
{
// Returns the array of captures; equivalent to `match.to_a[1..-1]`.
//
// ```ruby
// c1, c2 = 'abcd'.match(Regexp.new('a(b)(c)')).captures
// c1 #=> "b"
// c2 #=> "c"
// ```
//
// @return [Array]
Name: "captures",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%d", len(args))
}
offset := 1
g := receiver.(*MatchDataObject).match
n := len(g.Groups()) - offset
destCaptures := make([]Object, n, n)
for i := 0; i < n; i++ {
destCaptures[i] = t.vm.InitStringObject(g.GroupByNumber(i + offset).String())
}
return t.vm.InitArrayObject(destCaptures)
},
},
{
// Returns the array of captures.
//
// ```ruby
// c0, c1, c2 = 'abcd'.match(Regexp.new('a(b)(c)')).to_a
// c0 #=> "abc"
// c1 #=> "b"
// c2 #=> "c"
// ```
//
// @return [Array]
Name: "to_a",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%d", len(args))
}
g := receiver.(*MatchDataObject).match
n := len(g.Groups())
destCaptures := make([]Object, n, n)
for i := 0; i < n; i++ {
destCaptures[i] = t.vm.InitStringObject(g.GroupByNumber(i).String())
}
return t.vm.InitArrayObject(destCaptures)
},
},
{
// Returns the hash of captures, including the whole matched text(`0:`).
// You can use named-capture as well.
//
// ```ruby
// h = 'abcd'.match(Regexp.new('a(b)(c)')).to_h
// puts h #=> { "0": "abc", "1": "b", "2": "c" }
//
// h = 'abcd'.match(Regexp.new('a(?<first>b)(?<second>c)')).to_h
// puts h #=> { "0": "abc", "first": "b", "second": "c" }
// ```
//
// @return [Hash]
Name: "to_h",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%d", len(args))
}
groups := receiver.(*MatchDataObject).match
result := make(map[string]Object)
for _, g := range groups.Groups() {
result[g.Name] = t.vm.InitStringObject(g.String())
}
return t.vm.InitHashObject(result)
},
},
{
// Returns the length of the array; equivalent to `match.to_a.length`.
//
// ```ruby
// 'abcd'.match(Regexp.new('a(b)(c)')).length # => 3
// ```
// @return [Integer]
Name: "length",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if len(args) != 0 {
return t.vm.InitErrorObject(errors.ArgumentError, sourceLine, "Expect 0 argument. got=%d", len(args))
}
m := receiver.(*MatchDataObject).match
return t.vm.InitIntegerObject(m.GroupCount())
},
},
}
}
// Internal functions ===================================================
// Functions for initialization -----------------------------------------
// Initializes a MatchDataObject from a Match object, and the original pattern/text.
// Nothing prevents the programmer to pass pattern/text unrelated to the match, but this will
// create an inconsistent MatchData object.
func (vm *VM) initMatchDataObject(match *Match, pattern, text string) *MatchDataObject {
return &MatchDataObject{
baseObj: &baseObj{class: vm.topLevelClass(classes.MatchDataClass)},
match: match,
}
}
func (vm *VM) initMatchDataClass() *RClass {
klass := vm.initializeClass(classes.MatchDataClass)
klass.setBuiltinMethods(builtinMatchDataInstanceMethods(), false)
klass.setBuiltinMethods(builtInMatchDataClassMethods(), true)
return klass
}
// Polymorphic helper functions -----------------------------------------
// redirects to toString()
func (m *MatchDataObject) Value() interface{} {
return m.toString()
}
// returns a string representation of the object
func (m *MatchDataObject) toString() string {
result := "#<MatchData"
for _, c := range m.match.Groups() {
result += fmt.Sprintf(" %s:\"%s\"", c.Name, c.String())
}
result += ">"
return result
}
// returns a `{ captureNumber: captureValue }` JSON-encoded string
func (m *MatchDataObject) toJSON(t *Thread) string {
result := "{"
for _, c := range m.match.Groups() {
result += fmt.Sprintf(" %s:\"%s\"", c.Name, c.String())
}
result += "}"
return result
}
// equal checks if the string values between receiver and argument are equal
func (m *MatchDataObject) equal(other *MatchDataObject) bool {
return m.match == other.match
}