Factor out error related to unexpected tokens into a utility function.