Skip to content

Commit

Permalink
[wmlparser3] use less recursion when parsing
Browse files Browse the repository at this point in the history
specifically, avoid recursion for every double double-quite (i.e. the
escaping of " as "")
  • Loading branch information
allefant committed Mar 11, 2016
1 parent cd84293 commit 1279214
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions data/tools/wesnoth/wmlparser3.py
Expand Up @@ -384,7 +384,7 @@ def preprocess(self, defines):
out.decode("utf8") +
err.decode("utf8"))

def parse_line_without_commands(self, line):
def parse_line_without_commands_loop(self, line : str) -> str:
"""
Once the .plain commands are handled WML lines are passed to
this.
Expand All @@ -408,7 +408,7 @@ def parse_line_without_commands(self, line):
self.temp_key_nodes[self.commas].value.append(
self.temp_string_node)
self.in_arrows = False
self.parse_line_without_commands(line[arrows + 2:])
return line[arrows + 2:]
else:
self.temp_string += line
return
Expand All @@ -420,16 +420,14 @@ def parse_line_without_commands(self, line):
if arrows >= 0 and (quote < 0 or quote > arrows):
self.parse_line_without_commands(line[:arrows])
self.in_arrows = True
self.parse_line_without_commands(line[arrows + 2:])
return
return line[arrows + 2:]

if quote >= 0:
if self.in_string:
# double quote
if quote < len(line) - 1 and line[quote + 1] == b'"'[0]:
self.temp_string += line[:quote + 1]
self.parse_line_without_commands(line[quote + 2:])
return
return line[quote + 2:]
self.temp_string += line[:quote]
self.temp_string_node = StringNode(self.temp_string)
if self.translatable:
Expand All @@ -443,17 +441,23 @@ def parse_line_without_commands(self, line):
self.temp_string_node)

self.in_string = False
self.parse_line_without_commands(line[quote + 1:])
return line[quote + 1:]
else:
self.parse_outside_strings(line[:quote])
self.in_string = True
self.parse_line_without_commands(line[quote + 1:])
return line[quote + 1:]
else:
if self.in_string:
self.temp_string += line
else:
self.parse_outside_strings(line)

def parse_line_without_commands(self, line):
while True:
line = self.parse_line_without_commands_loop(line)
if not line:
break

def parse_outside_strings(self, line):
"""
Parse a WML fragment outside of strings.
Expand Down
4 changes: 2 additions & 2 deletions data/tools/wmlunits
Expand Up @@ -606,11 +606,11 @@ if __name__ == '__main__':
options.wesnoth = "wesnoth"

if not options.data_dir:
options.data_dir = shell_out([options.wesnoth, "--path"]).strip()
options.data_dir = shell_out([options.wesnoth, "--path"]).strip().decode("utf8")
print(("Using " + options.data_dir + " as data dir."))

if not options.config_dir:
options.config_dir = shell_out([options.wesnoth, "--config-path"]).strip()
options.config_dir = shell_out([options.wesnoth, "--config-path"]).strip().decode("utf8")
print(("Using " + options.config_dir + " as config dir."))

if not options.transdir:
Expand Down

0 comments on commit 1279214

Please sign in to comment.