def passport(): _fields = {} while True: fkey, fval = yield field _fields[fkey] = fval m = yield string_from(" ", "\n").optional() if m == "\n": n = yield peek(string("\n").optional()) e = yield peek(eof.result(True).optional()) if n == "\n" or e: break elif m is None: break return _fields
postfix = yield control_postfix.optional() return TableEndEntry(label, postfix) @parsy.generate def hook_rhs(): label = yield control_label return TableHookEntry(label) text_rhs = parsy.regex( '.+$', flags=re.M).map(lambda x: TableTextEntry(x)).desc('text entry') codepoint = parsy.regex('([0-9a-fA-F]{2})+').map(to_hex).desc( 'code point in hexadecimal') text_entry = parsy.peek( parsy.regex('[0-9a-fA-F]')).map(lambda x: TableEntryKind.TEXT) control_entry = parsy.string('$').map(lambda x: TableEntryKind.CONTROL) end_entry = parsy.string('/').map(lambda x: TableEntryKind.END) hook_entry = parsy.string('@').map(lambda x: TableEntryKind.HOOK) entry_kind = text_entry | control_entry | end_entry | hook_entry @parsy.generate def line(): kind = yield entry_kind binary = yield codepoint yield parsy.string('=') if kind == TableEntryKind.TEXT: entry = yield text_rhs elif kind == TableEntryKind.CONTROL: entry = yield control_rhs
def test_peek(self): self.assertEqual(peek(any_char).parse_partial("abc"), ("a", "abc")) with self.assertRaises(ParseError) as err: peek(digit).parse("a") self.assertEqual(str(err.exception), "expected 'a digit' at 0:0")