Skip to content

Commit 71760d2

Browse files
committed
Align parsing of constants to variables, closes #197
1 parent ca15a58 commit 71760d2

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

gdtoolkit/formatter/const_statement.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,42 @@
77

88

99
def format_const_statement(statement: Tree, context: Context) -> Outcome:
10-
if len(statement.children) == 4:
11-
prefix = f"const {statement.children[1].value} = "
12-
elif len(statement.children) == 5:
13-
prefix = f"const {statement.children[1].value} := "
14-
elif len(statement.children) == 6:
15-
prefix = (
16-
f"const {statement.children[1].value}: {statement.children[3].value} = "
17-
)
10+
concrete_const_stmt = statement.children[0]
11+
handlers = {
12+
"const_assigned": _format_const_assigned_statement,
13+
"const_typed_assigned": _format_const_typed_assigned_statement,
14+
"const_inf": _format_const_inferred_statement,
15+
}
16+
return handlers[concrete_const_stmt.data](concrete_const_stmt, context)
17+
18+
19+
def _format_const_assigned_statement(statement: Tree, context: Context) -> Outcome:
20+
expression_context = ExpressionContext(
21+
f"const {statement.children[0].value} = ",
22+
get_line(statement),
23+
"",
24+
get_end_line(statement),
25+
)
26+
return format_expression(statement.children[-1], expression_context, context)
27+
28+
29+
def _format_const_typed_assigned_statement(
30+
statement: Tree, context: Context
31+
) -> Outcome:
32+
expression_context = ExpressionContext(
33+
f"const {statement.children[0].value}: {statement.children[1].value} = ",
34+
get_line(statement),
35+
"",
36+
get_end_line(statement),
37+
)
38+
return format_expression(statement.children[-1], expression_context, context)
39+
40+
41+
def _format_const_inferred_statement(statement: Tree, context: Context) -> Outcome:
1842
expression_context = ExpressionContext(
19-
prefix, get_line(statement), "", get_end_line(statement)
43+
f"const {statement.children[0].value} := ",
44+
get_line(statement),
45+
"",
46+
get_end_line(statement),
2047
)
2148
return format_expression(statement.children[-1], expression_context, context)

gdtoolkit/gd2py/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _convert_statement(statement: Tree, context: Context) -> List[str]:
5656
"const_stmt": lambda s, c: [
5757
"{}{} = {}".format(
5858
c.indent_string,
59-
s.children[1].value,
59+
s.children[0].children[0].value,
6060
_convert_expression_to_str(s.children[-1]),
6161
)
6262
],

gdtoolkit/parser/gdscript.lark

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ class_var_typed_assgnd: _var_typed "=" expr [inline_property_body]
4747
class_var_inf: "var" NAME ":" "=" expr [inline_property_body]
4848
inline_property_body: ":" [property_delegate_set ["," property_delegate_get]]
4949
| ":" [property_delegate_get ["," property_delegate_set]]
50-
!const_stmt: "const" NAME [":" [TYPE_HINT]] "=" expr
50+
const_stmt: const_assigned
51+
| const_typed_assigned
52+
| const_inf
53+
const_assigned: "const" NAME "=" expr
54+
const_typed_assigned: "const" NAME ":" TYPE_HINT "=" expr
55+
const_inf: "const" NAME ":" "=" expr
5156
docstr_stmt: string
5257

5358
class_def: "class" NAME [extends_stmt] ":" (_class_body | _class_stmt)

0 commit comments

Comments
 (0)