exception.cxx 722 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // exception.cxx
  3. // dice
  4. //
  5. // Created by Sam Jaffe on 12/17/18.
  6. // Copyright © 2018 Sam Jaffe. All rights reserved.
  7. //
  8. #include "dice-roll/exception.h"
  9. #include <cmath>
  10. #include <string>
  11. static std::string carrot(long long pos) {
  12. if (pos == -1) return "<END>"; // For safety
  13. std::string out;
  14. // resize(0) followed by back() will cause UB
  15. out.resize(std::max(pos, 1ll), '~');
  16. out.back() = '^';
  17. return out;
  18. }
  19. namespace dice {
  20. unexpected_token::unexpected_token(std::string const & reason, long long pos)
  21. : std::runtime_error(reason), position(pos) {}
  22. std::string unexpected_token::pointer(long long backup_length) const {
  23. return carrot(position == -1 ? backup_length : position);
  24. }
  25. }