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-2023 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 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
148 
150 {
151  switch (tokType)
152  {
153  case token::tokenType::UNDEFINED: return "undefined";
154  case token::tokenType::BOOL: return "bool";
155  case token::tokenType::FLAG: return "flag";
156  case token::tokenType::PUNCTUATION: return "punctuation";
157 
158  // case token::tokenType::INT32: return "int32";
159  // case token::tokenType::INT64: return "int64";
160  case token::tokenType::LABEL: return "label";
161  case token::tokenType::FLOAT: return "float";
162  case token::tokenType::DOUBLE: return "double";
163  case token::tokenType::WORD: return "word";
164  case token::tokenType::DIRECTIVE: return "directive";
165  case token::tokenType::STRING: return "string";
166  case token::tokenType::EXPRESSION: return "expression";
167  case token::tokenType::VARIABLE: return "variable";
168  case token::tokenType::VERBATIM: return "verbatim";
169  case token::tokenType::CHAR_DATA: return "char_data";
170  case token::tokenType::COMPOUND: return "compound";
171  case token::tokenType::ERROR: return "error";
172 
173  default:
174  break;
175  }
177  return "unknown(" + std::to_string(int(tokType)) + ")";
178 }
179 
180 
181 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
182 
184 {
185  tok.reset();
186  return is.read(tok);
187 }
188 
189 
190 Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
191 {
192  switch (tok.type_)
193  {
194  case token::tokenType::UNDEFINED:
195  os << "UNDEFINED";
197  << "Undefined token" << endl;
198  break;
199 
200  case token::tokenType::FLAG:
201  // Swallow the flag
202  break;
203 
204  case token::tokenType::PUNCTUATION:
205  os << tok.data_.punctuationVal;
206  break;
207 
208  case token::tokenType::BOOL:
209  case token::tokenType::LABEL:
210  os << tok.data_.labelVal;
211  break;
212 
213  case token::tokenType::FLOAT:
214  os << tok.data_.floatVal;
215  break;
216 
217  case token::tokenType::DOUBLE:
218  os << tok.data_.doubleVal;
219  break;
220 
221  // Possibly different behaviour for serial/parallel streams:
222  // preserve types
223  case token::tokenType::DIRECTIVE:
224  case token::tokenType::EXPRESSION:
225  case token::tokenType::VARIABLE:
226  case token::tokenType::VERBATIM:
227  case token::tokenType::CHAR_DATA:
228  os.write(tok);
229  break;
230 
231  case token::tokenType::WORD:
232  os << *tok.data_.wordPtr;
233  break;
234 
235  case token::tokenType::STRING:
236  os << *tok.data_.stringPtr;
237  break;
238 
239  case token::tokenType::COMPOUND:
240  os << *tok.data_.compoundPtr;
241  break;
242 
243  case token::tokenType::ERROR:
244  os << "ERROR";
246  << "Error token" << endl;
247  break;
248 
249  default:
250  os << "UNKNOWN";
252  << "Unknown token" << endl;
253  }
254 
256  return os;
257 }
258 
260 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
261 {
262  return os << char(pt);
263 }
264 
267 {
268  return os << char(pt);
269 }
270 
271 
272 Foam::Ostream& Foam::operator<<(Ostream& os, const token::compound& ct)
273 {
274  os << ct.type();
275  if (!ct.pending())
276  {
277  // Do not write compound content if tagged as 'pending-read'
278  os << token::SPACE;
279  ct.write(os);
280  }
281 
282  return os;
283 }
284 
285 
286 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
287 
288 ostream& Foam::operator<<
289 (
290  ostream& os,
291  const InfoProxy<token>& iproxy
292 )
293 {
294  return printTokenInfo(os, *iproxy);
295 }
296 
297 
298 template<>
299 Foam::Ostream& Foam::operator<<
300 (
301  Ostream& os,
302  const InfoProxy<token>& iproxy
303 )
304 {
305  return printTokenInfo(os, *iproxy);
306 }
307 
308 
309 // ************************************************************************* //
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:531
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
#define WarningInFunction
Report a warning using Foam::Warning.
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.