Improve selection parsing error reporting

Now the selection parser tracks the part of selection text that
contributes to the current parsing context, and uses that for more
useful error messages.  At least in some cases, there is now also
context information for syntax errors, although this could likely be
improved further.

Exception handling during selection parsing is also improved:
 - The above context is now added to the exception, similar to what the
   old reporting mechanism does.
 - The full selection is added to the exception as context, also for
   non-interactive parsing.
 - More consistent exception handling, e.g., in cases where an exception
   is thrown during error handling.

Improve documentation for related functions.

Part of #655.

Change-Id: Ib276ce4e219692c9819e45a1637e3660d958803f