mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-19 10:08:57 +00:00
Refactoring step.
Eventually we'll use this to change the reporting format.
This commit is contained in:
parent
cdf6076a80
commit
fecd27d8f4
@ -136,7 +136,46 @@ def printScopeError(elementType, fname, lineno):
|
||||
"""Print out warning if a scope was unable to close"""
|
||||
printError(fname, 'attempt to close empty scope at', elementType, 'line', lineno+1)
|
||||
|
||||
def parseElements(text, fname, lineno, scopes):
|
||||
class WmlIterator(object):
|
||||
"""Return an iterable WML navigation object.
|
||||
Initialize with a list of lines or a file; if the the line list is
|
||||
empty and the filename is specified, lines will be read from the file.
|
||||
|
||||
Note: if changes are made to lines while iterating, this may produce
|
||||
unexpected results. In such case, seek() to the linenumber of a
|
||||
scope behind where changes were made.
|
||||
Important Attributes:
|
||||
lines - this is an internal list of all the physical lines
|
||||
scopes - this is an internal list of all open scopes (as iterators)
|
||||
note: when retreiving an iterator from this list, always
|
||||
use a copy to perform seek() or next(), and not the original
|
||||
element - the wml tag, key, or macro name for this logical line
|
||||
(in complex cases, this may be a tuple of elements...
|
||||
see parseElements for list of possible values)
|
||||
text - the exact text of this logical line, as it appears in the
|
||||
original source, and ending with a newline
|
||||
note: the logical line also includes multi-line quoted strings
|
||||
span - the number of physical lines in this logical line:
|
||||
always 1, unless text contains a multi-line quoted string
|
||||
lineno - a zero-based line index marking where this text begins
|
||||
"""
|
||||
def __init__(self, lines=None, filename=None, begin=-1, endScope=None):
|
||||
"Initialize a new WmlIterator."
|
||||
if lines is None:
|
||||
lines = []
|
||||
if filename:
|
||||
try:
|
||||
ifp = open(filename)
|
||||
lines = ifp.readlines()
|
||||
ifp.close()
|
||||
except Exception:
|
||||
printError(filename, 'error opening file')
|
||||
self.lines = lines
|
||||
self.fname = filename
|
||||
self.reset()
|
||||
self.seek(begin)
|
||||
|
||||
def parseElements(self, text):
|
||||
"""Remove any closed scopes, return a list of element names
|
||||
and list of new unclosed scopes
|
||||
Element Types:
|
||||
@ -177,12 +216,12 @@ Element Types:
|
||||
elif text.startswith('#ifndef'):
|
||||
return (['#ifndef'],)*2
|
||||
elif text.startswith('#else'):
|
||||
if not closeScope(scopes, '#else', fname, lineno):
|
||||
printScopeError('#else', fname, lineno)
|
||||
if not closeScope(self.scopes, '#else', self.fname, self.lineno):
|
||||
printScopeError('#else', self.fname, self.lineno)
|
||||
return (['#else'],)*2
|
||||
elif text.startswith('#endif'):
|
||||
if not closeScope(scopes, '#endif', fname, lineno):
|
||||
printScopeError('#endif', fname, lineno)
|
||||
if not closeScope(self.scopes, '#endif', self.fname, self.lineno):
|
||||
printScopeError('#endif', self.fname, self.lineno)
|
||||
return ['#endif'], []
|
||||
elif text.startswith('#define'):
|
||||
return (['#define'],)*2
|
||||
@ -213,9 +252,9 @@ Element Types:
|
||||
openedScopes = []
|
||||
for elem, sortPos, scopeDelta in elements:
|
||||
while scopeDelta < 0:
|
||||
if not(closeScope(openedScopes, elem, fname, lineno)\
|
||||
or closeScope(scopes, elem, fname, lineno)):
|
||||
printScopeError(elem, fname, lineno)
|
||||
if not(closeScope(openedScopes, elem, self.fname, self.lineno)\
|
||||
or closeScope(self.scopes, elem, self.fname, self.lineno)):
|
||||
printScopeError(elem, self.fname, self.lineno)
|
||||
scopeDelta += 1
|
||||
while scopeDelta > 0:
|
||||
openedScopes.append(elem)
|
||||
@ -224,45 +263,6 @@ Element Types:
|
||||
resultElements.append(elem)
|
||||
return resultElements, openedScopes
|
||||
|
||||
class WmlIterator(object):
|
||||
"""Return an iterable WML navigation object.
|
||||
Initialize with a list of lines or a file; if the the line list is
|
||||
empty and the filename is specified, lines will be read from the file.
|
||||
|
||||
Note: if changes are made to lines while iterating, this may produce
|
||||
unexpected results. In such case, seek() to the linenumber of a
|
||||
scope behind where changes were made.
|
||||
Important Attributes:
|
||||
lines - this is an internal list of all the physical lines
|
||||
scopes - this is an internal list of all open scopes (as iterators)
|
||||
note: when retreiving an iterator from this list, always
|
||||
use a copy to perform seek() or next(), and not the original
|
||||
element - the wml tag, key, or macro name for this logical line
|
||||
(in complex cases, this may be a tuple of elements...
|
||||
see parseElements for list of possible values)
|
||||
text - the exact text of this logical line, as it appears in the
|
||||
original source, and ending with a newline
|
||||
note: the logical line also includes multi-line quoted strings
|
||||
span - the number of physical lines in this logical line:
|
||||
always 1, unless text contains a multi-line quoted string
|
||||
lineno - a zero-based line index marking where this text begins
|
||||
"""
|
||||
def __init__(self, lines=None, filename=None, begin=-1, endScope=None):
|
||||
"Initialize a new WmlIterator."
|
||||
if lines is None:
|
||||
lines = []
|
||||
if filename:
|
||||
try:
|
||||
ifp = open(filename)
|
||||
lines = ifp.readlines()
|
||||
ifp.close()
|
||||
except Exception:
|
||||
printError(filename, 'error opening file')
|
||||
self.lines = lines
|
||||
self.fname = filename
|
||||
self.reset()
|
||||
self.seek(begin)
|
||||
|
||||
def __iter__(self):
|
||||
"""The magic iterator method"""
|
||||
return self
|
||||
@ -347,7 +347,7 @@ Important Attributes:
|
||||
self.lineno = self.lineno + self.span
|
||||
self.text, self.span = parseQuotes(self.lines, self.fname, self.lineno)
|
||||
self.scopes.extend(self.nextScopes)
|
||||
self.element, nextScopes = parseElements(self.text, self.fname, self.lineno, self.scopes)
|
||||
self.element, nextScopes = self.parseElements(self.text)
|
||||
self.nextScopes = []
|
||||
for elem in nextScopes:
|
||||
# remember scopes by storing a copy of the iterator
|
||||
|
Loading…
x
Reference in New Issue
Block a user