diff -r e46fa477007b -r 66c9591b733d parse.py --- a/parse.py Fri May 24 15:32:10 2019 +0300 +++ b/parse.py Fri May 24 17:37:10 2019 +0200 @@ -7,27 +7,36 @@ pass def parse_ldraw_code(line): - if isinstance(line, bytes): - line = line.decode() - line = line.strip() - if not line: - return linetypes.EmptyLine() - elif line == '0': - return linetypes.Comment('') - elif line.startswith('0 '): - return linetypes.Comment(line[2:]) - elif line.startswith('1 '): - return parse_ldraw_subfile_reference(line) - elif line.startswith('2 '): - return parse_ldraw_line(line) - elif line.startswith('3 '): - return parse_ldraw_triangle(line) - elif line.startswith('4 '): - return parse_ldraw_quadrilateral(line) - elif line.startswith('5 '): - return parse_ldraw_conditional_line(line) + try: + if isinstance(line, bytes): + line = line.decode() + line = line.strip() + if not line: + return linetypes.EmptyLine() + elif line == '0': + return linetypes.MetaCommand('') + elif line.startswith('0 '): + return parse_ldraw_meta_line(line) + elif line.startswith('1 '): + return parse_ldraw_subfile_reference(line) + elif line.startswith('2 '): + return parse_ldraw_line(line) + elif line.startswith('3 '): + return parse_ldraw_triangle(line) + elif line.startswith('4 '): + return parse_ldraw_quadrilateral(line) + elif line.startswith('5 '): + return parse_ldraw_conditional_line(line) + else: + raise BadLdrawLine('unknown line type') + except BadLdrawLine as error: + return linetypes.Error(line, str(error)) + +def parse_ldraw_meta_line(line): + if line.startswith('0 //'): + return linetypes.Comment(line[4:]) else: - raise BadLdrawLine('unknown line type') + return linetypes.MetaCommand(line[2:]) def parse_ldraw_subfile_reference(line): pattern = r'^1\s+([^ ]+)' + r'\s+([^ ]+)' * (3 + 9 + 1) + r'\s*$' @@ -74,8 +83,12 @@ except ValueError: raise BadLdrawLine('bad numeric values') vertices.append(Vertex(*coordinates)) + try: + colour = int(match.group(1), 0) + except ValueError: + raise BadLdrawLine('invalid syntax for colour: ' + repr(match.group(1))) return { - 'colour': Colour(match.group(1)), + 'colour': Colour(colour), 'vertices': vertices, }