@@ -49,7 +49,7 @@ class HsdParser:
4949 ... \" \" \" )
5050 >>> parser.parse(hsdfile)
5151 >>> dictbuilder.hsddict
52- {'Hamiltonian': {'Dftb': {'Scc': True, 'Filling': {'Fermi': {'Temperature.attrib ': 'Kelvin' , 'Temperature': 100 }}}}}
52+ {'Hamiltonian': {'Dftb': {'Scc': True, 'Filling': {'Fermi': {'Temperature': 100 , 'Temperature.attrib ': 'Kelvin' }}}}}
5353 """
5454
5555 def __init__ (self , eventhandler : Optional [HsdEventHandler ] = None ,
@@ -75,9 +75,10 @@ def __init__(self, eventhandler: Optional[HsdEventHandler] = None,
7575 self ._after_equal_sign = False # last tag was opened with equal sign
7676 self ._inside_attrib = False # parser inside attrib specification
7777 self ._inside_quote = False # parser inside quotation
78- self ._has_child = False
78+ self ._has_child = True # Whether current node has a child already
79+ self ._has_text = False # whether current node contains text already
7980 self ._oldbefore = "" # buffer for tagname
80- self ._lower_tag_names = lower_tag_names
81+ self ._lower_tag_names = lower_tag_names # whether tag names should be lowered
8182
8283
8384 def parse (self , fobj : Union [TextIO , str ]):
@@ -148,14 +149,13 @@ def _parse(self, line):
148149 # tagname was followed by an attribute -> append
149150 self ._oldbefore += before
150151 else :
151- self ._has_child = True
152152 self ._hsdattrib [common .HSD_ATTRIB_EQUAL ] = True
153153 self ._starttag (before , False )
154154 self ._after_equal_sign = True
155155
156156 # Opening tag by curly brace
157157 elif sign == "{" :
158- self ._has_child = True
158+ # self._has_child = True
159159 self ._starttag (before , self ._after_equal_sign )
160160 self ._buffer = []
161161 self ._after_equal_sign = False
@@ -188,7 +188,7 @@ def _parse(self, line):
188188 self ._oldbefore = before
189189 self ._buffer = []
190190 self ._inside_attrib = True
191- self ._opened_tags .append (("[" , self ._currline , None ))
191+ self ._opened_tags .append (("[" , self ._currline , None , None , None ))
192192 self ._checkstr = _ATTRIB_SPECIALS
193193
194194 # Closing attribute specification
@@ -212,7 +212,7 @@ def _parse(self, line):
212212 self ._checkstr = sign
213213 self ._inside_quote = True
214214 self ._buffer .append (before + sign )
215- self ._opened_tags .append (('"' , self ._currline , None ))
215+ self ._opened_tags .append (('"' , self ._currline , None , None , None ))
216216
217217 # Interrupt
218218 elif sign == "<" and not self ._after_equal_sign :
@@ -237,13 +237,18 @@ def _parse(self, line):
237237 def _text (self , text ):
238238 stripped = text .strip ()
239239 if stripped :
240+ if self ._has_child :
241+ self ._error (SYNTAX_ERROR , (self ._currline , self ._currline ))
240242 self ._eventhandler .add_text (stripped )
243+ self ._has_text = True
241244
242245
243246 def _starttag (self , tagname , closeprev ):
244247 txt = "" .join (self ._buffer )
245248 if txt :
246249 self ._text (txt )
250+ if self ._has_text :
251+ self ._error (SYNTAX_ERROR , (self ._currline , self ._currline ))
247252 tagname_stripped = tagname .strip ()
248253 if self ._oldbefore :
249254 if tagname_stripped :
@@ -254,15 +259,15 @@ def _starttag(self, tagname, closeprev):
254259 self ._error (SYNTAX_ERROR , (self ._currline , self ._currline ))
255260 self ._hsdattrib [common .HSD_ATTRIB_LINE ] = self ._currline
256261 if self ._lower_tag_names :
257- self ._hsdattrib [common .HSD_ATTRIB_TAG ] = tagname_stripped
262+ self ._hsdattrib [common .HSD_ATTRIB_NAME ] = tagname_stripped
258263 tagname_stripped = tagname_stripped .lower ()
259264 self ._eventhandler .open_tag (tagname_stripped , self ._attrib ,
260265 self ._hsdattrib )
261266 self ._opened_tags .append (
262- (tagname_stripped , self ._currline , closeprev , self ._has_child ))
267+ (tagname_stripped , self ._currline , closeprev , True , False ))
268+ self ._has_child = False
263269 self ._buffer = []
264270 self ._oldbefore = ""
265- self ._has_child = False
266271 self ._attrib = None
267272 self ._hsdattrib = {}
268273
@@ -271,7 +276,7 @@ def _closetag(self):
271276 if not self ._opened_tags :
272277 self ._error (SYNTAX_ERROR , (0 , self ._currline ))
273278 self ._buffer = []
274- tag , _ , closeprev , self ._has_child = self ._opened_tags .pop ()
279+ tag , _ , closeprev , self ._has_child , self . _has_text = self ._opened_tags .pop ()
275280 self ._eventhandler .close_tag (tag )
276281 if closeprev :
277282 self ._closetag ()
0 commit comments