Beispiel #1
0
# 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)
Beispiel #2
0
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)
Beispiel #3
0
    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)
Beispiel #4
0
 def __init__(self, filename):
     BaseContext.__init__(self, filename)
     self._scopes = {}
     self._expressions = []
Beispiel #5
0
#!/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)
Beispiel #6
0
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)