/
utils.go
159 lines (138 loc) · 3.35 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
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,
}
}
type sourceCtxHelper struct {
filename string
version string
}
func (s sourceCtxHelper) Get(ctx *antlr.BaseParserRuleContext) *sysl.SourceContext {
return s.get(ctx.GetStart(), ctx.GetStop(), false)
}
func (s sourceCtxHelper) GetWithText(ctx *antlr.BaseParserRuleContext) *sysl.SourceContext {
return s.get(ctx.GetStart(), ctx.GetStop(), true)
}
func (s sourceCtxHelper) get(start, end antlr.Token, withText bool) *sysl.SourceContext {
var text string
if withText {
is := start.GetInputStream()
endChar := end.GetStop()
if endChar == 0 {
endChar = is.Size() - 1
}
text = is.GetText(start.GetStart(), endChar)
}
return &sysl.SourceContext{
File: s.filename,
Start: &sysl.SourceContext_Location{
Line: int32(start.GetLine()),
Col: int32(start.GetColumn()),
},
End: &sysl.SourceContext_Location{
Line: int32(end.GetLine()),
Col: int32(end.GetColumn()),
},
Text: text,
Version: s.version,
}
}
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{
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{
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,
},
},
},
}
}
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(endpoint string) string {
s, err := url.PathUnescape(endpoint)
if err != nil {
panic(err)
}
return strings.TrimSpace(s)
}