diff options
author | KT <tran0563@umn.edu> | 2021-09-06 19:07:33 -0500 |
---|---|---|
committer | KT <tran0563@umn.edu> | 2021-09-06 19:07:33 -0500 |
commit | cccd3186305915d92b1751dc616979d64116a4aa (patch) | |
tree | 5dd4834daef547cd45fc0b643f44a10b581de0ad /dev/a4-dance/simple_parser.cc | |
parent | Added missing images for the A6 worksheet (diff) | |
download | csci4611-cccd3186305915d92b1751dc616979d64116a4aa.tar csci4611-cccd3186305915d92b1751dc616979d64116a4aa.tar.gz csci4611-cccd3186305915d92b1751dc616979d64116a4aa.tar.bz2 csci4611-cccd3186305915d92b1751dc616979d64116a4aa.tar.lz csci4611-cccd3186305915d92b1751dc616979d64116a4aa.tar.xz csci4611-cccd3186305915d92b1751dc616979d64116a4aa.tar.zst csci4611-cccd3186305915d92b1751dc616979d64116a4aa.zip |
Upload a1
Diffstat (limited to 'dev/a4-dance/simple_parser.cc')
-rw-r--r-- | dev/a4-dance/simple_parser.cc | 153 |
1 files changed, 0 insertions, 153 deletions
diff --git a/dev/a4-dance/simple_parser.cc b/dev/a4-dance/simple_parser.cc deleted file mode 100644 index 53eca6a..0000000 --- a/dev/a4-dance/simple_parser.cc +++ /dev/null @@ -1,153 +0,0 @@ -#include "simple_parser.h" -#include <cctype> -#include <sstream> - -SimpleParser::SimpleParser(std::istream *i) { - in_ = i; -} - -bool SimpleParser::Expect(std::string s) { - SwallowWhitespace(); - if (s.size() == 0) { - return true; - } - std::string swallowed; - char c; - in_->get(c); - swallowed += c; - while ((*in_) && swallowed.size() < s.size() && swallowed == s.substr(0, swallowed.size())) { - in_->get(c); - swallowed += c; - } - if (swallowed == s) { - return true; - } - else { - std::string::reverse_iterator it; - for (it = swallowed.rbegin(); it != swallowed.rend(); ++it) { - in_->putback(*it); - } - return false; - } -} - -bool SimpleParser::Peek(std::string s) { - SwallowWhitespace(); - bool retval = false; - if (s.size() == 0) { - return true; - } - std::string swallowed; - char c; - in_->get(c); - swallowed += c; - while ((*in_) && swallowed.size() < s.size() && swallowed == s.substr(0, swallowed.size())) { - in_->get(c); - swallowed += c; - } - if (swallowed == s) { - retval = true; - } - else { - retval = false; - } - std::string::reverse_iterator it; - for (it = swallowed.rbegin(); it != swallowed.rend(); ++it) { - in_->putback(*it); - } - return retval; -} - -void SimpleParser::SwallowWhitespace() { - char c; - in_->get(c); - while ((*in_) && std::isspace(c)) { - in_->get(c); - } - in_->putback(c); -} - -void SimpleParser::SwallowLine() { - std::string placeholder; - std::getline(*in_, placeholder); - SwallowWhitespace(); -} - -bool SimpleParser::ReadFloat(float &f) { - SwallowWhitespace(); - std::string accum; - char c; - in_->get(c); - while ((*in_) && FloatChar(c)) { - accum += c; - in_->get(c); - } - in_->putback(c); - std::stringstream ss(accum); - ss >> f; - return true; -} - -bool SimpleParser::ReadInt(int &i) { - SwallowWhitespace(); - std::string accum; - char c; - in_->get(c); - while ((*in_) && IntChar(c)) { - accum += c; - in_->get(c); - } - in_->putback(c); - std::stringstream ss(accum); - ss >> i; - return true; - -} - -bool SimpleParser::ReadToken(std::string &s) { - SwallowWhitespace(); - std::string accum; - char c; - in_->get(c); - while ((*in_) && TokenChar(c)) { - accum += c; - in_->get(c); - } - in_->putback(c); - s = accum; - return true; -} - -bool SimpleParser::Good() { - return (*in_).good(); -} - -bool SimpleParser::ReadLine(std::string &line) { - getline(*in_, line); - return true; -} - -bool SimpleParser::UpcomingInt() { - SwallowWhitespace(); - char c; - in_->get(c); - in_->putback(c); - return IntChar(c); -} - -bool SimpleParser::FloatChar(char c) { - return ( c == 'e' - || (c >= '0' && c <= '9') - || c == '.' - || c == '+' - || c == '-' - ); -} - -bool SimpleParser::IntChar(char c) { - return ( (c >= '0' && c <= '9') || c == '-'); -} - -bool SimpleParser::TokenChar(char c) { - return !std::isspace(c); -} |