lib/tree_parse.py
changeset 17 b538e1f7011c
parent 16 4a40718c7b4b
child 21 b05979822dee
equal deleted inserted replaced
16:4a40718c7b4b 17:b538e1f7011c
     2 """
     2 """
     3     Parsing trees of node stored using a python-like syntax.
     3     Parsing trees of node stored using a python-like syntax.
     4 
     4 
     5     A file consists of a number of lines, and each line consists of indenting whitespace and data. Each line has a parent
     5     A file consists of a number of lines, and each line consists of indenting whitespace and data. Each line has a parent
     6 """
     6 """
       
     7 
       
     8 class TreeParseError (Exception) :
       
     9     """
       
    10         Error parsing a tree file
       
    11     """
       
    12 
       
    13     pass
     7 
    14 
     8 def _read_lines (path, stop_tokens='') :
    15 def _read_lines (path, stop_tokens='') :
     9     """
    16     """
    10         Reads lines from the given path, ignoring empty lines, and yielding (line_number, indent, line) tuples, where 
    17         Reads lines from the given path, ignoring empty lines, and yielding (line_number, indent, line) tuples, where 
    11         line_number is the line number, indent counts the amount of leading whitespace, and line is the actual line
    18         line_number is the line number, indent counts the amount of leading whitespace, and line is the actual line
   107                         parent = stack_parent
   114                         parent = stack_parent
   108 
   115 
   109                         break
   116                         break
   110 
   117 
   111                     elif stack_indent < indent :
   118                     elif stack_indent < indent :
   112                         assert False, "Bad un-indent"
   119                         raise TreeParseError("Bad unindent on %s:%d, %d < %d" % (path, line_number, stack_indent, indent))
   113         
   120         
   114         # add to parent?
   121         # add to parent?
   115         if parent :
   122         if parent :
   116             parent[2].append(item)
   123             parent[2].append(item)
   117 
   124