diff options
author | Matt Strapp <matt@mattstrapp.net> | 2021-11-01 14:39:34 -0500 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2021-11-01 14:39:34 -0500 |
commit | 36b8bde22e15e7a8608bd8920b4d6d8edf78af18 (patch) | |
tree | 1c022ce8d1854c6120ed492eb0bcad2e016e0f1f /dev/a4-dance/simple_parser.cc | |
parent | do a3 (diff) | |
parent | Update a4_dance.md (diff) | |
download | csci4611-36b8bde22e15e7a8608bd8920b4d6d8edf78af18.tar csci4611-36b8bde22e15e7a8608bd8920b4d6d8edf78af18.tar.gz csci4611-36b8bde22e15e7a8608bd8920b4d6d8edf78af18.tar.bz2 csci4611-36b8bde22e15e7a8608bd8920b4d6d8edf78af18.tar.lz csci4611-36b8bde22e15e7a8608bd8920b4d6d8edf78af18.tar.xz csci4611-36b8bde22e15e7a8608bd8920b4d6d8edf78af18.tar.zst csci4611-36b8bde22e15e7a8608bd8920b4d6d8edf78af18.zip |
Merge branch 'support-code' of https://github.umn.edu/umn-csci-4611-f21/shared-upstream
Diffstat (limited to 'dev/a4-dance/simple_parser.cc')
-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); +} |