summaryrefslogtreecommitdiffstats
path: root/dev/a4-dance/simple_parser.cc
blob: 53eca6a8c4f0892a4d4fe7eb5d059f78f1c9ba73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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);
}