OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 <Ippl.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 protected:
170 
171  // Line number where statement begins.
173 
174  // Input stream name.
175  std::string buffer_name;
176 
177  // Token list.
181 };
182 
183 
184 // Output operator.
185 inline std::ostream &operator<<(std::ostream &os, const Statement &statement) {
186  statement.print(os);
187  return os;
188 }
189 
190 inline Inform &operator<<(Inform &os, const Statement &statement) {
191  std::ostringstream msg;
192  statement.print(msg);
193  os << msg.str();
194 
195  return os;
196 }
197 
198 #endif // MAD_Statement_HH
bool integer(int &value)
Return signed integer.
Definition: Statement.cpp:81
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:167
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
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
Interface for abstract language parser.
Definition: Parser.h:31
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
Interface for statements.
Definition: Statement.h:38
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
virtual void execute(const Parser &)=0
Execute.
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
std::string::iterator iterator
Definition: MSLang.h:16
bool delimiter(char c)
Test for delimiter.
Definition: Statement.cpp:103
Definition: Inform.h:41
unsigned int position() const
Return current character number in line.
Definition: Statement.cpp:192