@papercraftgames/estragon 0.0.0-stage → 0.1.0-beta.2
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/CHANGELOG.md +11 -0
- package/LICENSE +20 -0
- package/README.md +28 -2
- package/addons/estragon/compiler/bindable.gd +213 -0
- package/addons/estragon/compiler/bindable.gd.uid +1 -0
- package/addons/estragon/compiler/compiler.gd +24 -0
- package/addons/estragon/compiler/compiler.gd.uid +1 -0
- package/addons/estragon/compiler/composition.gd +51 -0
- package/addons/estragon/compiler/composition.gd.uid +1 -0
- package/addons/estragon/compiler/diagnostic_capture.gd.txt +18 -0
- package/addons/estragon/compiler/emitter.gd +209 -0
- package/addons/estragon/compiler/emitter.gd.uid +1 -0
- package/addons/estragon/compiler/frontend.gd +22 -0
- package/addons/estragon/compiler/frontend.gd.uid +1 -0
- package/addons/estragon/compiler/lexer.gd +78 -0
- package/addons/estragon/compiler/lexer.gd.uid +1 -0
- package/addons/estragon/compiler/model.gd +87 -0
- package/addons/estragon/compiler/model.gd.uid +1 -0
- package/addons/estragon/compiler/native/resolver.gd +200 -0
- package/addons/estragon/compiler/native/resolver.gd.uid +1 -0
- package/addons/estragon/compiler/parser.gd +252 -0
- package/addons/estragon/compiler/parser.gd.uid +1 -0
- package/addons/estragon/compiler/script_validation.gd +80 -0
- package/addons/estragon/compiler/script_validation.gd.uid +1 -0
- package/addons/estragon/compiler/source.gd +65 -0
- package/addons/estragon/compiler/source.gd.uid +1 -0
- package/addons/estragon/compiler/source_map.gd +59 -0
- package/addons/estragon/compiler/source_map.gd.uid +1 -0
- package/addons/estragon/compiler/validator.gd +277 -0
- package/addons/estragon/compiler/validator.gd.uid +1 -0
- package/addons/estragon/editor/buffer_guard.gd +84 -0
- package/addons/estragon/editor/buffer_guard.gd.uid +1 -0
- package/addons/estragon/editor/build_service.gd +252 -0
- package/addons/estragon/editor/build_service.gd.uid +1 -0
- package/addons/estragon/editor/highlighter.gd +116 -0
- package/addons/estragon/editor/highlighter.gd.uid +1 -0
- package/addons/estragon/editor/highlighter_association.gd +80 -0
- package/addons/estragon/editor/highlighter_association.gd.uid +1 -0
- package/addons/estragon/editor/plugin.cfg +7 -0
- package/addons/estragon/editor/plugin.gd +225 -0
- package/addons/estragon/editor/plugin.gd.uid +1 -0
- package/addons/estragon/editor/script_dependencies.gd +45 -0
- package/addons/estragon/editor/script_dependencies.gd.uid +1 -0
- package/addons/estragon/runtime/composition/binding.gd +62 -0
- package/addons/estragon/runtime/composition/binding.gd.uid +1 -0
- package/addons/estragon/runtime/composition/computation.gd +477 -0
- package/addons/estragon/runtime/composition/computation.gd.uid +1 -0
- package/addons/estragon/runtime/composition/element.gd +23 -0
- package/addons/estragon/runtime/composition/element.gd.uid +1 -0
- package/addons/estragon/runtime/composition/profile.gd +21 -0
- package/addons/estragon/runtime/composition/profile.gd.uid +1 -0
- package/addons/estragon/runtime/composition/profile.tres +6 -0
- package/addons/estragon/runtime/composition/state.gd +65 -0
- package/addons/estragon/runtime/composition/state.gd.uid +1 -0
- package/addons/estragon/runtime/composition/state.tres +6 -0
- package/addons/estragon/runtime/node/button.gd +53 -0
- package/addons/estragon/runtime/node/button.gd.uid +1 -0
- package/addons/estragon/runtime/node/component_2d.gd +105 -0
- package/addons/estragon/runtime/node/component_2d.gd.uid +1 -0
- package/addons/estragon/runtime/node/component_3d.gd +91 -0
- package/addons/estragon/runtime/node/component_3d.gd.uid +1 -0
- package/addons/estragon/runtime/node/estragon_2d.gd +32 -0
- package/addons/estragon/runtime/node/estragon_2d.gd.uid +1 -0
- package/addons/estragon/runtime/node/estragon_3d.gd +15 -0
- package/addons/estragon/runtime/node/estragon_3d.gd.uid +1 -0
- package/addons/estragon/runtime/node/hbox.gd +6 -0
- package/addons/estragon/runtime/node/hbox.gd.uid +1 -0
- package/addons/estragon/runtime/node/internal/coordinator.gd +145 -0
- package/addons/estragon/runtime/node/internal/coordinator.gd.uid +1 -0
- package/addons/estragon/runtime/node/internal/layout_core.gd +115 -0
- package/addons/estragon/runtime/node/internal/layout_core.gd.uid +1 -0
- package/addons/estragon/runtime/node/internal/native_bounds.gd +103 -0
- package/addons/estragon/runtime/node/internal/native_bounds.gd.uid +1 -0
- package/addons/estragon/runtime/node/internal/pointer_2d.gd +70 -0
- package/addons/estragon/runtime/node/internal/pointer_2d.gd.uid +1 -0
- package/addons/estragon/runtime/node/native_2d.gd +40 -0
- package/addons/estragon/runtime/node/native_2d.gd.uid +1 -0
- package/addons/estragon/runtime/node/native_3d.gd +40 -0
- package/addons/estragon/runtime/node/native_3d.gd.uid +1 -0
- package/addons/estragon/runtime/node/rect.gd +15 -0
- package/addons/estragon/runtime/node/rect.gd.uid +1 -0
- package/addons/estragon/runtime/node/text.gd +66 -0
- package/addons/estragon/runtime/node/text.gd.uid +1 -0
- package/addons/estragon/runtime/node/toggle.gd +23 -0
- package/addons/estragon/runtime/node/toggle.gd.uid +1 -0
- package/addons/estragon/runtime/node/vbox.gd +6 -0
- package/addons/estragon/runtime/node/vbox.gd.uid +1 -0
- package/package.json +31 -4
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
@tool
|
|
2
|
+
extends RefCounted
|
|
3
|
+
## Re-entrant token scanner: caller supplies the offset and lexical context.
|
|
4
|
+
## Regexes classify Unicode characters only; no source rewriting/evaluation.
|
|
5
|
+
const Model = preload("res://addons/estragon/compiler/model.gd")
|
|
6
|
+
const Source = preload("res://addons/estragon/compiler/source.gd")
|
|
7
|
+
const OPERATORS := ["**=", "<<=", ">>=", ":=", "==", "!=", "<=", ">=", "<<", ">>", "**", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "&&", "||", "->"]
|
|
8
|
+
var file: Source
|
|
9
|
+
var tokens: Array[Model.Token] = []
|
|
10
|
+
var _start := RegEx.new()
|
|
11
|
+
var _part := RegEx.new()
|
|
12
|
+
|
|
13
|
+
func _init(source: Source) -> void:
|
|
14
|
+
file = source
|
|
15
|
+
_start.compile("^[\\p{L}_]$")
|
|
16
|
+
_part.compile("^[\\p{L}\\p{N}\\p{M}_]$")
|
|
17
|
+
|
|
18
|
+
func identifier_start(character: String) -> bool:
|
|
19
|
+
return not character.is_empty() and _start.search(character) != null
|
|
20
|
+
|
|
21
|
+
func identifier_part(character: String) -> bool:
|
|
22
|
+
return not character.is_empty() and _part.search(character) != null
|
|
23
|
+
|
|
24
|
+
func token(kind: String, start: int, end: int) -> Model.Token:
|
|
25
|
+
var result := Model.Token.new()
|
|
26
|
+
result.kind = kind
|
|
27
|
+
result.text = file.text.substr(start, end - start)
|
|
28
|
+
result.span = file.span(start, end)
|
|
29
|
+
tokens.append(result)
|
|
30
|
+
return result
|
|
31
|
+
|
|
32
|
+
func char_at(index: int) -> String:
|
|
33
|
+
return file.text[index] if index < file.text.length() else ""
|
|
34
|
+
|
|
35
|
+
func next_token(start: int) -> Model.Token:
|
|
36
|
+
var text := file.text
|
|
37
|
+
var character := char_at(start)
|
|
38
|
+
var end := start + 1
|
|
39
|
+
if character.is_empty(): return token("eof", start, start)
|
|
40
|
+
if character in [" ", "\t"]:
|
|
41
|
+
while char_at(end) in [" ", "\t"]: end += 1
|
|
42
|
+
return token("whitespace", start, end)
|
|
43
|
+
if character in ["\n", "\r"]:
|
|
44
|
+
if character == "\r" and char_at(end) == "\n": end += 1
|
|
45
|
+
return token("newline", start, end)
|
|
46
|
+
if character == "#":
|
|
47
|
+
while not char_at(end) in ["", "\r", "\n"]: end += 1
|
|
48
|
+
return token("comment", start, end)
|
|
49
|
+
if character == "\\" and char_at(end) in ["\n", "\r"]:
|
|
50
|
+
end += 2 if char_at(end) == "\r" and char_at(end + 1) == "\n" else 1
|
|
51
|
+
return token("continuation", start, end)
|
|
52
|
+
if character in ['"', "'"]:
|
|
53
|
+
var delimiter := character.repeat(3) if text.substr(start, 3) == character.repeat(3) else character
|
|
54
|
+
end = start + delimiter.length()
|
|
55
|
+
while end < text.length():
|
|
56
|
+
if text.substr(end, delimiter.length()) == delimiter:
|
|
57
|
+
return token("string", start, end + delimiter.length())
|
|
58
|
+
if text[end] == "\\":
|
|
59
|
+
end += 1
|
|
60
|
+
if char_at(end) == "\r" and char_at(end + 1) == "\n": end += 1
|
|
61
|
+
end += 1
|
|
62
|
+
else:
|
|
63
|
+
if delimiter.length() == 1 and text[end] in ["\n", "\r"]:
|
|
64
|
+
file.fail("Unterminated GDScript string", start, end)
|
|
65
|
+
return null
|
|
66
|
+
end += 1
|
|
67
|
+
file.fail("Unterminated GDScript string", start, end)
|
|
68
|
+
return null
|
|
69
|
+
if identifier_start(character):
|
|
70
|
+
while identifier_part(char_at(end)): end += 1
|
|
71
|
+
return token("identifier", start, end)
|
|
72
|
+
if character in "0123456789":
|
|
73
|
+
while not char_at(end).is_empty() and char_at(end) in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.": end += 1
|
|
74
|
+
return token("number", start, end)
|
|
75
|
+
for operator in OPERATORS:
|
|
76
|
+
if text.substr(start, operator.length()) == operator:
|
|
77
|
+
return token("operator", start, start + operator.length())
|
|
78
|
+
return token("delimiter" if character in "()[]{}" else "operator", start, end)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uid://d1756nahrwmy4
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
@tool
|
|
2
|
+
extends RefCounted
|
|
3
|
+
## Native frontend data only. No Nodes, runtime components or editor services.
|
|
4
|
+
|
|
5
|
+
class Position extends RefCounted:
|
|
6
|
+
var index: int # Godot Unicode scalar index for slicing.
|
|
7
|
+
var offset: int # UTF-16 offset for cross-frontend diagnostics/maps.
|
|
8
|
+
var line: int
|
|
9
|
+
var column: int
|
|
10
|
+
|
|
11
|
+
class Span extends RefCounted:
|
|
12
|
+
var start: Position
|
|
13
|
+
var end: Position
|
|
14
|
+
|
|
15
|
+
class Diagnostic extends RefCounted:
|
|
16
|
+
var source_name: String
|
|
17
|
+
var span: Span
|
|
18
|
+
var message: String
|
|
19
|
+
|
|
20
|
+
func format() -> String:
|
|
21
|
+
return "%s:%d:%d: %s" % [source_name, span.start.line, span.start.column, message]
|
|
22
|
+
|
|
23
|
+
class Token extends RefCounted:
|
|
24
|
+
var kind: String
|
|
25
|
+
var text: String
|
|
26
|
+
var span: Span
|
|
27
|
+
|
|
28
|
+
class SyntaxNode extends RefCounted:
|
|
29
|
+
var kind: String
|
|
30
|
+
var span: Span
|
|
31
|
+
|
|
32
|
+
class Code extends SyntaxNode:
|
|
33
|
+
func _init() -> void:
|
|
34
|
+
kind = "gdscript-source"
|
|
35
|
+
|
|
36
|
+
class Bindable extends SyntaxNode:
|
|
37
|
+
var name: String
|
|
38
|
+
var name_span: Span
|
|
39
|
+
var type_span: Span
|
|
40
|
+
var type_name: String
|
|
41
|
+
var signal_name: String
|
|
42
|
+
var annotations_span: Span # Native property annotations, never attached to generated signal.
|
|
43
|
+
var declaration_span: Span
|
|
44
|
+
var comment_span: Span
|
|
45
|
+
func _init() -> void:
|
|
46
|
+
kind = "bindable-property"
|
|
47
|
+
|
|
48
|
+
class GdExpression extends SyntaxNode:
|
|
49
|
+
var parts: Array[SyntaxNode] = []
|
|
50
|
+
func _init() -> void:
|
|
51
|
+
kind = "gdscript-expression"
|
|
52
|
+
|
|
53
|
+
class Literal extends SyntaxNode:
|
|
54
|
+
var value: Variant
|
|
55
|
+
var type_name: String
|
|
56
|
+
func _init() -> void:
|
|
57
|
+
kind = "raw-literal"
|
|
58
|
+
|
|
59
|
+
class Attribute extends RefCounted:
|
|
60
|
+
var binding_path: Array = []
|
|
61
|
+
var kind: String = "property"
|
|
62
|
+
var name: String
|
|
63
|
+
var name_span: Span
|
|
64
|
+
var value: SyntaxNode
|
|
65
|
+
var type_name: String
|
|
66
|
+
|
|
67
|
+
class Component extends SyntaxNode:
|
|
68
|
+
var name: String
|
|
69
|
+
var name_span: Span
|
|
70
|
+
var runtime_class: String
|
|
71
|
+
var attributes: Array[Attribute] = []
|
|
72
|
+
var children: Array[SyntaxNode] = []
|
|
73
|
+
func _init() -> void:
|
|
74
|
+
kind = "component-expression"
|
|
75
|
+
|
|
76
|
+
class Module extends RefCounted:
|
|
77
|
+
var composition: bool = false
|
|
78
|
+
var parts: Array[SyntaxNode] = []
|
|
79
|
+
var identifiers: PackedStringArray = []
|
|
80
|
+
var indentation: String = "\t"
|
|
81
|
+
|
|
82
|
+
class Result extends RefCounted:
|
|
83
|
+
var module: Module
|
|
84
|
+
var tokens: Array[Token] = []
|
|
85
|
+
var diagnostics: Array[Diagnostic] = []
|
|
86
|
+
var ok: bool:
|
|
87
|
+
get: return module != null and diagnostics.is_empty()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uid://cq4tdeer2kq7i
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
@tool
|
|
2
|
+
extends RefCounted
|
|
3
|
+
## Static Node type metadata. Never instantiate authored classes or use editor/tree.
|
|
4
|
+
const Model = preload("res://addons/estragon/compiler/model.gd")
|
|
5
|
+
const Lexer = preload("res://addons/estragon/compiler/lexer.gd")
|
|
6
|
+
const Source = preload("res://addons/estragon/compiler/source.gd")
|
|
7
|
+
const Parser = preload("res://addons/estragon/compiler/parser.gd")
|
|
8
|
+
const Bindable = preload("res://addons/estragon/compiler/bindable.gd")
|
|
9
|
+
var visiting: Dictionary = {}
|
|
10
|
+
var file
|
|
11
|
+
var aliases: Dictionary = {}
|
|
12
|
+
var globals: Dictionary = {}
|
|
13
|
+
|
|
14
|
+
func _init(source) -> void:
|
|
15
|
+
file = source
|
|
16
|
+
for item in ProjectSettings.get_global_class_list(): globals[str(item["class"])] = str(item.path)
|
|
17
|
+
|
|
18
|
+
func scan(module: Model.Module) -> void:
|
|
19
|
+
# Only top-level lexical declarations in authored Code ranges are imports.
|
|
20
|
+
# No evaluation or regex parsing of mixed source.
|
|
21
|
+
for part in module.parts:
|
|
22
|
+
if not part is Model.Code: continue
|
|
23
|
+
var lexer := Lexer.new(file)
|
|
24
|
+
var tokens: Array = []
|
|
25
|
+
var cursor: int = part.span.start.index
|
|
26
|
+
while cursor < part.span.end.index:
|
|
27
|
+
var token = lexer.next_token(cursor)
|
|
28
|
+
if token == null: return
|
|
29
|
+
cursor = token.span.end.index
|
|
30
|
+
if token.kind not in ["whitespace", "comment", "newline", "continuation"]: tokens.append(token)
|
|
31
|
+
for index in range(tokens.size() - 5):
|
|
32
|
+
var token = tokens[index]
|
|
33
|
+
if token.text != "const" or file.text.substr(file.line_start(token.span.start.line), token.span.start.index - file.line_start(token.span.start.line)) != "": continue
|
|
34
|
+
var name: String = tokens[index + 1].text
|
|
35
|
+
var next: int = index + 2
|
|
36
|
+
# Optional explicit Script/GDScript annotation.
|
|
37
|
+
if tokens[next].text == ":": next += 2
|
|
38
|
+
if next + 4 >= tokens.size() or tokens[next].text != "=" or tokens[next + 1].text != "preload" or tokens[next + 2].text != "(" or tokens[next + 3].kind != "string" or tokens[next + 4].text != ")": continue
|
|
39
|
+
if aliases.has(name):
|
|
40
|
+
file.fail("Ambiguous duplicate preload alias " + name, tokens[index + 1].span.start.index)
|
|
41
|
+
return
|
|
42
|
+
var raw: String = tokens[next + 3].text
|
|
43
|
+
# Godot decodes only a lexer-proven string literal, never application code.
|
|
44
|
+
var literal := GDScript.new()
|
|
45
|
+
literal.source_code = "@tool\nextends RefCounted\nstatic func value() -> String:\n\treturn " + raw + "\n"
|
|
46
|
+
if literal.reload() != OK:
|
|
47
|
+
file.fail("Invalid preload path literal", tokens[next + 3].span.start.index)
|
|
48
|
+
return
|
|
49
|
+
var path: String = literal.call("value")
|
|
50
|
+
if not path.is_absolute_path():
|
|
51
|
+
var output: String = file.name.get_base_dir().path_join("generated").path_join(file.name.get_file().get_basename() + ".gd")
|
|
52
|
+
path = output.get_base_dir().path_join(path).simplify_path()
|
|
53
|
+
if not path.begins_with("res://"): path = "res://" + path
|
|
54
|
+
aliases[name] = path
|
|
55
|
+
|
|
56
|
+
func resolve(name: String, span: Model.Span) -> Dictionary:
|
|
57
|
+
if aliases.has(name):
|
|
58
|
+
if ClassDB.class_exists(name) or globals.has(name) and globals[name] != aliases[name]:
|
|
59
|
+
return _fail("Ambiguous GGX preload alias " + name, span)
|
|
60
|
+
return _script(name, aliases[name], span)
|
|
61
|
+
if globals.has(name): return _script(name, globals[name], span)
|
|
62
|
+
if not ClassDB.class_exists(name): return _fail("Unknown Node GGX component %s; use a native class or explicit preload alias" % JSON.stringify(name), span)
|
|
63
|
+
if not ClassDB.is_parent_class(name, "Node") or not ClassDB.can_instantiate(name):
|
|
64
|
+
return _fail("Unknown or non-constructible Node GGX type " + name, span)
|
|
65
|
+
return {"native": name, "script": null}
|
|
66
|
+
|
|
67
|
+
func _script(name: String, path: String, span: Model.Span) -> Dictionary:
|
|
68
|
+
var authored := path.get_base_dir().get_base_dir().path_join(path.get_file().get_basename()+".gogo")
|
|
69
|
+
if path.get_base_dir().get_file()=="generated" and FileAccess.file_exists(authored): return _gogo(name,path,authored,span)
|
|
70
|
+
if not ResourceLoader.exists(path, "GDScript"): return _fail("GGX script import not found: " + path, span)
|
|
71
|
+
var script: Script = load(path)
|
|
72
|
+
# CACHE_MODE_IGNORE still changes GDScript's internal cache on tested Godot.
|
|
73
|
+
# Never mutate an application's live Script during metadata analysis.
|
|
74
|
+
if script is GDScript and script.source_code.replace("\r\n","\n") != FileAccess.get_file_as_string(path).replace("\r\n","\n"):
|
|
75
|
+
return _fail("Imported Script has unsynchronized disk/cache source; reload it in Godot before compiling GGX: " + path, span)
|
|
76
|
+
if not script is GDScript or not script.can_instantiate() or not ClassDB.is_parent_class(script.get_instance_base_type(), "Node"):
|
|
77
|
+
return _fail("GGX hierarchy script must be a constructible Node: " + name, span)
|
|
78
|
+
for method in script.get_script_method_list():
|
|
79
|
+
if method.name == "_init" and method.args.size() > method.default_args.size():
|
|
80
|
+
return _fail("GGX construction requires a zero-argument initializer: " + name, span)
|
|
81
|
+
return {"native": script.get_instance_base_type(), "script": script}
|
|
82
|
+
|
|
83
|
+
func _gogo(name: String, path: String, authored: String, span: Model.Span) -> Dictionary:
|
|
84
|
+
if visiting.has(path): return _fail("Cyclic Gogo component inheritance: "+path,span)
|
|
85
|
+
visiting[path] = true
|
|
86
|
+
var source := Source.new(FileAccess.get_file_as_string(authored),authored)
|
|
87
|
+
var module = Parser.new(source).parse()
|
|
88
|
+
if module != null: Bindable.new(source).apply(module)
|
|
89
|
+
if module == null or not source.diagnostics.is_empty():
|
|
90
|
+
visiting.erase(path)
|
|
91
|
+
return _fail("Invalid authored Gogo component metadata: "+authored,span)
|
|
92
|
+
var imported = get_script().new(source)
|
|
93
|
+
imported.visiting = visiting
|
|
94
|
+
imported.scan(module)
|
|
95
|
+
var lexer := Lexer.new(source)
|
|
96
|
+
var tokens: Array = []
|
|
97
|
+
var cursor := 0
|
|
98
|
+
while cursor < source.text.length():
|
|
99
|
+
var token = lexer.next_token(cursor)
|
|
100
|
+
if token == null: break
|
|
101
|
+
cursor = token.span.end.index
|
|
102
|
+
if token.kind not in ["whitespace","comment","newline","continuation"]: tokens.append(token)
|
|
103
|
+
var base := {}
|
|
104
|
+
for index in range(tokens.size()-1):
|
|
105
|
+
if tokens[index].text!="extends" or tokens[index].span.start.column!=1: continue
|
|
106
|
+
var target = tokens[index+1]
|
|
107
|
+
if target.kind=="string":
|
|
108
|
+
var literal: String = target.text.substr(1,target.text.length()-2)
|
|
109
|
+
if not literal.is_absolute_path(): literal = path.get_base_dir().path_join(literal).simplify_path()
|
|
110
|
+
base = imported._script(name,literal,span)
|
|
111
|
+
else: base = imported.resolve(target.text,span)
|
|
112
|
+
break
|
|
113
|
+
visiting.erase(path)
|
|
114
|
+
if base.is_empty(): return _fail("Gogo component requires an explicit constructible Node base: "+authored,span)
|
|
115
|
+
var properties: Dictionary = base.get("properties",{}).duplicate()
|
|
116
|
+
var signals: Dictionary = base.get("signals",{}).duplicate()
|
|
117
|
+
var declarations: Array = []
|
|
118
|
+
for index in range(tokens.size()-3):
|
|
119
|
+
var token = tokens[index]
|
|
120
|
+
if token.text=="func" and token.span.start.column==1 and tokens[index+1].text=="_init":
|
|
121
|
+
var depth := 1
|
|
122
|
+
var parameter := false
|
|
123
|
+
var optional := false
|
|
124
|
+
for item in tokens.slice(index+3):
|
|
125
|
+
if item.text in ["(","[","{"]: depth += 1
|
|
126
|
+
elif item.text in [")","]","}"]:
|
|
127
|
+
depth -= 1
|
|
128
|
+
if depth==0:
|
|
129
|
+
if parameter and not optional: return _fail("GGX construction requires a zero-argument initializer: "+name,span)
|
|
130
|
+
break
|
|
131
|
+
elif depth==1:
|
|
132
|
+
if item.text=="=": optional = true
|
|
133
|
+
elif item.text==",":
|
|
134
|
+
if parameter and not optional: return _fail("GGX construction requires a zero-argument initializer: "+name,span)
|
|
135
|
+
parameter = false
|
|
136
|
+
optional = false
|
|
137
|
+
else: parameter = true
|
|
138
|
+
if token.text=="signal" and token.span.start.column==1: signals[tokens[index+1].text] = {"name":tokens[index+1].text}
|
|
139
|
+
if token.text!="var" or token.span.start.column!=1 or tokens[index+2].text!=":": continue
|
|
140
|
+
var end: int = index+3
|
|
141
|
+
while end+1<tokens.size() and tokens[end+1].span.start.line==token.span.start.line and tokens[end+1].text not in ["=",":"]: end += 1
|
|
142
|
+
declarations.append({"name":tokens[index+1].text,"type_name":source.text.substr(tokens[index+3].span.start.index,tokens[end].span.end.index-tokens[index+3].span.start.index),"signal_name":""})
|
|
143
|
+
for part in module.parts:
|
|
144
|
+
if part is Model.Bindable: declarations.append(part)
|
|
145
|
+
for part in declarations:
|
|
146
|
+
var type: String = part.type_name
|
|
147
|
+
var kind: int = {"bool":TYPE_BOOL,"int":TYPE_INT,"float":TYPE_FLOAT,"String":TYPE_STRING,"StringName":TYPE_STRING_NAME,"Vector2":TYPE_VECTOR2,"Vector2i":TYPE_VECTOR2I,"Rect2":TYPE_RECT2,"Rect2i":TYPE_RECT2I,"Vector3":TYPE_VECTOR3,"Vector3i":TYPE_VECTOR3I,"Transform2D":TYPE_TRANSFORM2D,"Vector4":TYPE_VECTOR4,"Vector4i":TYPE_VECTOR4I,"Plane":TYPE_PLANE,"Quaternion":TYPE_QUATERNION,"AABB":TYPE_AABB,"Basis":TYPE_BASIS,"Transform3D":TYPE_TRANSFORM3D,"Projection":TYPE_PROJECTION,"Color":TYPE_COLOR,"NodePath":TYPE_NODE_PATH,"RID":TYPE_RID,"Callable":TYPE_CALLABLE,"Signal":TYPE_SIGNAL,"Dictionary":TYPE_DICTIONARY,"Array":TYPE_ARRAY,"PackedByteArray":TYPE_PACKED_BYTE_ARRAY,"PackedInt32Array":TYPE_PACKED_INT32_ARRAY,"PackedInt64Array":TYPE_PACKED_INT64_ARRAY,"PackedFloat32Array":TYPE_PACKED_FLOAT32_ARRAY,"PackedFloat64Array":TYPE_PACKED_FLOAT64_ARRAY,"PackedStringArray":TYPE_PACKED_STRING_ARRAY,"PackedVector2Array":TYPE_PACKED_VECTOR2_ARRAY,"PackedVector3Array":TYPE_PACKED_VECTOR3_ARRAY,"PackedColorArray":TYPE_PACKED_COLOR_ARRAY}.get(type,TYPE_OBJECT)
|
|
148
|
+
if type.begins_with("Array["): kind = TYPE_ARRAY
|
|
149
|
+
properties[part.name] = {"name":part.name,"type":kind,"class_name":(type if ClassDB.class_exists(type) else name+"."+type) if kind==TYPE_OBJECT else "","usage":PROPERTY_USAGE_DEFAULT}
|
|
150
|
+
if not part.signal_name.is_empty(): signals[part.signal_name] = {"name":part.signal_name}
|
|
151
|
+
return {"native":base.native,"script":base.script,"properties":properties,"signals":signals}
|
|
152
|
+
|
|
153
|
+
func _fail(message: String, span: Model.Span) -> Dictionary:
|
|
154
|
+
file.fail(message, span.start.index, span.end.index)
|
|
155
|
+
return {}
|
|
156
|
+
|
|
157
|
+
# Source-only proof for a writable Gogo path. Native fields never gain an
|
|
158
|
+
# observation contract merely because an unrelated signal shares their name.
|
|
159
|
+
func bindable_path(module, names: Array) -> bool:
|
|
160
|
+
if names.is_empty(): return false
|
|
161
|
+
for part in module.parts:
|
|
162
|
+
if not part is Model.Bindable or part.name!=names[0]: continue
|
|
163
|
+
if names.size()==1: return part.type_name=="bool"
|
|
164
|
+
var path: String = aliases.get(part.type_name,globals.get(part.type_name,""))
|
|
165
|
+
if path.is_empty(): return false
|
|
166
|
+
var authored := path.get_base_dir().get_base_dir().path_join(path.get_file().get_basename()+".gogo")
|
|
167
|
+
if path.get_base_dir().get_file()!="generated" or not FileAccess.file_exists(authored): return false
|
|
168
|
+
var source := Source.new(FileAccess.get_file_as_string(authored),authored)
|
|
169
|
+
var parsed = Parser.new(source).parse()
|
|
170
|
+
if parsed==null: return false
|
|
171
|
+
Bindable.new(source).apply(parsed)
|
|
172
|
+
if not source.diagnostics.is_empty(): return false
|
|
173
|
+
var imported = get_script().new(source)
|
|
174
|
+
imported.scan(parsed)
|
|
175
|
+
return imported.bindable_path(parsed,names.slice(1))
|
|
176
|
+
return false
|
|
177
|
+
|
|
178
|
+
# Godot 4.0 reports TYPE_NIL even for typed GDScript signal arguments.
|
|
179
|
+
# Recover only this lexical declaration contract; native reload remains the
|
|
180
|
+
# semantic authority. Never instantiate a control to inspect its signals.
|
|
181
|
+
func signal_contract(script: Script, event: Dictionary) -> Dictionary:
|
|
182
|
+
var args: Array = event.get("args",[])
|
|
183
|
+
if args.size()!=1 or args[0].type!=TYPE_NIL or not script is GDScript: return event
|
|
184
|
+
var source := Source.new(script.source_code,script.resource_path)
|
|
185
|
+
var lexer := Lexer.new(source)
|
|
186
|
+
var tokens: Array = []
|
|
187
|
+
var cursor := 0
|
|
188
|
+
while cursor<source.text.length():
|
|
189
|
+
var token = lexer.next_token(cursor)
|
|
190
|
+
if token==null: return event
|
|
191
|
+
cursor = token.span.end.index
|
|
192
|
+
if token.kind not in ["whitespace","newline","comment","continuation"]: tokens.append(token)
|
|
193
|
+
for index in range(tokens.size()-6):
|
|
194
|
+
if tokens[index].text!="signal" or tokens[index].span.start.column!=1 or tokens[index+1].text!=event.name: continue
|
|
195
|
+
if tokens[index+2].text!="(" or tokens[index+3].kind!="identifier" or tokens[index+4].text!=":" or tokens[index+6].text!=")": return event
|
|
196
|
+
var kind: int = {"bool":TYPE_BOOL,"int":TYPE_INT,"float":TYPE_FLOAT,"String":TYPE_STRING}.get(tokens[index+5].text,TYPE_NIL)
|
|
197
|
+
var result := event.duplicate(true)
|
|
198
|
+
result.args[0].type = kind
|
|
199
|
+
return result
|
|
200
|
+
return event
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uid://dsn131vya6axb
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
@tool
|
|
2
|
+
extends RefCounted
|
|
3
|
+
## Recursive-descent component islands composed with lexically retained GDScript.
|
|
4
|
+
const Model = preload("res://addons/estragon/compiler/model.gd")
|
|
5
|
+
const Source = preload("res://addons/estragon/compiler/source.gd")
|
|
6
|
+
const Lexer = preload("res://addons/estragon/compiler/lexer.gd")
|
|
7
|
+
const OPERANDS := ["return", "yield", "await", "if", "elif", "else", "while", "in", "and", "or", "not", "assert", "match"]
|
|
8
|
+
const OPEN := {"(": ")", "[": "]", "{": "}"}
|
|
9
|
+
var file: Source
|
|
10
|
+
var lexer: Lexer
|
|
11
|
+
var module := Model.Module.new()
|
|
12
|
+
var _cursor: int = 0
|
|
13
|
+
var _classes: Array[Dictionary] = []
|
|
14
|
+
var _indentation_seen: bool = false
|
|
15
|
+
var _island_depth: int = 0
|
|
16
|
+
|
|
17
|
+
func _init(source: Source) -> void:
|
|
18
|
+
file = source
|
|
19
|
+
lexer = Lexer.new(source)
|
|
20
|
+
|
|
21
|
+
func parse() -> Model.Module:
|
|
22
|
+
var first: int = 0
|
|
23
|
+
const TRIM := "\u0009\u000a\u000b\u000c\u000d \u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff"
|
|
24
|
+
while first < file.text.length() and file.text[first] in TRIM: first += 1
|
|
25
|
+
if first < file.text.length() and file.text[first] == "<":
|
|
26
|
+
file.fail("Gogo requires a GDScript-first module",first)
|
|
27
|
+
return null
|
|
28
|
+
var scanned := _scan(0, false)
|
|
29
|
+
module.parts.assign(scanned.parts)
|
|
30
|
+
return module if file.diagnostics.is_empty() else null
|
|
31
|
+
|
|
32
|
+
func _at(value: String) -> bool:
|
|
33
|
+
return file.text.substr(_cursor, value.length()) == value
|
|
34
|
+
|
|
35
|
+
func _take(value: String) -> bool:
|
|
36
|
+
if not _at(value):
|
|
37
|
+
file.fail("Expected " + JSON.stringify(value), _cursor)
|
|
38
|
+
return false
|
|
39
|
+
lexer.token("markup-punctuation", _cursor, _cursor + value.length())
|
|
40
|
+
_cursor += value.length()
|
|
41
|
+
return true
|
|
42
|
+
|
|
43
|
+
func _whitespace() -> bool:
|
|
44
|
+
var start := _cursor
|
|
45
|
+
while lexer.char_at(_cursor) in [" ", "\t", "\r", "\n"]: _cursor += 1
|
|
46
|
+
if _cursor > start: lexer.token("whitespace", start, _cursor)
|
|
47
|
+
return _cursor > start
|
|
48
|
+
|
|
49
|
+
func _trivia() -> void:
|
|
50
|
+
_whitespace()
|
|
51
|
+
while _at("<!--") and file.diagnostics.is_empty():
|
|
52
|
+
var start := _cursor
|
|
53
|
+
var end := file.text.find("-->", start + 4)
|
|
54
|
+
if end < 0:
|
|
55
|
+
file.fail("Unterminated comment", start, file.text.length())
|
|
56
|
+
return
|
|
57
|
+
_cursor = end + 3
|
|
58
|
+
lexer.token("markup-comment", start, _cursor)
|
|
59
|
+
_whitespace()
|
|
60
|
+
|
|
61
|
+
func _name() -> Model.Token:
|
|
62
|
+
var start := _cursor
|
|
63
|
+
if lexer.char_at(_cursor).is_empty() or not lexer.char_at(_cursor) in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_":
|
|
64
|
+
file.fail("Expected component or property name", start)
|
|
65
|
+
return null
|
|
66
|
+
_cursor += 1
|
|
67
|
+
while not lexer.char_at(_cursor).is_empty() and lexer.char_at(_cursor) in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789.:-": _cursor += 1
|
|
68
|
+
return lexer.token("markup-name", start, _cursor)
|
|
69
|
+
|
|
70
|
+
func _component(depth: int) -> Model.Component:
|
|
71
|
+
_trivia()
|
|
72
|
+
var start := _cursor
|
|
73
|
+
if depth >= 256:
|
|
74
|
+
file.fail("Component nesting exceeds 256", start)
|
|
75
|
+
return null
|
|
76
|
+
if not _take("<"): return null
|
|
77
|
+
var name_token := _name()
|
|
78
|
+
if name_token == null: return null
|
|
79
|
+
var node := Model.Component.new()
|
|
80
|
+
node.name = name_token.text
|
|
81
|
+
node.name_span = name_token.span
|
|
82
|
+
while file.diagnostics.is_empty():
|
|
83
|
+
var separated := _whitespace()
|
|
84
|
+
if _at("/>"):
|
|
85
|
+
_take("/>")
|
|
86
|
+
node.span = file.span(start, _cursor)
|
|
87
|
+
return node
|
|
88
|
+
if _at(">"):
|
|
89
|
+
_take(">")
|
|
90
|
+
break
|
|
91
|
+
if not separated:
|
|
92
|
+
file.fail("Expected whitespace before a property, or a tag terminator", _cursor)
|
|
93
|
+
return null
|
|
94
|
+
var attribute_name := _name()
|
|
95
|
+
if attribute_name == null: return null
|
|
96
|
+
_whitespace()
|
|
97
|
+
if not _take("="): return null
|
|
98
|
+
_whitespace()
|
|
99
|
+
var attribute := Model.Attribute.new()
|
|
100
|
+
attribute.name = attribute_name.text
|
|
101
|
+
attribute.name_span = attribute_name.span
|
|
102
|
+
if _at("{"):
|
|
103
|
+
attribute.value = _expression()
|
|
104
|
+
else:
|
|
105
|
+
var quote := lexer.char_at(_cursor)
|
|
106
|
+
if quote not in ['"', "'"]:
|
|
107
|
+
file.fail("Expected a quoted string or {GDScript expression} property value", _cursor)
|
|
108
|
+
return null
|
|
109
|
+
var quote_start := _cursor
|
|
110
|
+
_cursor += 1
|
|
111
|
+
var value_start := _cursor
|
|
112
|
+
while lexer.char_at(_cursor) != quote:
|
|
113
|
+
if _cursor >= file.text.length():
|
|
114
|
+
file.fail("Unterminated property value", value_start, _cursor)
|
|
115
|
+
return null
|
|
116
|
+
if lexer.char_at(_cursor) in ["<", "&", "\\"]:
|
|
117
|
+
file.fail("Markup delimiters, entities and escapes are not supported in literal values", _cursor)
|
|
118
|
+
return null
|
|
119
|
+
_cursor += 1
|
|
120
|
+
var value := Model.Literal.new()
|
|
121
|
+
value.value = file.text.substr(value_start, _cursor - value_start)
|
|
122
|
+
value.span = file.span(value_start, _cursor)
|
|
123
|
+
attribute.value = value
|
|
124
|
+
_cursor += 1
|
|
125
|
+
lexer.token("markup-string", quote_start, _cursor)
|
|
126
|
+
if attribute.value == null: return null
|
|
127
|
+
node.attributes.append(attribute)
|
|
128
|
+
_trivia()
|
|
129
|
+
while not _at("</") and file.diagnostics.is_empty():
|
|
130
|
+
if _cursor >= file.text.length():
|
|
131
|
+
file.fail("Missing closing tag </%s>" % node.name, _cursor)
|
|
132
|
+
return null
|
|
133
|
+
var child: Model.SyntaxNode
|
|
134
|
+
if _at("{"): child = _expression()
|
|
135
|
+
elif _at("<"): child = _component(depth + 1)
|
|
136
|
+
else:
|
|
137
|
+
file.fail("GGX children must be Node elements", _cursor)
|
|
138
|
+
return null
|
|
139
|
+
if child == null: return null
|
|
140
|
+
node.children.append(child)
|
|
141
|
+
_trivia()
|
|
142
|
+
if not file.diagnostics.is_empty() or not _take("</"): return null
|
|
143
|
+
var closing := _name()
|
|
144
|
+
if closing == null: return null
|
|
145
|
+
if closing.text != node.name:
|
|
146
|
+
file.fail("Expected </%s>, found </%s>" % [node.name, closing.text], closing.span.start.index, _cursor)
|
|
147
|
+
return null
|
|
148
|
+
_whitespace()
|
|
149
|
+
if not _take(">"): return null
|
|
150
|
+
node.span = file.span(start, _cursor)
|
|
151
|
+
return node
|
|
152
|
+
|
|
153
|
+
func _expression() -> Model.GdExpression:
|
|
154
|
+
var start := _cursor
|
|
155
|
+
lexer.token("expression-open", start, start + 1)
|
|
156
|
+
var scanned := _scan(start + 1, true)
|
|
157
|
+
if not file.diagnostics.is_empty(): return null
|
|
158
|
+
if not scanned.has_token:
|
|
159
|
+
file.fail("Expected a GDScript expression inside braces", start, scanned.end)
|
|
160
|
+
return null
|
|
161
|
+
var expression := Model.GdExpression.new()
|
|
162
|
+
expression.parts.assign(scanned.parts)
|
|
163
|
+
_cursor = scanned.end + 1
|
|
164
|
+
lexer.token("expression-close", scanned.end, _cursor)
|
|
165
|
+
expression.span = file.span(start, _cursor)
|
|
166
|
+
return expression
|
|
167
|
+
|
|
168
|
+
func _copy(parts: Array[Model.SyntaxNode], start: int, end: int) -> void:
|
|
169
|
+
if start >= end: return
|
|
170
|
+
var code := Model.Code.new()
|
|
171
|
+
code.span = file.span(start, end)
|
|
172
|
+
parts.append(code)
|
|
173
|
+
|
|
174
|
+
func _scan(start: int, braced: bool) -> Dictionary:
|
|
175
|
+
var parts: Array[Model.SyntaxNode] = []
|
|
176
|
+
var stack: Array[Dictionary] = []
|
|
177
|
+
var offset := start
|
|
178
|
+
var copied := start
|
|
179
|
+
var operand := true
|
|
180
|
+
var has_token := false
|
|
181
|
+
while offset < file.text.length() and file.diagnostics.is_empty():
|
|
182
|
+
var character := lexer.char_at(offset)
|
|
183
|
+
if braced and stack.is_empty() and character == "}":
|
|
184
|
+
_copy(parts, copied, offset)
|
|
185
|
+
return {"parts": parts, "end": offset, "has_token": has_token}
|
|
186
|
+
if character == "<" and operand and lexer.identifier_start(lexer.char_at(offset + 1)):
|
|
187
|
+
_context(offset, braced, stack.is_empty())
|
|
188
|
+
if not braced and stack.is_empty() and file.position(offset).column == 1:
|
|
189
|
+
file.fail("Expected a GDScript expression context before component markup", offset)
|
|
190
|
+
break
|
|
191
|
+
if not _classes.is_empty():
|
|
192
|
+
file.fail("Component expressions inside named inner classes are outside the supported hybrid subset; use an outer-class helper", offset)
|
|
193
|
+
break
|
|
194
|
+
_copy(parts, copied, offset)
|
|
195
|
+
_cursor = offset
|
|
196
|
+
_island_depth += 1
|
|
197
|
+
if _island_depth > 256:
|
|
198
|
+
file.fail("Hybrid expression nesting exceeds 256", offset)
|
|
199
|
+
break
|
|
200
|
+
var component := _component(0)
|
|
201
|
+
_island_depth -= 1
|
|
202
|
+
if component == null: break
|
|
203
|
+
parts.append(component)
|
|
204
|
+
offset = _cursor
|
|
205
|
+
copied = offset
|
|
206
|
+
operand = false
|
|
207
|
+
has_token = true
|
|
208
|
+
continue
|
|
209
|
+
var token := lexer.next_token(offset)
|
|
210
|
+
if token == null: break
|
|
211
|
+
var from := offset
|
|
212
|
+
offset = token.span.end.index
|
|
213
|
+
if token.kind in ["whitespace", "comment", "continuation"]: continue
|
|
214
|
+
if token.kind == "newline":
|
|
215
|
+
if stack.is_empty() and not braced: operand = true
|
|
216
|
+
continue
|
|
217
|
+
has_token = true
|
|
218
|
+
var context := _context(from, braced, stack.is_empty())
|
|
219
|
+
var line: int = context.line
|
|
220
|
+
var prefix: String = context.prefix
|
|
221
|
+
var first_on_line: bool = context.first
|
|
222
|
+
if token.kind == "string": operand = false
|
|
223
|
+
elif token.kind == "identifier":
|
|
224
|
+
if not module.identifiers.has(token.text): module.identifiers.append(token.text)
|
|
225
|
+
if not braced and stack.is_empty() and first_on_line and token.text == "class": _classes.append({"indentation": prefix.length(), "line": line})
|
|
226
|
+
operand = token.text in OPERANDS
|
|
227
|
+
elif token.kind == "number": operand = false
|
|
228
|
+
elif OPEN.has(character):
|
|
229
|
+
stack.append({"close": OPEN[character], "start": from})
|
|
230
|
+
operand = true
|
|
231
|
+
elif character in [")", "]", "}"]:
|
|
232
|
+
if stack.is_empty() or stack.pop_back().close != character:
|
|
233
|
+
file.fail("Unmatched GDScript delimiter " + character, from)
|
|
234
|
+
break
|
|
235
|
+
operand = false
|
|
236
|
+
else: operand = character != "."
|
|
237
|
+
if file.diagnostics.is_empty():
|
|
238
|
+
if not stack.is_empty(): file.fail("Unclosed GDScript delimiter; expected " + stack[-1].close, stack[-1].start, offset)
|
|
239
|
+
elif braced: file.fail("Unterminated GDScript expression; expected }", start - 1, offset)
|
|
240
|
+
_copy(parts, copied, offset)
|
|
241
|
+
return {"parts": parts, "end": offset, "has_token": has_token}
|
|
242
|
+
|
|
243
|
+
func _context(offset: int, braced: bool, top: bool) -> Dictionary:
|
|
244
|
+
var line := file.position(offset).line
|
|
245
|
+
var prefix := file.text.substr(file.line_start(line), offset - file.line_start(line))
|
|
246
|
+
var first := prefix.replace(" ", "").replace("\t", "").is_empty()
|
|
247
|
+
if not braced and top and first:
|
|
248
|
+
while not _classes.is_empty() and line > _classes[-1].line and prefix.length() <= _classes[-1].indentation: _classes.pop_back()
|
|
249
|
+
if not prefix.is_empty() and not _indentation_seen:
|
|
250
|
+
module.indentation = prefix
|
|
251
|
+
_indentation_seen = true
|
|
252
|
+
return {"line": line, "prefix": prefix, "first": first}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uid://14gcnoq3q852
|