/
utils.go
188 lines (165 loc) · 4.18 KB
/
utils.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
package parse
import (
"math"
"net/url"
"strings"
"github.com/antlr/antlr4/runtime/Go/antlr"
sysl "github.com/anz-bank/sysl/pkg/sysl"
)
func makeExpr(ctx *sysl.SourceContext) *sysl.Expr {
return &sysl.Expr{
SourceContext: ctx,
SourceContexts: []*sysl.SourceContext{ctx},
}
}
type sourceCtxHelper struct {
filename string
version string
}
func (s *sourceCtxHelper) get(start, end antlr.Token) *sysl.SourceContext {
// In Antlr, lines are 1-based, columns are 0-based.
// In Sysl SourceContext, both lines and columns are 0-based.
ctx := &sysl.SourceContext{
File: s.filename,
Start: &sysl.SourceContext_Location{
Line: int32(start.GetLine() - 1),
Col: int32(start.GetColumn()),
},
End: &sysl.SourceContext_Location{
Line: int32(end.GetLine() - 1),
Col: int32(end.GetColumn()),
},
Version: s.version,
}
if text := end.GetText(); text != "" {
ctx.End.Col += int32(len(text))
}
return ctx
}
// lastToken returns the last token in the AST.
func lastToken(tree antlr.Tree) antlr.Token {
switch t := tree.(type) {
case antlr.TerminalNode:
return t.GetSymbol()
default:
return lastToken(tree.GetChild(tree.GetChildCount() - 1))
}
}
// getBitWidth returns the bit width of the type, or 0 if no such constraint exists.
func getBitWidth(t *sysl.Type) int32 {
if t.Constraint != nil {
for _, item := range t.Constraint {
if item != nil && item.BitWidth > 0 {
return item.BitWidth
}
}
}
return 0
}
func primitiveFromNativeDataType(native antlr.TerminalNode) (*sysl.Type_Primitive_, *sysl.Type_Constraint) {
if native == nil {
return nil, nil
}
text := strings.ToUpper(native.GetText())
primitiveType := sysl.Type_Primitive(sysl.Type_Primitive_value[text])
if primitiveType != sysl.Type_NO_Primitive {
return &sysl.Type_Primitive_{Primitive: primitiveType}, nil
}
var constraint *sysl.Type_Constraint
switch text {
case "INT32":
primitiveType = sysl.Type_Primitive(sysl.Type_Primitive_value["INT"])
constraint = &sysl.Type_Constraint{
BitWidth: 32,
Range: &sysl.Type_Constraint_Range{
Min: &sysl.Value{
Value: &sysl.Value_I{
I: math.MinInt32,
},
},
Max: &sysl.Value{
Value: &sysl.Value_I{
I: math.MaxInt32,
},
},
},
}
case "INT64":
primitiveType = sysl.Type_Primitive(sysl.Type_Primitive_value["INT"])
constraint = &sysl.Type_Constraint{
BitWidth: 64,
Range: &sysl.Type_Constraint_Range{
Min: &sysl.Value{
Value: &sysl.Value_I{
I: math.MinInt64,
},
},
Max: &sysl.Value{
Value: &sysl.Value_I{
I: math.MaxInt64,
},
},
},
}
case "FLOAT32":
primitiveType = sysl.Type_Primitive(sysl.Type_Primitive_value["FLOAT"])
constraint = &sysl.Type_Constraint{BitWidth: 32}
case "FLOAT64":
primitiveType = sysl.Type_Primitive(sysl.Type_Primitive_value["FLOAT"])
constraint = &sysl.Type_Constraint{BitWidth: 64}
}
return &sysl.Type_Primitive_{Primitive: primitiveType}, constraint
}
type PathStack struct {
sep string
parts []string
path string
prefix string
}
func NewPathStack(sep string) PathStack {
return PathStack{
sep: sep,
parts: []string{},
}
}
func (p PathStack) Get() string {
return p.path
}
func (p *PathStack) Push(items ...string) string {
for _, i := range items {
p.parts = append(p.parts, strings.TrimSpace(i))
}
return p.update()
}
func (p *PathStack) Pop() string {
p.parts = p.parts[:len(p.parts)-1]
return p.update()
}
func (p *PathStack) Reset() string {
p.parts = []string{}
return p.update()
}
// Parts() clones the path items into a new slice so that it is safe to store
func (p PathStack) Parts() []string {
return append([]string{}, p.parts...)
}
func (p *PathStack) update() string {
p.path = p.prefix + strings.Join(p.parts, p.sep)
return p.path
}
// MustUnescape will escape URL escaped characters.
func MustUnescape(str string) string {
s, err := url.PathUnescape(str)
if err != nil {
panic(err)
}
return strings.TrimSpace(s)
}
// MustUnescapeStrings will escape URL escaped characters in an array of strings.
func MustUnescapeStrings(strs []string) []string {
escaped := make([]string, 0, len(strs))
for _, str := range strs {
escaped = append(escaped, MustUnescape(str))
}
return escaped
}