diff options
author | Jessie Speert <speer034@umn.edu> | 2021-03-08 19:09:55 -0800 |
---|---|---|
committer | Jessie Speert <speer034@umn.edu> | 2021-03-08 19:09:55 -0800 |
commit | f30fcc505dc3ce2df25dce16b329c90c32049862 (patch) | |
tree | 88e51f09844d0df1627643ccc3a1335d5fd74cbf /dev/a4-dance/simple_parser.cc | |
parent | Added Assignment 3 worksheet and code (diff) | |
download | csci4611-f30fcc505dc3ce2df25dce16b329c90c32049862.tar csci4611-f30fcc505dc3ce2df25dce16b329c90c32049862.tar.gz csci4611-f30fcc505dc3ce2df25dce16b329c90c32049862.tar.bz2 csci4611-f30fcc505dc3ce2df25dce16b329c90c32049862.tar.lz csci4611-f30fcc505dc3ce2df25dce16b329c90c32049862.tar.xz csci4611-f30fcc505dc3ce2df25dce16b329c90c32049862.tar.zst csci4611-f30fcc505dc3ce2df25dce16b329c90c32049862.zip |
Uploading Assignment 4
Diffstat (limited to '')
-rw-r--r-- | dev/a4-dance/simple_parser.cc | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/dev/a4-dance/simple_parser.cc b/dev/a4-dance/simple_parser.cc new file mode 100644 index 0000000..53eca6a --- /dev/null +++ b/dev/a4-dance/simple_parser.cc @@ -0,0 +1,153 @@ +#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); +} |