Skip to content

Commit d751f3a

Browse files
Support @abstract functions
1 parent b1bcaa1 commit d751f3a

File tree

7 files changed

+62
-0
lines changed

7 files changed

+62
-0
lines changed

gdtoolkit/common/ast.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def _load_sub_statements(self):
3939
raise NotImplementedError
4040
if self.kind in ["func_def", "static_func_def"]:
4141
self.sub_statements = [Statement(n) for n in self.lark_node.children[1:]]
42+
elif self.kind == "abstract_func_def":
43+
# Abstract functions don't have a body, so no sub-statements
44+
pass
4245
elif self.kind == "if_stmt":
4346
for branch in self.lark_node.children:
4447
if branch.data in ["if_branch", "elif_branch"]:
@@ -141,6 +144,10 @@ def _load_data_from_node_children(self, node: Tree) -> None:
141144
function = Function(stmt)
142145
self.functions.append(function)
143146
self.all_functions.append(function)
147+
if stmt.data == "abstract_func_def":
148+
function = Function(stmt)
149+
self.functions.append(function)
150+
self.all_functions.append(function)
144151

145152
def _load_data_from_class_def(self, class_def: Tree) -> None:
146153
name_token = find_name_token_among_children(class_def)

gdtoolkit/formatter/annotation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .expression_to_str import expression_to_str
1010

1111
_STANDALONE_ANNOTATIONS = [
12+
"abstract",
1213
"export_category",
1314
"export_group",
1415
"export_subgroup",

gdtoolkit/formatter/class_statement.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def format_class_statement(statement: Tree, context: Context) -> Outcome:
4040
"static_func_def": lambda s, c: _format_func_statement(
4141
s.children[0], c, "static "
4242
),
43+
"abstract_func_def": _format_abstract_func_statement,
4344
"annotation": format_standalone_annotation,
4445
"property_body_def": format_property_body,
4546
} # type: Dict[str, Callable]
@@ -203,3 +204,21 @@ def _format_enum_statement(statement: Tree, context: Context) -> Outcome:
203204
)
204205
enum_body = actual_enum.children[-1]
205206
return format_concrete_expression(enum_body, expression_context, context)
207+
208+
209+
def _format_abstract_func_statement(statement: Tree, context: Context) -> Outcome:
210+
abstract_func_header = statement.children[0]
211+
return _format_abstract_func_header(abstract_func_header, context)
212+
213+
214+
def _format_abstract_func_header(statement: Tree, context: Context) -> Outcome:
215+
name = statement.children[0].value
216+
has_return_type = len(statement.children) > 2
217+
expression_context = ExpressionContext(
218+
f"func {name}",
219+
get_line(statement),
220+
f" -> {statement.children[2].value}" if has_return_type else "",
221+
get_end_line(statement),
222+
)
223+
func_args = statement.children[1]
224+
return format_concrete_expression(func_args, expression_context, context)

gdtoolkit/linter/class_checks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def _map_statement_to_section(statement: Statement) -> str:
122122
return "others"
123123
if statement.kind == "static_func_def":
124124
return "others"
125+
if statement.kind == "abstract_func_def":
126+
return "others"
125127
if statement.kind == "docstr_stmt":
126128
return "docstrings"
127129
if statement.kind == "static_class_var_stmt":

gdtoolkit/parser/gdscript.lark

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ _simple_class_stmt: annotation* single_class_stmt (";" annotation* single_class_
1818
| property_body_def
1919
| func_def
2020
| "static" func_def -> static_func_def
21+
| abstract_func_def
2122
annotation: "@" NAME [annotation_args]
2223
annotation_args: "(" [test_expr ("," test_expr)* [trailing_comma]] ")"
2324

@@ -71,7 +72,9 @@ _property_delegates: property_delegate_set ["," [_NL] property_delegate_get]
7172
property_custom_getter_args: "(" ")"
7273

7374
func_def: func_header _func_suite
75+
abstract_func_def: abstract_func_header
7476
func_header: "func" NAME func_args ["->" TYPE_HINT] ":"
77+
abstract_func_header: "func" NAME func_args ["->" TYPE_HINT]
7578
func_args: "(" [func_arg ("," func_arg)* [trailing_comma]] ")"
7679
?func_arg: func_arg_regular
7780
| func_arg_inf
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@abstract class_name BaseClass
2+
3+
@abstract func simple_abstract()
4+
5+
@abstract func abstract_with_params(param1: String, param2: int)
6+
7+
@abstract func abstract_with_return_type() -> String
8+
9+
@abstract func abstract_with_params_and_return(input: String) -> int
10+
11+
func concrete_method():
12+
pass
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@abstract
2+
class_name BaseClass
3+
4+
@abstract
5+
func simple_abstract()
6+
7+
@abstract
8+
func abstract_with_params(param1: String, param2: int)
9+
10+
@abstract
11+
func abstract_with_return_type() -> String
12+
13+
@abstract
14+
func abstract_with_params_and_return(input: String) -> int
15+
16+
17+
func concrete_method():
18+
pass

0 commit comments

Comments
 (0)