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