import string,re TOKEN_RE = re.compile(r"(?P\s+)|[*]?\s*(?P(=|>=|=<)\s*(\w|[._/+-])+)|(?P\w+[ \t]*(?=[=]))|(?P#.*)") VALUE_RE = re.compile(r"=[ \t]*(\"(?P(\\.|[^\"])*)\"|(?P[^ \t\n\r\f\v\#]+))") def parse(filename): file = open(filename) text = file.read() file.close() index = index0 = 0 line = 1 length = len(text) hunks = [] current_hunk = None state = 'preamble' def signal_error(msg): raise "%s (file: %s, line: %s, column: %s)" \ % (msg,filename,line,index-index0) def read_result(): for key,val in res.groupdict().items(): if val is not None: return key,val while index0: index0 = nlidx + 1 index = res.end() if type=='space' : pass elif type=='comment': pass elif type=='cond' : if state != 'conds': current_hunk = {'conds':[],'env':{}} hunks.append(current_hunk) state = 'conds' current_hunk['conds'].append(value) elif type=='var' : res = VALUE_RE.match(text,index) if not res: signal_error("syntax error for assignment to %s" % value) if state != 'env': if state != 'conds': current_hunk = {'conds':[],'env':{}} hunks.append(current_hunk) state = 'env' var = value type,value = read_result() line = line + string.count(text,'\n',index,res.end()) nlidx = string.rfind(text,'\n',index,res.end()) if nlidx>0: index0 = nlidx + 1 index = res.end() current_hunk['env'][var] = value else: signal_error("this should never happen") return hunks