Skip to content
← Back

src/syntax/process/expand_namespaces.ghul

1
namespace Syntax.Process is
2
@IF.dotnet()
3
use System.IO.Abstractions
4
5
@IF.not.dotnet()
6
use IFileSystem = SOURCE_CORE_FILE_SYSTEM
7
8
@IF.not.dotnet()
9
use IDirectoryInfo = SOURCE_CORE_DIRECTORY_INFO
10
11
use Source
12
use Trees
13
14
use Ghul.Pipes
15
16
class EXPAND_NAMESPACES: Visitor is
17
_file_system: IFileSystem
18
_depth: int
19
20
init(file_system: IFileSystem) is
21
super.init()
22
23
_file_system = file_system
24
si
25
26
apply(node: Node) is
27
assert isa Definitions.LIST(node) else "expected the root syntax tree node of file to be a definition list"
28
29
let list = cast Definitions.LIST(node)
30
31
let seen_any_namespaces mut = false
32
let seen_any_other_definitions mut = false
33
34
// A definition is classified by what it is under any pragmas
35
// written on it: a pragma-wrapped function is still a global
36
// definition, and a pragma-wrapped namespace still a namespace.
37
// One conditional compilation dropped is an empty list, and is
38
// neither.
39
for definition in list do
40
if isa Definitions.NAMESPACE(definition.without_pragmas) then
41
seen_any_namespaces = true
42
elif !_is_dropped(definition) then
43
seen_any_other_definitions = true
44
fi
45
od
46
47
if seen_any_namespaces /\ seen_any_other_definitions then
48
let logger = IoC.CONTAINER.instance.logger
49
50
for definition in list do
51
if !isa Definitions.NAMESPACE(definition.without_pragmas) /\ !_is_dropped(definition) then
52
logger.error(definition.location, "cannot mix global definitions and namespaces in the same file")
53
fi
54
od
55
elif seen_any_other_definitions then
56
let root_namespace_name: string mut
57
58
if IoC.CONTAINER.instance.build_flags.want_global_namespace then
59
// An empty name aggregates every file's global
60
// definitions into the single unnamed root namespace,
61
// so they are mutually visible across the project.
62
root_namespace_name = ""
63
else
64
// The per-file name keeps each file's global
65
// definitions private to that file.
66
let first_node = list |> first()
67
let path = first_node!.location.file_name
68
69
root_namespace_name =
70
IoC.CONTAINER.instance.build_flags.submission_name ??
71
if IoC.CONTAINER.instance.build_flags.want_cells then
72
let stem = convert_file_name_to_identifier(
73
_file_system.path.get_file_name_without_extension(_local_path(path)) ?? "")
74
75
// The session's cells, in the order their files
76
// are walked, which is the order they were given:
77
// a cell's closed-hierarchy decisions are made
78
// over the cells up to and including its own.
79
if !IoC.CONTAINER.instance.build_flags.session_cell_names.contains(stem) then
80
IoC.CONTAINER.instance.build_flags.session_cell_names.add(stem)
81
fi
82
83
stem
84
else
85
get_namespace_name(path)
86
fi
87
fi
88
89
let new_definitions = Collections.LIST[Definitions.Definition](
90
list
91
)
92
93
if IoC.CONTAINER.instance.build_flags.want_implicit_default_use then
94
new_definitions.insert(_after_pragmas(new_definitions), _implicit_default_use(list.location))
95
fi
96
97
let root_namespace =
98
Definitions.NAMESPACE(
99
list.location,
100
Trees.Identifiers.Identifier(Source.LOCATION.internal, root_namespace_name),
101
Definitions.LIST(list.location, new_definitions),
102
true
103
)
104
105
list.clear_definitions()
106
107
list.add(root_namespace)
108
fi
109
110
node.walk(self)
111
si
112
113
_is_dropped(definition: Definitions.Definition) -> bool =>
114
let unwrapped = definition.without_pragmas in
115
isa Definitions.LIST(unwrapped) /\ (cast Definitions.LIST(unwrapped) |> count()) == 0
116
117
// Where an implicit `use default` goes: after any pragmas, so a
118
// file-level pragma still comes first.
119
_after_pragmas(definitions: Collections.List[Definitions.Definition]) -> int is
120
let index mut = 0
121
122
while index < definitions.count /\ isa Definitions.PRAGMA(definitions[index]) do
123
index = index + 1
124
od
125
126
return index
127
si
128
129
_implicit_default_use(location: LOCATION) -> Definitions.USE is
130
let result = Definitions.USE(location, null, null)
131
132
result.is_default = true
133
134
return result
135
si
136
137
is_root_directory(info: IDirectoryInfo) -> bool is
138
let root_path = _file_system.path.get_path_root(info.full_name)
139
140
return root_path =~ info.full_name
141
si
142
143
convert_file_name_to_identifier(name: string mut) -> string static is
144
if name.length == 0 then
145
return name
146
fi
147
148
name =
149
name
150
.replace('-', '_')
151
.replace('.', '_') |>
152
filter(c => char.is_letter_or_digit(c) \/ c == '_') |> join("")
153
154
// Filesystem root names ("/", "C:\\") and any other input
155
// whose characters are all dropped by the filter reduce to
156
// empty here. The post-parse pass walks up parent
157
// directories when no ghul.json / *.ghulproj marks the
158
// project root, so a file opened from a path with no
159
// upstream marker can deliver such a name. Earlier versions
160
// crashed on the index below, taking down the whole
161
// build pipeline (hover, completion, …) for the file.
162
if name.length == 0 then
163
return name
164
fi
165
166
if !char.is_letter(name[0]) then
167
name = "`{name}"
168
fi
169
170
return name
171
si
172
173
get_namespace_name(path: string) -> string is
174
let local_path = _local_path(path)
175
176
let result mut =
177
convert_file_name_to_identifier(
178
_file_system.path.get_file_name_without_extension(local_path) ?? ""
179
)
180
181
let folder = _file_system.path.get_directory_name(local_path) ?? ""
182
183
result = "{get_namespace_name_for_folder(folder)}{result}"
184
185
return result
186
si
187
188
// A file named by a file: URI, as an editor names one, is read from
189
// the path the URI holds.
190
@IF.dotnet()
191
_local_path(path: string) -> string is
192
if System.Uri.is_well_formed_uri_string(path, System.UriKind.ABSOLUTE) then
193
let uri = System.Uri(path)
194
195
if uri.is_file then
196
return uri.local_path
197
fi
198
fi
199
200
return path
201
si
202
203
@IF.not.dotnet()
204
_local_path(path: string) -> string => path
205
206
get_namespace_name_for_folder(path: string? mut) -> string is
207
let original_path = path
208
209
if !path? \/ path =~ "" then
210
path = _file_system.directory.get_current_directory()
211
fi
212
213
let info = _file_system.directory_info.`new(path)
214
215
let result = get_namespace_name_for_folder(info)
216
217
return result
218
si
219
220
get_namespace_name_for_folder(info: IDirectoryInfo) -> string is
221
let result mut = "{convert_file_name_to_identifier(info.name)}__"
222
223
if
224
_file_system.file.exists(_file_system.path.combine(info.full_name, "ghul.json")) \/
225
_file_system.directory.get_files(info.full_name, "*.ghulproj").count > 0
226
then
227
return result
228
fi
229
230
if info.parent? /\ !is_root_directory(info) then
231
result = "{get_namespace_name_for_folder(info.parent!)}{result}"
232
fi
233
234
return result
235
si
236
237
pre(`namespace: Definitions.NAMESPACE) -> bool is
238
_depth = _depth + 1
239
return false
240
si
241
242
visit(`namespace: Definitions.NAMESPACE) is
243
let names = `namespace.name.qualifier_names
244
245
if _depth == 1 then
246
// `Ghul.Intrinsics` is the ambient set: the names the
247
// language lets every namespace block reference without
248
// a `use` - the magic types and the intrinsic operators.
249
// Anything else declared in `Ghul`, by the runtime or by
250
// any consumed assembly, is imported like any other
251
// namespace's symbols.
252
`namespace.body.push(
253
Definitions.USE(
254
Source.LOCATION.internal,
255
null,
256
Trees.Identifiers.QUALIFIED(
257
Source.LOCATION.internal,
258
Trees.Identifiers.Identifier(Source.LOCATION.internal, "Ghul"),
259
"Intrinsics",
260
Source.LOCATION.internal,
261
Source.LOCATION.internal
262
)
263
)
264
)
265
fi
266
267
if names.count > 0 then
268
`namespace.name = Identifiers.Identifier(`namespace.name.location, `namespace.name.name)
269
let body = `namespace.body
270
let current = `namespace
271
272
for n in names do
273
current.body =
274
Definitions.LIST(
275
current.location,
276
Collections.LIST[Definitions.Definition](
277
[Definitions.NAMESPACE(current.location, current.name, current.body, current.is_compiler_generated)]: Definitions.Definition
278
)
279
)
280
current.name = Identifiers.Identifier(`namespace.name.location, n)
281
od
282
fi
283
284
_depth = _depth - 1
285
si
286
si
287
si