Repository navigation
Expand file tree
/
Copy pathregexp_corpus_rust_test.go
More file actions
552 lines (522 loc) · 13.1 KB
/
Copy pathregexp_corpus_rust_test.go
File metadata and controls
552 lines (522 loc) · 13.1 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package regexp2
import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
)
// TestCorpusRustRegexToml runs the raw Rust regex TOML corpus.
//
// Corpus source:
//
// https://github.com/rust-lang/regex/tree/master/testdata
//
// The raw TOML files are checked into testdata/corpus/rust-regex unchanged.
// This runner parses the source format directly and maps only the search modes
// exposed by regexp2. Skipped categories are documented in
// testdata/corpus/rust-regex/skipped.md.
func TestCorpusRustRegexToml(t *testing.T) {
files, err := filepath.Glob("testdata/corpus/rust-regex/*.toml")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
file := file
t.Run(filepath.Base(file), func(t *testing.T) {
tests := parseRustRegexCorpusFile(t, file)
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
runRustRegexCorpusTest(t, tc)
})
}
})
}
}
type rustRegexCorpusTest struct {
file string
name string
regexRaw string
regexes []string
haystack string
matchesRaw string
matches []rustRegexMatch
options map[string]string
}
type rustRegexMatch struct {
span []int
captures [][]int
}
func runRustRegexCorpusTest(t *testing.T, tc rustRegexCorpusTest) {
if reason := rustRegexSkipReason(tc); reason != "" {
t.Skip(reason)
}
pattern := tc.regexes[0]
if len(tc.regexes) > 1 {
parts := make([]string, 0, len(tc.regexes))
for _, regex := range tc.regexes {
parts = append(parts, "(?:"+regex+")")
}
pattern = strings.Join(parts, "|")
}
options := rustRegexCompileOptions(tc)
if tc.options["compiles"] == "false" {
if _, err := Compile(pattern, options...); err == nil {
t.Fatalf("Compile(%q) succeeded, want error", pattern)
}
return
}
re, err := Compile(pattern, options...)
if err != nil {
t.Fatalf("Compile(%q) failed: %v", pattern, err)
}
got, err := rustRegexAllMatches(re, tc.haystack, rustRegexMatchLimit(tc))
if err != nil {
t.Fatalf("matching failed: %v", err)
}
if !rustRegexMatchesEqual(got, tc.matches) {
t.Fatalf("matches = %v, want %v", got, tc.matches)
}
assertCorpusOptimizedEquivalent(t, pattern, options, tc.haystack)
}
func rustRegexCompileOptions(tc rustRegexCorpusTest) []CompileOption {
var options []CompileOption
if rustRegexOption(tc, "case-insensitive") == "true" {
options = append(options, IgnoreCase)
}
if rustRegexOption(tc, "unicode") == "false" ||
strings.Contains(tc.regexRaw, "[:") ||
strings.Contains(tc.regexRaw, "$") ||
strings.Contains(tc.regexRaw, "(?P<") ||
strings.Contains(tc.regexRaw, "(?P=") {
options = append(options, RE2)
}
return options
}
func rustRegexSkipReason(tc rustRegexCorpusTest) string {
if len(tc.regexes) == 0 {
return "empty Rust regex set has no single-regexp alternation equivalent"
}
if rustRegexOption(tc, "utf8") == "false" {
return "byte/invalid-UTF8 mode is not exposed by regexp2"
}
if rustRegexOption(tc, "unicode") == "false" {
return "Rust ASCII-only behavior is not supported"
}
if filepath.Base(tc.file) == "regex-lite.toml" {
return "Rust regex-lite ASCII-only behavior is not supported"
}
if rustRegexOption(tc, "anchored") == "true" {
return "Rust anchored automata mode is not exposed by regexp2"
}
if rustRegexOption(tc, "bounds") != "" {
return "bounded search end is not exposed by regexp2"
}
if rustRegexOption(tc, "line-terminator") != "" || strings.Contains(tc.regexRaw, "(?R") || strings.Contains(tc.regexRaw, "(?mR") {
return "Rust CRLF/line-terminator mode is not supported"
}
if strings.Contains(tc.regexRaw, "(?U") || strings.Contains(tc.regexRaw, "(?-U") {
return "Rust swap-greed mode is not supported"
}
if strings.Contains(tc.regexRaw, `\b{`) {
return "Rust special word-boundary assertions are not supported"
}
if rustRegexOption(tc, "search-kind") == "overlapping" || rustRegexOption(tc, "search-kind") == "earliest" {
return "Rust overlapping/earliest search mode is not exposed by regexp2"
}
if rustRegexOption(tc, "match-kind") == "all" {
return "Rust all-match mode is not equivalent to normal leftmost-first regexp matching"
}
if len(tc.regexes) > 1 && strings.Contains(tc.matchesRaw, "id =") {
return "Rust regex-set all/id reporting is not equivalent to a single alternation regexp"
}
return ""
}
func rustRegexOption(tc rustRegexCorpusTest, key string) string {
value := strings.TrimSpace(tc.options[key])
if value == "" {
return ""
}
if strings.HasPrefix(value, "'") && strings.HasSuffix(value, "'") {
return strings.Trim(value, "'")
}
if strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
if unquoted, err := strconv.Unquote(value); err == nil {
return unquoted
}
}
return value
}
func rustRegexMatchLimit(tc rustRegexCorpusTest) int {
if raw := tc.options["match-limit"]; raw != "" {
n, err := strconv.Atoi(raw)
if err == nil {
return n
}
}
return -1
}
func rustRegexAllMatches(re *Regexp, input string, limit int) ([]rustRegexMatch, error) {
var out []rustRegexMatch
prevEnd := -1
m, err := re.FindStringMatch(input)
for m != nil && err == nil && limit != 0 {
if m.RuneLength != 0 || m.RuneIndex != prevEnd {
out = append(out, rustRegexFromMatch(m))
prevEnd = m.RuneIndex + m.RuneLength
if limit > 0 {
limit--
}
}
m, err = re.FindNextMatch(m)
}
return out, err
}
func rustRegexFromMatch(m *Match) rustRegexMatch {
idx, length := m.ByteRange()
rm := rustRegexMatch{span: []int{idx, idx + length}}
groups := m.Groups()
if len(groups) > 1 {
rm.captures = make([][]int, len(groups))
for i := range groups {
if len(groups[i].Captures) == 0 {
rm.captures[i] = nil
continue
}
start, groupLength := groups[i].ByteRange()
rm.captures[i] = []int{start, start + groupLength}
}
}
return rm
}
func parseRustRegexCorpusFile(t *testing.T, file string) []rustRegexCorpusTest {
t.Helper()
b, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
blocks := splitRustRegexTestBlocks(string(b))
tests := make([]rustRegexCorpusTest, 0, len(blocks))
for _, block := range blocks {
values := parseRustRegexTomlBlock(block)
tc := rustRegexCorpusTest{
file: file,
name: strings.Trim(parseRustRegexString(t, values["name"]), `"`),
regexRaw: values["regex"],
haystack: parseRustRegexString(t, values["haystack"]),
matchesRaw: values["matches"],
options: values,
}
tc.regexes = parseRustRegexRegexes(t, values["regex"])
tc.matches = parseRustRegexMatches(t, values["matches"])
tests = append(tests, tc)
}
return tests
}
func splitRustRegexTestBlocks(raw string) []string {
var blocks []string
var current strings.Builder
inBlock := false
for _, line := range strings.Split(raw, "\n") {
if strings.TrimSpace(line) == "[[test]]" {
if inBlock {
blocks = append(blocks, current.String())
current.Reset()
}
inBlock = true
continue
}
if inBlock {
current.WriteString(line)
current.WriteByte('\n')
}
}
if inBlock {
blocks = append(blocks, current.String())
}
return blocks
}
func parseRustRegexTomlBlock(block string) map[string]string {
values := map[string]string{}
lines := strings.Split(block, "\n")
for i := 0; i < len(lines); i++ {
line := strings.TrimSpace(lines[i])
if line == "" || strings.HasPrefix(line, "#") || !strings.Contains(line, "=") {
continue
}
key, value, _ := strings.Cut(line, "=")
key = strings.TrimSpace(key)
value = stripRustRegexComment(strings.TrimSpace(value))
for !rustRegexValueBalanced(value) && i+1 < len(lines) {
i++
value += "\n" + stripRustRegexComment(strings.TrimSpace(lines[i]))
}
values[key] = strings.TrimSpace(value)
}
return values
}
func parseRustRegexRegexes(t *testing.T, raw string) []string {
t.Helper()
raw = strings.TrimSpace(raw)
if strings.HasPrefix(raw, "[") {
return parseRustRegexStringArray(t, raw)
}
return []string{parseRustRegexString(t, raw)}
}
func parseRustRegexStringArray(t *testing.T, raw string) []string {
t.Helper()
var out []string
for i := 0; i < len(raw); {
for i < len(raw) && !isRustRegexQuote(raw[i]) {
i++
}
if i >= len(raw) {
break
}
s, next := parseRustRegexStringAt(t, raw, i)
out = append(out, s)
i = next
}
return out
}
func parseRustRegexString(t *testing.T, raw string) string {
t.Helper()
s, next := parseRustRegexStringAt(t, strings.TrimSpace(raw), 0)
if strings.TrimSpace(raw[next:]) != "" {
t.Fatalf("trailing TOML string data in %q", raw)
}
return s
}
func parseRustRegexStringAt(t *testing.T, raw string, start int) (string, int) {
t.Helper()
if strings.HasPrefix(raw[start:], "'''") {
end := strings.Index(raw[start+3:], "'''")
if end < 0 {
t.Fatalf("unterminated TOML literal string %q", raw[start:])
}
return raw[start+3 : start+3+end], start + 3 + end + 3
}
if raw[start] == '\'' {
end := strings.IndexByte(raw[start+1:], '\'')
if end < 0 {
t.Fatalf("unterminated TOML literal string %q", raw[start:])
}
return raw[start+1 : start+1+end], start + 1 + end + 1
}
if raw[start] == '"' {
for end := start + 1; end < len(raw); end++ {
if raw[end] == '\\' {
end++
continue
}
if raw[end] == '"' {
s, err := strconv.Unquote(raw[start : end+1])
if err != nil {
t.Fatalf("invalid TOML basic string %q: %v", raw[start:end+1], err)
}
return s, end + 1
}
}
}
t.Fatalf("expected TOML string at %q", raw[start:])
return "", 0
}
func parseRustRegexMatches(t *testing.T, raw string) []rustRegexMatch {
t.Helper()
raw = strings.TrimSpace(raw)
if raw == "" || raw == "[]" || strings.Contains(raw, "id =") {
return nil
}
nums := parseRustRegexInts(raw)
if len(nums)%2 != 0 {
t.Fatalf("odd number of match coordinates in %q", raw)
}
depths := rustRegexNumberDepths(raw)
captureMode := false
for _, depth := range depths {
if depth >= 3 {
captureMode = true
break
}
}
if !captureMode {
out := make([]rustRegexMatch, 0, len(nums)/2)
for i := 0; i < len(nums); i += 2 {
out = append(out, rustRegexMatch{span: []int{nums[i], nums[i+1]}})
}
return out
}
var out []rustRegexMatch
for _, matchRaw := range splitRustRegexTopLevelArrays(raw) {
captureRaws := splitRustRegexTopLevelArrays(matchRaw)
captures := make([][]int, 0, len(captureRaws))
for _, captureRaw := range captureRaws {
matchNums := parseRustRegexInts(captureRaw)
if len(matchNums) == 0 {
captures = append(captures, nil)
continue
}
if len(matchNums) != 2 {
t.Fatalf("invalid capture coordinates in %q", captureRaw)
}
captures = append(captures, []int{matchNums[0], matchNums[1]})
}
rm := rustRegexMatch{captures: captures}
if len(captures) > 0 {
rm.span = captures[0]
}
out = append(out, rm)
}
return out
}
func parseRustRegexInts(raw string) []int {
var out []int
for i := 0; i < len(raw); i++ {
if raw[i] < '0' || raw[i] > '9' {
continue
}
start := i
for i < len(raw) && raw[i] >= '0' && raw[i] <= '9' {
i++
}
n, _ := strconv.Atoi(raw[start:i])
out = append(out, n)
}
return out
}
func rustRegexNumberDepths(raw string) []int {
var depths []int
depth := 0
for i := 0; i < len(raw); i++ {
switch raw[i] {
case '[':
depth++
case ']':
depth--
default:
if raw[i] >= '0' && raw[i] <= '9' {
depths = append(depths, depth)
for i+1 < len(raw) && raw[i+1] >= '0' && raw[i+1] <= '9' {
i++
}
}
}
}
return depths
}
func splitRustRegexTopLevelArrays(raw string) []string {
raw = strings.TrimSpace(raw)
if len(raw) < 2 {
return nil
}
raw = strings.TrimSpace(raw[1 : len(raw)-1])
var out []string
for i := 0; i < len(raw); i++ {
if raw[i] != '[' {
continue
}
start := i
depth := 0
for ; i < len(raw); i++ {
if raw[i] == '[' {
depth++
} else if raw[i] == ']' {
depth--
if depth == 0 {
out = append(out, raw[start:i+1])
break
}
}
}
}
return out
}
func rustRegexMatchesEqual(a, b []rustRegexMatch) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !corpusIntSlicesEqual(a[i].span, b[i].span) {
return false
}
if len(b[i].captures) > 0 {
if len(a[i].captures) < len(b[i].captures) {
return false
}
for j := range b[i].captures {
if !corpusIntSlicesEqual(a[i].captures[j], b[i].captures[j]) {
return false
}
}
}
}
return true
}
func stripRustRegexComment(s string) string {
if strings.HasPrefix(s, "'''") || strings.HasPrefix(s, `"""`) {
return strings.TrimSpace(s)
}
in := byte(0)
escaped := false
for i := 0; i < len(s); i++ {
c := s[i]
if escaped {
escaped = false
continue
}
if in == '"' && c == '\\' {
escaped = true
continue
}
if in != 0 {
if c == in {
in = 0
}
continue
}
if c == '\'' || c == '"' {
in = c
continue
}
if c == '#' {
return strings.TrimSpace(s[:i])
}
}
return strings.TrimSpace(s)
}
func rustRegexValueBalanced(s string) bool {
depth := 0
in := byte(0)
escaped := false
for i := 0; i < len(s); i++ {
c := s[i]
if escaped {
escaped = false
continue
}
if in == '"' && c == '\\' {
escaped = true
continue
}
if in != 0 {
if c == in {
in = 0
}
continue
}
if c == '\'' || c == '"' {
in = c
continue
}
if c == '[' {
depth++
} else if c == ']' {
depth--
}
}
return depth <= 0
}
func isRustRegexQuote(c byte) bool {
return c == '\'' || c == '"'
}