Skip to content

Commit

Permalink
Handle broken sl2 keywords properly
Browse files Browse the repository at this point in the history
  • Loading branch information
CensoredUsername committed May 16, 2021
1 parent 47ac3df commit 77e01d1
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions decompiler/sl2decompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,46 @@ def print_keywords_and_children(self, keywords, children, lineno, needs_colon=Fa
keywords_somewhere.extend(("tag", tag))
else:
current_line[1].extend(("tag", tag))

force_newline = False
for key, value in keywords:
if value is None:
value = ""
if current_line[0] is None:
# ok, so normally this wouldn't make sense to be None, it should be a PyExpr. However
# ren'py's parser is broken and instead of erroring on a keyword argument that hasn't got
# an actual value given it instead just inserts `None` as the value. Since basically every keyword
# is technically a valid expression the only way for this to happen is at the end of a line,
# so after this we have to force a line break

# if this is the first keyword, or the previous was broken we need to force a newline
if current_line[0] is None or force_newline:
force_newline = False
keywords_by_line.append(current_line)
current_line = (0, [])
elif current_line[0] is None or value.linenumber > current_line[0]:
keywords_by_line.append(current_line)
current_line = (value.linenumber, [])
current_line[1].extend((key, value))

# force a newline
force_newline = True

# just output the key
current_line[1].append(key)

else:
if current_line[0] is None or value.linenumber > current_line[0] or force_newline:
force_newline = False
keywords_by_line.append(current_line)
current_line = (value.linenumber, [])

current_line[1].extend((key, value))

if keywords_by_line:
if force_newline:
keywords_by_line.append(current_line)
current_line = (0, [])

# Easy case: we have at least one line inside the block that already has keywords.
# Just put the ones from keywords_somewhere with them.
current_line[1].extend(keywords_somewhere)
keywords_somewhere = []

keywords_by_line.append(current_line)
last_keyword_line = keywords_by_line[-1][0]
children_with_keywords = []
Expand Down

0 comments on commit 77e01d1

Please sign in to comment.