Handle errors a bit more sensibly, especially with quoted strings

This commit is contained in:
Thomas Hurst 2008-08-26 17:12:29 +01:00
parent 9baf905cab
commit c304b519ad
1 changed files with 26 additions and 29 deletions

View File

@ -89,8 +89,7 @@ class ConfigFile:
while True: while True:
mo = QuotedStrings[chr].match(self._line, self._pos) mo = QuotedStrings[chr].match(self._line, self._pos)
if mo is None: if mo is None:
self.parse_error(_("Unterminated quoted string")) raise ConfigSyntaxError(_("Unterminated quoted string"), self)
return False
self._pos = mo.end() self._pos = mo.end()
if self._line[self._pos - 2] == '\\': if self._line[self._pos - 2] == '\\':
string += mo.group(1)[0:-1] + chr string += mo.group(1)[0:-1] + chr
@ -103,13 +102,6 @@ class ConfigFile:
else: else:
return False return False
def parse_error(self, error):
e = ConfigSyntaxError(error, self)
if self.errors_are_fatal:
raise e
else:
self.errors.append(e)
def parse(self): def parse(self):
file = open(self.filename) file = open(self.filename)
rc = file.readlines() rc = file.readlines()
@ -126,30 +118,35 @@ class ConfigFile:
self.errors = [] self.errors = []
for self._line in rc: for self._line in rc:
self._lnum += 1 try:
self._pos = 0 self._lnum += 1
self._max = len(self._line) self._pos = 0
dbg("Line %d: %s" % (self._lnum, repr(self._line))) self._max = len(self._line)
dbg("Line %d: %s" % (self._lnum, repr(self._line)))
self._call_if_match(WhitespaceRE, None) self._call_if_match(WhitespaceRE, None)
# [Section] # [Section]
self._call_if_match(Section, self._section, 1) self._call_if_match(Section, self._section, 1)
# setting = # setting =
if self._call_if_match(Setting, self._setting, 1): if self._call_if_match(Setting, self._setting, 1):
# "quoted value" # "quoted value"
if not self._call_if_quoted_string(self._value): if not self._call_if_quoted_string(self._value):
# #000000 # colour that would otherwise be a comment # #000000 # colour that would otherwise be a comment
if not self._call_if_match(Colourvalue, self._value, 1): if not self._call_if_match(Colourvalue, self._value, 1):
# bare value # bare value
if not self._call_if_match(Barevalue, self._value, 1): if not self._call_if_match(Barevalue, self._value, 1):
self.parse_error(_("Setting without a value")) raise ConfigSyntaxError(_("Setting without a value"), self)
continue
self._call_if_match(Ignore, lambda junk: dbg("Ignoring: %s" % junk)) self._call_if_match(Ignore, lambda junk: dbg("Ignoring: %s" % junk))
if self._line[self._pos:] != '': if self._line[self._pos:] != '':
self.parse_error(_("Unexpected token")) raise ConfigSyntaxError(_("Unexpected token"), self)
except ConfigSyntaxError, e:
if self.errors_are_fatal:
raise e
else:
self.errors.append(e)
if self.errors: if self.errors:
raise ParsedWithErrors(self.filename, self.errors) raise ParsedWithErrors(self.filename, self.errors)