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::INT32:
61  // os << "int32 " << tok.int32Token();
62  // break;
63  //
64  // case token::tokenType::INT64:
65  // os << "int64 " << tok.int64Token();
66  // break;
67 
68  case token::tokenType::LABEL:
69  os << "label " << tok.labelToken();
70  break;
71 
72  case token::tokenType::FLOAT:
73  os << "float " << tok.floatToken();
74  break;
75 
76  case token::tokenType::DOUBLE:
77  os << "double " << tok.doubleToken();
78  break;
79 
80  case token::tokenType::WORD:
81  os << "word '" << tok.wordToken() << '\'';
82  break;
83 
84  case token::tokenType::DIRECTIVE:
85  os << "directive '" << tok.wordToken() << '\'';
86  break;
87 
88  case token::tokenType::STRING:
89  os << "string " << tok.stringToken();
90  break;
91 
92  case token::tokenType::EXPRESSION:
93  os << "expression " << tok.stringToken();
94  break;
95 
96  case token::tokenType::VARIABLE:
97  os << "variable " << tok.stringToken();
98  break;
99 
100  case token::tokenType::VERBATIM:
101  os << "verbatim " << tok.stringToken();
102  break;
103 
104  case token::tokenType::CHAR_DATA:
105  os << "char_data " << tok.stringToken();
106  break;
107 
108  case token::tokenType::COMPOUND:
109  {
110  if (tok.compoundToken().pending())
111  {
112  os << "pending ";
113  }
114  if (tok.compoundToken().moved())
115  {
116  os << "moved ";
117  }
118  os << "compound of type "
119  << tok.compoundToken().type();
120  }
121  break;
122 
123  case token::tokenType::ERROR:
124  os << "error";
125  break;
126 
127  default:
128  os << "unknown token type '" << int(tok.type()) << '\'';
129  break;
130  }
131 
132  return os;
133 }
134 } // End namespace Foam
135 
136 
137 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
138 
139 Foam::token::token(Istream& is)
140 :
141  token()
142 {
143  is.read(*this);
144 }
145 
146 
147 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
148 
149 bool Foam::token::read(Istream& is)
150 {
151  reset();
152  if (is.good())
153  {
154  is.read(*this);
155  }
156  return good();
157 }
158 
159 
160 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
161 
163 {
164  switch (tokType)
165  {
166  case token::tokenType::UNDEFINED: return "undefined";
167  case token::tokenType::BOOL: return "bool";
168  case token::tokenType::FLAG: return "flag";
169  case token::tokenType::PUNCTUATION: return "punctuation";
170 
171  // case token::tokenType::INT32: return "int32";
172  // case token::tokenType::INT64: return "int64";
173  case token::tokenType::LABEL: return "label";
174  case token::tokenType::FLOAT: return "float";
175  case token::tokenType::DOUBLE: return "double";
176  case token::tokenType::WORD: return "word";
177  case token::tokenType::DIRECTIVE: return "directive";
178  case token::tokenType::STRING: return "string";
179  case token::tokenType::EXPRESSION: return "expression";
180  case token::tokenType::VARIABLE: return "variable";
181  case token::tokenType::VERBATIM: return "verbatim";
182  case token::tokenType::CHAR_DATA: return "char_data";
183  case token::tokenType::COMPOUND: return "compound";
184  case token::tokenType::ERROR: return "error";
185 
186  default:
187  break;
188  }
190  return "unknown(" + std::to_string(int(tokType)) + ")";
191 }
192 
193 
194 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
195 
197 {
198  tok.reset();
199  return is.read(tok);
200 }
201 
202 
203 Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
204 {
205  switch (tok.type_)
206  {
207  case token::tokenType::UNDEFINED:
208  os << "UNDEFINED";
210  << "Undefined token" << endl;
211  break;
212 
213  case token::tokenType::FLAG:
214  // Swallow the flag
215  break;
216 
217  case token::tokenType::PUNCTUATION:
218  os << tok.data_.punctuationVal;
219  break;
220 
221  case token::tokenType::BOOL:
222  case token::tokenType::LABEL:
223  os << tok.data_.labelVal;
224  break;
225 
226  case token::tokenType::FLOAT:
227  os << tok.data_.floatVal;
228  break;
229 
230  case token::tokenType::DOUBLE:
231  os << tok.data_.doubleVal;
232  break;
233 
234  // Possibly different behaviour for serial/parallel streams:
235  // preserve types
236  case token::tokenType::DIRECTIVE:
237  case token::tokenType::EXPRESSION:
238  case token::tokenType::VARIABLE:
239  case token::tokenType::VERBATIM:
240  case token::tokenType::CHAR_DATA:
241  os.write(tok);
242  break;
243 
244  case token::tokenType::WORD:
245  os << *tok.data_.wordPtr;
246  break;
247 
248  case token::tokenType::STRING:
249  os << *tok.data_.stringPtr;
250  break;
251 
252  case token::tokenType::COMPOUND:
253  os << *tok.data_.compoundPtr;
254  break;
255 
256  case token::tokenType::ERROR:
257  os << "ERROR";
259  << "Error token" << endl;
260  break;
261 
262  default:
263  os << "UNKNOWN";
265  << "Unknown token" << endl;
266  }
267 
269  return os;
270 }
271 
273 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
274 {
275  return os << char(pt);
276 }
277 
280 {
281  return os << char(pt);
282 }
283 
284 
285 Foam::Ostream& Foam::operator<<(Ostream& os, const token::compound& ct)
286 {
287  os << ct.type();
288  if (!ct.pending())
289  {
290  // Do not write compound content if tagged as 'pending-read'
291  os << token::SPACE;
292  ct.write(os);
293  }
294 
295  return os;
296 }
297 
298 
299 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
300 
301 ostream& Foam::operator<<
302 (
303  ostream& os,
304  const InfoProxy<token>& iproxy
305 )
306 {
307  return printTokenInfo(os, *iproxy);
308 }
309 
310 
311 template<>
312 Foam::Ostream& Foam::operator<<
313 (
314  Ostream& os,
315  const InfoProxy<token>& iproxy
316 )
317 {
318  return printTokenInfo(os, *iproxy);
319 }
320 
321 
322 // ************************************************************************* //
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition: tokenI.H:507
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:587
constexpr token() noexcept
Default construct, initialized to an UNDEFINED token.
Definition: tokenI.H:113
virtual Ostream & write(const char c) override
Write character.
Definition: OBJstream.C:69
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:747
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:65
tokenType
Enumeration defining the types of token.
Definition: token.H:76
bool moved() const noexcept
Get compound transferred status.
Definition: token.H:265
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:529
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:405
static OS & printTokenInfo(OS &os, const token &tok)
Definition: tokenIO.C:31
float floatToken() const
Return float value.
Definition: tokenI.H:651
bool boolToken() const
Return boolean token value.
Definition: tokenI.H:531
word name() const
Return the name of the current token type.
Definition: tokenI.H:399
#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:549
label lineNumber() const noexcept
The line number for the token.
Definition: tokenI.H:493
virtual Istream & read(token &)=0
Return next token from stream.
double doubleToken() const
Return double value.
Definition: tokenI.H:669
A class for handling words, derived from Foam::string.
Definition: word.H:63
Istream & operator>>(Istream &, directionInfo &)
Space [isspace].
Definition: token.H:131
punctuationToken
Standard punctuation tokens (a character)
Definition: token.H:126
const compound & compoundToken() const
Const reference to compound token. Fatal if the wrong type.
Definition: tokenI.H:849
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:801
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:335
bool read(Istream &is)
Read a token from Istream, calls reset() first.
Definition: tokenIO.C:142
#define WarningInFunction
Report a warning using Foam::Warning.
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
label labelToken() const
Return label value.
Definition: tokenI.H:633
bool pending() const noexcept
Get compound pending-read status.
Definition: token.H:278
Namespace for OpenFOAM.