www.cemf.ir
iIstream.cpp
Go to the documentation of this file.
1 
2 #include "iIstream.hpp"
3 
4 
6 {
7  if (bad())
8  {
10  "stream is bad.\n";
11  fatalExit;
12  }
13  else if (putBack_)
14  {
16  "putBack is already full. \n";
17  fatalExit;
18  }
19  else
20  {
21  putBackToken_ = tok;
22  putBack_ = true;
23  }
24 }
25 
26 
28 {
29 
30  if (bad())
31  {
33  return false;
34  }
35  else if (putBack_)
36  {
37  tok = putBackToken_;
38  putBack_ = false;
39  return true;
40  }
41 
42  return false;
43 }
44 
45 
47 {
48  if (putBack_)
49  {
50  tok = putBackToken_;
51  }
52  else
53  {
54  tok.reset();
55  }
56 
57  return putBack_;
58 }
59 
61 {
62  rewind();
63  return findTokenResume(w);
64 }
65 
67 {
68  token next;
69  bool isFirstToken = true;
70 
71  while( !eof() && good() )
72  {
73  read(next);
74  if(next.error())
75  {
77  "error in reading stream " << name() <<
78  " at line number "<< lineNumber()<<endl;
79  return false;
80  }
81 
82  if( next.isWord() && isFirstToken)
83  {
84  if(next.wordToken() == w ) return true;
85  }
86 
87  if(next.isEndStatement()|| next.isEndBlock())
88  isFirstToken = true;
89  else
90  isFirstToken = false;
91  }
92 
93  return false;
94 }
95 
96 bool pFlow::iIstream::findTokenSilent( const word & w, int32 limitLine )
97 {
98  rewind();
99  return findTokenResumeSilent(w,limitLine);
100 }
101 
103 {
104  token next;
105  bool isFirstToken = true;
106 
107  while( !eof() && good() && lineNumber()<limitLine )
108  {
109  read(next);
110  if(next.error())
111  {
112  return false;
113  }
114 
115  if( next.isWord() && isFirstToken)
116  {
117  if(next.wordToken() == w) return true;
118  }
119 
120  if(next.isEndStatement()|| next.isEndBlock())
121  isFirstToken = true;
122  else
123  isFirstToken = false;
124  }
125 
126  return false;
127 }
128 
130 (
131  const word& w,
132  word& nextW,
133  bool checkEndStatement
134 )
135 {
136  if( findToken(w) )
137  {
138  // next token
139  token next(*this);
140 
141  if(next.error())
142  {
143  ioErrorInFile( name(), lineNumber());
144  return false;
145  }
146  if( next.isWord() )
147  {
148  nextW = next.wordToken();
149 
150  if( checkEndStatement )
151  {
152  read(next);
153 
154  if( !next.isEndStatement() )
155  {
156  ioErrorInFile(name(), lineNumber()) <<
157  " expected ; but found " << next;
158  return false;
159  }
160 
161  }
162 
163  return true;
164 
165  }else
166  {
167  ioErrorInFile( name(), lineNumber() ) <<
168  " token is not a word" << next <<endl;
169  return false;
170  }
171  }
172 
173  return false;
174 }
175 
176 bool pFlow::iIstream::findTokenAndNextSilent(const word& w, word& nextW, int32 limitLine)
177 {
178  if( findTokenSilent(w, limitLine) )
179  {
180  // next token
181  token next(*this);
182 
183  if(next.error())
184  {
185  return false;
186  }
187 
188  if( next.isWord() )
189  {
190  nextW = next.wordToken();
191 
192  read(next);
193 
194  if( !next.isEndStatement() )
195  {
196  return false;
197  }
198 
199  return true;
200 
201  }else
202  {
203  return false;
204  }
205  }
206 
207  return false;
208 }
209 
210 
211 bool pFlow::iIstream::readBegin(const char* funcName)
212 {
213 
214  const token delimiter(*this);
215 
216  if (delimiter != token::BEGIN_LIST)
217  {
218  setBad();
219 
220  ioErrorInFile( name(), lineNumber() )
221  << "Expected a '" << token::BEGIN_LIST
222  << "' while reading " << funcName
223  << ", found " << delimiter << endl;
224  fatalExit;
225  }
226 
227  return true;
228 }
229 
230 
231 bool pFlow::iIstream::readEnd(const char* funcName)
232 {
233  const token delimiter(*this);
234 
235  if (delimiter != token::END_LIST)
236  {
237  setBad();
238 
239  ioErrorInFile( name(), lineNumber())
240  << "Expected a '" << token::END_LIST
241  << "' while reading " << funcName
242  << ", found "
243  << delimiter <<endl;
244  fatalExit;
245  }
246 
247  return true;
248 }
249 
250 bool pFlow::iIstream::readBeginSquare(const char* funcName)
251 {
252  const token delimiter(*this);
253 
254  if (delimiter != token::BEGIN_SQR)
255  {
256  setBad();
257 
258  ioErrorInFile( name(), lineNumber())
259  << "Expected a '" << token::BEGIN_SQR
260  << "' while reading " << funcName
261  << ", found "
262  << delimiter <<endl;
263  fatalExit;
264  }
265 
266  return true;
267 }
268 
269 bool pFlow::iIstream::readEndSquare(const char* funcName)
270 {
271  const token delimiter(*this);
272 
273  if (delimiter != token::END_SQR)
274  {
275  setBad();
276 
277  ioErrorInFile( name(), lineNumber())
278  << "Expected a '" << token::END_SQR
279  << "' while reading " << funcName
280  << ", found "
281  << delimiter <<endl;
282  fatalExit;
283  }
284 
285  return true;
286 }
287 
288 char pFlow::iIstream::readBeginList(const char* funcName)
289 {
290  const token delimiter(*this);
291 
292  if (delimiter != token::BEGIN_LIST && delimiter != token::BEGIN_BLOCK)
293  {
294  setBad();
295  ioErrorInFile( name(), lineNumber() )
296  << "Expected a '" << token::BEGIN_LIST
297  << "' or a '" << token::BEGIN_BLOCK
298  << "' while reading " << funcName
299  << ", found " << delimiter<<endl;
300  fatalExit;
301 
302  return '\0';
303  }
304 
305  return delimiter.pToken();
306 }
307 
308 
309 char pFlow::iIstream::readEndList(const char* funcName)
310 {
311  const token delimiter(*this);
312 
313  if (delimiter != token::END_LIST && delimiter != token::END_BLOCK)
314  {
315  setBad();
316 
317  ioErrorInFile( name(), lineNumber() )
318  << "Expected a '" << token::END_LIST
319  << "' or a '" << token::END_BLOCK
320  << "' while reading " << funcName
321  << ", found " << delimiter
322  << " at stream position " << endl;
323  fatalExit;
324 
325  return '\0';
326  }
327 
328  return delimiter.pToken();
329 }
330 
331 
332 char pFlow::iIstream::readEndStatement(const char* funcName)
333 {
334  CONSUME_PARAM(funcName);
335  const token delimiter(*this);
336 
337  if (!delimiter.isEndStatement())
338  {
339  setBad();
340  ioErrorInFile( name(), lineNumber() ) <<
341  " expected ; but found " << delimiter << endl;
342  fatalExit;
343 
344  return '\0';
345  }
346 
347  return delimiter.pToken();
348 }
349 
351 {
352  if (!good())
353  {
354  check("iIstream::operator()");
355  fatalExit;
356  }
357 
358  return const_cast<iIstream&>(*this);
359 }
pFlow::iIstream::readBegin
bool readBegin(const char *funcName)
Definition: iIstream.cpp:211
pFlow::iIstream::findTokenAndNextSilent
virtual bool findTokenAndNextSilent(const word &w, word &nextW, int32 limitLine=100)
Definition: iIstream.cpp:176
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::token
Definition: token.hpp:42
pFlow::token::pToken
punctuationToken pToken() const
Definition: tokenI.hpp:452
iIstream.hpp
pFlow::token::isEndBlock
bool isEndBlock() const
Definition: tokenI.hpp:442
pFlow::token::END_BLOCK
@ END_BLOCK
End block [isseparator].
Definition: token.hpp:94
pFlow::token::error
bool error() const
Definition: tokenI.hpp:384
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::iIstream::readEnd
bool readEnd(const char *funcName)
Definition: iIstream.cpp:231
pFlow::iIstream::readEndStatement
char readEndStatement(const char *funcName)
Definition: iIstream.cpp:332
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:320
pFlow::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Begin block [isseparator].
Definition: token.hpp:93
pFlow::iIstream::findTokenSilent
virtual bool findTokenSilent(const word &w, int32 limitLine=100)
Definition: iIstream.cpp:96
pFlow::iIstream::findTokenResume
virtual bool findTokenResume(const word &w)
search for all tokesn after the current file position and find the first word token tbat matchs w
Definition: iIstream.cpp:66
pFlow::token::reset
void reset()
Definition: tokenI.hpp:245
pFlow::iIstream::findTokenResumeSilent
virtual bool findTokenResumeSilent(const word &w, int32 limitLine=100)
search for all tokesn after the current file position and find the first word token tbat matchs w
Definition: iIstream.cpp:102
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.hpp:202
pFlow::iIstream::getBack
bool getBack(token &tok)
Definition: iIstream.cpp:27
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::iIstream::readBeginList
char readBeginList(const char *funcName)
Definition: iIstream.cpp:288
pFlow::iIstream::putBack_
bool putBack_
Definition: iIstream.hpp:41
pFlow::iIstream::putBack
void putBack(const token &tok)
Definition: iIstream.cpp:5
pFlow::iIstream::findTokenAndNext
virtual bool findTokenAndNext(const word &w, word &nextW, bool checkEndStatement=true)
Definition: iIstream.cpp:130
pFlow::token::END_LIST
@ END_LIST
End list [isseparator].
Definition: token.hpp:90
pFlow::iIstream::operator()
iIstream & operator()() const
Definition: iIstream.cpp:350
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::iIstream::readBeginSquare
bool readBeginSquare(const char *funcName)
Definition: iIstream.cpp:250
pFlow::token::BEGIN_LIST
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.hpp:89
pFlow::token::BEGIN_SQR
@ BEGIN_SQR
Begin dimensions [isseparator].
Definition: token.hpp:91
pFlow::iIstream::putBackToken_
token putBackToken_
Definition: iIstream.hpp:44
pFlow::iIstream::readEndList
char readEndList(const char *funcName)
Definition: iIstream.cpp:309
CONSUME_PARAM
#define CONSUME_PARAM(x)
Definition: pFlowMacros.hpp:38
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:221
pFlow::iIstream::findToken
virtual bool findToken(const word &w)
Definition: iIstream.cpp:60
pFlow::token::isEndStatement
bool isEndStatement() const
Definition: tokenI.hpp:431
pFlow::token::END_SQR
@ END_SQR
End dimensions [isseparator].
Definition: token.hpp:92
pFlow::token::wordToken
const word & wordToken() const
Definition: tokenI.hpp:600
pFlow::token::isWord
bool isWord() const
Definition: tokenI.hpp:584
pFlow::iIstream::readEndSquare
bool readEndSquare(const char *funcName)
Definition: iIstream.cpp:269
pFlow::iIstream::peekBack
bool peekBack(token &tok)
Definition: iIstream.cpp:46