from simpleparse.common import numbers, strings, comments from simpleparse.parser import Parser from simpleparse.dispatchprocessor import DispatchProcessor, dispatch class MyProcessor(DispatchProcessor): # http://simpleparse.sourceforge.net/pydoc/simpleparse.dispatchprocessor.html#DispatchProcessor # The method names here correspond to the grammar's productions (below) def file(self, (tag, start, stop, subtags), buffer): print tag, start, stop def section(self, (tag, start, stop, subtags), buffer): print tag, start, stop for x in subtags: dispatch(self, x, buffer) def body(self, (tag, start, stop, subtags), buffer): print tag, start, stop for x in subtags: dispatch(self, x, buffer) def statement(self, (tag, start, stop, subtags), buffer): print tag, start, stop, buffer[start:stop].strip() def identifier(self, (tag, start, stop, subtags), buffer): print tag, start, stop, buffer[start:stop] def ts(self, (tag, start, stop, subtags), buffer): print tag, start, stop class MyParser(Parser): # http://simpleparse.sourceforge.net/pydoc/simpleparse.parser.html#Parser def buildProcessor(self): return MyProcessor() # This is copied straight out of the following website which # has more details, # http://simpleparse.sourceforge.net/scanning_with_simpleparse.html # Here is simpleparser's grammar for reading grammars: # http://simpleparse.sourceforge.net/simpleparse_grammars.html parser = MyParser( # The EBNF: r''' file := [ \t\n]*, section+ section := '[',identifier!,']'!, ts,'\n', body body := statement* statement := (ts,semicolon_comment)/equality/nullline nullline := ts,'\n' equality := ts, identifier,ts,'=',ts,identified,ts,'\n' identifier := [a-zA-Z], [a-zA-Z0-9_]* identified := string/number/identifier ts := [ \t]* ''', # The root/starting production: 'file') data = ''' [foobar] baz = notbaz [gooober] banana = apple pear = peach ''' success, result, nextchar = parser.parse(data)