@@ -74,6 +74,8 @@ def __getattr__(self, name):
7474
7575 def input (self , s ):
7676 startidx = s .find ('ISO-10303-21;' , 0 , self .header_limit )
77+ # TODO: It would be better to raise a suitable exception and
78+ # let the application decide how to handle it.
7779 if startidx == - 1 :
7880 sys .exit ('Aborting... ISO-10303-21; header not found' )
7981 self .lexer .input (s [startidx :])
@@ -211,6 +213,8 @@ def __init__(self, lexer=None, debug=0):
211213 def parse (self , p21_data , ** kwargs ):
212214 self .lexer .input (p21_data )
213215 self .refs = {}
216+ self .in_p21_exchange_structure = False
217+
214218 if 'debug' in kwargs :
215219 result = self .parser .parse (lexer = self .lexer , debug = logger ,
216220 ** { k : kwargs [k ] for k in kwargs if k != 'debug' })
@@ -219,9 +223,23 @@ def parse(self, p21_data, **kwargs):
219223 return result
220224
221225 def p_exchange_file (self , p ):
222- """exchange_file : PART21_START header_section data_section_list PART21_END """
226+ """exchange_file : p21_start header_section data_section_list part21_end """
223227 p [0 ] = P21File (p [2 ], p [3 ])
224228
229+ def p_p21_start (self , p ):
230+ """p21_start : PART21_START"""
231+ if self .in_p21_exchange_structure :
232+ raise SyntaxError
233+ self .in_p21_exchange_structure = True
234+ p [0 ] = p [1 ]
235+
236+ def p_p21_end (self , p ):
237+ """p21_end : PART21_END"""
238+ if not self .in_p21_exchange_structure :
239+ raise SyntaxError
240+ self .in_p21_exchange_structure = False
241+ p [0 ] = p [1 ]
242+
225243 # TODO: Specialise the first 3 header entities
226244 def p_header_section (self , p ):
227245 """header_section : HEADER_SEC header_entity header_entity header_entity ENDSEC"""
0 commit comments