header.py

changeset 77
d98502ae1f33
parent 76
c73432653fd9
child 79
eb93feb6d3a3
equal deleted inserted replaced
76:c73432653fd9 77:d98502ae1f33
40 ) 40 )
41 @property 41 @property
42 def valid(self): 42 def valid(self):
43 return False 43 return False
44 44
45 geometrical_types = [
46 linetypes.LineSegment,
47 linetypes.Triangle,
48 linetypes.Quadrilateral,
49 linetypes.ConditionalLine,
50 ]
51
52 def is_invertnext(entry): 45 def is_invertnext(entry):
53 return isinstance(entry, linetypes.MetaCommand) \ 46 return isinstance(entry, linetypes.MetaCommand) \
54 and entry.text == "BFC INVERTNEXT" 47 and entry.text == "BFC INVERTNEXT"
55 48
56 def is_suitable_header_object(entry): 49 def is_suitable_header_object(entry):
58 # BFC INVERTNEXT is not a header command anymore. 51 # BFC INVERTNEXT is not a header command anymore.
59 return False 52 return False
60 return not any( 53 return not any(
61 isinstance(entry, linetype) 54 isinstance(entry, linetype)
62 for linetype in [ 55 for linetype in [
63 *geometrical_types, 56 linetypes.SubfileReference,
57 linetypes.LineSegment,
58 linetypes.Triangle,
59 linetypes.Quadrilateral,
60 linetypes.ConditionalLine,
64 linetypes.Comment, 61 linetypes.Comment,
65 linetypes.Error, 62 linetypes.Error,
66 ] 63 ]
67 ) 64 )
68 65
164 elif self.try_to_match( 161 elif self.try_to_match(
165 r'!CMDLINE (.+)', 162 r'!CMDLINE (.+)',
166 'cmdline'): 163 'cmdline'):
167 result.cmdline = self.groups[0] 164 result.cmdline = self.groups[0]
168 else: 165 else:
169 self.parse_error("couldn't understand header syntax: " + repr(header_entry.text)) 166 self.cursor -= 1
167 break
170 if not result.filetype: 168 if not result.filetype:
171 self.parse_error('LDRAW_ORG line is missing') 169 self.parse_error('LDRAW_ORG line is missing')
172 return { 170 return {
173 'header': result, 171 'header': result,
174 'end-index': self.cursor + 1, 172 'end-index': self.cursor + 1,
175 } 173 }
176 def parse_error(self, message): 174 def parse_error(self, message):
177 raise HeaderError(index = self.cursor, reason = message) 175 raise HeaderError(index = self.cursor, reason = message)
178 def get_more_header_stuff(self): 176 def get_more_header_stuff(self):
179 while True: 177 self.cursor += 1
180 self.cursor += 1 178 new_cursor = self.cursor
181 if self.cursor >= len(self.model_body): 179 while new_cursor < len(self.model_body):
182 break 180 entry = self.model_body[new_cursor]
183 entry = self.model_body[self.cursor]
184 if not is_suitable_header_object(entry): 181 if not is_suitable_header_object(entry):
185 break 182 break
186 if isinstance(entry, linetypes.MetaCommand): 183 if isinstance(entry, linetypes.MetaCommand):
184 self.cursor = new_cursor
187 yield entry 185 yield entry
186 new_cursor += 1
188 def skip_to_next(self, *, spaces_expected = 0): 187 def skip_to_next(self, *, spaces_expected = 0):
189 while True: 188 while True:
190 if self.cursor + 1 >= len(self.model_body): 189 if self.cursor + 1 >= len(self.model_body):
191 self.parse_error('file does not have a proper header') 190 self.parse_error('file does not have a proper header')
192 self.cursor += 1 191 self.cursor += 1

mercurial