Skip to content

Commit

Permalink
Make custom lexer more generic, #327
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Oct 4, 2024
1 parent 4b628ed commit e204a49
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
61 changes: 33 additions & 28 deletions gdtoolkit/parser/gdscript_indenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,36 @@ def _dedent_lambda_at_token(self, token: Token):
yield Token.new_borrow_pos(self.DEDENT_type, "N/A", token)

def _current_token_is_just_after_lambda_header(self):
# TODO: handle newlines etc. in between tokens
return (
len(self.processed_tokens) > 0
and self.processed_tokens[-2].type == "COLON"
and self.processed_tokens[-3].type == "RPAR"
and self.processed_tokens[-4].type == "LPAR"
and (
self.processed_tokens[-5].type == "FUNC"
or (
self.processed_tokens[-5].type == "NAME"
and self.processed_tokens[-6].type == "FUNC"
)
)
) or (
len(self.processed_tokens) > 0
and self.processed_tokens[-2].type == "COLON"
and self.processed_tokens[-3].type == "TYPE_HINT"
and self.processed_tokens[-4].value == "->"
and self.processed_tokens[-5].type == "RPAR"
and self.processed_tokens[-6].type == "LPAR"
and (
self.processed_tokens[-7].type == "FUNC"
or (
self.processed_tokens[-7].type == "NAME"
and self.processed_tokens[-8].type == "FUNC"
)
)
)
extra_rpars = [0]
pattern_functions = [
lambda t: t.type == "COLON",
lambda t: t.type == "RPAR",
lambda t: t.type == "LPAR" and extra_rpars[0] == 0,
lambda t: t.type == "FUNC",
]

def lpar_accept_function(token: Token) -> bool:
if token.type == "RPAR":
extra_rpars[0] += 1
elif token.type == "LPAR":
if extra_rpars[0] <= 0:
return False
extra_rpars[0] -= 1
return True

accept_functions = [
lambda t: t.type == "_NL",
lambda t: t.type in ["_NL", "TYPE_HINT"] or t.value == "->",
lpar_accept_function,
lambda t: t.type in ["_NL", "NAME"],
]
i = 0
for processed_token in reversed(self.processed_tokens):
if i >= len(pattern_functions):
return True
if pattern_functions[i](processed_token):
i += 1
continue
if not accept_functions[i](processed_token):
return False
return i >= len(pattern_functions)
7 changes: 7 additions & 0 deletions tests/valid-gd-scripts/bug_327_multiline_lambda.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ func foo(new_button, button_name, menu, _game_flow):
var test := ""
_game_flow.ref.request_transition(menu[button_name].transition, menu[button_name].data)
)

func bar(new_button, button_name, menu, _game_flow):
new_button.pressed.connect(
func(x=(1)) -> void:
var test := ""
_game_flow.ref.request_transition(menu[button_name].transition, menu[button_name].data)
)

0 comments on commit e204a49

Please sign in to comment.