Advertisement
Frumkin

Untitled

Aug 16th, 2021
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. void Diagnostics::ThrowSyntaxError(const std::string& error, const std::size_t selection) const {
  2.     std::array<std::size_t, 2> position = map(selection);
  3.     std::cerr << "\033[1;31mSyntax error\033[0m at line " << "\033[0;32m" + std::to_string(position[0] + 1) + "\033[0m" << ", column " << "\033[0;32m" + std::to_string(position[1] + 1) + "\033[0m" << ":\n" << error << std::endl;
  4.  
  5.     constexpr std::size_t distance = 10;
  6.  
  7.     std::size_t start = selection - distance;
  8.     std::size_t end = selection + distance;
  9.  
  10.     if (position[1] < distance) {
  11.         start = selection - position[1];
  12.     }
  13.  
  14.     if (position[1] + distance >= lines[position[0]]) {
  15.         end = selection + lines[position[0]] - position[1];
  16.     }
  17.  
  18.     std::string peekCode = "";
  19.     for (std::size_t index = start; index < end + 1; index += 1) {
  20.         peekCode += (*code)[index];
  21.     }
  22.     std::cout << "\"" << peekCode << "\"" << std::endl;
  23.  
  24.     for (std::size_t index = 0; index < selection - start; index += 1) {
  25.         std::cout << " ";
  26.     }
  27.     std::cout << "\033[1;32m~~~\033[0m" << std::endl;
  28.  
  29.     Fail();
  30. }
  31.  
  32. void Diagnostics::Fail() const {
  33.     End(-1.0);
  34.  
  35.     exit(1);
  36. }
  37.  
  38. void Diagnostics::End(const double time) const {
  39.     int exitCode = 1;
  40.  
  41.     if (time >= 0.0) {
  42.         std::cout << "\nTranspile time: \033[0;32m" + std::to_string(time) + "\033[0m seconds." << std::endl;
  43.         exitCode = 0;
  44.     }
  45.  
  46.     std::cout << "\nExited with exit code: \033[1;31m" + std::to_string(exitCode) + "\033[0m." << std::endl;
  47.  
  48.     enclose();
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement