Ejemplo n.º 1
0
def capture1(p):
    return capture(p) >> (lambda x: x[0])  # Ouch
Ejemplo n.º 2
0
def capture1(p): return capture(p) >> (lambda x: x[0]) # Ouch
var = capture1(anyone)
Ejemplo n.º 3
0
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