wmlxgettext: Improve parser resiliency for malformed files (#7625)

* wmlxgettext: Improve parser resiliency for malformed files

- WML files with no translatable strings will no longer
crash the script on unbalanced tags (but translatable will).
- .cfg files belonging to man(1) will no longer crash the script.

* Remove stray quotes from textdomain declarations

These are not recognized by the wmlxgettext parser.
This commit is contained in:
Slayer95 2023-05-17 14:40:36 -05:00 committed by GitHub
parent 594f5b3d89
commit 0620822ab2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 8 deletions

View file

@ -1,4 +1,4 @@
#textdomain "wesnoth-lib"
#textdomain wesnoth-lib
#define _GUI_ACHIEVEMENT_TITLE_FONT_COLOR STATE
"(

View file

@ -1,4 +1,4 @@
#textdomain "wesnoth"
#textdomain wesnoth
#define SINGLE_UNIT_CODE
-1#enddef

View file

@ -9,12 +9,20 @@ fileref = None
fileno = None
nodes = None
onDefineMacro = False
unbalanced_wml = None
def _closenode_update_dict(podict):
global unbalanced_wml
if nodes[-1].sentences is not None:
for i in nodes[-1].sentences:
if unbalanced_wml is not None:
# At this point we know that the file has translatable strings.
# If it has unbalanced tags, raise an exception. Note that would the error
# be ignored, then wrong context info would be attached to these sentences,
# or to an undetermined future sentence.
wmlerr(*unbalanced_wml)
posentence = podict.get(i.sentence)
if posentence is None:
podict[i.sentence] = (
@ -40,6 +48,7 @@ def newfile(file_ref, file_no):
def closefile(mydict, lineno):
global unbalanced_wml
if nodes is not None:
if len(nodes) > 1:
err_message = ("End of WML file reached, but some tags were " +
@ -47,9 +56,12 @@ def closefile(mydict, lineno):
"(nearest unclosed tag is: " +
nodes[-1].tagname + ")" )
finfo = fileref + ":" + str(lineno)
wmlerr(finfo, err_message)
else:
_closenode_update_dict(mydict)
wmlwarn(finfo, err_message)
while len(nodes) > 1:
closenode("", mydict, lineno)
_closenode_update_dict(mydict)
unbalanced_wml = None
def newnode(tagname):
@ -71,11 +83,13 @@ def closenode(closetag, mydict, lineno):
global fileref
global fileno
global nodes
global unbalanced_wml
if nodes is None:
err_message = ("unexpected closing tag '" +
closetag + "' outside any scope.")
finfo = fileref + ":" + str(lineno)
wmlerr(finfo, err_message)
unbalanced_wml = (finfo, err_message)
wmlwarn(*unbalanced_wml)
else:
# node to close is the LAST element in self.nodes list
mytag = nodes[-1].tagname
@ -85,15 +99,19 @@ def closenode(closetag, mydict, lineno):
if mynode.tagname == "":
err_message = ("unexpected closing tag '" +
closetag + "' outside any scope.")
wmlerr(finfo, err_message)
unbalanced_wml = (finfo, err_message)
wmlwarn(*unbalanced_wml)
else:
if closetag != expected_closetag:
err_message = ("expected closing tag '" +
expected_closetag + "' but '" +
closetag + "' found.")
wmlerr(finfo, err_message)
unbalanced_wml = (finfo, err_message)
wmlwarn(*unbalanced_wml)
_closenode_update_dict(mydict)
nodes.pop()
if len(nodes) == 0:
nodes = None
def addNodeSentence(sentence, *, ismultiline, lineno, lineno_sub,