-
Notifications
You must be signed in to change notification settings - Fork 588
/
Copy pathtype_regexp.go
144 lines (127 loc) · 3.63 KB
/
type_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
package otto
import (
"fmt"
"regexp"
"github.com/robertkrimen/otto/parser"
)
type regExpObject struct {
regularExpression *regexp.Regexp
source string
flags string
global bool
ignoreCase bool
multiline bool
}
func (rt *runtime) newRegExpObject(pattern string, flags string) *object {
o := rt.newObject()
o.class = classRegExpName
global := false
ignoreCase := false
multiline := false
re2flags := ""
// TODO Maybe clean up the panicking here... TypeError, SyntaxError, ?
for _, chr := range flags {
switch chr {
case 'g':
if global {
panic(rt.panicSyntaxError("newRegExpObject: %s %s", pattern, flags))
}
global = true
case 'm':
if multiline {
panic(rt.panicSyntaxError("newRegExpObject: %s %s", pattern, flags))
}
multiline = true
re2flags += "m"
case 'i':
if ignoreCase {
panic(rt.panicSyntaxError("newRegExpObject: %s %s", pattern, flags))
}
ignoreCase = true
re2flags += "i"
}
}
re2pattern, err := parser.TransformRegExp(pattern)
if err != nil {
panic(rt.panicTypeError("Invalid regular expression: %s", err.Error()))
}
if len(re2flags) > 0 {
re2pattern = fmt.Sprintf("(?%s:%s)", re2flags, re2pattern)
}
regularExpression, err := regexp.Compile(re2pattern)
if err != nil {
panic(rt.panicSyntaxError("Invalid regular expression: %s", err.Error()[22:]))
}
o.value = regExpObject{
regularExpression: regularExpression,
global: global,
ignoreCase: ignoreCase,
multiline: multiline,
source: pattern,
flags: flags,
}
o.defineProperty("global", boolValue(global), 0, false)
o.defineProperty("ignoreCase", boolValue(ignoreCase), 0, false)
o.defineProperty("multiline", boolValue(multiline), 0, false)
o.defineProperty("lastIndex", intValue(0), 0o100, false)
o.defineProperty("source", stringValue(pattern), 0, false)
return o
}
func (o *object) regExpValue() regExpObject {
value, _ := o.value.(regExpObject)
return value
}
func execRegExp(this *object, target string) (bool, []int) {
if this.class != classRegExpName {
panic(this.runtime.panicTypeError("Calling RegExp.exec on a non-RegExp object"))
}
lastIndex := this.get("lastIndex").number().int64
index := lastIndex
global := this.get("global").bool()
if !global {
index = 0
}
var result []int
if 0 > index || index > int64(len(target)) {
} else {
result = this.regExpValue().regularExpression.FindStringSubmatchIndex(target[index:])
}
if result == nil {
this.put("lastIndex", intValue(0), true)
return false, nil
}
startIndex := index
endIndex := int(lastIndex) + result[1]
// We do this shift here because the .FindStringSubmatchIndex above
// was done on a local subordinate slice of the string, not the whole string
for index, offset := range result {
if offset != -1 {
result[index] += int(startIndex)
}
}
if global {
this.put("lastIndex", intValue(endIndex), true)
}
return true, result
}
func execResultToArray(rt *runtime, target string, result []int) *object {
captureCount := len(result) / 2
valueArray := make([]Value, captureCount)
for index := range captureCount {
offset := 2 * index
if result[offset] != -1 {
valueArray[index] = stringValue(target[result[offset]:result[offset+1]])
} else {
valueArray[index] = Value{}
}
}
matchIndex := result[0]
if matchIndex != 0 {
// Find the utf16 index in the string, not the byte index.
matchIndex = utf16Length(target[:matchIndex])
}
match := rt.newArrayOf(valueArray)
match.defineProperty("input", stringValue(target), 0o111, false)
match.defineProperty("index", intValue(matchIndex), 0o111, false)
return match
}