import re


def _parse_parameters(parameters):
    """ Parses parameters string and returns a dict of overrides.

    This function assumes that parameters string is in the form of '"key1="value1" key2="value2"'.
    Use of single quotes is optional but is helpful for strings that contain spaces.

    Args:
        parameters (str): A string in the form of '"key="value" key="value"'.

    Returns:
        dict: A dict containing key/value pairs parsed from the parameters string.

    Raises:
        ValueError: if the parameters string is malformed.
    """

    if not re.match(r'^(\w+)="([^=]+)"(\s{1}(\w+)="([^=]+)")*$', parameters):
        raise ValueError

    # first we add tokens that separate key/value pairs.
    # in case of key='ss  sss  ss', we skip tokenizing when we se the first single quote
    # and resume when we see the second
    replace_space = True
    tokenized = ""
    for c in parameters:
        if c == '\"':
            replace_space = not replace_space
        elif c == ' ' and replace_space:
            tokenized += "$$"
        else:
            tokenized += c

    # now get the tokens
    tokens = tokenized.split('$$')
    result = {}
    for token in tokens:
        # separate key/values
        key_value = token.split("=")
        result[key_value[0]] = key_value[1]
    return result