summaryrefslogtreecommitdiffstats
path: root/dev/a4-dance/simple_parser.cc
diff options
context:
space:
mode:
authorKT <tran0563@umn.edu>2021-10-19 14:03:28 -0500
committerKT <tran0563@umn.edu>2021-10-19 14:03:28 -0500
commit12b0b74d43b7d93eee82ae6fa3dced499cd8a1f6 (patch)
tree8e89330e51de3aa45ad3fafc57dc14eb5e1786e2 /dev/a4-dance/simple_parser.cc
parentPublish a3 (diff)
downloadcsci4611-12b0b74d43b7d93eee82ae6fa3dced499cd8a1f6.tar
csci4611-12b0b74d43b7d93eee82ae6fa3dced499cd8a1f6.tar.gz
csci4611-12b0b74d43b7d93eee82ae6fa3dced499cd8a1f6.tar.bz2
csci4611-12b0b74d43b7d93eee82ae6fa3dced499cd8a1f6.tar.lz
csci4611-12b0b74d43b7d93eee82ae6fa3dced499cd8a1f6.tar.xz
csci4611-12b0b74d43b7d93eee82ae6fa3dced499cd8a1f6.tar.zst
csci4611-12b0b74d43b7d93eee82ae6fa3dced499cd8a1f6.zip
Publish a4
Diffstat (limited to '')
-rw-r--r--dev/a4-dance/simple_parser.cc153
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);
+}