OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Statement.h
Go to the documentation of this file.
1 #ifndef MAD_Statement_HH
2 #define MAD_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 "Parser/Token.h"
24 #include <iosfwd>
25 #include <list>
26 #include <string>
27 
28 #include "Utility/Inform.h"
29 
30 class Parser;
31 
32 
33 // class Statement
34 // ------------------------------------------------------------------------
36 // The statement is stored as a list of Token's.
37 
38 class Statement {
39 
40 public:
41 
43  typedef std::list<Token> TokenList;
44 
46  // Store the stream name and the line where the statement begins.
47  Statement(const std::string &name, int line);
48 
50  // Stores a name (e.g. for a macro) and the token list.
51  Statement(const std::string &name, TokenList &);
52 
53  virtual ~Statement();
54 
56  void append(const Token &);
57 
59  // This method is called by the parser in order to find out wether
60  // the end of the statement has been reached.
61  bool atEnd() const;
62 
64  // If the next item is a boolean literal:
65  // [ol][li]Set [b]value[/b] to its value.
66  // [li]Skip the value in the token list.
67  // [li]Return true.
68  // [/ol]
69  // Otherwise return false.
70  bool boolean(bool &value);
71 
73  // If the next item is the given character,
74  // skip it and return true, otherwise return false.
75  bool delimiter(char c);
76 
78  // If the next item is one of the characters in the given string,
79  // skip it and return true, otherwise return false.
80  bool delimiter(const char *s);
81 
83  // This method must be specially defined in conditional or loop
84  // statements. Normally it just calls the parser to execute the
85  // statement.
86  virtual void execute(const Parser &) = 0;
87 
89  Token &getCurrent();
90 
92  // If the next item is an integer:
93  // [ol][li]Set [b]value[/b] to its value.
94  // [li]Skip the value in the token list.
95  // [li]Return true.
96  // [/ol]
97  // Otherwise return false.
98  bool integer(int &value);
99 
101  // If the next item is an integer:
102  // [ol][li]Set [b]value[/b] to its value.
103  // [li]Skip the value in the token list.
104  // [li]Return true.
105  // [/ol]
106  // Otherwise return false.
107  bool integer(unsigned &value);
108 
110  // If the next item is the keyword [b]s[/b], skip it and return true,
111  // otherwise return false.
112  bool keyword(const char *s);
113 
115  // If the next item is a real number:
116  // [ol][li]Set [b]value[/b] to its value.
117  // [li]Skip the value in the token list.
118  // [li]Return true.
119  // [/ol]
120  // Otherwise return false.
121  bool real(double &value);
122 
124  // If the next item is a string literal:
125  // [ol][li]Set [b]value[/b] to its value.
126  // [li]Skip the value in the token list.
127  // [li]Return true.
128  // [/ol]
129  // Otherwise return false.
130  bool str(std::string &value);
131 
133  // If the next item is a word:
134  // [ol][li]Set [b]value[/b] to its value.
135  // [li]Skip the value in the token list.
136  // [li]Return true.
137  // [/ol]
138  // Otherwise return false.
139  bool word(std::string &value);
140 
142  // Parsing can later be resumed by calling restore().
143  void mark();
144 
146  // Resume parsing at the position wher mark() was last called.
147  void restore();
148 
150  // Resume parsing at the beginning of the statement.
151  void start();
152 
154  // Skip tokens up to next comma or end of statement, whichever comes first.
155  void skip();
156 
158  unsigned int position() const;
159 
161  // Print the statement on [b]os[/b].
162  virtual void print(std::ostream &os) const;
163 
165  // Print a message, containing the stream name and its line in the input
166  // stream. If [b]withToken[/b] is true, print also the last token parsed.
167  virtual void printWhere(Inform &msg, bool withToken) const;
168 
169  std::string str() const;
170 protected:
171 
172  // Line number where statement begins.
174 
175  // Input stream name.
176  std::string buffer_name;
177 
178  // Token list.
182 };
183 
184 
185 // Output operator.
186 inline std::ostream &operator<<(std::ostream &os, const Statement &statement) {
187  statement.print(os);
188  return os;
189 }
190 
191 inline Inform &operator<<(Inform &os, const Statement &statement) {
192  std::ostringstream msg;
193  statement.print(msg);
194  os << msg.str();
195 
196  return os;
197 }
198 
199 #endif // MAD_Statement_HH
std::ostream & operator<<(std::ostream &os, const Statement &statement)
Definition: Statement.h:186
const std::string name
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:51
std::string::iterator iterator
Definition: MSLang.h:16
Interface for abstract language parser.
Definition: Parser.h:31
Interface for statements.
Definition: Statement.h:38
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
virtual void execute(const Parser &)=0
Execute.
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