diff --git a/godot_parser/objects.py b/godot_parser/objects.py index ff614e1..2f32bee 100644 --- a/godot_parser/objects.py +++ b/godot_parser/objects.py @@ -55,7 +55,7 @@ def from_parser(cls: Type[GDObjectType], parse_result) -> GDObjectType: return factory(*parse_result[1:]) def __str__(self) -> str: - return "%s( %s )" % ( + return "%s(%s)" % ( self.name, ", ".join([stringify_object(v) for v in self.args]), ) diff --git a/godot_parser/sections.py b/godot_parser/sections.py index 7f22f5a..f651e87 100644 --- a/godot_parser/sections.py +++ b/godot_parser/sections.py @@ -12,12 +12,30 @@ "GDExtResourceSection", "GDSubResourceSection", "GDResourceSection", + "GDTypedArray" ] GD_SECTION_REGISTRY = {} +class GDTypedArray(object): + def __init__(self, inner_type, inner_value): + self.inner_type = inner_type + self.inner_value = inner_value + + def __repr__(self) -> str: + return f"TypedArray({self.__str__()})" + + def __str__(self) -> str: + return f"Array[{self.inner_type}]({self.inner_value})" + + @classmethod + def from_parser(cls, parse_result): + inner_type, inner_value = parse_result + return cls(inner_type, inner_value) + + class GDSectionHeader(object): """ Represents the header for a section diff --git a/godot_parser/values.py b/godot_parser/values.py index bcf9527..513711e 100644 --- a/godot_parser/values.py +++ b/godot_parser/values.py @@ -5,7 +5,9 @@ Forward, Group, Keyword, + Literal, Opt, + Optional, QuotedString, Suppress, Word, @@ -15,6 +17,7 @@ ) from .objects import GDObject +from .sections import GDTypedArray boolean = ( (Keyword("true") | Keyword("false")) @@ -31,10 +34,11 @@ value = Forward() # Vector2( 1, 2 ) +# May not have args: PackedStringArray() obj_type = ( Word(alphas, alphanums).set_results_name("object_name") + Suppress("(") - + DelimitedList(value) + + Opt(DelimitedList(value)) + Suppress(")") ).set_parse_action(GDObject.from_parser) @@ -44,7 +48,6 @@ Suppress("[") + Opt(DelimitedList(value)) + Opt(Suppress(",")) + Suppress("]") ) .set_name("list") - .set_parse_action(lambda p: p.as_list()) ) key_val = Group(QuotedString('"', escChar="\\") + Suppress(":") + value) @@ -57,6 +60,19 @@ .set_parse_action(lambda d: {k: v for k, v in d}) ) +# Typed arrays: e.g. Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3, 4, 5)]) +# Need to support, e.g.: +# polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3), PackedInt32Array(3, 2, 4, 5)]) +typed_array = ( + Literal("Array").suppress() + + Suppress("[") + + Word(alphanums + "_").set_results_name("inner_type") + + Suppress("]") + + Suppress("(") + + list_.set_results_name("inner_value") + + Suppress(")") +).set_name("typed_array").set_parse_action(GDTypedArray.from_parser) + # Exports -value <<= primitive | list_ | dict_ | obj_type +value <<= primitive | list_ | dict_ | obj_type | typed_array