def construct(pattern): patterns = (curry(cons) ** Pattern.parse * -(Char(" ") >> -Pattern.parse))(pattern) def run(args): results = list(pat.match(x) for pat, x in zip(patterns, args)) if all(x is not None for x in results): return dict(concat(results)) return run
def construct(pattern): patterns = (curry(cons)**Pattern.parse * -(Char(" ") >> -Pattern.parse))(pattern) def run(args): results = list(pat.match(x) for pat, x in zip(patterns, args)) if all(x is not None for x in results): return dict(concat(results)) return run
class Cons(Multi("x", "xs")): parse = Delay(lambda: curry(Cons)**Pattern.parse_noncons * (Char(":") >> Pattern.parse)) def match(self, value): if len(value) > 0: y = self.x.match(value[0]) if y: ys = self.xs.match(value[1:]) return ys and y + ys
from Prelude.ParserCombinators.Entities import * from Prelude.ParserCombinators.String import * from Prelude import cons, curry #http://www.python.jp/doc/release/reference/expressions.html expression_list = lambda x: curry(cons) ** x * -(Char(",") >> x) << Optional(Char(",")) identifier = (alpha | Char("_")) + "".join ** -(alnum | Char("_")) parens = lambda x: Char("(") >> x << Char(")") brackets = lambda x: Char("[") >> x << Char("]") escapeseq = Char("\\") >> ascii stringprefix = reduce(Parser.__or__, map(Char, ["r", "u", "ur", "R", "U", "UR", "Ur", "uR", "b", "B", "br", "Br", "bR", "BR"])) shortstringchar = lambda quote: Sat(lambda c: c != "\\" and c != "\n" and c != quote) shortstringitem = lambda quote: shortstringchar(quote) | escapeseq shortstring = Char("'") + "".join ** -shortstringitem("'") + Char("'") | Char('"') + "".join ** -shortstringitem('"') + Char('"') stringliteral = eval ^ Optional(stringprefix) + shortstring """ longstring = String("'''") >> -longstringitem << String("'''") | String('\"\""') >> -longstringitem << String('\""\"') longstringchar = Sat("\\".__ne__) longstringitem = longstringchar | escapeseq """ nonzerodigit = Sat(lambda c: "0" < c <= "9") octdigit = Sat(lambda c: "0" <= c <= "7") bindigit = Char("0") | Char("1") hexdigit = digit | Sat(lambda c: "a" <= c <= "f" or "A" <= c <= "F") octinteger = Char("0") + (Char("o") | Char("O")) + "".join ** +octdigit | Char("0") + "".join ** +octdigit
from Prelude.ParserCombinators.Entities import * from Prelude.ParserCombinators.String import * from Prelude import cons, curry #http://www.python.jp/doc/release/reference/expressions.html expression_list = lambda x: curry(cons)**x * -(Char(",") >> x) << Optional( Char(",")) identifier = (alpha | Char("_")) + "".join**-(alnum | Char("_")) parens = lambda x: Char("(") >> x << Char(")") brackets = lambda x: Char("[") >> x << Char("]") escapeseq = Char("\\") >> ascii stringprefix = reduce( Parser.__or__, map(Char, [ "r", "u", "ur", "R", "U", "UR", "Ur", "uR", "b", "B", "br", "Br", "bR", "BR" ])) shortstringchar = lambda quote: Sat(lambda c: c != "\\" and c != "\n" and c != quote) shortstringitem = lambda quote: shortstringchar(quote) | escapeseq shortstring = Char("'") + "".join**-shortstringitem("'") + Char("'") | Char( '"') + "".join**-shortstringitem('"') + Char('"') stringliteral = eval ^ Optional(stringprefix) + shortstring """ longstring = String("'''") >> -longstringitem << String("'''") | String('\"\""') >> -longstringitem << String('\""\"') longstringchar = Sat("\\".__ne__) longstringitem = longstringchar | escapeseq