-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
149 lines (139 loc) · 2.78 KB
/
parser.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
package fstring
import (
"fmt"
"strconv"
"strings"
)
type parser struct {
data []rune
result []rune
idx int
values map[string]any
}
func newParser(s string, values map[string]any) *parser {
if len(values) == 0 {
values = map[string]any{}
}
return &parser{
data: []rune(s),
result: nil,
idx: 0,
values: values,
}
}
func (r *parser) parse() error {
for r.hasMore() {
existLeftCurlyBracket, tmp, err := r.scanToLeftCurlyBracket()
if err != nil {
return err
}
r.result = append(r.result, tmp...)
if !existLeftCurlyBracket {
continue
}
tmp = r.scanToRightCurlyBracket()
valName := strings.TrimSpace(string(tmp))
if valName == "" {
return ErrEmptyExpression
}
val, ok := r.values[valName]
if !ok {
return fmt.Errorf("%w: %s", ErrArgsNotDefined, valName)
}
r.result = append(r.result, []rune(toString(val))...)
}
return nil
}
func (r *parser) scanToLeftCurlyBracket() (bool, []rune, error) {
res := []rune{}
for r.hasMore() {
s := r.get()
r.idx++
switch s {
case '}':
if r.hasMore() && r.get() == '}' {
res = append(res, '}') // nolint:ineffassign,staticcheck
r.idx++
continue
}
return false, nil, ErrRightBracketNotClosed
case '{':
if !r.hasMore() {
return false, nil, ErrLeftBracketNotClosed
}
if r.get() == '{' {
// {{ -> {
r.idx++
res = append(res, '{')
continue
}
return true, res, nil
default:
res = append(res, s)
}
}
return false, res, nil
}
func (r *parser) scanToRightCurlyBracket() []rune {
var res []rune
for r.hasMore() {
s := r.get()
if s != '}' {
// xxx
res = append(res, s)
r.idx++
continue
}
r.idx++
break
}
return res
}
func (r *parser) hasMore() bool {
return r.idx < len(r.data)
}
func (r *parser) get() rune {
return r.data[r.idx]
}
// nolint: cyclop
func toString(val any) string {
if val == nil {
return "nil" // f'None' -> "None"
}
switch val := val.(type) {
case string:
return val
case []rune:
return string(val)
case []byte:
return string(val)
case int:
return strconv.FormatInt(int64(val), 10)
case int8:
return strconv.FormatInt(int64(val), 10)
case int16:
return strconv.FormatInt(int64(val), 10)
case int32:
return strconv.FormatInt(int64(val), 10)
case int64:
return strconv.FormatInt(val, 10)
case uint:
return strconv.FormatUint(uint64(val), 10)
case uint8:
return strconv.FormatUint(uint64(val), 10)
case uint16:
return strconv.FormatUint(uint64(val), 10)
case uint32:
return strconv.FormatUint(uint64(val), 10)
case uint64:
return strconv.FormatUint(val, 10)
case float32:
return strconv.FormatFloat(float64(val), 'f', -1, 32)
case float64:
return strconv.FormatFloat(val, 'f', -1, 64)
case bool:
return strconv.FormatBool(val)
default:
return fmt.Sprintf("%s", val)
}
}