import yaml
import re
from typing import Any


def load_yaml(stream: Any):
    """
    Parse the first YAML document in a stream and produce the corresponding Python object.
    This function support parsing float values like `1e-4`.

    Implementation credit: https://stackoverflow.com/questions/30458977/yaml-loads-5e-6-as-string-and-not-a-number

    Args:
        stream:
    """

    loader = yaml.SafeLoader
    loader.add_implicit_resolver(
        "tag:yaml.org,2002:float",
        re.compile(
            """^(?:
         [-+]?(?:[0-9][0-9_]*)\\.[0-9_]*(?:[eE][-+]?[0-9]+)?
        |[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
        |\\.[0-9_]+(?:[eE][-+][0-9]+)?
        |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*
        |[-+]?\\.(?:inf|Inf|INF)
        |\\.(?:nan|NaN|NAN))$""",
            re.X,
        ),
        list("-+0123456789."),
    )

    return yaml.load(stream, Loader=loader)