Silver Challenge

The error messages printed out by evaluate(_:) are useful, but not as useful as they could be. Here are a couple of erroneous inputs and the error messages they produce:

evaluate("1 + 3 + 7a + 8")
> Input contained an invalid character: a

evaluate("10 + 3 3 + 7")
> Invalid token during parsing: .Number(3)

Make these messages more helpful by including the character position where the error occurred. After completing this challenge, you should see error messages like this:

evaluate("1 + 3 + 7a + 8")
> Input contained an invalid character at index 9: a

evaluate("10 + 3 3 + 7")
> Invalid token during parsing at index 7: 3

Hint: Printing a String.CharacterView.Index will show the character position corresponding ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.