Exemple #1
0
    def _parser(it):
        expected = set([])
        v, it = primitives.any_(it)

        did_advance = False

        # This is a bookkeeping function that will mark did_advance if we indeed
        # advanced the iterator.
        def _wrap_advance(it):
            nonlocal did_advance
            did_advance = True
            yield from it

        for parser in parsers:
            try:
                return parser(itertools.chain([v], _wrap_advance(it)))
            except primitives.ParseError as e:
                # If the parser state was advanced, we throw the original error.
                if did_advance:
                    raise

                v = e.value
                it = e.it

                expected.update(e.expected)
        raise primitives.ParseError(v, it, expected)
Exemple #2
0
    def _parser(it):
        v, it = primitives.any_(it)

        did_advance = False

        # This is a bookkeeping function that will mark did_advance if we indeed
        # advanced the iterator.
        def _wrap_advance(it):
            nonlocal did_advance
            did_advance = True
            yield from it

        try:
            v, it = parser(itertools.chain([v], _wrap_advance(it)))
        except primitives.ParseError as e:
            # If the parsing state was advanced, throw an error.
            if did_advance:
                raise

            v = default()
            it = e.unpeek()
        return v, it