# 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)
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) csv = CSV() print "CSV:", csv from bnf import Context c = Context('test.csv') c.rows = [[]] csv.parse(c) print "Found rows:" for i, row in enumerate(c.rows): print str(i) + ": " + ''.join(("%-10s" % col) for col in row)
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) csv = CSV() print "CSV:", csv from bnf import Context c = Context('test.csv') c.rows = [[]] csv.parse(c) print "Found rows:" for i, row in enumerate(c.rows): print str(i) + ": " + ''.join(("%-10s" % col) for col in row)
def __init__(self, filename): BaseContext.__init__(self, filename) self._scopes = {} self._expressions = []
#!/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)
class EOL(Identifier): __default_regex__ = r'[\r\n]+' __whitespaces__ = [] # 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)