'Translate TYPEOF into DAL expression
@param l: the left operand
@param r: the right operand'

def _query_typeof(self, l, r):

    (hierarchy, field, nodeset, none) = self._resolve_hierarchy(l, r)
    if (not hierarchy):
        return self._query_belongs(l, r)
    if (not field):
        return None
    list_type = (str(field.type)[:5] == 'list:')
    if nodeset:
        if list_type:
            q = field.contains(list(nodeset))
        elif (len(nodeset) > 1):
            q = field.belongs(nodeset)
        else:
            q = (field == tuple(nodeset)[0])
    else:
        q = None
    if none:
        if (not list_type):
            if (q is None):
                q = (field == None)
            else:
                q |= (field == None)
    if (q is None):
        q = field.belongs(set())
    return q
