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 -------------------------------------------------------------------------------
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 "scalar.H"
32 #include "IOstreams.H"
33 
34 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 template<class OS>
39 static OS& printTokenInfo(OS& os, const token& tok)
40 {
41  os << "on line " << tok.lineNumber() << ": ";
42 
43  switch (tok.type())
44  {
45  case token::tokenType::UNDEFINED:
46  os << "undefined token";
47  break;
48 
49  case token::tokenType::BOOL:
50  os << "bool '" << (tok.boolToken() ? "true" : "false") << '\'';
51  break;
52 
53  case token::tokenType::FLAG:
54  os << "flag '" << int(tok.flagToken()) << '\'';
55  break;
56 
57  case token::tokenType::PUNCTUATION:
58  os << "punctuation '" << tok.pToken() << '\'';
59  break;
60 
61  case token::tokenType::LABEL:
62  os << "label " << tok.labelToken();
63  break;
64 
65  case token::tokenType::FLOAT:
66  os << "float " << tok.floatToken();
67  break;
68 
69  case token::tokenType::DOUBLE:
70  os << "double " << tok.doubleToken();
71  break;
72 
73  case token::tokenType::WORD:
74  os << "word '" << tok.wordToken() << '\'';
75  break;
76 
77  case token::tokenType::DIRECTIVE:
78  os << "directive '" << tok.wordToken() << '\'';
79  break;
80 
81  case token::tokenType::STRING:
82  os << "string " << tok.stringToken();
83  break;
84 
85  case token::tokenType::EXPRESSION:
86  os << "expression " << tok.stringToken();
87  break;
88 
89  case token::tokenType::VARIABLE:
90  os << "variable " << tok.stringToken();
91  break;
92 
93  case token::tokenType::VERBATIM:
94  os << "verbatim " << tok.stringToken();
95  break;
96 
97  case token::tokenType::COMPOUND:
98  {
99  if (tok.compoundToken().moved())
100  {
101  os << "moved ";
102  }
103  os << "compound of type "
104  << tok.compoundToken().type();
105  }
106  break;
107 
108  case token::tokenType::ERROR:
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  {
175  case token::tokenType::UNDEFINED:
176  os << "UNDEFINED";
178  << "Undefined token" << endl;
179  break;
180 
181  case token::tokenType::FLAG:
182  // Swallow the flag
183  break;
184 
185  case token::tokenType::PUNCTUATION:
186  os << tok.data_.punctuationVal;
187  break;
188 
189  case token::tokenType::BOOL:
190  case token::tokenType::LABEL:
191  os << tok.data_.labelVal;
192  break;
193 
194  case token::tokenType::FLOAT:
195  os << tok.data_.floatVal;
196  break;
197 
198  case token::tokenType::DOUBLE:
199  os << tok.data_.doubleVal;
200  break;
201 
202  // Possibly different behaviour for serial/parallel streams:
203  // preserve types
204  case token::tokenType::DIRECTIVE:
205  case token::tokenType::EXPRESSION:
206  case token::tokenType::VARIABLE:
207  case token::tokenType::VERBATIMSTRING:
208  os.write(tok);
209  break;
210 
211  case token::tokenType::WORD:
212  os << *tok.data_.wordPtr;
213  break;
214 
215  case token::tokenType::STRING:
216  os << *tok.data_.stringPtr;
217  break;
218 
219  case token::tokenType::COMPOUND:
220  os << *tok.data_.compoundPtr;
221  break;
222 
223  case token::tokenType::ERROR:
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 
240 ostream& 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 
263 ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
264 {
265  return printTokenInfo(os, ip.t_);
266 }
267 
268 
269 template<>
270 Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<token>& ip)
271 {
272  return printTokenInfo(os, ip.t_);
273 }
274 
275 
276 // ************************************************************************* //
token.H
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::token::compound
Abstract base class for complex tokens.
Definition: token.H:168
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::InfoProxy
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:47
Foam::token::lineNumber
label lineNumber() const noexcept
The line number for the token.
Definition: tokenI.H:391
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:689
Foam::printTokenInfo
static OS & printTokenInfo(OS &os, const token &tok)
Definition: tokenIO.C:39
Foam::token::boolToken
bool boolToken() const
Return boolean token value.
Definition: tokenI.H:429
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::token::compound::write
virtual void write(Ostream &os) const =0
Redirect write to underlying content.
Foam::token::compoundToken
const compound & compoundToken() const
Read access for compound token.
Definition: tokenI.H:722
Foam::OBJstream::write
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
Foam::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:485
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
error.H
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::token::reset
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.H:257
Foam::InfoProxy::t_
const T & t_
Definition: InfoProxy.H:55
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:306
Foam::token::compound::moved
bool moved() const noexcept
Get compound transferred status.
Definition: token.H:219
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
scalar.H
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::token::floatToken
floatScalar floatToken() const
Return float value.
Definition: tokenI.H:531
Foam::token::doubleToken
doubleScalar doubleToken() const
Return double value.
Definition: tokenI.H:549
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:631
Foam::token::type
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:311
Foam::token::name
word name() const
Return the name of the token type.
Definition: tokenIO.C:134
Foam::token::SPACE
Space [isspace].
Definition: token.H:125
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::token::token
constexpr token() noexcept
Default construct, initialized to an UNDEFINED token.
Definition: tokenI.H:97
Foam::token::flagToken
int flagToken() const
Return flag bitmask value.
Definition: tokenI.H:447
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::token::punctuationToken
punctuationToken
Standard punctuation tokens (a character)
Definition: token.H:120
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.