tokenIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2017-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "error.H"
30 #include "token.H"
31 #include "IOstreams.H"
32 
33 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 template<class OS>
38 static OS& printTokenInfo(OS& os, const token& tok)
39 {
40  os << "on line " << tok.lineNumber() << ": ";
41 
42  switch (tok.type())
43  {
44  case token::tokenType::UNDEFINED:
45  os << "undefined token";
46  break;
47 
48  case token::tokenType::BOOL:
49  os << "bool '" << (tok.boolToken() ? "true" : "false") << '\'';
50  break;
51 
52  case token::tokenType::FLAG:
53  os << "flag '" << int(tok.flagToken()) << '\'';
54  break;
55 
56  case token::tokenType::PUNCTUATION:
57  os << "punctuation '" << tok.pToken() << '\'';
58  break;
59 
60  case token::tokenType::INTEGER_32:
61  os << "int32 " << tok.int32Token();
62  break;
63 
64  case token::tokenType::INTEGER_64:
65  os << "int64 " << tok.int64Token();
66  break;
67 
68  case token::tokenType::UNSIGNED_INTEGER_32:
69  os << "uint32 " << tok.uint32Token();
70  break;
71 
72  case token::tokenType::UNSIGNED_INTEGER_64:
73  os << "uint64 " << tok.uint64Token();
74  break;
75 
76  case token::tokenType::FLOAT:
77  os << "float " << tok.floatToken();
78  break;
79 
80  case token::tokenType::DOUBLE:
81  os << "double " << tok.doubleToken();
82  break;
83 
84  case token::tokenType::WORD:
85  os << "word '" << tok.wordToken() << '\'';
86  break;
87 
88  case token::tokenType::DIRECTIVE:
89  os << "directive '" << tok.wordToken() << '\'';
90  break;
91 
92  case token::tokenType::STRING:
93  os << "string " << tok.stringToken();
94  break;
95 
96  case token::tokenType::EXPRESSION:
97  os << "expression " << tok.stringToken();
98  break;
99 
100  case token::tokenType::VARIABLE:
101  os << "variable " << tok.stringToken();
102  break;
103 
104  case token::tokenType::VERBATIM:
105  os << "verbatim " << tok.stringToken();
106  break;
107 
108  case token::tokenType::CHAR_DATA:
109  os << "char_data " << tok.stringToken();
110  break;
111 
112  case token::tokenType::COMPOUND:
113  {
114  if (tok.compoundToken().pending())
115  {
116  os << "pending ";
117  }
118  if (tok.compoundToken().moved())
119  {
120  os << "moved ";
121  }
122  os << "compound of type "
123  << tok.compoundToken().type();
124  }
125  break;
126 
127  case token::tokenType::ERROR:
128  os << "error";
129  break;
130 
131  default:
132  os << "unknown token type '" << int(tok.type()) << '\'';
133  break;
134  }
135 
136  return os;
137 }
138 } // End namespace Foam
139 
140 
141 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
142 
143 Foam::token::token(Istream& is)
144 :
145  token()
146 {
147  is.read(*this);
148 }
149 
150 
151 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
152 
153 bool Foam::token::read(Istream& is)
154 {
155  reset();
156  if (is.good())
157  {
158  is.read(*this);
159  }
160  return good();
161 }
162 
163 
164 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
165 
167 {
168  switch (tokType)
169  {
170  case token::tokenType::UNDEFINED: return "undefined";
171  case token::tokenType::BOOL: return "bool";
172  case token::tokenType::FLAG: return "flag";
173  case token::tokenType::PUNCTUATION: return "punctuation";
174 
175  case token::tokenType::INTEGER_32: return "int32";
176  case token::tokenType::INTEGER_64: return "int64";
177  case token::tokenType::UNSIGNED_INTEGER_32: return "uint32";
178  case token::tokenType::UNSIGNED_INTEGER_64: return "uint64";
179  case token::tokenType::FLOAT: return "float";
180  case token::tokenType::DOUBLE: return "double";
181  case token::tokenType::WORD: return "word";
182  case token::tokenType::DIRECTIVE: return "directive";
183  case token::tokenType::STRING: return "string";
184  case token::tokenType::EXPRESSION: return "expression";
185  case token::tokenType::VARIABLE: return "variable";
186  case token::tokenType::VERBATIM: return "verbatim";
187  case token::tokenType::CHAR_DATA: return "char_data";
188  case token::tokenType::COMPOUND: return "compound";
189  case token::tokenType::ERROR: return "error";
190 
191  default:
192  break;
193  }
195  return "unknown(" + std::to_string(int(tokType)) + ")";
196 }
197 
198 
199 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
200 
202 {
203  tok.reset();
204  return is.read(tok);
205 }
206 
207 
208 Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
209 {
210  switch (tok.type_)
211  {
212  case token::tokenType::UNDEFINED:
213  os << "UNDEFINED";
215  << "Undefined token" << endl;
216  break;
217 
218  case token::tokenType::FLAG:
219  // Swallow the flag
220  break;
221 
222  case token::tokenType::PUNCTUATION:
223  os << tok.data_.punctuationVal;
224  break;
225 
226  case token::tokenType::BOOL:
227  os << tok.data_.flagVal; // An int value
228  break;
229 
230  case token::tokenType::INTEGER_32:
231  os << tok.data_.int32Val;
232  break;
233 
234  case token::tokenType::INTEGER_64:
235  os << tok.data_.int64Val;
236  break;
237 
238  case token::tokenType::UNSIGNED_INTEGER_32:
239  os << tok.data_.uint32Val;
240  break;
241 
242  case token::tokenType::UNSIGNED_INTEGER_64:
243  os << tok.data_.uint64Val;
244  break;
245 
246  case token::tokenType::FLOAT:
247  os << tok.data_.floatVal;
248  break;
249 
250  case token::tokenType::DOUBLE:
251  os << tok.data_.doubleVal;
252  break;
253 
254  // Possibly different behaviour for serial/parallel streams:
255  // preserve types
256  case token::tokenType::DIRECTIVE:
257  case token::tokenType::EXPRESSION:
258  case token::tokenType::VARIABLE:
259  case token::tokenType::VERBATIM:
260  case token::tokenType::CHAR_DATA:
261  os.write(tok);
262  break;
263 
264  case token::tokenType::WORD:
265  os << *tok.data_.wordPtr;
266  break;
267 
268  case token::tokenType::STRING:
269  os << *tok.data_.stringPtr;
270  break;
271 
272  case token::tokenType::COMPOUND:
273  os << *tok.data_.compoundPtr;
274  break;
275 
276  case token::tokenType::ERROR:
277  os << "ERROR";
279  << "Error token" << endl;
280  break;
281 
282  default:
283  os << "UNKNOWN";
285  << "Unknown token" << endl;
286  }
287 
288  os.check(FUNCTION_NAME);
289  return os;
290 }
291 
293 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
294 {
295  return os << char(pt);
296 }
297 
300 {
301  return os << char(pt);
302 }
303 
304 
305 Foam::Ostream& Foam::operator<<(Ostream& os, const token::compound& ct)
306 {
307  os << ct.type();
308  if (!ct.pending())
309  {
310  // Do not write compound content if tagged as 'pending-read'
311  os << token::SPACE;
312  ct.write(os);
313  }
314 
315  return os;
316 }
317 
318 
319 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
320 
321 ostream& Foam::operator<<
322 (
323  ostream& os,
324  const InfoProxy<token>& iproxy
325 )
326 {
327  return printTokenInfo(os, *iproxy);
328 }
329 
330 
331 template<>
332 Foam::Ostream& Foam::operator<<
333 (
334  Ostream& os,
335  const InfoProxy<token>& iproxy
336 )
337 {
338  return printTokenInfo(os, *iproxy);
339 }
340 
341 
342 // ************************************************************************* //
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:584
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:676
constexpr token() noexcept
Default construct, initialized to an UNDEFINED token.
Definition: tokenI.H:160
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:1022
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds an item read from Istream.
Definition: token.H:69
tokenType
Enumeration defining the types of token.
Definition: token.H:80
bool moved() const noexcept
Get compound transferred status.
Definition: token.H:278
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:518
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:482
static OS & printTokenInfo(OS &os, const token &tok)
Definition: tokenIO.C:31
int32_t int32Token() const
Return int32 value, convert from other integer type or Error.
Definition: tokenI.H:739
float floatToken() const
Return float value.
Definition: tokenI.H:923
bool boolToken() const
Return boolean token value.
Definition: tokenI.H:608
word name() const
Return the name of the current token type.
Definition: tokenI.H:476
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
int flagToken() const
Return flag bitmask value.
Definition: tokenI.H:638
label lineNumber() const noexcept
The line number for the token.
Definition: tokenI.H:570
virtual Istream & read(token &)=0
Return next token from stream.
double doubleToken() const
Return double value.
Definition: tokenI.H:950
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
Space [isspace].
Definition: token.H:144
punctuationToken
Standard punctuation tokens (a character)
Definition: token.H:139
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
const compound & compoundToken() const
Const reference to compound token. Fatal if the wrong type.
Definition: tokenI.H:1124
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
#define FUNCTION_NAME
uint32_t uint32Token() const
Return int32 value, convert from other integer type or Error.
Definition: tokenI.H:798
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:1076
int64_t int64Token() const
Return int64 value, convert from other integer type or Error.
Definition: tokenI.H:767
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.H:412
bool read(Istream &is)
Read a token from Istream, calls reset() first.
Definition: tokenIO.C:146
#define WarningInFunction
Report a warning using Foam::Warning.
uint64_t uint64Token() const
Return int64 value, convert from other integer type or Error.
Definition: tokenI.H:829
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
bool pending() const noexcept
Get compound pending-read status.
Definition: token.H:291
Namespace for OpenFOAM.