forked from getgauge/gauge
/
stepProcessor.go
171 lines (143 loc) · 4.73 KB
/
stepProcessor.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
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"
)
const (
inDefault = 1 << iota
inQuotes = 1 << iota
inEscape = 1 << iota
inDynamicParam = 1 << iota
inSpecialParam = 1 << iota
)
const (
quotes = '"'
escape = '\\'
dynamicParamStart = '<'
dynamicParamEnd = '>'
specialParamIdentifier = ':'
)
var allEscapeChars = map[string]string{`\t`: "\t", `\n`: "\n", `\r`: "\r"}
type acceptFn func(rune, int) (int, bool)
func acceptor(start rune, end rune, onEachChar func(rune, int) int, after func(state int), inState int) acceptFn {
return func(element rune, currentState int) (int, bool) {
currentState = onEachChar(element, currentState)
if element == start {
if currentState == inDefault {
return inState, true
}
}
if element == end {
if currentState&inState != 0 {
after(currentState)
return inDefault, true
}
}
return currentState, false
}
}
func simpleAcceptor(start rune, end rune, after func(int), inState int) acceptFn {
onEach := func(currentChar rune, state int) int {
return state
}
return acceptor(start, end, onEach, after, inState)
}
func processStep(parser *specParser, token *token) (*parseError, bool) {
if len(token.value) == 0 {
return &parseError{lineNo: token.lineNo, lineText: token.lineText, message: "Step should not be blank"}, true
}
stepValue, args, err := processStepText(token.value)
if err != nil {
return &parseError{lineNo: token.lineNo, lineText: token.lineText, message: err.Error()}, true
}
token.value = stepValue
token.args = args
parser.clearState()
return nil, false
}
func processStepText(text string) (string, []string, error) {
reservedChars := map[rune]struct{}{'{': {}, '}': {}}
var stepValue, argText bytes.Buffer
var args []string
curBuffer := func(state int) *bytes.Buffer {
if isInAnyState(state, inQuotes, inDynamicParam) {
return &argText
} else {
return &stepValue
}
}
currentState := inDefault
lastState := -1
acceptStaticParam := simpleAcceptor(rune(quotes), rune(quotes), func(int) {
stepValue.WriteString("{static}")
args = append(args, argText.String())
argText.Reset()
}, inQuotes)
acceptSpecialDynamicParam := acceptor(rune(dynamicParamStart), rune(dynamicParamEnd), func(currentChar rune, state int) int {
if currentChar == specialParamIdentifier {
return state | inSpecialParam
}
return state
}, func(currentState int) {
if isInState(currentState, inSpecialParam) {
stepValue.WriteString("{special}")
} else {
stepValue.WriteString("{dynamic}")
}
args = append(args, argText.String())
argText.Reset()
}, inDynamicParam)
var inParamBoundary bool
for _, element := range text {
if currentState == inEscape {
currentState = lastState
element = getEscapedRuneIfValid(element)
} else if element == escape {
lastState = currentState
currentState = inEscape
continue
} else if currentState, inParamBoundary = acceptSpecialDynamicParam(element, currentState); inParamBoundary {
continue
} else if currentState, inParamBoundary = acceptStaticParam(element, currentState); inParamBoundary {
continue
} else if _, isReservedChar := reservedChars[element]; currentState == inDefault && isReservedChar {
return "", nil, errors.New(fmt.Sprintf("'%c' is a reserved character and should be escaped", element))
}
curBuffer(currentState).WriteRune(element)
}
// If it is a valid step, the state should be default when the control reaches here
if currentState == inQuotes {
return "", nil, errors.New(fmt.Sprintf("String not terminated"))
} else if isInState(currentState, inDynamicParam) {
return "", nil, errors.New(fmt.Sprintf("Dynamic parameter not terminated"))
}
return strings.TrimSpace(stepValue.String()), args, nil
}
func getEscapedRuneIfValid(element rune) rune {
allEscapeChars := map[string]rune{"t": '\t', "n": '\n'}
elementToStr, err := strconv.Unquote(strconv.QuoteRune(element))
if err != nil {
return element
}
for key, val := range allEscapeChars {
if key == elementToStr {
return val
}
}
return element
}