def capture1(p): return capture(p) >> (lambda x: x[0]) # Ouch
def capture1(p): return capture(p) >> (lambda x: x[0]) # Ouch var = capture1(anyone)
import parsimonious # MIT, Pure Python grammar = parsimonious.Grammar('balanced = "(" ( ~"[^()]+" / balanced )* ")"') print('parsimonious:', grammar.parse(s)) import peglet # GPL parser = peglet.Parser(r'balanced = \( ([^()]+ | balanced)* \) ') # AFAICT peglet doesn't support referring to rules inside a capture # (balanced) matches/returns the string 'balanced', not the rule #print(parser(s)) # sre_constants.error: unbalanced parenthesis import parson # GPL grammar = parson.Grammar("balanced : '(' ( /[^()]+/ | balanced )* ')'.") parser = grammar() print('parson:', parser.balanced(s)) print('parson:', parson.capture(parser.balanced)(s)) # The curly braces denote capture grammar = parson.Grammar("balanced : '(' { /[^()]+/ | balanced }* ')'.") parser = grammar() print('parson:', parser.balanced(s)) grammar = parson.Grammar("balanced : {'(' ( /[^()]+/ | balanced )* ')'}.") parser = grammar() print('parson:', parser.balanced(s)) #import pyrser #"balanced = ['(' [ [~['(' | ')']]+ | balanced ] ')']" # requires subclassing from rpython.rlib import parsing