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-2020 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::VARIABLE:
86  os << "variable " << tok.stringToken();
87  break;
88 
89  case token::tokenType::VERBATIM:
90  os << "verbatim " << tok.stringToken();
91  break;
92 
93  case token::tokenType::COMPOUND:
94  {
95  if (tok.compoundToken().empty())
96  {
97  os << "empty ";
98  }
99  os << "compound of type "
100  << tok.compoundToken().type();
101  }
102  break;
103 
104  case token::tokenType::ERROR:
105  os << "error";
106  break;
107 
108  default:
109  os << "unknown token type '" << int(tok.type()) << '\'';
110  break;
111  }
112 
113  return os;
114 }
115 } // End namespace Foam
116 
117 
118 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
119 
121 :
122  token()
123 {
124  is.read(*this);
125 }
126 
127 
128 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 
131 {
132  switch (type_)
133  {
134  case token::tokenType::UNDEFINED: return "undefined";
135  case token::tokenType::BOOL: return "bool";
136  case token::tokenType::FLAG: return "flag";
137  case token::tokenType::PUNCTUATION: return "punctuation";
138  case token::tokenType::LABEL: return "label";
139  case token::tokenType::FLOAT: return "float";
140  case token::tokenType::DOUBLE: return "double";
141  case token::tokenType::WORD: return "word";
142  case token::tokenType::DIRECTIVE: return "directive";
143  case token::tokenType::STRING: return "string";
144  case token::tokenType::VERBATIM: return "verbatim";
145  case token::tokenType::VARIABLE: return "variable";
146  case token::tokenType::COMPOUND: return "compound";
147  case token::tokenType::ERROR: return "error";
148 
149  default:
150  break;
151  }
152 
153  return "unknown(" + std::to_string(int(type_)) + ")";
154 }
155 
156 
157 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
158 
160 {
161  tok.reset();
162  return is.read(tok);
163 }
164 
165 
167 {
168  switch (tok.type_)
169  {
170  case token::tokenType::UNDEFINED:
171  os << "UNDEFINED";
173  << "Undefined token" << endl;
174  break;
175 
176  case token::tokenType::FLAG:
177  // Swallow the flag
178  break;
179 
180  case token::tokenType::PUNCTUATION:
181  os << tok.data_.punctuationVal;
182  break;
183 
184  case token::tokenType::BOOL:
185  case token::tokenType::LABEL:
186  os << tok.data_.labelVal;
187  break;
188 
189  case token::tokenType::FLOAT:
190  os << tok.data_.floatVal;
191  break;
192 
193  case token::tokenType::DOUBLE:
194  os << tok.data_.doubleVal;
195  break;
196 
197  // Different behaviour for (serial/parallel) streams: preserve types
198  case token::tokenType::DIRECTIVE:
199  case token::tokenType::VARIABLE:
200  case token::tokenType::VERBATIMSTRING:
201  os.write(tok);
202  break;
203 
204  case token::tokenType::WORD:
205  os << *tok.data_.wordPtr;
206  break;
207 
208  case token::tokenType::STRING:
209  os << *tok.data_.stringPtr;
210  break;
211 
212  case token::tokenType::COMPOUND:
213  os << *tok.data_.compoundPtr;
214  break;
215 
216  case token::tokenType::ERROR:
217  os << "ERROR";
219  << "Error token" << endl;
220  break;
221 
222  default:
223  os << "UNKNOWN";
225  << "Unknown token" << endl;
226  }
227 
228  os.check(FUNCTION_NAME);
229  return os;
230 }
231 
232 
233 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
234 {
235  return os << char(pt);
236 }
237 
238 
240 {
241  return os << char(pt);
242 }
243 
244 
246 {
247  os << ct.type() << token::SPACE;
248  ct.write(os);
249 
250  return os;
251 }
252 
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
257 {
258  return printTokenInfo(os, ip.t_);
259 }
260 
261 
262 template<>
263 Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<token>& ip)
264 {
265  return printTokenInfo(os, ip.t_);
266 }
267 
268 
269 // ************************************************************************* //
token.H
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:487
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:148
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
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:387
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:644
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:423
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
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:676
Foam::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:459
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::token::compound::empty
bool empty() const noexcept
Has compound been transferred?
Definition: token.H:198
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:256
Foam::InfoProxy::t_
const T & t_
Definition: InfoProxy.H:55
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:281
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
scalar.H
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::token::floatToken
floatScalar floatToken() const
Return float value.
Definition: tokenI.H:505
Foam::token::doubleToken
doubleScalar doubleToken() const
Return double value.
Definition: tokenI.H:523
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:599
Foam::token::type
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:309
Foam::token::name
word name() const
Return the name of the token type.
Definition: tokenIO.C:130
Foam::token::SPACE
Space [isspace].
Definition: token.H:117
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
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:441
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:303
Foam::token::punctuationToken
punctuationToken
Standard punctuation tokens (a character)
Definition: token.H:114
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.