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-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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 "scalar.H"
32#include "IOstreams.H"
33
34// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
35
36namespace Foam
37{
38template<class OS>
39static OS& printTokenInfo(OS& os, const token& tok)
40{
41 os << "on line " << tok.lineNumber() << ": ";
42
43 switch (tok.type())
44 {
46 os << "undefined token";
47 break;
48
50 os << "bool '" << (tok.boolToken() ? "true" : "false") << '\'';
51 break;
52
54 os << "flag '" << int(tok.flagToken()) << '\'';
55 break;
56
58 os << "punctuation '" << tok.pToken() << '\'';
59 break;
60
62 os << "label " << tok.labelToken();
63 break;
64
66 os << "float " << tok.floatToken();
67 break;
68
70 os << "double " << tok.doubleToken();
71 break;
72
74 os << "word '" << tok.wordToken() << '\'';
75 break;
76
78 os << "directive '" << tok.wordToken() << '\'';
79 break;
80
82 os << "string " << tok.stringToken();
83 break;
84
86 os << "expression " << tok.stringToken();
87 break;
88
90 os << "variable " << tok.stringToken();
91 break;
92
94 os << "verbatim " << tok.stringToken();
95 break;
96
98 {
99 if (tok.compoundToken().moved())
100 {
101 os << "moved ";
102 }
103 os << "compound of type "
104 << tok.compoundToken().type();
105 }
106 break;
107
109 os << "error";
110 break;
111
112 default:
113 os << "unknown token type '" << int(tok.type()) << '\'';
114 break;
115 }
116
117 return os;
118}
119} // End namespace Foam
120
121
122// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
123
125:
126 token()
127{
128 is.read(*this);
129}
130
131
132// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133
135{
136 switch (type_)
137 {
138 case token::tokenType::UNDEFINED: return "undefined";
139 case token::tokenType::BOOL: return "bool";
140 case token::tokenType::FLAG: return "flag";
141 case token::tokenType::PUNCTUATION: return "punctuation";
142 case token::tokenType::LABEL: return "label";
143 case token::tokenType::FLOAT: return "float";
144 case token::tokenType::DOUBLE: return "double";
145 case token::tokenType::WORD: return "word";
146 case token::tokenType::DIRECTIVE: return "directive";
147 case token::tokenType::STRING: return "string";
148 case token::tokenType::EXPRESSION: return "expression";
149 case token::tokenType::VERBATIM: return "verbatim";
150 case token::tokenType::VARIABLE: return "variable";
151 case token::tokenType::COMPOUND: return "compound";
152 case token::tokenType::ERROR: return "error";
153
154 default:
155 break;
156 }
157
158 return "unknown(" + std::to_string(int(type_)) + ")";
159}
160
161
162// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
163
165{
166 tok.reset();
167 return is.read(tok);
168}
169
170
172{
173 switch (tok.type_)
174 {
176 os << "UNDEFINED";
178 << "Undefined token" << endl;
179 break;
180
182 // Swallow the flag
183 break;
184
186 os << tok.data_.punctuationVal;
187 break;
188
191 os << tok.data_.labelVal;
192 break;
193
195 os << tok.data_.floatVal;
196 break;
197
199 os << tok.data_.doubleVal;
200 break;
201
202 // Possibly different behaviour for serial/parallel streams:
203 // preserve types
208 os.write(tok);
209 break;
210
212 os << *tok.data_.wordPtr;
213 break;
214
216 os << *tok.data_.stringPtr;
217 break;
218
220 os << *tok.data_.compoundPtr;
221 break;
222
224 os << "ERROR";
226 << "Error token" << endl;
227 break;
228
229 default:
230 os << "UNKNOWN";
232 << "Unknown token" << endl;
233 }
234
236 return os;
237}
238
239
240ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
241{
242 return os << char(pt);
243}
244
245
247{
248 return os << char(pt);
249}
250
251
253{
254 os << ct.type() << token::SPACE;
255 ct.write(os);
256
257 return os;
258}
259
260
261// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
262
263ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
264{
265 return printTokenInfo(os, ip.t_);
266}
267
268
269template<>
271{
272 return printTokenInfo(os, ip.t_);
273}
274
275
276// ************************************************************************* //
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:58
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:52
const T & t_
Definition: InfoProxy.H:55
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
virtual Istream & read(token &)=0
Return next token from stream.
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Abstract base class for complex tokens.
Definition: token.H:171
bool moved() const noexcept
Get compound transferred status.
Definition: token.H:219
virtual void write(Ostream &os) const =0
Redirect write to underlying content.
A token holds an item read from Istream.
Definition: token.H:69
floatScalar floatToken() const
Return float value.
Definition: tokenI.H:531
@ LABEL
label (integer) type
Definition: token.H:85
@ ERROR
Token error encountered.
Definition: token.H:79
@ DOUBLE
double (double-precision) type
Definition: token.H:87
@ VARIABLE
Definition: token.H:98
@ FLAG
stream flag (1-byte bitmask)
Definition: token.H:82
@ WORD
Foam::word.
Definition: token.H:90
@ EXPRESSION
Definition: token.H:96
@ UNDEFINED
An undefined token-type.
Definition: token.H:78
@ COMPOUND
Compound type such as List<label> etc.
Definition: token.H:92
@ FLOAT
float (single-precision) type
Definition: token.H:86
@ VERBATIM
Definition: token.H:100
@ VERBATIMSTRING
Definition: token.H:106
@ DIRECTIVE
Definition: token.H:94
@ BOOL
boolean type
Definition: token.H:84
@ STRING
Foam::string (usually double-quoted)
Definition: token.H:91
@ PUNCTUATION
single character punctuation
Definition: token.H:83
label lineNumber() const noexcept
The line number for the token.
Definition: tokenI.H:391
word name() const
Return the name of the token type.
Definition: tokenIO.C:134
punctuationToken
Standard punctuation tokens (a character)
Definition: token.H:121
@ SPACE
Space [isspace].
Definition: token.H:125
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:689
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:485
label labelToken() const
Return label value.
Definition: tokenI.H:513
constexpr token() noexcept
Default construct, initialized to an UNDEFINED token.
Definition: tokenI.H:97
bool boolToken() const
Return boolean token value.
Definition: tokenI.H:429
const compound & compoundToken() const
Read access for compound token.
Definition: tokenI.H:722
int flagToken() const
Return flag bitmask value.
Definition: tokenI.H:447
doubleScalar doubleToken() const
Return double value.
Definition: tokenI.H:549
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:311
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.H:257
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:631
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
#define FUNCTION_NAME
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Namespace for OpenFOAM.
static OS & printTokenInfo(OS &os, const token &tok)
Definition: tokenIO.C:39
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
Istream & operator>>(Istream &, directionInfo &)