Improve support for reference modifiers

These changes implement full support for the `~` and `^` modifiers to references, including optional trailing numbers.

I mainly wanted to add support for `~`.  I'm more accustomed to using that than `^`, since with my shell and configuration `^` needs to be quoted but `~` doesn't.  While I was at it I decided to try adding the full support.