def parse(self, filename): self.init_parser() #open file in reading mode unicode f = open(filename, 'rU') func_name = None for line in f: self.n += 1 self.d['line_count'] += 1 m = REMatcher(line.rstrip('\n')) # line are stored with their \n self.lines.append(line) # match a libido tag if m.match(r'libido:'): self.d['libido'] += 1 self.libido_parser.analyze_line(self, m) if m.match(r'^\s*#'): self.d['comments'] += 1 elif m.match(r'^\s*$'): self.d['empty'] += 1 elif m.match(r'^(function)?\s*([a-zA-Z][a-zA-Z0-9_]*)\s*\(\)'): self.d['function'] += 1 func_name = m.group(2) self.chunks[func_name] = { 'start' : self.n } elif m.match(r'^\}') and func_name: self.chunks[func_name]['end'] = self.n func_name = None f.close() return self.d
def test_REMatcher(): l = 'some string' m = REMatcher(l) assert m.match(r'(some)') assert m.group(1) == 'some' assert m.all() == ('some',)