def parse(data, forgiving_mode=False):
    decl = _forgiving_EBNFDeclaration if forgiving_mode else _EBNFDeclaration
    parser = simpleparse.parser.Parser(decl, "Collection")
    processor = _SGFProcessor()
    success, collection, next_char = parser.parse(data, processor=processor)
    if not success:
        if forgiving_mode:
            raise SGFParseError("Parse Failed!")
        else:
            warnings.warn("Parse failed, attempting parse in forgiving mode")
            return parse(data, forgiving_mode=True)
    else:
        return collection
section := '[',identifier,']', ts,'\n', body
body := statement*
statement := (ts,';',comment,'\n')/equality/nullline
nullline := ts,'\n'
comment := -'\n'*
equality := ts, identifier,ts,'=',ts,identified,ts,'\n'
identifier := [a-zA-Z], [a-zA-Z0-9_]*
identified := ('\'',string,'\'')/number/identifier
ts := [ \t]*
char := -[\134\']+
number := [0-9eE+.-]+
string := (char/escapedchar)*
escapedchar := '\134\''/'\134\134'
'''

testdata = '''
    [test1]
    val=23
    val2='23'
    wherefore='art thou'
    ; why not
    log=heavy_wood

[test2]
loose=lips

'''

parser = simpleparse.parser.Parser(declaration, 'file')
pprint.pprint(parser.parse(testdata))
Beispiel #3
0
nullline := ts,'\n'
comment := -'\n'*
equality := ts, identifier,ts,'=',ts,identified,ts,'\n'
identifier := [a-zA-Z], [a-zA-Z0-9_]*
identified := ('\'',string,'\'')/number/identifier
ts := [ \t]*
char := -[\134\']+
number := [0-9eE+.-]+
string := (char/escapedchar)*
escapedchar := '\134\''/'\134\134'
'''

testdata = '''
	[test1]
	val=23
	val2='23'
	wherefore='art thou'
	; why not
	log=heavy_wood

[test2]
loose=lips

'''

import simpleparse.parser  # for Parser
import pprint  # for pprint

parser = simpleparse.parser.Parser(declaration, 'file')
pprint.pprint(parser.parse(testdata))
ws		:= [ \t\v]*
'''


class MyProcessorClass(simpleparse.dispatchprocessor.DispatchProcessor):
#	def __init__(self):
#		print('cons')

    def number(self, tup, buf):
        print('in number')
        '''Process the given production and it's children'''

    def expr(self, tup, buf):
        print('in expr')
        '''Process the given production and it's children'''

    def __call__(self, value, data):
        print('value is ' + str(value))
        print('data is ' + str(data))
        return value
        # return super(self.__class__,self).__call__(self,value,data)


class MyParser(simpleparse.parser.Parser):

    def buildProcessor(self):
        return MyProcessorClass()

parser = MyParser(declaration, 'full')
pprint.pprint(parser.parse(sys.argv[1]))
Beispiel #5
0
sys.path.append('esrapy')
import esrapy # GPL, pure python, not pip installable
pattern = esrapy.compile("balanced = '(' (<[^()]+> | balanced)* ')'")
print('esrapy:', pattern.match(s))


import pyparsing # MIT
pattern = pyparsing.nestedExpr('(', ')', ignoreExpr=None)
print('payparsing:', pattern.parseString(s))


import simpleparse.parser # BSD, bundles mxTextTools under mxLicense
parser = simpleparse.parser.Parser("balanced := '(', (-[()]+ / balanced)*, ')'",
                                   root='balanced')
print('simpleparse:', parser.parse(s)) # some sort of result tree

#import arpeggio
#def balanced():
#    return '(', arpeggio.ZeroOrMore([arpeggio.Not(['(', ')']), balanced]), ')'
#parser = arpeggio.ParserPython(balanced)
#print('arpeggio:', parser.parse(s))

#import pypeg
# Subclass a load of primitives, sod that

#import spark
# http://pages.cpsc.ucalgary.ca/~aycock/spark/

import parsimonious # MIT, Pure Python
grammar = parsimonious.Grammar('balanced = "(" ( ~"[^()]+" / balanced )* ")"')