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