-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtype-line.go
192 lines (147 loc) · 3.56 KB
/
type-line.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
package main
import (
"fmt"
"regexp"
"strings"
)
func newLineFromFile(context *context) (*line, error) {
var inputText string
var err error
var line *line
for {
inputText, err = context.File.readLine()
if err != nil {
return nil, err
}
line, err =
newLineFromText(context.File.PathName, context.File.LineNumber, inputText)
if err != nil {
return nil, err
}
if !line.isComment() {
break
}
}
return line, nil
}
func newLineFromText(pathName string, lineNumber int, inputText string) (*line, error) {
ioPrefix, text := split(inputText)
line := &line{
PathName: pathName,
LineNumber: lineNumber,
InputText: inputText,
IOPrefix: ioPrefix,
Text: text,
}
err := line.validate()
if err != nil {
return nil, err
}
return line, nil
}
func split(inputText string) (string, string) {
length := len(inputText)
switch length {
case 0:
return "", ""
case 1:
return string(inputText[0]), ""
case 2:
return string(inputText[0:1]), ""
default:
return string(inputText[0:1]), string(inputText[2:length])
}
}
type line struct {
LineNumber int
PathName string
InputText string
IOPrefix string
Text string
RegexpNames []string
Regexps []*regexp.Regexp
}
func (line *line) validate() error {
if line.isBlank() ||
line.isComment() ||
line.isEmpty() ||
line.isRequest() ||
line.isResponse() ||
line.isSleep() {
return nil
}
return fmt.Errorf("malformed line: %s", line.String())
}
func (line *line) isBlank() bool {
return line.InputText == ""
}
func (line *line) isComment() bool {
return line.IOPrefix != "" && string(line.IOPrefix[0]) == "#"
}
func (line *line) isEmpty() bool {
return line.InputText != "" && line.Text == ""
}
func (line *line) isSleep() bool {
return line.IOPrefix != "" && string(line.IOPrefix[0]) == "+"
}
func (line *line) isRequest() bool {
return line.IOPrefix != "" && string(line.IOPrefix[0]) == ">"
}
func (line *line) isResponse() bool {
return line.IOPrefix != "" && string(line.IOPrefix[0]) == "<"
}
func (line *line) substitute(context *context) error {
parts := strings.Split(line.Text, substitutionIdentifier)
count := len(parts)
if count == 1 {
return nil
}
if (count-3)%2 != 0 {
return fmt.Errorf("malformed substitution: %s", line)
}
substitutedText := parts[0]
for i := 1; i < count-1; i += 2 {
substitution, known := context.Substitutions[parts[i]]
if !known {
return fmt.Errorf("unknown tag: %v", parts[i])
}
substitutedText += substitution + parts[i+1]
}
line.Text = substitutedText
return nil
}
func comparisonError(line, otherLine *line) error {
return fmt.Errorf(
"line: %d: |%v| != |%v|",
line.LineNumber,
line.Content(),
otherLine.Content(),
)
}
func (line *line) compare(context *context, otherLine *line) error {
if line.RegexpNames == nil && line.Text == otherLine.Text {
return nil
}
if line.RegexpNames == nil && line.Text != otherLine.Text {
return comparisonError(line, otherLine)
}
for i, regexpName := range line.RegexpNames {
match := line.Regexps[i].FindString(otherLine.Text)
if match == "" {
return comparisonError(line, otherLine)
}
if regexpName != "" && regexpName != ":prefix" && regexpName != ":postfix" {
context.Substitutions[regexpName] = match
}
}
return nil
}
func (line *line) Location() string {
return fmt.Sprintf("[%s:%3d]", line.PathName, line.LineNumber)
}
func (line *line) Content() string {
return fmt.Sprintf("%s %s", line.IOPrefix, line.Text)
}
func (line *line) String() string {
return fmt.Sprintf("%s %s", line.Location(), line.Content())
}