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::LABEL:
61  os << "label " << tok.labelToken();
62  break;
63 
64  case token::tokenType::FLOAT:
65  os << "float " << tok.floatToken();
66  break;
67 
68  case token::tokenType::DOUBLE:
69  os << "double " << tok.doubleToken();
70  break;
71 
72  case token::tokenType::WORD:
73  os << "word '" << tok.wordToken() << '\'';
74  break;
75 
76  case token::tokenType::DIRECTIVE:
77  os << "directive '" << tok.wordToken() << '\'';
78  break;
79 
80  case token::tokenType::STRING:
81  os << "string " << tok.stringToken();
82  break;
83 
84  case token::tokenType::EXPRESSION:
85  os << "expression " << tok.stringToken();
86  break;
87 
88  case token::tokenType::VARIABLE:
89  os << "variable " << tok.stringToken();
90  break;
91 
92  case token::tokenType::VERBATIM:
93  os << "verbatim " << tok.stringToken();
94  break;
95 
96  case token::tokenType::CHAR_DATA:
97  os << "char_data " << tok.stringToken();
98  break;
99 
100  case token::tokenType::COMPOUND:
101  {
102  if (tok.compoundToken().pending())
103  {
104  os << "pending ";
105  }
106  if (tok.compoundToken().moved())
107  {
108  os << "moved ";
109  }
110  os << "compound of type "
111  << tok.compoundToken().type();
112  }
113  break;
114 
115  case token::tokenType::ERROR:
116  os << "error";
117  break;
118 
119  default:
120  os << "unknown token type '" << int(tok.type()) << '\'';
121  break;
122  }
123 
124  return os;
125 }
126 } // End namespace Foam
127 
128 
129 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
130 
131 Foam::token::token(Istream& is)
132 :
133  token()
134 {
135  is.read(*this);
136 }
137 
138 
139 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
140 
142 {
143  switch (tokType)
144  {
145  case token::tokenType::UNDEFINED: return "undefined";
146  case token::tokenType::BOOL: return "bool";
147  case token::tokenType::FLAG: return "flag";
148  case token::tokenType::PUNCTUATION: return "punctuation";
149  case token::tokenType::LABEL: return "label";
150  case token::tokenType::FLOAT: return "float";
151  case token::tokenType::DOUBLE: return "double";
152  case token::tokenType::WORD: return "word";
153  case token::tokenType::DIRECTIVE: return "directive";
154  case token::tokenType::STRING: return "string";
155  case token::tokenType::EXPRESSION: return "expression";
156  case token::tokenType::VARIABLE: return "variable";
157  case token::tokenType::VERBATIM: return "verbatim";
158  case token::tokenType::CHAR_DATA: return "char_data";
159  case token::tokenType::COMPOUND: return "compound";
160  case token::tokenType::ERROR: return "error";
161 
162  default:
163  break;
164  }
166  return "unknown(" + std::to_string(int(tokType)) + ")";
167 }
168 
169 
170 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
171 
173 {
174  tok.reset();
175  return is.read(tok);
176 }
177 
178 
179 Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
180 {
181  switch (tok.type_)
182  {
183  case token::tokenType::UNDEFINED:
184  os << "UNDEFINED";
186  << "Undefined token" << endl;
187  break;
188 
189  case token::tokenType::FLAG:
190  // Swallow the flag
191  break;
192 
193  case token::tokenType::PUNCTUATION:
194  os << tok.data_.punctuationVal;
195  break;
196 
197  case token::tokenType::BOOL:
198  case token::tokenType::LABEL:
199  os << tok.data_.labelVal;
200  break;
201 
202  case token::tokenType::FLOAT:
203  os << tok.data_.floatVal;
204  break;
205 
206  case token::tokenType::DOUBLE:
207  os << tok.data_.doubleVal;
208  break;
209 
210  // Possibly different behaviour for serial/parallel streams:
211  // preserve types
212  case token::tokenType::DIRECTIVE:
213  case token::tokenType::EXPRESSION:
214  case token::tokenType::VARIABLE:
215  case token::tokenType::VERBATIM:
216  case token::tokenType::CHAR_DATA:
217  os.write(tok);
218  break;
219 
220  case token::tokenType::WORD:
221  os << *tok.data_.wordPtr;
222  break;
223 
224  case token::tokenType::STRING:
225  os << *tok.data_.stringPtr;
226  break;
227 
228  case token::tokenType::COMPOUND:
229  os << *tok.data_.compoundPtr;
230  break;
231 
232  case token::tokenType::ERROR:
233  os << "ERROR";
235  << "Error token" << endl;
236  break;
237 
238  default:
239  os << "UNKNOWN";
241  << "Unknown token" << endl;
242  }
243 
245  return os;
246 }
247 
249 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
250 {
251  return os << char(pt);
252 }
253 
256 {
257  return os << char(pt);
258 }
259 
260 
261 Foam::Ostream& Foam::operator<<(Ostream& os, const token::compound& ct)
262 {
263  os << ct.type();
264  if (!ct.pending())
265  {
266  // Do not write compound content if tagged as 'pending-read'
267  os << token::SPACE;
268  ct.write(os);
269  }
270 
271  return os;
272 }
273 
274 
275 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
276 
277 ostream& Foam::operator<<
278 (
279  ostream& os,
280  const InfoProxy<token>& iproxy
281 )
282 {
283  return printTokenInfo(os, *iproxy);
284 }
285 
286 
287 template<>
288 Foam::Ostream& Foam::operator<<
289 (
290  Ostream& os,
291  const InfoProxy<token>& iproxy
292 )
293 {
294  return printTokenInfo(os, *iproxy);
295 }
296 
297 
298 // ************************************************************************* //
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:729
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:264
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:633
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:651
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:831
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:783
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:615
bool pending() const noexcept
Get compound pending-read status.
Definition: token.H:277
Namespace for OpenFOAM.