def constant_machine(bytes_data, value, emit):
    """
    Parse a constant

    Args:
        bytes_data (bytes): expected constant
        value (str): collected value
        emit (callable): callback for parsed value

    Returns:
        callable: constant parsing state machine
    """
    i = 0
    length = len(bytes_data)

    def _constant(byte_data):
        nonlocal i
        if byte_data != bytes_data[i]:
            i += 1
            raise Exception("Expected {}, got {}".format(
                hex(bytes_data[i-1]), hex(byte_data)))

        i += 1
        if i < length:
            return _constant
        return emit(value)

    return _constant