-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_parse.go
More file actions
80 lines (67 loc) · 1.99 KB
/
Copy pathfile_parse.go
File metadata and controls
80 lines (67 loc) · 1.99 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
package testastic
import (
"errors"
"fmt"
"regexp"
"strings"
)
type lineMatcher struct {
original string // raw line text
pattern *regexp.Regexp // nil if no matchers (exact match mode)
matchers []Matcher // matchers found in this line, in order
captureIndexes []int
embedded *embeddedMatcher
}
type fileMatcherPreparer struct{}
func (fileMatcherPreparer) prepare(source string) (preparedMatcherSource, error) {
return preparedMatcherSource{
source: source,
sites: matcherSourceSites(
source,
matcherSourceRules{
unclosedBacktickConsumes: true,
unclosedDoubleQuoteConsumes: true,
},
rawMatcherPlaceholder,
),
placeholder: "__TESTASTIC_TEXT_MATCHER_",
collides: func(candidate string) bool { return strings.Contains(source, candidate) },
embedded: true,
trimWholeMatcher: false,
}, nil
}
func parseLine(line string) (*lineMatcher, error) {
program, err := compileMatcherProgram(line, fileMatcherPreparer{})
if err != nil {
compileErr, ok := errors.AsType[*matcherCompileError](err)
if ok {
return nil, fmt.Errorf("line %q: %w", line, compileErr.cause)
}
return nil, fmt.Errorf("line %q: %w", line, err)
}
if len(program.sites) == 0 {
return &lineMatcher{original: line}, nil
}
segments, original := program.embeddedSegments(program.sourceForParser())
embedded, err := newEmbeddedMatcher(original, segments)
if err != nil {
return nil, fmt.Errorf("line %q: %w", line, err)
}
matchers := make([]Matcher, 0, len(program.sites))
for _, site := range program.sites {
matchers = append(matchers, site.matcher)
}
captureIndexes := make([]int, 0, len(program.sites))
for _, segment := range embedded.segments {
if segment.matcher != nil {
captureIndexes = append(captureIndexes, segment.captureIndex)
}
}
return &lineMatcher{
original: line,
pattern: embedded.pattern,
matchers: matchers,
captureIndexes: captureIndexes,
embedded: embedded,
}, nil
}