'Replace tokens by an instance of *grp_cls*.'

def group_tokens(self, grp_cls, start, end, include_end=True, extend=False):

    start_idx = start
    start = self.tokens[start_idx]
    end_idx = (end + include_end)
    if (extend and isinstance(start, grp_cls)):
        subtokens = self.tokens[(start_idx + 1):end_idx]
        grp = start
        grp.tokens.extend(subtokens)
        del self.tokens[(start_idx + 1):end_idx]
        grp.value = text_type(start)
    else:
        subtokens = self.tokens[start_idx:end_idx]
        grp = grp_cls(subtokens)
        self.tokens[start_idx:end_idx] = [grp]
        grp.parent = self
    for token in subtokens:
        token.parent = grp
    return grp
