# Data ::= "[^;\r\n"]*" class Data(Identifier): __default_regex__ = r'[^;\r\n"]*' __whitespaces__ = [] def onMatch(self, context): context.rows[-1].append(self.id) # Row ::= Data [Separator Data]* EOL class Row(Group): __group__ = [Data, Group([Separator, Data], min=0, max=-1), EOL] def onMatch(self, context): context.rows.append([]) # CSV ::= [Row]+ class CSV(Group): __group__ = Group([Row], max=-1) context = Context('test.csv') context.rows = [[]] csv.parse(context) print "Found rows:" for i, row in enumerate(c.rows): print str(i) + ": " + ''.join(str(col) + '|' for col in row)
#!/usr/bin/env python from bnf import Context from bnf import Literal # Language ::= "Hello, World!" class Language(Literal): __token__ = "Hello, World!" if __name__ == '__main__': context = Context('filename') language = Language() language.parse(context)