tokenIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include "token.H"
28 
29 #include "IOstreams.H"
30 #include "scalar.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
35 :
36  type_(UNDEFINED)
37 {
38  is.read(*this);
39 }
40 
41 
42 // * * * * * * * * * * * * IOstream operators * * * * * * * * * * * * * * * //
43 
45 {
46  t.clear();
47  return is.read(t);
48 }
49 
50 
52 {
53  switch (t.type_)
54  {
55  case token::UNDEFINED:
56  os << "UNDEFINED";
58  << "Undefined token" << endl;
59  break;
60 
61  case token::PUNCTUATION:
62  os << t.punctuationToken_;
63  break;
64 
65  case token::WORD:
66  os << *t.wordTokenPtr_;
67  break;
68 
70  os << *t.functionNameTokenPtr_;
71  break;
72 
73  case token::VARIABLE:
74  os << *t.variableTokenPtr_;
75  break;
76 
77  case token::STRING:
78  os << *t.stringTokenPtr_;
79  break;
80 
82  os << *t.verbatimStringTokenPtr_;
83  break;
84 
85  case token::INTEGER_32:
86  os << t.integer32Token_;
87  break;
88 
89  case token::INTEGER_64:
90  os << t.integer64Token_;
91  break;
92 
95  break;
96 
99  break;
100 
101  case token::FLOAT_SCALAR:
102  os << t.floatScalarToken_;
103  break;
104 
106  os << t.doubleScalarToken_;
107  break;
108 
110  os << *t.longDoubleScalarTokenPtr_;
111  break;
112 
113  case token::COMPOUND:
114  os << *t.compoundTokenPtr_;
115  break;
116 
117  case token::ERROR:
118  os << "ERROR";
120  << "Error token" << endl;
121  break;
122 
123  default:
124  os << "UNKNOWN";
126  << "Unknown token"
127  << endl;
128  }
129 
130  // Check state of stream
131  os.check("Ostream& operator<<(Ostream&, const token&)");
132 
133  return os;
134 }
135 
136 
137 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
138 {
139  return os << char(pt);
140 }
141 
142 
144 {
145  return os << char(pt);
146 }
147 
148 
150 {
151  os << ct.type() << token::SPACE;
152  ct.write(os);
153 
154  return os;
155 }
156 
157 
158 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
159 
160 ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
161 {
162  const token& t = ip.t_;
163 
164  os << "on line " << t.lineNumber();
165 
166  switch (t.type())
167  {
168  case token::UNDEFINED:
169  os << " an undefined token";
170  break;
171 
172  case token::PUNCTUATION:
173  os << " the punctuation token " << '\'' << t.pToken() << '\'';
174  break;
175 
176  case token::WORD:
177  os << " the word " << '\'' << t.wordToken() << '\'';
178  break;
179 
180  case token::STRING:
181  os << " the string " << t.stringToken();
182  break;
183 
185  os << " the verbatim string " << t.verbatimStringToken();
186  break;
187 
188  case token::FUNCTIONNAME:
189  os << " the functionName " << t.functionNameToken();
190  break;
191 
192  case token::VARIABLE:
193  os << " the variable " << t.variableToken();
194  break;
195 
196  case token::INTEGER_32:
197  os << " the 32-bit integer " << t.integer32Token();
198  break;
199 
200  case token::INTEGER_64:
201  os << " the 64-bit integer " << t.integer64Token();
202  break;
203 
205  os << " the unsigned 32-bit integer "
206  << t.unsignedInteger32Token();
207  break;
208 
210  os << " the unsigned 64-bit integer "
211  << t.unsignedInteger64Token();
212  break;
213 
214  case token::FLOAT_SCALAR:
215  os << " the floatScalar " << t.floatScalarToken();
216  break;
217 
219  os << " the doubleScalar " << t.doubleScalarToken();
220  break;
221 
223  os << " the longDoubleScalar " << t.longDoubleScalarToken();
224  break;
225 
226  case token::COMPOUND:
227  {
228  if (t.compoundToken().empty())
229  {
230  os << " the empty compound of type "
231  << t.compoundToken().type();
232  }
233  else
234  {
235  os << " the compound of type "
236  << t.compoundToken().type();
237  }
238  }
239  break;
240 
241  case token::ERROR:
242  os << " an error";
243  break;
244 
245  default:
246  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
247  }
248 
249  return os;
250 }
251 
252 
253 template<>
255 {
256  const token& t = ip.t_;
257 
258  os << "on line " << t.lineNumber();
259 
260  switch (t.type())
261  {
262  case token::UNDEFINED:
263  os << " an undefined token";
264  break;
265 
266  case token::PUNCTUATION:
267  os << " the punctuation token " << '\'' << t.pToken() << '\'';
268  break;
269 
270  case token::WORD:
271  os << " the word " << '\'' << t.wordToken() << '\'';
272  break;
273 
274  case token::STRING:
275  os << " the string " << t.stringToken();
276  break;
277 
279  os << " the verbatim string " << t.verbatimStringToken();
280  break;
281 
282  case token::FUNCTIONNAME:
283  os << " the functionName " << t.functionNameToken();
284  break;
285 
286  case token::VARIABLE:
287  os << " the variable " << t.variableToken();
288  break;
289 
290  break;
291  case token::INTEGER_32:
292  os << " the 32-bit integer " << t.integer32Token();
293  break;
294 
295  case token::INTEGER_64:
296  os << " the 64-bit integer " << t.integer64Token();
297  break;
298 
300  os << " the unsigned 32-bit integer "
301  << t.unsignedInteger32Token();
302  break;
303 
305  os << " the unsigned 64-bit integer "
306  << t.unsignedInteger64Token();
307  break;
308 
309  case token::FLOAT_SCALAR:
310  os << " the floatScalar " << t.floatScalarToken();
311  break;
312 
314  os << " the doubleScalar " << t.doubleScalarToken();
315  break;
316 
318  os << " the longDoubleScalar " << t.longDoubleScalarToken();
319  break;
320 
321  case token::COMPOUND:
322  {
323  if (t.compoundToken().empty())
324  {
325  os << " the empty compound of type "
326  << t.compoundToken().type();
327  }
328  else
329  {
330  os << " the compound of type "
331  << t.compoundToken().type();
332  }
333  }
334  break;
335 
336  case token::ERROR:
337  os << " an error";
338  break;
339 
340  default:
341  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
342  }
343 
344  return os;
345 }
346 
347 
348 // ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:50
const T & t_
Definition: InfoProxy.H:53
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
virtual Istream & read(token &)=0
Return next token from stream.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Abstract base class for complex tokens.
Definition: token.H:133
virtual void write(Ostream &) const =0
bool empty() const
Definition: token.H:183
A token holds items read from Istream.
Definition: token.H:73
longDoubleScalar longDoubleScalarToken() const
Definition: tokenI.H:702
const variable & variableToken() const
Definition: tokenI.H:339
punctuationToken punctuationToken_
Definition: token.H:256
@ ERROR
Definition: token.H:97
@ VARIABLE
Definition: token.H:85
@ WORD
Definition: token.H:83
@ UNSIGNED_INTEGER_32
Definition: token.H:90
@ UNDEFINED
Definition: token.H:80
@ COMPOUND
Definition: token.H:95
@ FLOAT_SCALAR
Definition: token.H:92
@ INTEGER_64
Definition: token.H:89
@ DOUBLE_SCALAR
Definition: token.H:93
@ LONG_DOUBLE_SCALAR
Definition: token.H:94
@ VERBATIMSTRING
Definition: token.H:87
@ FUNCTIONNAME
Definition: token.H:84
@ UNSIGNED_INTEGER_64
Definition: token.H:91
@ INTEGER_32
Definition: token.H:88
@ STRING
Definition: token.H:86
@ PUNCTUATION
Definition: token.H:82
verbatimString * verbatimStringTokenPtr_
Definition: token.H:261
int32_t integer32Token() const
Definition: tokenI.H:441
compound * compoundTokenPtr_
Definition: token.H:269
functionName * functionNameTokenPtr_
Definition: token.H:258
floatScalar floatScalarToken_
Definition: token.H:266
const functionName & functionNameToken() const
Definition: tokenI.H:321
variable * variableTokenPtr_
Definition: token.H:259
floatScalar floatScalarToken() const
Definition: tokenI.H:664
punctuationToken
Standard punctuation tokens.
Definition: token.H:102
const string & stringToken() const
Definition: tokenI.H:357
punctuationToken pToken() const
Definition: tokenI.H:285
uint32_t unsignedInteger32Token() const
Definition: tokenI.H:512
int64_t integer64Token() const
Definition: tokenI.H:475
string * stringTokenPtr_
Definition: token.H:260
tokenType type() const
Definition: tokenI.H:255
int64_t integer64Token_
Definition: token.H:263
int32_t integer32Token_
Definition: token.H:262
const compound & compoundToken() const
Definition: tokenI.H:789
uint64_t unsignedInteger64Token() const
Definition: tokenI.H:546
doubleScalar doubleScalarToken() const
Definition: tokenI.H:683
doubleScalar doubleScalarToken_
Definition: token.H:267
word * wordTokenPtr_
Definition: token.H:257
token()
Construct null.
Definition: tokenI.H:75
const word & wordToken() const
Definition: tokenI.H:303
const verbatimString & verbatimStringToken() const
Definition: tokenI.H:375
uint32_t unsignedInteger32Token_
Definition: token.H:264
uint64_t unsignedInteger64Token_
Definition: token.H:265
label lineNumber() const
Definition: tokenI.H:803
longDoubleScalar * longDoubleScalarTokenPtr_
Definition: token.H:268
#define WarningInFunction
Report a warning using Foam::Warning.
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
Istream & operator>>(Istream &, pistonPointEdgeData &)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)