def identifier(first=partial(one_of, ascii_letters), consecutive=partial(one_of, ascii_letters + digit_chars + '_'), must_contain=set(digit_chars)): """Expects a letter followed by one or more alphanumerical characters. If ``must_contain`` is given, the following letters must include one from this set. The default option is to expect a letter followed by a number of letters and digits, but with a requirement of at least one digit (this allows an easy distinction between names and identifiers). >>> parse(identifier, 'abc123') 'abc123' >>> parse(identifier, 'abc') # doctest: +ELLIPSIS Traceback (most recent call last): ... NoMatch: ... >>> parse(partial(identifier, must_contain=None), 'abc') 'abc' """ result = [] if first is not None: result.append(first()) if must_contain is None: chars = many(consecutive) else: chars = many1(partial(choice, consecutive, partial(one_of, must_contain))) if not set(chars) & must_contain: fail() result.extend(chars) return result
def name(): """Parses one or more names separated by whitespace, and concatenates with a single space. >>> parse(name, 'John') 'John' >>> parse(name, 'John Smith') 'John Smith' >>> parse(name, 'John, Smith') 'John' >>> parse(name, 'John Smith ') 'John Smith' """ names = map(partial("".join), sep1( partial(many, partial(satisfies, lambda l: l and l.isalpha())), whitespace1)) name = " ".join(names).strip() if not name: fail() return name
def _parse_date_format(date_format): tokens = tuple(date_format.replace('%', '')) parsers = map(tri, map(_date_format_mapping.get, tokens)) date_string = "".join(itertools.chain(*[parser() for parser in parsers])) try: return datetime.datetime.strptime(date_string, date_format) except: fail()
def end_element(name): whitespace() string("</") commit() if name != xml_name(): fail() whitespace() close_angle()
def parse(cls): if optional(any_token, None): fail()
def named_entity(): name = build_string(many1(partial(not_one_of, ";#"))) if name not in named_entities: fail() return named_entities[name]
def named_entity(): name = build_string(many1(partial(not_one_of,';#'))) if name not in named_entities: fail() return named_entities[name]