def METHOD_NAME(text, match_mode=None):
    """Escape $NAME and ${NAME} syntax in "text"."""
    ret = ""
    tail = text
    while tail:
        match = _MATCH_MODES[match_mode].match(tail)
        if match:
            groups = match.groupdict()
            ret += (
                groups["head"] + groups["escape"] * 2 + "\\" + groups["symbol"]
            )
            tail = groups["tail"]
        else:
            ret += tail
            tail = ""
    return ret