Пример #1
0
    def parse():
        one_of('+')
        one_of_strings('register', 'reg')

        result = {}

        @tri
        def ident():
            whitespace1()
            one_of('#')
            commit()
            result['ident'] = "".join(many1(partial(not_one_of, ',')))

        @tri
        def name():
            whitespace1()
            result['name'] = "".join(many1(partial(not_one_of, ',')))

        optional(partial(choice, ident, name), None)
        return result
Пример #2
0
    def parse(cls):
        one_of('+')
        caseless_string('epi')

        aggregates = {}

        if whitespace():
            while peek():
                try:
                    code = "".join(one_of_strings(*(tuple(cls.TOKENS) + tuple(cls.ALIAS))))
                    code = code.upper()
                except:
                    raise FormatError(
                        "Expected an epidemiological indicator "
                        "such as TB or MA.")

                # rewrite alias
                code = cls.ALIAS.get(code, code)

                if code in aggregates:
                    raise FormatError("Duplicate value for %s." % code)

                whitespace1()
                try:
                    minus = optional(partial(one_of, '-'), '')
                    value = int("".join([minus]+digits()))
                except:
                    raise FormatError("Expected a value for %s." % code)

                if value < 0:
                    raise FormatError("Got %d for %s. You must report a positive value." % (
                        value, cls.TOKENS[code].lower()))

                aggregates[code] = value
                whitespace()

        return {
            'aggregates': aggregates
            }