OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
Statement.h
Go to the documentation of this file.
1#ifndef OPALX_Statement_HH
2#define OPALX_Statement_HH 1
3
4// ------------------------------------------------------------------------
5// $RCSfile: Statement.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.1.1.1.2.1 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Class: Statement
13//
14// ------------------------------------------------------------------------
15// Class category: Parser
16// ------------------------------------------------------------------------
17//
18// $Date: 2002/10/05 00:48:29 $
19// $Author: dbruhwil $
20//
21// ------------------------------------------------------------------------
22
23#include <iosfwd>
24#include <list>
25#include <string>
26#include "OpalParser/Token.h"
27
28#include "Utility/Inform.h"
29
30class Parser;
31
32// class Statement
33// ------------------------------------------------------------------------
35// The statement is stored as a list of Token's.
36
37class Statement {
38public:
40 typedef std::list<Token> TokenList;
41
43 // Store the stream name and the line where the statement begins.
44 Statement(const std::string& name, int line);
45
47 // Stores a name (e.g. for a macro) and the token list.
48 Statement(const std::string& name, TokenList&);
49
50 virtual ~Statement();
51
53 void append(const Token&);
54
56 // This method is called by the parser in order to find out wether
57 // the end of the statement has been reached.
58 bool atEnd() const;
59
61 // If the next item is a boolean literal:
62 // [ol][li]Set [b]value[/b] to its value.
63 // [li]Skip the value in the token list.
64 // [li]Return true.
65 // [/ol]
66 // Otherwise return false.
67 bool boolean(bool& value);
68
70 // If the next item is the given character,
71 // skip it and return true, otherwise return false.
72 bool delimiter(char c);
73
75 // If the next item is one of the characters in the given string,
76 // skip it and return true, otherwise return false.
77 bool delimiter(const char* s);
78
80 // This method must be specially defined in conditional or loop
81 // statements. Normally it just calls the parser to execute the
82 // statement.
83 virtual void execute(const Parser&) = 0;
84
87
89 // If the next item is an integer:
90 // [ol][li]Set [b]value[/b] to its value.
91 // [li]Skip the value in the token list.
92 // [li]Return true.
93 // [/ol]
94 // Otherwise return false.
95 bool integer(int& value);
96
98 // If the next item is an integer:
99 // [ol][li]Set [b]value[/b] to its value.
100 // [li]Skip the value in the token list.
101 // [li]Return true.
102 // [/ol]
103 // Otherwise return false.
104 bool integer(unsigned& value);
105
107 // If the next item is the keyword [b]s[/b], skip it and return true,
108 // otherwise return false.
109 bool keyword(const char* s);
110
112 // If the next item is a real number:
113 // [ol][li]Set [b]value[/b] to its value.
114 // [li]Skip the value in the token list.
115 // [li]Return true.
116 // [/ol]
117 // Otherwise return false.
118 bool real(double& value);
119
121 // If the next item is a string literal:
122 // [ol][li]Set [b]value[/b] to its value.
123 // [li]Skip the value in the token list.
124 // [li]Return true.
125 // [/ol]
126 // Otherwise return false.
127 bool str(std::string& value);
128
130 // If the next item is a word:
131 // [ol][li]Set [b]value[/b] to its value.
132 // [li]Skip the value in the token list.
133 // [li]Return true.
134 // [/ol]
135 // Otherwise return false.
136 bool word(std::string& value);
137
139 // Parsing can later be resumed by calling restore().
140 void mark();
141
143 // Resume parsing at the position wher mark() was last called.
144 void restore();
145
147 // Resume parsing at the beginning of the statement.
148 void start();
149
151 // Skip tokens up to next comma or end of statement, whichever comes first.
152 void skip();
153
155 unsigned int position() const;
156
158 // Print the statement on [b]os[/b].
159 virtual void print(std::ostream& os) const;
160
162 // Print a message, containing the stream name and its line in the input
163 // stream. If [b]withToken[/b] is true, print also the last token parsed.
164 virtual void printWhere(Inform& msg, bool withToken) const;
165
166 std::string str() const;
167
168protected:
169 // Line number where statement begins.
171
172 // Input stream name.
173 std::string buffer_name;
174
175 // Token list.
177 TokenList::iterator curr;
178 TokenList::iterator keep;
179};
180
181// Output operator.
182inline std::ostream& operator<<(std::ostream& os, const Statement& statement) {
183 statement.print(os);
184 return os;
185}
186
187inline Inform& operator<<(Inform& os, const Statement& statement) {
188 std::ostringstream msg;
189 statement.print(msg);
190 os << msg.str();
191
192 return os;
193}
194
195#endif // OPALX_Statement_HH
std::ostream & operator<<(std::ostream &os, const Statement &statement)
Definition Statement.h:182
Interface for abstract language parser.
Definition Parser.h:30
Interface for statements.
Definition Statement.h:37
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.
virtual void execute(const Parser &)=0
Execute.
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