Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "parser.h"
- #include <locale>
- parser::parser(int rWidth) :
- width_(rWidth), head_(new buffer()), elems_(0), sumSize_(0), first_(true), writed_(false)
- {
- if (rWidth < 25)
- {
- std::cerr << "incorrect width\n";
- exit(EXIT_FAILURE);
- }
- pList temp2 = head_;
- for (int i = 0; i < 3; i++)
- {
- pList temp(new buffer());
- temp2->next = temp;
- temp2 = temp;
- }
- temp2->next = head_;
- }
- parser::~parser()
- {
- pList temp = head_;
- for (int i = 0; i < 3; i++)
- {
- temp = head_->next;
- delete head_;
- head_ = temp;
- }
- delete temp;
- }
- void parser::parse()
- {
- pList iter = head_->next->next->next;
- type_t t = UNDEFINED;
- int i = 0;
- char dot = '.';
- bool numberWithDot = false;
- bool wordWithMinus = false;
- std::cin >> std::noskipws;
- while (std::cin >> arr_[i])
- {
- if (t == WORD)
- {
- if (isalpha(arr_[i]))
- {
- i++;
- wordWithMinus = false;
- continue;
- }
- else if (isspace(arr_[i]))
- {
- save(iter, 0, i, t);
- i = 0;
- t = UNDEFINED;
- wordWithMinus = false;
- continue;
- }
- else if ((ispunct(arr_[i])) && (arr_[i] != '-'))
- {
- save(iter, 0, i, t);
- if (arr_[i] == ',')
- {
- save(iter, i, i + 1, COMMA);
- }
- else
- {
- save(iter, i, i + 1, PUNCTUATION);
- }
- i = 0;
- t = UNDEFINED;
- wordWithMinus = false;
- continue;
- }
- else if (arr_[i] == '-')
- {
- if (wordWithMinus == false)
- {
- wordWithMinus = true;
- }
- else
- {
- t = DASH;
- }
- }
- else if (isdigit(arr_[i]))
- {
- std::cerr << "letter+digit\n";
- exit(EXIT_FAILURE);
- }
- }
- else if (t == NUMBER)
- {
- if (isdigit(arr_[i]))
- {
- i++;
- continue;
- }
- else if (isspace(arr_[i]))
- {
- if (arr_[i - 1] == dot)
- {
- save(iter, 0, i - 1, t);
- save(iter, i - 1, i, PUNCTUATION);
- }
- else
- {
- save(iter, 0, i, t);
- }
- i = 0;
- t = UNDEFINED;
- numberWithDot = false;
- continue;
- }
- else if ((arr_[i] == dot) && (numberWithDot == false))
- {
- numberWithDot = true;
- }
- else if ((ispunct(arr_[i])) && (arr_[i] != '-'))
- {
- if (arr_[i - 1] == dot)
- {
- std::cerr << "wrong combination of number with dot and punctuation\n";
- exit(EXIT_FAILURE);
- }
- else
- {
- save(iter, 0, i, NUMBER);
- if (arr_[i] == ',')
- {
- save(iter, i, i + 1, COMMA);
- }
- else
- {
- save(iter, i, i + 1, PUNCTUATION);
- }
- i = 0;
- t = UNDEFINED;
- numberWithDot = false;
- continue;
- }
- }
- else if (arr_[i] == '-')
- {
- if (i != 1)
- {
- save(iter, 0, i, NUMBER);
- }
- t = DASH;
- }
- else if (isalpha(arr_[i]))
- {
- std::cerr << "letter+digit\n";
- exit(EXIT_FAILURE);
- }
- }
- else if (t == DASH)
- {
- if (arr_[i - 2] == '-')
- {
- if (arr_[i] == '-')
- {
- if ((iter->t == PUNCTUATION) || (iter->t == UNDEFINED))
- {
- std::cerr << "punctuation error\n";
- exit(EXIT_FAILURE);
- }
- else
- {
- save(iter, i - 2, i + 1, DASH);
- i = 0;
- t = UNDEFINED;
- wordWithMinus = false;
- numberWithDot = false;
- continue;
- }
- }
- }
- else
- {
- std::cerr << "--\n";
- exit(EXIT_FAILURE);
- }
- if (arr_[i] != '-')
- {
- std::cerr << "wrong dash\n";
- exit(EXIT_FAILURE);
- }
- }
- else if (t == UNDEFINED)
- {
- if (isspace(arr_[i]))
- {
- continue;
- }
- else if (isalpha(arr_[i]))
- {
- t = WORD;
- }
- else if (isdigit(arr_[i]) || (arr_[i] == '+') || (arr_[i] == '-'))
- {
- t = NUMBER;
- }
- else if ((ispunct(arr_[i])) && (arr_[i] != '-'))
- {
- if ((iter->t == UNDEFINED) || (iter->t == PUNCTUATION) || (iter->t == COMMA) || (iter->t == DASH))
- {
- std::cerr << "punctuation error\n";
- exit(EXIT_FAILURE);
- }
- else if (arr_[i] == ',')
- {
- save(iter, 0, 1, COMMA);
- }
- else
- {
- save(iter, 0, 1, PUNCTUATION);
- }
- continue;
- }
- }
- i++;
- }
- if (i > 0)
- {
- save(iter, 0, i, t);
- }
- while (elems_ != 0)
- {
- print();
- }
- std::cout << std::endl;
- }
- void parser::print()
- {
- pList iter = head_;
- std::string temp = iter->str; //current word + punctuations
- elems_--;
- if (iter->next->t == COMMA)
- {
- temp += ',';
- iter = iter->next;
- iter->t = UNDEFINED;
- elems_--;
- if (iter->next->t == DASH)
- {
- temp += " ---";
- iter = iter->next;
- iter->t = UNDEFINED;
- elems_--;
- }
- }
- else if (iter->next->t == DASH)
- {
- temp += " ---";
- iter = iter->next;
- iter->t = UNDEFINED;
- elems_--;
- }
- else if (iter->next->t == PUNCTUATION)
- {
- temp += iter->next->str;
- iter = iter->next;
- iter->t = UNDEFINED;
- elems_--;
- }
- if (!first_)
- {
- sumSize_++;
- }
- sumSize_ += temp.size();
- if (sumSize_ > width_)
- {
- std::cout << std::endl;
- sumSize_ = temp.size();
- }
- else if (!first_)
- {
- std::cout << ' ';
- }
- std::cout << temp;
- first_ = false;
- head_ = iter->next;
- }
- void parser::save(pList & iter, int begin, int end, type_t t)
- {
- if ((end - begin) > 20)
- {
- std::cerr << "too large\n";
- exit(EXIT_FAILURE);
- }
- if ((iter->next == head_) && (writed_))
- {
- print();
- }
- iter = iter->next;
- std::string temp(arr_ + begin, arr_ + end);
- iter->t = t;
- iter->str = temp;
- writed_ = true;
- elems_++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement