@papercraftgames/estragon 0.1.0-beta.27 → 0.1.0-beta.28

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.
@@ -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
@@ -55,9 +57,20 @@ func _resource(resource: Resource) -> String:
55
57
  func emit() -> MappedWriter:
56
58
  _parts(_module.parts)
57
59
  _writer.append("\n")
60
+ var exports := {}
61
+ for binding in _module.presets:
62
+ if binding.exported: exports[binding.name] = binding
63
+ for name in exports:
64
+ var binding = exports[name]
65
+ _writer.append("\nvar "+name+":\n"+_module.indentation+"get: return "+binding.symbol+"\n",binding.span)
66
+ if binding.mutable: _writer.append(_module.indentation+"set(value): "+binding.symbol+" = value\n",binding.span)
58
67
  for path in _module.project_imports:
59
68
  var imported: Dictionary = _module.project_imports[path]
60
69
  _writer.append("\nconst "+imported.alias+" = preload("+_string(Source.relative_path(_output_directory,path))+")\n",imported.span)
70
+ for method in _preset_methods:
71
+ _writer.append("\nfunc "+method.name+"(): return ",method.binding.span)
72
+ _default_groups(method.binding)
73
+ _writer.append("\n",method.binding.span)
61
74
  if _module.composition:
62
75
  _writer.append("\nfunc _notification(_gogo_what: int) -> void:\n"+_module.indentation+"preload("+_resource(State)+").lifecycle(self, _gogo_what)\n")
63
76
  if not _factories.is_empty():
@@ -98,6 +111,16 @@ func _argument(expression: Model.GdExpression, type_name: String, factory: Facto
98
111
  _arguments[expression] = argument
99
112
 
100
113
  func _collect(node: Model.Component, factory: Factory) -> void:
114
+ if node.preset!=null:
115
+ var expression := Model.GdExpression.new()
116
+ expression.span = node.name_span
117
+ var reference := Model.Literal.new()
118
+ reference.kind = "preset-reference"
119
+ reference.value = node.preset.symbol
120
+ reference.span = node.name_span
121
+ expression.parts.append(reference)
122
+ node.preset_expression = expression
123
+ _argument(expression,"Variant",factory)
101
124
  for attribute in node.attributes:
102
125
  if attribute.value is Model.GdExpression:
103
126
  _argument(attribute.value, attribute.type_name, factory)
@@ -113,16 +136,23 @@ func _parts(parts: Array[Model.SyntaxNode]) -> void:
113
136
  for part in parts:
114
137
  if part is Model.Code: _code(part.span)
115
138
  elif part is Model.Bindable: _bindable(part)
139
+ elif part is Model.Layout: _layout(part)
140
+ elif part is Model.Literal and part.kind=="preset-reference": _writer.append(str(part.value),part.span)
116
141
  else: _call(part)
117
142
 
118
143
  func _code(span: Model.Span) -> void:
119
144
  var cursor: int = span.start.index
145
+ var edits: Array = []
120
146
  for imported in _module.resource_imports:
121
- var literal_span: Model.Span = imported.span
147
+ edits.append({"span":imported.span,"value":_string(Source.relative_path(_output_directory,imported.path))})
148
+ for imported in _module.identifier_imports: edits.append({"span":imported.span,"value":imported.alias})
149
+ edits.sort_custom(func(a,b): return a.span.start.index<b.span.start.index)
150
+ for edit in edits:
151
+ var literal_span: Model.Span = edit.span
122
152
  if literal_span.start.index < cursor or literal_span.end.index > span.end.index: continue
123
153
  var before := _file.span(cursor,literal_span.start.index)
124
154
  _writer.append(_file.slice(before),before,true)
125
- _writer.append(_string(Source.relative_path(_output_directory,imported.path)),literal_span)
155
+ _writer.append(edit.value,literal_span)
126
156
  cursor = literal_span.end.index
127
157
  var rest := _file.span(cursor,span.end.index)
128
158
  _writer.append(_file.slice(rest),rest,true)
@@ -198,6 +228,7 @@ func _description(node: Model.Component, indentation: String) -> String:
198
228
  for child in node.children:
199
229
  children.append(_arguments[child].name if child is Model.GdExpression else _description(child,indentation))
200
230
  var native_type := _string(node.runtime_class) if ClassDB.class_exists(node.runtime_class) else node.runtime_class
231
+ if node.preset!=null: native_type = "preload("+_resource(Preset)+").type_of("+_arguments[node.preset_expression].name+")"
201
232
  _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
233
  for attribute in node.attributes:
203
234
  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 +241,47 @@ func _description(node: Model.Component, indentation: String) -> String:
210
241
  parts.append("[%s, %s]" % [value,_string("%s:%d:%d" % [_file.name,part.span.start.line,part.span.start.column])])
211
242
  _line(variable+".set_text_body(["+", ".join(parts)+"])",node.span,indentation)
212
243
  if not bindings.is_empty(): _line(variable+".bindings = {"+", ".join(bindings)+"}",node.span,indentation)
244
+ if node.preset!=null: _line(variable+" = preload("+_resource(Preset)+").apply("+_arguments[node.preset_expression].name+", "+variable+")",node.span,indentation)
213
245
  return variable
214
246
 
247
+ func _layout(layout: Model.Layout) -> void:
248
+ var indent := " ".repeat(layout.span.start.column-1)
249
+ # Preserve the file's indentation convention for native statements/closures.
250
+ if _module.indentation.begins_with("\t"): indent = "\t".repeat((layout.span.start.column-1)/_module.indentation.length())
251
+ for index in range(layout.bindings.size()):
252
+ var binding = layout.bindings[index]
253
+ if index>0: _writer.append("\n"+indent,binding.span)
254
+ _writer.append("var "+binding.symbol+" = preload("+_resource(Preset)+").new("+binding.base+", ",binding.span)
255
+ if binding.scope==0:
256
+ var method: String = binding.symbol+"_defaults"
257
+ while _module.identifiers.has(method): method += "_"
258
+ _module.identifiers.append(method)
259
+ _preset_methods.append({"name":method,"binding":binding})
260
+ _writer.append("Callable(self, "+_string(method)+")",binding.span)
261
+ else:
262
+ _writer.append("func(): return ",binding.span)
263
+ _default_groups(binding)
264
+ _writer.append(", "+var_to_str(binding.generated)+", "+_string("%s:%d:%d" % [_file.name,binding.span.start.line,binding.span.start.column])+")",binding.span)
265
+
266
+ func _default_groups(binding) -> void:
267
+ _writer.append("[",binding.span)
268
+ for group_index in range(binding.groups.size()):
269
+ if group_index>0: _writer.append(", ",binding.span)
270
+ _writer.append("[",binding.span)
271
+ var group = binding.groups[group_index]
272
+ for attr_index in range(group.attributes.size()):
273
+ var attr = group.attributes[attr_index]
274
+ if attr_index>0: _writer.append(", ",attr.span)
275
+ _writer.append("["+("null" if attr.kind=="spread" else _string(attr.name))+", ",attr.span)
276
+ if attr.kind=="spread": _writer.append("preload("+_resource(Element)+").snapshot(",attr.span)
277
+ if attr.value is Model.GdExpression: _parts(attr.value.parts)
278
+ else: _writer.append(literal(attr.value),attr.value.span)
279
+ if attr.kind=="spread": _writer.append(")",attr.span)
280
+ _writer.append(", "+_string("%s:%d:%d" % [_file.name,attr.name_span.start.line,attr.name_span.start.column])+"]",attr.span)
281
+ _writer.append("]",binding.span)
282
+ _writer.append("]",binding.span)
283
+
284
+
215
285
  func _bindable(property: Model.Bindable) -> void:
216
286
  var incoming := "_gogo_incoming_"+property.name
217
287
  while _module.identifiers.has(incoming): incoming += "_"
@@ -219,13 +289,13 @@ func _bindable(property: Model.Bindable) -> void:
219
289
  while _module.identifiers.has(previous): previous += "_"
220
290
  var indent := _module.indentation
221
291
  _writer.append("signal "+property.signal_name+"(previous: ",property.name_span)
222
- _writer.append(_file.slice(property.type_span),property.type_span,true)
292
+ _code(property.type_span)
223
293
  _writer.append(", value: ",property.name_span)
224
- _writer.append(_file.slice(property.type_span),property.type_span,true)
294
+ _code(property.type_span)
225
295
  _writer.append(")\n",property.name_span)
226
296
  if property.annotations_span != null:
227
297
  _writer.append(_file.slice(property.annotations_span),property.annotations_span,true)
228
- _writer.append(_file.slice(property.declaration_span),property.declaration_span,true)
298
+ _code(property.declaration_span)
229
299
  _writer.append(":",property.name_span)
230
300
  if property.comment_span != null:
231
301
  _writer.append(" ")
@@ -234,7 +304,9 @@ func _bindable(property: Model.Bindable) -> void:
234
304
  # Use the native property backing slot: accesses inside its own setter are
235
305
  # direct, and declaration initialization does not call the setter.
236
306
  _writer.append(indent.repeat(2)+"if is_same("+property.name+", "+incoming+"): return\n",property.name_span)
237
- _writer.append(indent.repeat(2)+"var "+previous+": "+property.type_name+" = "+property.name+"\n",property.name_span)
307
+ _writer.append(indent.repeat(2)+"var "+previous+": ",property.name_span)
308
+ _code(property.type_span)
309
+ _writer.append(" = "+property.name+"\n",property.name_span)
238
310
  _writer.append(indent.repeat(2)+property.name+" = "+incoming+"\n",property.name_span)
239
311
  _writer.append(indent.repeat(2)+property.signal_name+".emit("+previous+", "+incoming+")\n",property.name_span)
240
312
  _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)
@@ -7,15 +7,19 @@ 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 ImportAliases = preload("import_aliases.gd")
10
12
 
11
13
  static func analyze(text: String, source_name: String = "<source>", output_path: String = "", project_sources = null) -> Model.Result:
12
14
  var file := Source.new(text, source_name, output_path, project_sources)
13
15
  var parser := Parser.new(file)
14
16
  var result := Model.Result.new()
15
17
  var module := parser.parse()
18
+ if module != null and file.diagnostics.is_empty(): Layouts.new(file).apply(module)
16
19
  if module != null: Bindable.new(file).apply(module)
17
20
  if module != null and file.diagnostics.is_empty(): Composition.new(file).apply(module)
18
21
  if module != null and file.diagnostics.is_empty(): Validator.new(file).validate(module)
22
+ if module != null and file.diagnostics.is_empty(): ImportAliases.new(file).apply(module)
19
23
  result.tokens = parser.lexer.tokens
20
24
  result.diagnostics = file.diagnostics
21
25
  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
@@ -0,0 +1,412 @@
1
+ @tool
2
+ extends RefCounted
3
+ ## Lexical component presets. Scope boundaries come from native tokens/indentation,
4
+ ## never markup substitutions or evaluation of authored expressions.
5
+ const Source = preload("source.gd")
6
+ const Parser = preload("parser.gd")
7
+ const Model = preload("model.gd")
8
+ const Lexer = preload("lexer.gd")
9
+ const Resolver = preload("native/resolver.gd")
10
+ const Definition = preload("../runtime/node/internal/grid_definition.gd")
11
+ const Grid = preload("../runtime/node/grid.gd")
12
+ const Box = preload("../runtime/node/box.gd")
13
+ var visiting: Dictionary = {}
14
+ var file
15
+ var module
16
+ var resolver
17
+ var tokens: Array = []
18
+ var scopes: Array = [{"start":0,"end":0,"indent":0,"parent":-1}]
19
+ var symbols: Array = []
20
+ func _init(source) -> void: file = source
21
+ func apply(value) -> void:
22
+ module = value
23
+ # Qualified instance exports can be used by modules without declarations too.
24
+ if not module.parts.any(func(part): return part is Model.Layout) and not _has_qualified(module.parts): return
25
+ resolver = Resolver.new(file)
26
+ resolver.scan(module)
27
+ if not file.diagnostics.is_empty(): return
28
+ _index()
29
+ for part in module.parts:
30
+ if part is Model.Layout: _declaration(part)
31
+ if not file.diagnostics.is_empty(): return
32
+ _references(module.parts)
33
+
34
+ func _error(message: String, span) -> void:
35
+ file.fail(message,span.start.index,span.end.index)
36
+ func _unique() -> String:
37
+ var name := "_GogoLayout_"+str(module.presets.size())
38
+ while module.identifiers.has(name): name += "_"
39
+ module.identifiers.append(name)
40
+ return name
41
+ func _index() -> void:
42
+ var anchors: Array = []
43
+ var lexer := Lexer.new(file)
44
+ for part in module.parts:
45
+ if part is Model.Layout:
46
+ anchors.append(part.span.start)
47
+ continue
48
+ if not part is Model.Code: continue
49
+ var cursor: int = part.span.start.index
50
+ var depth := 0
51
+ while cursor<part.span.end.index:
52
+ var token = lexer.next_token(cursor)
53
+ if token==null: return
54
+ cursor = token.span.end.index
55
+ if token.kind in ["whitespace","newline","comment","continuation"]: continue
56
+ tokens.append(token)
57
+ var prefix: String = file.text.substr(file.line_start(token.span.start.line),token.span.start.index-file.line_start(token.span.start.line))
58
+ if depth==0 and prefix.strip_edges().is_empty(): anchors.append(token.span.start)
59
+ if token.text in ["(","[","{"]: depth += 1
60
+ elif token.text in [")","]","}"]: depth -= 1
61
+ anchors.sort_custom(func(a,b): return a.index<b.index)
62
+ scopes[0].end = file.text.length()+1
63
+ var stack: Array = [0]
64
+ for point in anchors:
65
+ var indent: int = point.column-1
66
+ while scopes[stack[-1]].indent>indent:
67
+ scopes[stack.pop_back()].end = point.index
68
+ if indent>scopes[stack[-1]].indent:
69
+ var index := scopes.size()
70
+ scopes.append({"start":point.index,"end":file.text.length()+1,"indent":indent,"parent":stack[-1]})
71
+ stack.append(index)
72
+ _parameters()
73
+ for index in range(tokens.size()-1):
74
+ if tokens[index].text in ["var","const","func","class","signal","for"] and tokens[index+1].kind=="identifier":
75
+ symbols.append({"name":tokens[index+1].text,"span":tokens[index+1].span,"scope":_scope(tokens[index].span.start.index),"kind":tokens[index].text})
76
+ func _scope(offset: int) -> int:
77
+ var selected := 0
78
+ for index in range(1,scopes.size()):
79
+ if offset>=scopes[index].start and offset<scopes[index].end and scopes[index].indent>scopes[selected].indent: selected = index
80
+ return selected
81
+ func _visible(name: String, offset: int, own: bool = false):
82
+ var scope := 0 if own else _scope(offset)
83
+ while scope>=0:
84
+ var found = null
85
+ for binding in module.presets:
86
+ if binding.name==name and binding.scope==scope and binding.visible<=offset and (found==null or binding.visible>found.visible): found = binding
87
+ for symbol in symbols:
88
+ if symbol.name==name and symbol.scope==scope and symbol.span.start.index<offset and (found==null or symbol.span.start.index>found.visible): return null
89
+ if found!=null: return found
90
+ scope = scopes[scope].parent
91
+ return null
92
+ func _declaration(layout) -> void:
93
+ var scope := _scope(layout.span.start.index)
94
+ if layout.exported and scope!=0:
95
+ _error("export layout is only valid at module scope",layout.span)
96
+ return
97
+ var root = layout.root
98
+ if not root.name.is_valid_identifier() or root.name in Lexer.KEYWORDS:
99
+ _error("layout bindings require unqualified Gogo identifiers; import a qualified component under an explicit alias",root.name_span)
100
+ return
101
+ var rows: Array = []
102
+ var groups := {}
103
+ var line := -1
104
+ for child in root.children:
105
+ if child is Model.Literal and str(child.value).strip_edges().is_empty(): continue
106
+ if not child is Model.Component or not child.children.is_empty():
107
+ _error("layout body requires an area matrix of empty named tags",child.span)
108
+ return
109
+ if not child.name.is_valid_identifier() or child.name in Lexer.KEYWORDS or child.name==root.name:
110
+ _error("layout area requires a distinct unqualified identifier",child.name_span)
111
+ return
112
+ if line!=child.span.start.line:
113
+ rows.append([])
114
+ line = child.span.start.line
115
+ rows[-1].append(child.name)
116
+ if not groups.has(child.name): groups[child.name] = []
117
+ groups[child.name].append(child)
118
+ if rows.is_empty():
119
+ _error("layout requires at least one area row",root.span)
120
+ return
121
+ var areas: Array = []
122
+ for row in rows: areas.append(" ".join(row))
123
+ var canonical := Definition.normalize(rows[0].size(),rows.size(),areas)
124
+ if canonical.has("error"):
125
+ _error("layout matrix: "+canonical.error,root.span)
126
+ return
127
+ # All bases are resolved before this declaration's new bindings are visible.
128
+ var pending: Array = []
129
+ var root_binding = _binding(root,[root],layout,scope,true)
130
+ if root_binding==null: return
131
+ root_binding.generated = {"direction":"grid","columns":rows[0].size(),"rows":null,"template":"","areas":areas}
132
+ pending.append(root_binding)
133
+ for name in groups:
134
+ var binding = _binding(groups[name][0],groups[name],layout,scope,false)
135
+ if binding==null: return
136
+ binding.generated = {"area":name}
137
+ pending.append(binding)
138
+ layout.bindings = pending
139
+ module.presets.append_array(pending)
140
+
141
+ func _binding(node, groups: Array, layout, scope: int, root: bool):
142
+ var binding := Model.Preset.new()
143
+ binding.name = node.name
144
+ binding.span = node.name_span
145
+ binding.scope = scope
146
+ binding.visible = layout.span.end.index
147
+ binding.mutable = layout.mutable
148
+ binding.exported = layout.exported
149
+ binding.groups = groups
150
+ # Reserve immediately; the pending bindings are not yet lookup-visible.
151
+ binding.symbol = "_GogoLayout_"+str(module.identifiers.size())
152
+ while module.identifiers.has(binding.symbol): binding.symbol += "_"
153
+ module.identifiers.append(binding.symbol)
154
+ if layout.exported and (ClassDB.class_exists(node.name) or resolver.globals.has(node.name)):
155
+ _error("Cannot export a preset under a native/global class name; choose an explicit local alias: "+node.name,node.name_span)
156
+ return null
157
+ if layout.exported and resolver.aliases.has(node.name) and not module.import_aliases.has(node.name):
158
+ var private_import := "_GogoImport_"+str(module.import_aliases.size())
159
+ while module.identifiers.has(private_import): private_import += "_"
160
+ module.identifiers.append(private_import)
161
+ module.import_aliases[node.name] = private_import
162
+ var previous = _visible(node.name,layout.span.start.index)
163
+ if previous==null: previous = _alias_before(node.name,layout.span.start.index)
164
+ if previous!=null:
165
+ if layout.exported and previous.symbol==node.name and previous.groups.is_empty():
166
+ previous.symbol = binding.symbol+"_base"
167
+ module.identifiers.append(previous.symbol)
168
+ for symbol in symbols:
169
+ if symbol.name==node.name and symbol.span.start.index<previous.visible: module.identifier_imports.append({"span":symbol.span,"alias":previous.symbol})
170
+ binding.base = previous.symbol
171
+ binding.resolved = previous.resolved
172
+ else:
173
+ var authored = _native_before(node.name,layout.span.start.index)
174
+ var imported: bool = authored!=null and authored.kind=="const" and authored.scope==0 and (resolver.aliases.has(node.name) or resolver.native_aliases.has(node.name))
175
+ var project = resolver.scope()
176
+ var candidates: Array = project.named(node.name).filter(func(path): return path!=project.authored(file.name))
177
+ var known: bool = imported or resolver.globals.has(node.name) or ClassDB.class_exists(node.name) or not candidates.is_empty()
178
+ if authored!=null and not imported:
179
+ _error("layout base collides with a non-component binding: "+node.name,node.name_span)
180
+ return null
181
+ if known:
182
+ binding.resolved = resolver.resolve(node.name,node.name_span)
183
+ if binding.resolved.is_empty(): return null
184
+ binding.base = binding.resolved.get("project_alias",binding.resolved.get("runtime_alias",binding.resolved.get("native_alias",node.name)))
185
+ else:
186
+ var script = Grid if root else Box
187
+ binding.resolved = {"native":"Node2D","script":script}
188
+ var path: String = script.resource_path
189
+ if not module.project_imports.has(path): module.project_imports[path] = {"alias":_unique(),"span":node.name_span}
190
+ binding.base = module.project_imports[path].alias
191
+ binding.resolved.runtime_alias = binding.base
192
+ if root and not Definition.marked(binding.resolved.script):
193
+ _error("layout root base must support native Grid properties; use a Grid-derived component",node.name_span)
194
+ return null
195
+ var literals := {}
196
+ for group in groups:
197
+ var seen := {}
198
+ for attribute in group.attributes:
199
+ if attribute.name in ["key","bind:value"]:
200
+ _error("layout defaults cannot preset key or bind:value; author instance controls at the use site",attribute.name_span)
201
+ return null
202
+ if attribute.kind=="spread": continue
203
+ if seen.has(attribute.name):
204
+ _error("Duplicate layout default "+attribute.name,attribute.name_span)
205
+ return null
206
+ seen[attribute.name] = true
207
+ var constant := _constant(attribute.value)
208
+ if constant.known:
209
+ if literals.has(attribute.name) and (typeof(literals[attribute.name])!=typeof(constant.value) or literals[attribute.name]!=constant.value):
210
+ _error("Conflicting repeated layout default "+attribute.name,attribute.name_span)
211
+ return null
212
+ literals[attribute.name] = constant.value
213
+ return binding
214
+
215
+ func _references(parts: Array) -> void:
216
+ for part in parts:
217
+ if part is Model.Code: _code(part.span)
218
+ elif part is Model.Bindable: _code(part.declaration_span)
219
+ elif part is Model.GdExpression: _references(part.parts)
220
+ elif part is Model.Layout:
221
+ for binding in part.bindings:
222
+ for group in binding.groups:
223
+ for attr in group.attributes:
224
+ if attr.value is Model.GdExpression: _references(attr.value.parts)
225
+ elif part is Model.Component:
226
+ part.preset = _visible(part.name,part.name_span.start.index)
227
+ if part.preset==null and not part.name.contains("."): part.preset = _alias_before(part.name,part.name_span.start.index)
228
+ if part.preset==null and part.name.contains("."): part.preset = _qualified(part.name,part.name_span)
229
+ if part.preset==null and _parameter_hidden(part.name,part.name_span.start.index):
230
+ _error("Component preset is hidden by a native parameter: "+part.name,part.name_span)
231
+ return
232
+ for attr in part.attributes:
233
+ if attr.value is Model.GdExpression: _references(attr.value.parts)
234
+ _references(part.children)
235
+ func _code(span) -> void:
236
+ var lexer := Lexer.new(file)
237
+ var items: Array = []
238
+ var cursor: int = span.start.index
239
+ while cursor<span.end.index:
240
+ var token = lexer.next_token(cursor)
241
+ if token==null: return
242
+ cursor = token.span.end.index
243
+ if token.kind not in ["whitespace","newline","comment","continuation"]: items.append(token)
244
+ for index in range(items.size()):
245
+ var token = items[index]
246
+ if token.kind!="identifier": continue
247
+ if index>0 and items[index-1].text=="." and (index<2 or items[index-2].text!="self"): continue
248
+ if index+1<items.size() and items[index+1].text=="=" and index>0 and items[index-1].text in ["{",","]: continue
249
+ var binding = _visible(token.text,token.span.start.index,index>1 and items[index-1].text=="." and items[index-2].text=="self")
250
+ if binding==null: continue
251
+ if index>0 and items[index-1].text=="." and binding.scope!=0: continue
252
+ if index>0 and items[index-1].text in ["var","const","func","class","signal","for"]: continue
253
+ if symbols.any(func(item): return item.kind=="parameter" and item.span.start.index==token.span.start.index): continue
254
+ if not binding.mutable and index+1<items.size() and items[index+1].text in ["=","+=","-=","*=","/=","%=","&=","|=","^=","**=","<<=",">>="]:
255
+ _error("Cannot assign to layout const "+token.text,token.span)
256
+ return
257
+ module.identifier_imports.append({"span":token.span,"alias":binding.symbol})
258
+
259
+ func _has_qualified(parts: Array) -> bool:
260
+ for part in parts:
261
+ if part is Model.Component:
262
+ if part.name.contains(".") or _has_qualified(part.children): return true
263
+ for attr in part.attributes:
264
+ if attr.value is Model.GdExpression and _has_qualified(attr.value.parts): return true
265
+ elif part is Model.GdExpression and _has_qualified(part.parts): return true
266
+ return false
267
+ func _qualified(name: String, span):
268
+ var path := name.split(".")
269
+ if path.size()!=2:
270
+ _error("Qualified preset tags require a typed host instance and exported binding",span)
271
+ return null
272
+ if path[0]=="self":
273
+ var own = _visible(path[1],span.start.index,true)
274
+ if own!=null and own.scope==0: return own
275
+ _error("Unknown lexical component preset "+name,span)
276
+ return null
277
+ var type := ""
278
+ var receiver := path[0]
279
+ for index in range(tokens.size()-2):
280
+ if tokens[index].text==receiver and tokens[index+1].text==":" and tokens[index+2].kind=="identifier" and tokens[index].span.start.index<span.start.index:
281
+ var declared = _native_before(receiver,span.start.index)
282
+ if declared!=null and declared.span.start.index==tokens[index].span.start.index: type = tokens[index+2].text
283
+ if not resolver.aliases.has(type):
284
+ _error("Qualified preset tags require a typed Gogo host instance; use an explicit preload alias for native component types",span)
285
+ return null
286
+ var output: String = resolver.aliases[type]
287
+ var authored: String = output.get_base_dir().get_base_dir().path_join(output.get_file().get_basename()+".gogo")
288
+ if not FileAccess.file_exists(authored):
289
+ _error("Qualified preset host has no authored Gogo source: "+type,span)
290
+ return null
291
+ if visiting.has(authored):
292
+ _error("Cyclic exported layout composition: "+authored,span)
293
+ return null
294
+ visiting[authored] = true
295
+ var project = resolver.scope()
296
+ var source_key: String = project.authored(authored)
297
+ var text: String = project.read(source_key) if project.sources.has(source_key) else FileAccess.get_file_as_string(authored)
298
+ var source := Source.new(text,authored,output,file.project)
299
+ var parsed = Parser.new(source).parse()
300
+ if parsed!=null:
301
+ var layouts = get_script().new(source)
302
+ layouts.visiting = visiting
303
+ layouts.apply(parsed)
304
+ visiting.erase(authored)
305
+ if parsed==null or not source.diagnostics.is_empty():
306
+ _error("Invalid exported layout source: "+authored+("; "+source.diagnostics[0].message if not source.diagnostics.is_empty() else ""),span)
307
+ return null
308
+ var exported = null
309
+ for binding in parsed.presets:
310
+ if binding.name==path[1] and binding.exported and binding.scope==0: exported = binding
311
+ if exported==null:
312
+ _error("Unknown or unexported layout binding "+name,span)
313
+ return null
314
+ # Metadata is lexical; runtime reads the existing host's public value. No
315
+ # exporting host is ever instantiated by the compiler or by an import.
316
+ var result := Model.Preset.new()
317
+ result.name = name
318
+ result.symbol = name
319
+ result.span = span
320
+ result.resolved = exported.resolved.duplicate(true)
321
+ var script: Script = result.resolved.script
322
+ var resource: String = result.resolved.get("gogo_path",script.resource_path if script!=null else "")
323
+ if not resource.is_empty():
324
+ if not module.project_imports.has(resource): module.project_imports[resource] = {"alias":_unique(),"span":span}
325
+ var alias: String = module.project_imports[resource].alias
326
+ var previous: String = result.resolved.get("project_alias",result.resolved.get("runtime_alias",""))
327
+ for property in result.resolved.get("properties",{}).values():
328
+ var qualified: String = str(property.get("class_name",""))
329
+ if not previous.is_empty() and (qualified==previous or qualified.begins_with(previous+".")): property["class_name"] = alias+qualified.trim_prefix(previous)
330
+ result.resolved.project_alias = alias
331
+ return result
332
+
333
+ func _alias_before(name: String, offset: int):
334
+ var declaration := -1
335
+ for index in range(tokens.size()-5):
336
+ if tokens[index].text in ["var","const"] and tokens[index+1].text==name and tokens[index+2].text=="=" and tokens[index+3].kind=="identifier" and tokens[index+4].text=="." and tokens[index+5].kind=="identifier" and tokens[index].span.start.index<offset:
337
+ var candidate_scope := _scope(tokens[index].span.start.index)
338
+ var scope := _scope(offset)
339
+ while scope>=0 and scope!=candidate_scope: scope = scopes[scope].parent
340
+ if scope>=0: declaration = index
341
+ if declaration<0: return null
342
+ var first = tokens[declaration+3]
343
+ var last = tokens[declaration+5]
344
+ var value = _qualified(first.text+"."+last.text,file.span(first.span.start.index,last.span.end.index))
345
+ if value==null: return null
346
+ value.name = name
347
+ value.symbol = name
348
+ value.visible = last.span.end.index
349
+ value.scope = _scope(first.span.start.index)
350
+ value.mutable = tokens[declaration].text=="var"
351
+ module.presets.append(value)
352
+ return value
353
+
354
+ func _parameters() -> void:
355
+ for index in range(tokens.size()-2):
356
+ if tokens[index].text!="func": continue
357
+ var opening := index+1
358
+ if tokens[opening].kind=="identifier": opening += 1
359
+ if opening>=tokens.size() or tokens[opening].text!="(": continue
360
+ var depth := 0
361
+ var closing := -1
362
+ var parameters: Array = []
363
+ for cursor in range(opening,tokens.size()):
364
+ var token = tokens[cursor]
365
+ if token.text in ["(","[","{"]: depth += 1
366
+ elif token.text in [")","]","}"]:
367
+ depth -= 1
368
+ if depth==0:
369
+ closing = cursor
370
+ break
371
+ elif depth==1 and token.kind=="identifier" and tokens[cursor-1].text in ["(",","]: parameters.append(token)
372
+ if closing<0: continue
373
+ var colon := closing+1
374
+ while colon<tokens.size() and tokens[colon].text!=":": colon += 1
375
+ if colon>=tokens.size(): continue
376
+ var parent := _scope(tokens[index].span.start.index)
377
+ var scope := -1
378
+ if colon+1<tokens.size() and tokens[colon+1].span.start.line==tokens[colon].span.start.line:
379
+ scope = scopes.size()
380
+ scopes.append({"start":tokens[colon].span.end.index,"end":file.line_start(tokens[colon].span.end.line+1),"indent":scopes[parent].indent+1,"parent":parent})
381
+ else:
382
+ for candidate in range(scopes.size()):
383
+ if scopes[candidate].parent==parent and scopes[candidate].start>tokens[colon].span.end.index and (scope<0 or scopes[candidate].start<scopes[scope].start): scope = candidate
384
+ if scope<0: continue
385
+ for parameter in parameters: symbols.append({"name":parameter.text,"span":parameter.span,"scope":scope,"kind":"parameter"})
386
+
387
+ func _parameter_hidden(name: String, offset: int) -> bool:
388
+ var scope := _scope(offset)
389
+ while scope>=0:
390
+ for symbol in symbols:
391
+ if symbol.name==name and symbol.scope==scope and symbol.kind=="parameter": return true
392
+ scope = scopes[scope].parent
393
+ return false
394
+
395
+ func _constant(value) -> Dictionary:
396
+ if value is Model.Literal: return {"known":true,"value":value.value}
397
+ if not value is Model.GdExpression or value.parts.size()!=1 or not value.parts[0] is Model.Code: return {"known":false}
398
+ var text: String = file.slice(value.parts[0].span).strip_edges()
399
+ if text.is_valid_int(): return {"known":true,"value":int(text)}
400
+ if text.is_valid_float(): return {"known":true,"value":float(text)}
401
+ if text in ["true","false","null"]: return {"known":true,"value":str_to_var(text)}
402
+ return {"known":false}
403
+
404
+ func _native_before(name: String, offset: int):
405
+ var scope := _scope(offset)
406
+ while scope>=0:
407
+ var latest = null
408
+ for symbol in symbols:
409
+ if symbol.name==name and symbol.scope==scope and symbol.span.start.index<offset and (latest==null or symbol.span.start.index>latest.span.start.index): latest = symbol
410
+ if latest!=null: return latest
411
+ scope = scopes[scope].parent
412
+ return null
@@ -0,0 +1 @@
1
+ uid://btmwlkjko32xg