def quoted(parser=any_token): """Parses as much as possible until it encounters a matching closing quote. By default matches any_token, but can be provided with a more specific parser if required. Returns a string """ quote_char = quote() value, _ = many_until(parser, partial(one_of, quote_char)) return build_string(value)
def parse(cls): result = {} prefix = optional(tri(identifier), None) if prefix is not None: result['patient_id'] = "".join(prefix) whitespace() one_of('+') caseless_string('muac') if prefix is None: try: whitespace1() part = optional(tri(identifier), None) if part is not None: result['patient_id'] = "".join(part) else: result['name'] = name() except: raise FormatError("Expected a patient id or name.") if 'name' in result: try: separator() result['sex'] = one_of('MmFf').upper() except: raise FormatError("Expected either M or F to indicate the patient's gender.") try: separator() except: raise FormatError("Expected age or birthdate of patient.") try: result['age'] = choice(*map(tri, (date, timedelta))) except: received, stop = many_until(any_token, comma) raise FormatError("Expected age or birthdate of patient, but " "received %s." % "".join(received)) try: if prefix is None: separator() else: whitespace1() reading = choice( partial(one_of_strings, 'red', 'green', 'yellow', 'r', 'g', 'y'), digits) try: reading = int("".join(reading)) except: reading = reading[0].upper() else: whitespace() unit = optional(partial(one_of_strings, 'mm', 'cm'), None) if unit is None: reading = cls.get_reading_in_mm(reading) elif "".join(unit) == 'cm': reading = reading * 10 result['reading'] = reading except: raise FormatError( "Expected MUAC reading (either green, yellow or red), but " "received %s." % "".join(remaining())) if optional(separator, None): result['tags'] = tags() return result
def doctype(): string("<!DOCTYPE") commit() many_until(any_token, close_angle)
def comment(): string("<!--") commit() result, _ = many_until(any_token, tri(partial(string, "-->"))) return "COMMENT", build_string(result)
def doctype(): string('<!DOCTYPE') commit() many_until(any_token, close_angle)