@papercraftgames/estragon 0.1.0-beta.27 → 0.1.0-beta.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/addons/estragon/compiler/emitter.gd +150 -7
- package/addons/estragon/compiler/frontend.gd +6 -0
- package/addons/estragon/compiler/import_aliases.gd +64 -0
- package/addons/estragon/compiler/import_aliases.gd.uid +1 -0
- package/addons/estragon/compiler/layouts.gd +412 -0
- package/addons/estragon/compiler/layouts.gd.uid +1 -0
- package/addons/estragon/compiler/lexer.gd +1 -0
- package/addons/estragon/compiler/model.gd +39 -0
- package/addons/estragon/compiler/native/resolver.gd +15 -4
- package/addons/estragon/compiler/parser.gd +46 -0
- package/addons/estragon/compiler/render_calls.gd +354 -0
- package/addons/estragon/compiler/render_calls.gd.uid +1 -0
- package/addons/estragon/compiler/validator.gd +17 -8
- package/addons/estragon/editor/completion.gd +47 -3
- package/addons/estragon/editor/components.gd +1 -1
- package/addons/estragon/editor/format_edit.gd +54 -0
- package/addons/estragon/editor/format_edit.gd.uid +1 -0
- package/addons/estragon/editor/formatter.gd +166 -0
- package/addons/estragon/editor/formatter.gd.uid +1 -0
- package/addons/estragon/editor/highlighter.gd +1 -1
- package/addons/estragon/editor/imports.gd +5 -1
- package/addons/estragon/editor/language_services.gd +43 -0
- package/addons/estragon/editor/lexical_context.gd +2 -2
- package/addons/estragon/editor/plugin.cfg +1 -1
- package/addons/estragon/editor/plugin.gd +7 -0
- package/addons/estragon/runtime/composition/binding.gd +16 -1
- package/addons/estragon/runtime/composition/computation.gd +65 -18
- package/addons/estragon/runtime/composition/preset.gd +102 -0
- package/addons/estragon/runtime/composition/preset.gd.uid +1 -0
- package/addons/estragon/runtime/composition/render_cache.gd +36 -0
- package/addons/estragon/runtime/composition/render_cache.gd.uid +1 -0
- package/addons/estragon/runtime/composition/state.gd +158 -1
- package/addons/estragon/runtime/node/box.gd +12 -5
- package/addons/estragon/runtime/node/button.gd +4 -4
- package/addons/estragon/runtime/node/grid.gd +21 -17
- package/addons/estragon/runtime/node/internal/stack_contract.gd +10 -2
- package/addons/estragon/runtime/node/internal/visual_style_2d.gd +1 -1
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
@tool
|
|
2
2
|
extends RefCounted
|
|
3
3
|
## Typed frontend -> ordinary GDScript. All output is range-mapped, never rewritten.
|
|
4
|
+
const Preset = preload("../runtime/composition/preset.gd")
|
|
4
5
|
const Element = preload("../runtime/composition/element.gd")
|
|
5
6
|
const State = preload("../runtime/composition/state.tres")
|
|
6
7
|
const Binding = preload("../runtime/composition/binding.gd")
|
|
@@ -25,6 +26,7 @@ var _module: Model.Module
|
|
|
25
26
|
var _file: Source
|
|
26
27
|
var _writer: MappedWriter
|
|
27
28
|
var _factories: Array[Factory] = []
|
|
29
|
+
var _preset_methods: Array = []
|
|
28
30
|
var _arguments: Dictionary = {}
|
|
29
31
|
var _sequence: int = 0
|
|
30
32
|
var _variable: int = 0
|
|
@@ -54,10 +56,24 @@ func _resource(resource: Resource) -> String:
|
|
|
54
56
|
|
|
55
57
|
func emit() -> MappedWriter:
|
|
56
58
|
_parts(_module.parts)
|
|
59
|
+
var metadata := "_GOGO_RENDER_FUNCTIONS_"+_file.name.sha256_text().substr(0,16)
|
|
60
|
+
while _module.identifiers.has(metadata): metadata += "_"
|
|
61
|
+
_writer.append("\nconst "+metadata+" = "+var_to_str({"reactive":_module.render_functions,"ordinary":_module.ordinary_functions}))
|
|
57
62
|
_writer.append("\n")
|
|
63
|
+
var exports := {}
|
|
64
|
+
for binding in _module.presets:
|
|
65
|
+
if binding.exported: exports[binding.name] = binding
|
|
66
|
+
for name in exports:
|
|
67
|
+
var binding = exports[name]
|
|
68
|
+
_writer.append("\nvar "+name+":\n"+_module.indentation+"get: return "+binding.symbol+"\n",binding.span)
|
|
69
|
+
if binding.mutable: _writer.append(_module.indentation+"set(value): "+binding.symbol+" = value\n",binding.span)
|
|
58
70
|
for path in _module.project_imports:
|
|
59
71
|
var imported: Dictionary = _module.project_imports[path]
|
|
60
72
|
_writer.append("\nconst "+imported.alias+" = preload("+_string(Source.relative_path(_output_directory,path))+")\n",imported.span)
|
|
73
|
+
for method in _preset_methods:
|
|
74
|
+
_writer.append("\nfunc "+method.name+"(): return ",method.binding.span)
|
|
75
|
+
_default_groups(method.binding)
|
|
76
|
+
_writer.append("\n",method.binding.span)
|
|
61
77
|
if _module.composition:
|
|
62
78
|
_writer.append("\nfunc _notification(_gogo_what: int) -> void:\n"+_module.indentation+"preload("+_resource(State)+").lifecycle(self, _gogo_what)\n")
|
|
63
79
|
if not _factories.is_empty():
|
|
@@ -98,6 +114,16 @@ func _argument(expression: Model.GdExpression, type_name: String, factory: Facto
|
|
|
98
114
|
_arguments[expression] = argument
|
|
99
115
|
|
|
100
116
|
func _collect(node: Model.Component, factory: Factory) -> void:
|
|
117
|
+
if node.preset!=null:
|
|
118
|
+
var expression := Model.GdExpression.new()
|
|
119
|
+
expression.span = node.name_span
|
|
120
|
+
var reference := Model.Literal.new()
|
|
121
|
+
reference.kind = "preset-reference"
|
|
122
|
+
reference.value = node.preset.symbol
|
|
123
|
+
reference.span = node.name_span
|
|
124
|
+
expression.parts.append(reference)
|
|
125
|
+
node.preset_expression = expression
|
|
126
|
+
_argument(expression,"Variant",factory)
|
|
101
127
|
for attribute in node.attributes:
|
|
102
128
|
if attribute.value is Model.GdExpression:
|
|
103
129
|
_argument(attribute.value, attribute.type_name, factory)
|
|
@@ -113,16 +139,24 @@ func _parts(parts: Array[Model.SyntaxNode]) -> void:
|
|
|
113
139
|
for part in parts:
|
|
114
140
|
if part is Model.Code: _code(part.span)
|
|
115
141
|
elif part is Model.Bindable: _bindable(part)
|
|
142
|
+
elif part is Model.Layout: _layout(part)
|
|
143
|
+
elif part is Model.RenderCall: _render_call(part)
|
|
144
|
+
elif part is Model.Literal and part.kind=="preset-reference": _writer.append(str(part.value),part.span)
|
|
116
145
|
else: _call(part)
|
|
117
146
|
|
|
118
147
|
func _code(span: Model.Span) -> void:
|
|
119
148
|
var cursor: int = span.start.index
|
|
149
|
+
var edits: Array = []
|
|
120
150
|
for imported in _module.resource_imports:
|
|
121
|
-
|
|
151
|
+
edits.append({"span":imported.span,"value":_string(Source.relative_path(_output_directory,imported.path))})
|
|
152
|
+
for imported in _module.identifier_imports: edits.append({"span":imported.span,"value":imported.alias})
|
|
153
|
+
edits.sort_custom(func(a,b): return a.span.start.index<b.span.start.index)
|
|
154
|
+
for edit in edits:
|
|
155
|
+
var literal_span: Model.Span = edit.span
|
|
122
156
|
if literal_span.start.index < cursor or literal_span.end.index > span.end.index: continue
|
|
123
157
|
var before := _file.span(cursor,literal_span.start.index)
|
|
124
158
|
_writer.append(_file.slice(before),before,true)
|
|
125
|
-
_writer.append(
|
|
159
|
+
_writer.append(edit.value,literal_span)
|
|
126
160
|
cursor = literal_span.end.index
|
|
127
161
|
var rest := _file.span(cursor,span.end.index)
|
|
128
162
|
_writer.append(_file.slice(rest),rest,true)
|
|
@@ -136,6 +170,16 @@ func _call(node: Model.Component) -> void:
|
|
|
136
170
|
factory.root = node
|
|
137
171
|
_factories.append(factory)
|
|
138
172
|
_collect(node, factory)
|
|
173
|
+
var keyed: Model.Attribute
|
|
174
|
+
for attribute in node.attributes:
|
|
175
|
+
if attribute.kind=="key": keyed = attribute
|
|
176
|
+
var key_symbol := "_gogo_mount_key"
|
|
177
|
+
while _module.identifiers.has(key_symbol): key_symbol += "_"
|
|
178
|
+
if keyed!=null:
|
|
179
|
+
_writer.append("preload("+_resource(State)+").in_slot("+_string(_file.name+":"+str(node.span.start.offset))+", ",node.span)
|
|
180
|
+
if keyed.value is Model.GdExpression: _parts(keyed.value.parts)
|
|
181
|
+
else: _writer.append(literal(keyed.value),keyed.value.span)
|
|
182
|
+
_writer.append(", func("+key_symbol+"): return ",node.span)
|
|
139
183
|
var multiline := factory.arguments.size() > 3
|
|
140
184
|
var start := _file.line_start(node.span.start.line)
|
|
141
185
|
var end := start
|
|
@@ -148,11 +192,13 @@ func _call(node: Model.Component) -> void:
|
|
|
148
192
|
if multiline: _writer.append("\n" + base + _module.indentation, argument.expression.span)
|
|
149
193
|
_writer.append("(", argument.expression.span)
|
|
150
194
|
if argument.spread: _writer.append("preload("+_resource(Element)+").snapshot(",argument.expression.span)
|
|
151
|
-
if argument.
|
|
195
|
+
if keyed!=null and argument.expression==keyed.value: _writer.append(key_symbol,argument.expression.span)
|
|
196
|
+
elif argument.binding_path.is_empty(): _parts(argument.expression.parts)
|
|
152
197
|
else: _binding_argument(argument)
|
|
153
198
|
if argument.spread: _writer.append(")",argument.expression.span)
|
|
154
199
|
_writer.append(")", argument.expression.span)
|
|
155
200
|
_writer.append(("\n" + base if multiline else "") + ")", node.span)
|
|
201
|
+
if keyed!=null: _writer.append(")",node.span)
|
|
156
202
|
|
|
157
203
|
func _factory(factory: Factory) -> void:
|
|
158
204
|
_writer.append("\n\n" + _module.indentation + "static func " + factory.name + "(", factory.root.span)
|
|
@@ -198,6 +244,7 @@ func _description(node: Model.Component, indentation: String) -> String:
|
|
|
198
244
|
for child in node.children:
|
|
199
245
|
children.append(_arguments[child].name if child is Model.GdExpression else _description(child,indentation))
|
|
200
246
|
var native_type := _string(node.runtime_class) if ClassDB.class_exists(node.runtime_class) else node.runtime_class
|
|
247
|
+
if node.preset!=null: native_type = "preload("+_resource(Preset)+").type_of("+_arguments[node.preset_expression].name+")"
|
|
201
248
|
_line("var %s := %s.new(%s, {%s}, {%s}, [%s], %s, %s, %s)" % [variable,_element_class,native_type,", ".join(props),", ".join(events),", ".join(children),_string("%s:%d:%d" % [_file.name,node.span.start.line,node.span.start.column]),key,keyed],node.span,indentation)
|
|
202
249
|
for attribute in node.attributes:
|
|
203
250
|
if attribute.kind=="placement" and not expanded: _line(variable+".origins["+_string(attribute.name)+"] = "+_string("%s:%d:%d" % [_file.name,attribute.name_span.start.line,attribute.name_span.start.column]),attribute.name_span,indentation)
|
|
@@ -210,8 +257,47 @@ func _description(node: Model.Component, indentation: String) -> String:
|
|
|
210
257
|
parts.append("[%s, %s]" % [value,_string("%s:%d:%d" % [_file.name,part.span.start.line,part.span.start.column])])
|
|
211
258
|
_line(variable+".set_text_body(["+", ".join(parts)+"])",node.span,indentation)
|
|
212
259
|
if not bindings.is_empty(): _line(variable+".bindings = {"+", ".join(bindings)+"}",node.span,indentation)
|
|
260
|
+
if node.preset!=null: _line(variable+" = preload("+_resource(Preset)+").apply("+_arguments[node.preset_expression].name+", "+variable+")",node.span,indentation)
|
|
213
261
|
return variable
|
|
214
262
|
|
|
263
|
+
func _layout(layout: Model.Layout) -> void:
|
|
264
|
+
var indent := " ".repeat(layout.span.start.column-1)
|
|
265
|
+
# Preserve the file's indentation convention for native statements/closures.
|
|
266
|
+
if _module.indentation.begins_with("\t"): indent = "\t".repeat((layout.span.start.column-1)/_module.indentation.length())
|
|
267
|
+
for index in range(layout.bindings.size()):
|
|
268
|
+
var binding = layout.bindings[index]
|
|
269
|
+
if index>0: _writer.append("\n"+indent,binding.span)
|
|
270
|
+
_writer.append("var "+binding.symbol+" = preload("+_resource(Preset)+").new("+binding.base+", ",binding.span)
|
|
271
|
+
if binding.scope==0:
|
|
272
|
+
var method: String = binding.symbol+"_defaults"
|
|
273
|
+
while _module.identifiers.has(method): method += "_"
|
|
274
|
+
_module.identifiers.append(method)
|
|
275
|
+
_preset_methods.append({"name":method,"binding":binding})
|
|
276
|
+
_writer.append("Callable(self, "+_string(method)+")",binding.span)
|
|
277
|
+
else:
|
|
278
|
+
_writer.append("func(): return ",binding.span)
|
|
279
|
+
_default_groups(binding)
|
|
280
|
+
_writer.append(", "+var_to_str(binding.generated)+", "+_string("%s:%d:%d" % [_file.name,binding.span.start.line,binding.span.start.column])+")",binding.span)
|
|
281
|
+
|
|
282
|
+
func _default_groups(binding) -> void:
|
|
283
|
+
_writer.append("[",binding.span)
|
|
284
|
+
for group_index in range(binding.groups.size()):
|
|
285
|
+
if group_index>0: _writer.append(", ",binding.span)
|
|
286
|
+
_writer.append("[",binding.span)
|
|
287
|
+
var group = binding.groups[group_index]
|
|
288
|
+
for attr_index in range(group.attributes.size()):
|
|
289
|
+
var attr = group.attributes[attr_index]
|
|
290
|
+
if attr_index>0: _writer.append(", ",attr.span)
|
|
291
|
+
_writer.append("["+("null" if attr.kind=="spread" else _string(attr.name))+", ",attr.span)
|
|
292
|
+
if attr.kind=="spread": _writer.append("preload("+_resource(Element)+").snapshot(",attr.span)
|
|
293
|
+
if attr.value is Model.GdExpression: _parts(attr.value.parts)
|
|
294
|
+
else: _writer.append(literal(attr.value),attr.value.span)
|
|
295
|
+
if attr.kind=="spread": _writer.append(")",attr.span)
|
|
296
|
+
_writer.append(", "+_string("%s:%d:%d" % [_file.name,attr.name_span.start.line,attr.name_span.start.column])+"]",attr.span)
|
|
297
|
+
_writer.append("]",binding.span)
|
|
298
|
+
_writer.append("]",binding.span)
|
|
299
|
+
|
|
300
|
+
|
|
215
301
|
func _bindable(property: Model.Bindable) -> void:
|
|
216
302
|
var incoming := "_gogo_incoming_"+property.name
|
|
217
303
|
while _module.identifiers.has(incoming): incoming += "_"
|
|
@@ -219,13 +305,13 @@ func _bindable(property: Model.Bindable) -> void:
|
|
|
219
305
|
while _module.identifiers.has(previous): previous += "_"
|
|
220
306
|
var indent := _module.indentation
|
|
221
307
|
_writer.append("signal "+property.signal_name+"(previous: ",property.name_span)
|
|
222
|
-
|
|
308
|
+
_code(property.type_span)
|
|
223
309
|
_writer.append(", value: ",property.name_span)
|
|
224
|
-
|
|
310
|
+
_code(property.type_span)
|
|
225
311
|
_writer.append(")\n",property.name_span)
|
|
226
312
|
if property.annotations_span != null:
|
|
227
313
|
_writer.append(_file.slice(property.annotations_span),property.annotations_span,true)
|
|
228
|
-
|
|
314
|
+
_code(property.declaration_span)
|
|
229
315
|
_writer.append(":",property.name_span)
|
|
230
316
|
if property.comment_span != null:
|
|
231
317
|
_writer.append(" ")
|
|
@@ -234,7 +320,9 @@ func _bindable(property: Model.Bindable) -> void:
|
|
|
234
320
|
# Use the native property backing slot: accesses inside its own setter are
|
|
235
321
|
# direct, and declaration initialization does not call the setter.
|
|
236
322
|
_writer.append(indent.repeat(2)+"if is_same("+property.name+", "+incoming+"): return\n",property.name_span)
|
|
237
|
-
_writer.append(indent.repeat(2)+"var "+previous+": "
|
|
323
|
+
_writer.append(indent.repeat(2)+"var "+previous+": ",property.name_span)
|
|
324
|
+
_code(property.type_span)
|
|
325
|
+
_writer.append(" = "+property.name+"\n",property.name_span)
|
|
238
326
|
_writer.append(indent.repeat(2)+property.name+" = "+incoming+"\n",property.name_span)
|
|
239
327
|
_writer.append(indent.repeat(2)+property.signal_name+".emit("+previous+", "+incoming+")\n",property.name_span)
|
|
240
328
|
_writer.append(indent+"get:\n"+indent.repeat(2)+"preload("+_resource(State)+").observe(self, &"+_string(property.signal_name)+")\n"+indent.repeat(2)+"return "+property.name+"\n",property.name_span)
|
|
@@ -254,3 +342,58 @@ func _binding_argument(argument: Argument) -> void:
|
|
|
254
342
|
endpoint += "."+str(name)
|
|
255
343
|
checks.append(endpoint+" != null")
|
|
256
344
|
_writer.append("preload("+_resource(Binding)+").new(self, func("+owner+"): return "+endpoint+" if "+" and ".join(checks)+" else null, "+_string(field)+", func("+source+", "+edit+": bool): "+source+"."+field+" = "+edit+", "+_string(".".join(argument.binding_path))+")",argument.expression.span)
|
|
345
|
+
|
|
346
|
+
func _render_call(node: Model.RenderCall) -> void:
|
|
347
|
+
var cast: String = node.result_type
|
|
348
|
+
if node.statement or cast in ["void","Variant"]: cast = ""
|
|
349
|
+
if not cast.is_empty(): _writer.append("(",node.span)
|
|
350
|
+
var member := not node.receiver.is_empty()
|
|
351
|
+
var base := member and node.receiver.size()==1 and node.receiver[0] is Model.Code and _file.text.substr(node.receiver[0].span.start.index,node.receiver[0].span.end.index-node.receiver[0].span.start.index).strip_edges()=="super"
|
|
352
|
+
_writer.append("preload("+_resource(State)+")."+("base_at" if base else "method_at" if member else "call_at")+"("+_string(_file.name+":"+str(node.span.start.offset))+", ",node.span)
|
|
353
|
+
if base:
|
|
354
|
+
_writer.append("self, "+_string(_output_directory.path_join(_file.generated_path().get_file()))+", "+_string(node.method),node.span)
|
|
355
|
+
elif member:
|
|
356
|
+
_parts(node.receiver)
|
|
357
|
+
_writer.append(", "+_string(node.method),node.span)
|
|
358
|
+
else: _parts(node.callee)
|
|
359
|
+
_writer.append(", [",node.span)
|
|
360
|
+
for index in range(node.arguments.size()):
|
|
361
|
+
if index>0: _writer.append(", ",node.span)
|
|
362
|
+
_parts(node.arguments[index].parts)
|
|
363
|
+
if member:
|
|
364
|
+
var receiver := "_gogo_receiver"
|
|
365
|
+
var args := "_gogo_arguments"
|
|
366
|
+
while _module.identifiers.has(receiver): receiver += "_"
|
|
367
|
+
while _module.identifiers.has(args): args += "_"
|
|
368
|
+
_writer.append("], func("+receiver+", "+args+"): "+("" if node.result_type=="void" or node.statement else "return ")+("super" if base else receiver)+"."+node.method+"(",node.span)
|
|
369
|
+
for index in range(node.arguments.size()):
|
|
370
|
+
if index>0: _writer.append(", ",node.span)
|
|
371
|
+
_writer.append(args+"["+str(index)+"]",node.span)
|
|
372
|
+
_writer.append(")",node.span)
|
|
373
|
+
else: _writer.append("]",node.span)
|
|
374
|
+
# Never run/retain the proof closure: native static checking still sees the
|
|
375
|
+
# authored call and its original argument types before source publication.
|
|
376
|
+
_writer.append(", func(): ",node.span)
|
|
377
|
+
if node.callable_value:
|
|
378
|
+
_parts(node.callee)
|
|
379
|
+
_writer.append(".call(",node.span)
|
|
380
|
+
for index in range(node.arguments.size()):
|
|
381
|
+
if index>0: _writer.append(", ",node.span)
|
|
382
|
+
_parts(node.arguments[index].parts)
|
|
383
|
+
_writer.append(")",node.span)
|
|
384
|
+
else: _parts(node.original)
|
|
385
|
+
if node.reactive_lambda: _writer.append(", true",node.span)
|
|
386
|
+
_writer.append(")",node.span)
|
|
387
|
+
if not cast.is_empty(): _writer.append(" as "+_memo_type(cast)+")",node.span)
|
|
388
|
+
|
|
389
|
+
func _memo_type(type: String) -> String:
|
|
390
|
+
var source := Source.new(type)
|
|
391
|
+
var lexer = preload("lexer.gd").new(source)
|
|
392
|
+
var cursor := 0
|
|
393
|
+
var result := ""
|
|
394
|
+
while cursor<type.length():
|
|
395
|
+
var token = lexer.next_token(cursor)
|
|
396
|
+
if token==null: return type
|
|
397
|
+
cursor = token.span.end.index
|
|
398
|
+
result += _module.import_aliases.get(token.text,token.text) if token.kind=="identifier" else token.text
|
|
399
|
+
return result
|
|
@@ -7,15 +7,21 @@ const Parser = preload("parser.gd")
|
|
|
7
7
|
const Bindable = preload("bindable.gd")
|
|
8
8
|
const Validator = preload("validator.gd")
|
|
9
9
|
const Composition = preload("composition.gd")
|
|
10
|
+
const Layouts = preload("layouts.gd")
|
|
11
|
+
const RenderCalls = preload("render_calls.gd")
|
|
12
|
+
const ImportAliases = preload("import_aliases.gd")
|
|
10
13
|
|
|
11
14
|
static func analyze(text: String, source_name: String = "<source>", output_path: String = "", project_sources = null) -> Model.Result:
|
|
12
15
|
var file := Source.new(text, source_name, output_path, project_sources)
|
|
13
16
|
var parser := Parser.new(file)
|
|
14
17
|
var result := Model.Result.new()
|
|
15
18
|
var module := parser.parse()
|
|
19
|
+
if module != null and file.diagnostics.is_empty(): Layouts.new(file).apply(module)
|
|
16
20
|
if module != null: Bindable.new(file).apply(module)
|
|
17
21
|
if module != null and file.diagnostics.is_empty(): Composition.new(file).apply(module)
|
|
18
22
|
if module != null and file.diagnostics.is_empty(): Validator.new(file).validate(module)
|
|
23
|
+
if module != null and file.diagnostics.is_empty(): ImportAliases.new(file).apply(module)
|
|
24
|
+
if module != null and file.diagnostics.is_empty(): RenderCalls.new(file).apply(module)
|
|
19
25
|
result.tokens = parser.lexer.tokens
|
|
20
26
|
result.diagnostics = file.diagnostics
|
|
21
27
|
result.module = module if result.diagnostics.is_empty() else null
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
@tool
|
|
2
|
+
extends RefCounted
|
|
3
|
+
## Explicit native-name imports are lexical Gogo names, not native registrations.
|
|
4
|
+
## Only lexer-proven identifier spans enter the existing mapped emitter.
|
|
5
|
+
const Model = preload("model.gd")
|
|
6
|
+
const Lexer = preload("lexer.gd")
|
|
7
|
+
var file
|
|
8
|
+
var module
|
|
9
|
+
var declarations: Dictionary = {}
|
|
10
|
+
func _init(source) -> void: file = source
|
|
11
|
+
func apply(value) -> void:
|
|
12
|
+
module = value
|
|
13
|
+
if module.import_aliases.is_empty(): return
|
|
14
|
+
_parts(module.parts)
|
|
15
|
+
func _parts(parts: Array) -> void:
|
|
16
|
+
for part in parts:
|
|
17
|
+
if part is Model.Code: _code(part.span)
|
|
18
|
+
elif part is Model.Bindable:
|
|
19
|
+
_code(part.declaration_span)
|
|
20
|
+
elif part is Model.GdExpression: _parts(part.parts)
|
|
21
|
+
elif part is Model.Layout:
|
|
22
|
+
for binding in part.bindings:
|
|
23
|
+
for group in binding.groups:
|
|
24
|
+
for attribute in group.attributes:
|
|
25
|
+
if attribute.value is Model.GdExpression: _parts(attribute.value.parts)
|
|
26
|
+
elif part is Model.Component:
|
|
27
|
+
for attribute in part.attributes:
|
|
28
|
+
if attribute.value is Model.GdExpression: _parts(attribute.value.parts)
|
|
29
|
+
_parts(part.children)
|
|
30
|
+
_parts(part.text_body)
|
|
31
|
+
func _code(span) -> void:
|
|
32
|
+
var lexer := Lexer.new(file)
|
|
33
|
+
var cursor: int = span.start.index
|
|
34
|
+
var tokens: Array = []
|
|
35
|
+
while cursor<span.end.index:
|
|
36
|
+
var token = lexer.next_token(cursor)
|
|
37
|
+
if token==null: return
|
|
38
|
+
cursor = token.span.end.index
|
|
39
|
+
if token.kind not in ["whitespace","newline","comment","continuation"]: tokens.append(token)
|
|
40
|
+
var parameter_depth := 0
|
|
41
|
+
var in_function := false
|
|
42
|
+
for index in range(tokens.size()):
|
|
43
|
+
var token = tokens[index]
|
|
44
|
+
if token.text=="func": in_function = true
|
|
45
|
+
elif in_function and token.text=="(": parameter_depth += 1
|
|
46
|
+
elif in_function and token.text==")":
|
|
47
|
+
parameter_depth -= 1
|
|
48
|
+
if parameter_depth==0: in_function = false
|
|
49
|
+
if token.kind!="identifier" or not module.import_aliases.has(token.text): continue
|
|
50
|
+
if module.identifier_imports.any(func(item): return item.span.start.index==token.span.start.index): continue
|
|
51
|
+
if in_function and parameter_depth==1 and index>0 and tokens[index-1].text in ["(",","] and index+1<tokens.size() and tokens[index+1].text in [":","=",",",")"]:
|
|
52
|
+
file.fail("Imported native-name alias cannot be a parameter: "+token.text,token.span.start.index,token.span.end.index)
|
|
53
|
+
return
|
|
54
|
+
# GDScript dictionary shorthand is a string key, not an expression.
|
|
55
|
+
if index+1<tokens.size() and tokens[index+1].text=="=" and index>0 and tokens[index-1].text in ["{",","]: continue
|
|
56
|
+
# Member access is a different name, except explicit self.constant.
|
|
57
|
+
if index>0 and tokens[index-1].text=="." and (index<2 or tokens[index-2].text!="self"): continue
|
|
58
|
+
if index>0 and tokens[index-1].text in ["var","const","func","signal","class","for"]:
|
|
59
|
+
var prefix: String = file.text.substr(file.line_start(token.span.start.line),tokens[index-1].span.start.index-file.line_start(token.span.start.line))
|
|
60
|
+
if tokens[index-1].text!="const" or not prefix.is_empty() or declarations.has(token.text):
|
|
61
|
+
file.fail("Imported native-name alias cannot be redeclared: "+token.text,token.span.start.index,token.span.end.index)
|
|
62
|
+
return
|
|
63
|
+
declarations[token.text] = true
|
|
64
|
+
module.identifier_imports.append({"span":token.span,"alias":module.import_aliases[token.text]})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uid://d0nahj3l1v7n5
|