/
es5pather.go
218 lines (175 loc) · 6.11 KB
/
es5pather.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2015 The Serulian Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package shared
import (
"path/filepath"
"regexp"
"strings"
"github.com/rainycape/unidecode"
"unicode"
"github.com/serulian/compiler/graphs/scopegraph"
"github.com/serulian/compiler/graphs/typegraph"
)
// Pather defines a helper type for generating paths.
type Pather struct {
scopegraph *scopegraph.ScopeGraph
}
// NewPather creates a new path generator for the given graph.
func NewPather(scopegraph *scopegraph.ScopeGraph) Pather {
return Pather{scopegraph}
}
// TypeReferenceCall returns source for retrieving an object reference to the type defined by the given
// type reference.
func (p Pather) TypeReferenceCall(typeRef typegraph.TypeReference) string {
if typeRef.IsAny() {
return "$t.any"
}
if typeRef.IsStruct() {
return "$t.struct"
}
if typeRef.IsNull() {
return "$t.null"
}
if typeRef.IsVoid() {
return "$t.void"
}
referredType := typeRef.ReferredType()
if referredType.TypeKind() == typegraph.GenericType {
return referredType.Name()
}
// Add the type name.
typePath := p.GetTypePath(referredType)
// If there are no generics, then simply return the type path.
if !typeRef.HasGenerics() {
return typePath
}
// Invoke the type with generics (if any).
var genericsString = "("
for index, generic := range typeRef.Generics() {
if index > 0 {
genericsString = genericsString + ", "
}
genericsString = genericsString + p.TypeReferenceCall(generic)
}
genericsString = genericsString + ")"
return typePath + genericsString
}
// InnerInstanceName returns the name of an inner instance of the given type, when accessed under a
// type instance which structurally composes it.
func (p Pather) InnerInstanceName(innerType typegraph.TypeReference) string {
var name = unidecode.Unidecode(innerType.ReferredType().Name())
if !innerType.HasGenerics() {
return name
}
for _, generic := range innerType.Generics() {
name = name + "$"
name = name + p.InnerInstanceName(generic)
}
return name
}
// GetMemberName returns the name of the given member.
func (p Pather) GetMemberName(member typegraph.TGMember) string {
return strings.Replace(unidecode.Unidecode(member.ChildName()), "*", "$", 1)
}
// GetSetterName returns the name of the setter for the given member.
func (p Pather) GetSetterName(member typegraph.TGMember) string {
return "set$" + p.GetMemberName(member)
}
// GetStaticTypePath returns the global path for the given defined type.
func (p Pather) GetStaticTypePath(typedecl typegraph.TGTypeDecl, referenceType typegraph.TypeReference) string {
instanceTypeRef := typedecl.GetTypeReference().TransformUnder(referenceType)
return p.TypeReferenceCall(instanceTypeRef)
}
// GetStaticMemberPath returns the global path for the given statically defined type member.
func (p Pather) GetStaticMemberPath(member typegraph.TGMember, referenceType typegraph.TypeReference) string {
sourceGraphID := member.SourceGraphId()
if sourceGraphID != "srg" {
integration := p.scopegraph.MustGetLanguageIntegration(sourceGraphID)
pathHandler := integration.PathHandler()
if pathHandler != nil {
staticPath := integration.PathHandler().GetStaticMemberPath(member, referenceType)
if staticPath != "" {
return staticPath
}
}
}
name := p.GetMemberName(member)
parent := member.Parent()
if parent.IsType() {
return p.GetStaticTypePath(parent.(typegraph.TGTypeDecl), referenceType) + "." + name
}
return p.GetModulePath(parent.(typegraph.TGModule)) + "." + name
}
// GetTypePath returns the global path for the given type.
func (p Pather) GetTypePath(typedecl typegraph.TGTypeDecl) string {
if typedecl.TypeKind() == typegraph.GenericType {
return typedecl.Name()
}
return p.GetModulePath(typedecl.ParentModule()) + "." + typedecl.Name()
}
// GetModulePath returns the global path for the given module.
func (p Pather) GetModulePath(module typegraph.TGModule) string {
sourceGraphID := module.SourceGraphId()
if sourceGraphID != "srg" {
integration := p.scopegraph.MustGetLanguageIntegration(sourceGraphID)
pathHandler := integration.PathHandler()
if pathHandler != nil {
modulePath := pathHandler.GetModulePath(module)
if modulePath != "" {
return modulePath
}
}
}
return "$g." + p.GetRelativeModulePath(module)
}
// GetRelativeModulePath returns the relative path for the given module.
func (p Pather) GetRelativeModulePath(module typegraph.TGModule) string {
// We create the exported path based on the location of this module's source file relative
// to the entrypoint file.
basePath := filepath.Dir(p.scopegraph.RootSourceFilePath())
rel, err := filepath.Rel(basePath, string(module.Path()))
if err != nil {
rel = string(module.Path())
}
return normalizeModulePath(rel)
}
var allowedModulePathCharacters, _ = regexp.Compile("[^a-zA-Z_0-9\\$\\.]")
var allowedPathPartCharacters, _ = regexp.Compile("[^a-zA-Z_0-9\\$]")
func normalizeModulePath(rel string) string {
if rel == "" {
return ""
}
// Replace any relative pathing with underscores.
rel = strings.Replace(rel, "../", "__", -1)
// Strip off any starting .
if rel[0] == '.' {
rel = rel[1:len(rel)]
}
// Remove any Serulian source file suffix.
if strings.HasSuffix(rel, ".seru") {
rel = rel[0 : len(rel)-5]
}
// Replace any dot parts and change slashes into submodules.
parts := strings.Split(rel, "/")
updatedParts := make([]string, 0, len(parts))
for _, part := range parts {
if part == "." || part == "" {
continue
}
// If the part does not start with a letter or underscore, prepend a prefix, as literals in ES5 cannot start
// with numbers.
newPart := part
firstRune := rune(part[0])
if firstRune != '_' && !unicode.IsLetter(firstRune) {
newPart = "m$" + part
}
// Ensure any non-identifier pieces are replaced with a dash.
newPart = allowedPathPartCharacters.ReplaceAllLiteralString(newPart, "_")
updatedParts = append(updatedParts, newPart)
}
path := strings.Join(updatedParts, ".")
// Replace any other bad characters.
path = allowedModulePathCharacters.ReplaceAllLiteralString(path, "_")
return path
}