OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Statement.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: Statement.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: Statement
10 // An abstract base class for all input language statements.
11 //
12 // ------------------------------------------------------------------------
13 // Class category: Parser
14 // ------------------------------------------------------------------------
15 //
16 // $Date: 2000/03/27 09:32:37 $
17 // $Author: fci $
18 //
19 // ------------------------------------------------------------------------
20 
21 #include "Parser/Statement.h"
22 #include "Parser/Token.h"
23 #include <iostream>
24 
25 extern Inform *gmsg;
26 
27 // Class Statement
28 // ------------------------------------------------------------------------
29 
30 Statement::Statement(const std::string &name, int line):
31  stat_line(line), buffer_name(name), tokens()
32 {}
33 
34 
35 Statement::Statement(const std::string &name, TokenList &list):
36  stat_line(1), buffer_name(name), tokens() {
37  tokens.swap(list);
38  curr = tokens.begin();
39 }
40 
41 
43  tokens.erase(tokens.begin(), tokens.end());
44 }
45 
46 
47 void Statement::append(const Token &token) {
48  tokens.push_back(token);
49 }
50 
51 
52 bool Statement::atEnd() const {
53  return TokenList::const_iterator(curr) == tokens.end();
54 }
55 
56 
57 bool Statement::boolean(bool &value) {
58  if(curr != tokens.end() && curr->isWord()) {
59  std::string word = curr->getWord();
60 
61  if(word == "TRUE") {
62  value = true;
63  ++curr;
64  return true;
65  } else if(word == "FALSE") {
66  value = false;
67  ++curr;
68  return true;
69  }
70  }
71 
72  return false;
73 }
74 
75 
77  return *(curr++);
78 }
79 
80 
81 bool Statement::integer(int &value) {
82  if(curr != tokens.end() && curr->isInteger()) {
83  value = curr->getInteger();
84  ++curr;
85  return true;
86  } else {
87  return false;
88  }
89 }
90 
91 
92 bool Statement::integer(unsigned &value) {
93  if(curr != tokens.end() && curr->isInteger()) {
94  value = curr->getInteger();
95  ++curr;
96  return true;
97  } else {
98  return false;
99  }
100 }
101 
102 
104  if(curr != tokens.end() && (*curr).isDel(c)) {
105  ++curr;
106  return true;
107  } else {
108  return false;
109  }
110 }
111 
112 
113 bool Statement::delimiter(const char *s) {
114  if(curr != tokens.end() && (*curr).isDel(s)) {
115  ++curr;
116  return true;
117  } else {
118  return false;
119  }
120 }
121 
122 
123 bool Statement::keyword(const char *key) {
124  if(curr != tokens.end() && (*curr).isKey(key)) {
125  ++curr;
126  return true;
127  } else {
128  return false;
129  }
130 }
131 
132 
133 bool Statement::real(double &value) {
134  if(curr != tokens.end()) {
135  if(curr->isReal()) {
136  value = curr->getReal();
137  ++curr;
138  return true;
139  } else if(curr->isInteger()) {
140  value = double(curr->getInteger());
141  ++curr;
142  return true;
143  }
144  }
145 
146  return false;
147 }
148 
149 
150 bool Statement::str(std::string &value) {
151  if(curr != tokens.end() && curr->isString()) {
152  value = curr->getLex();
153  ++curr;
154  return true;
155  } else {
156  return false;
157  }
158 }
159 
160 
161 bool Statement::word(std::string &value) {
162  if(curr != tokens.end() && curr->isWord()) {
163  value = curr->getLex();
164  ++curr;
165  return true;
166  } else {
167  return false;
168  }
169 }
170 
171 
173  keep = curr;
174 }
175 
176 
178  curr = keep;
179 }
180 
181 
183  curr = tokens.begin();
184 }
185 
186 
188  while(! atEnd() && !(*curr).isDel(','))
189  curr++;
190 }
191 
192 unsigned int Statement::position() const {
193  std::ostringstream os;
194  bool white = false;
195 
196  for(TokenList::const_iterator c = tokens.begin(); c != curr; ++c) {
197  if(white && !c->isDel()) os << ' ';
198  white = !c->isDel();
199  os << *c;
200  }
201  if(white && !std::next(curr)->isDel()) os << ' ';
202 
203  return os.str().length() - 1;
204 }
205 
206 void Statement::print(std::ostream &msg) const {
207  bool white = false;
208 
209  for(TokenList::const_iterator c = tokens.begin(); c != tokens.end(); ++c) {
210  if(white && !c->isDel()) msg << ' ';
211  white = !c->isDel();
212  msg << *c;
213  }
214 
215  msg << ';' << std::endl;
216 }
217 
218 
219 void Statement::printWhere(Inform &msg, bool withToken) const {
220 
221  msg << "*** in line " << stat_line << " of file \"" << buffer_name << "\"";
222 
223  if(withToken) {
224  if(TokenList::const_iterator(curr) == tokens.end()) {
225  msg << " at end of statement:" << endl;
226  } else {
227  msg << " before token \"" << *curr << "\":" << endl;
228  }
229  } else {
230  msg << ":\n";
231  }
232 }
bool integer(int &value)
Return signed integer.
Definition: Statement.cpp:81
Statement(const std::string &name, int line)
Constructor.
Definition: Statement.cpp:30
virtual ~Statement()
Definition: Statement.cpp:42
TokenList::iterator keep
Definition: Statement.h:180
bool keyword(const char *s)
Test for keyword.
Definition: Statement.cpp:123
TokenList tokens
Definition: Statement.h:178
void mark()
Mark position in command.
Definition: Statement.cpp:172
bool atEnd() const
Test for end of command.
Definition: Statement.cpp:52
Inform * gmsg
Definition: Main.cpp:21
void skip()
Skip.
Definition: Statement.cpp:187
void restore()
Return to marked position.
Definition: Statement.cpp:177
bool real(double &value)
Return real value.
Definition: Statement.cpp:133
bool word(std::string &value)
Return word value.
Definition: Statement.cpp:161
std::list< Token > TokenList
The type of the enclosed token list.
Definition: Statement.h:43
bool boolean(bool &value)
Return boolean value.
Definition: Statement.cpp:57
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
int stat_line
Definition: Statement.h:172
Representation of a single input token.
Definition: Token.h:33
virtual void printWhere(Inform &msg, bool withToken) const
Print position.
Definition: Statement.cpp:219
void start()
Return to start.
Definition: Statement.cpp:182
virtual void print(std::ostream &os) const
Print statement.
Definition: Statement.cpp:206
void append(const Token &)
Append a token.
Definition: Statement.cpp:47
TokenList::iterator curr
Definition: Statement.h:179
const std::string name
bool str(std::string &value)
Return string value.
Definition: Statement.cpp:150
Token & getCurrent()
Return current token and skip it.
Definition: Statement.cpp:76
std::string buffer_name
Definition: Statement.h:175
bool delimiter(char c)
Test for delimiter.
Definition: Statement.cpp:103
Definition: Inform.h:41
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
unsigned int position() const
Return current character number in line.
Definition: Statement.cpp:192