Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.68 KB | None | 0 0
  1. #include "parser.h"
  2. #include <locale>
  3.  
  4. parser::parser(int rWidth)
  5.     : width_(rWidth),
  6.       head_(new buffer()),
  7.       elems_(0),
  8.       sumSize_(0),
  9.       first_(true),
  10.       writed_(false) {
  11.   if (rWidth < 25) {
  12.     std::cerr << "incorrect width\n";
  13.     exit(EXIT_FAILURE);
  14.   }
  15.   pList temp2 = head_;
  16.   for (int i = 0; i < 3; i++) {
  17.     pList temp(new buffer());
  18.     temp2->next = temp;
  19.     temp2 = temp;
  20.   }
  21.   temp2->next = head_;
  22. }
  23.  
  24. parser::~parser() {
  25.   pList temp = head_;
  26.   for (int i = 0; i < 3; i++) {
  27.     temp = head_->next;
  28.     delete head_;
  29.     head_ = temp;
  30.   }
  31.   delete temp;
  32. }
  33.  
  34. void parser::parse() {
  35.   pList iter = head_->next->next->next;
  36.   type_t t = UNDEFINED;
  37.   int i = 0;
  38.   char dot = '.';
  39.   bool numberWithDot = false;
  40.   bool wordWithMinus = false;
  41.   std::cin >> std::noskipws;
  42.   while (std::cin >> arr_[i]) {
  43.     if (t == WORD) {
  44.       if (isalpha(arr_[i])) {
  45.         i++;
  46.         wordWithMinus = false;
  47.         continue;
  48.       } else if (isspace(arr_[i])) {
  49.         save(iter, 0, i, t);
  50.         i = 0;
  51.         t = UNDEFINED;
  52.         wordWithMinus = false;
  53.         continue;
  54.       } else if ((ispunct(arr_[i])) && (arr_[i] != '-')) {
  55.         save(iter, 0, i, t);
  56.         if (arr_[i] == ',') {
  57.           save(iter, i, i + 1, COMMA);
  58.         } else {
  59.           save(iter, i, i + 1, PUNCTUATION);
  60.         }
  61.         i = 0;
  62.         t = UNDEFINED;
  63.         wordWithMinus = false;
  64.         continue;
  65.       } else if (arr_[i] == '-') {
  66.         if (wordWithMinus == false) {
  67.           wordWithMinus = true;
  68.         } else {
  69.           t = DASH;
  70.         }
  71.       } else if (isdigit(arr_[i])) {
  72.         std::cerr << "letter+digit\n";
  73.         exit(EXIT_FAILURE);
  74.       }
  75.     }
  76.  
  77.     else if (t == NUMBER) {
  78.       if (isdigit(arr_[i])) {
  79.         i++;
  80.         continue;
  81.       } else if (isspace(arr_[i])) {
  82.         if (arr_[i - 1] == dot) {
  83.           save(iter, 0, i - 1, t);
  84.           save(iter, i - 1, i, PUNCTUATION);
  85.         } else {
  86.           save(iter, 0, i, t);
  87.         }
  88.         i = 0;
  89.         t = UNDEFINED;
  90.         numberWithDot = false;
  91.         continue;
  92.       } else if ((arr_[i] == dot) && (numberWithDot == false)) {
  93.         numberWithDot = true;
  94.       } else if ((ispunct(arr_[i])) && (arr_[i] != '-')) {
  95.         if (arr_[i - 1] == dot) {
  96.           std::cerr << "wrong combination of number with dot and punctuation\n";
  97.           exit(EXIT_FAILURE);
  98.         } else {
  99.           save(iter, 0, i, NUMBER);
  100.           if (arr_[i] == ',') {
  101.             save(iter, i, i + 1, COMMA);
  102.           } else {
  103.             save(iter, i, i + 1, PUNCTUATION);
  104.           }
  105.           i = 0;
  106.           t = UNDEFINED;
  107.           numberWithDot = false;
  108.           continue;
  109.         }
  110.       } else if (arr_[i] == '-') {
  111.         if (i != 1) {
  112.           save(iter, 0, i, NUMBER);
  113.         }
  114.         t = DASH;
  115.       } else if (isalpha(arr_[i])) {
  116.         std::cerr << "letter+digit\n";
  117.         exit(EXIT_FAILURE);
  118.       }
  119.     }
  120.  
  121.     else if (t == DASH) {
  122.       if (arr_[i - 2] == '-') {
  123.         if (arr_[i] == '-') {
  124.           if ((iter->t == PUNCTUATION) || (iter->t == UNDEFINED)) {
  125.             std::cerr << "punctuation error\n";
  126.             exit(EXIT_FAILURE);
  127.           } else {
  128.             save(iter, i - 2, i + 1, DASH);
  129.             i = 0;
  130.             t = UNDEFINED;
  131.             wordWithMinus = false;
  132.             numberWithDot = false;
  133.             continue;
  134.           }
  135.         }
  136.       } else {
  137.         std::cerr << "--\n";
  138.         exit(EXIT_FAILURE);
  139.       }
  140.  
  141.       if (arr_[i] != '-') {
  142.         std::cerr << "wrong dash\n";
  143.         exit(EXIT_FAILURE);
  144.       }
  145.     }
  146.  
  147.     else if (t == UNDEFINED) {
  148.       if (isspace(arr_[i])) {
  149.         continue;
  150.       } else if (isalpha(arr_[i])) {
  151.         t = WORD;
  152.       } else if (isdigit(arr_[i]) || (arr_[i] == '+') || (arr_[i] == '-')) {
  153.         t = NUMBER;
  154.       } else if ((ispunct(arr_[i])) && (arr_[i] != '-')) {
  155.         if ((iter->t == UNDEFINED) || (iter->t == PUNCTUATION) ||
  156.             (iter->t == COMMA) || (iter->t == DASH)) {
  157.           std::cerr << "punctuation error\n";
  158.           exit(EXIT_FAILURE);
  159.         } else if (arr_[i] == ',') {
  160.           save(iter, 0, 1, COMMA);
  161.         } else {
  162.           save(iter, 0, 1, PUNCTUATION);
  163.         }
  164.         continue;
  165.       }
  166.     }
  167.  
  168.     i++;
  169.   }
  170.   if (i > 0) {
  171.     save(iter, 0, i, t);
  172.   }
  173.   while (elems_ != 0) {
  174.     print();
  175.   }
  176.   std::cout << std::endl;
  177. }
  178.  
  179. void parser::print() {
  180.   pList iter = head_;
  181.   std::string temp = iter->str;  // current word + punctuations
  182.   elems_--;
  183.   if (iter->next->t == COMMA) {
  184.     temp += ',';
  185.     iter = iter->next;
  186.     iter->t = UNDEFINED;
  187.     elems_--;
  188.     if (iter->next->t == DASH) {
  189.       temp += " ---";
  190.       iter = iter->next;
  191.       iter->t = UNDEFINED;
  192.       elems_--;
  193.     }
  194.   } else if (iter->next->t == DASH) {
  195.     temp += " ---";
  196.     iter = iter->next;
  197.     iter->t = UNDEFINED;
  198.     elems_--;
  199.   } else if (iter->next->t == PUNCTUATION) {
  200.     temp += iter->next->str;
  201.     iter = iter->next;
  202.     iter->t = UNDEFINED;
  203.     elems_--;
  204.   }
  205.  
  206.   if (!first_) {
  207.     sumSize_++;
  208.   }
  209.   sumSize_ += temp.size();
  210.   if (sumSize_ > width_) {
  211.     std::cout << std::endl;
  212.     sumSize_ = temp.size();
  213.   } else if (!first_) {
  214.     std::cout << ' ';
  215.   }
  216.  
  217.   std::cout << temp;
  218.   first_ = false;
  219.   head_ = iter->next;
  220. }
  221.  
  222. void parser::save(pList& iter, int begin, int end, type_t t) {
  223.   if ((end - begin) > 20) {
  224.     std::cerr << "too large\n";
  225.     exit(EXIT_FAILURE);
  226.   }
  227.   if ((iter->next == head_) && (writed_)) {
  228.     print();
  229.   }
  230.   iter = iter->next;
  231.   std::string temp(arr_ + begin, arr_ + end);
  232.   iter->t = t;
  233.   iter->str = temp;
  234.   writed_ = true;
  235.   elems_++;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement