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-2018 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_SCALAR:
66  os << "float " << tok.floatScalarToken();
67  break;
68 
69  case token::tokenType::DOUBLE_SCALAR:
70  os << "double " << tok.doubleScalarToken();
71  break;
72 
73  case token::tokenType::WORD:
74  os << "word '" << tok.wordToken() << '\'';
75  break;
76 
77  case token::tokenType::STRING:
78  os << "string " << tok.stringToken();
79  break;
80 
81  case token::tokenType::VARIABLE:
82  os << "variable " << tok.stringToken();
83  break;
84 
85  case token::tokenType::VERBATIMSTRING:
86  os << "verbatim string " << tok.stringToken();
87  break;
88 
89  case token::tokenType::COMPOUND:
90  {
91  if (tok.compoundToken().empty())
92  {
93  os << "empty ";
94  }
95  os << "compound of type "
96  << tok.compoundToken().type();
97  }
98  break;
99 
100  case token::tokenType::ERROR:
101  os << "error";
102  break;
103 
104  default:
105  os << "unknown token type '" << int(tok.type()) << '\'';
106  break;
107  }
108 
109  return os;
110 }
111 } // End namespace Foam
112 
113 
114 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
115 
117 :
118  token()
119 {
120  is.read(*this);
121 }
122 
123 
124 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
125 
127 {
128  switch (type_)
129  {
130  case token::tokenType::UNDEFINED: return "undefined";
131  case token::tokenType::BOOL: return "bool";
132  case token::tokenType::FLAG: return "flag";
133  case token::tokenType::PUNCTUATION: return "punctuation";
134  case token::tokenType::LABEL: return "label";
135  case token::tokenType::FLOAT_SCALAR: return "float";
136  case token::tokenType::DOUBLE_SCALAR: return "double";
137  case token::tokenType::WORD: return "word";
138  case token::tokenType::STRING: return "string";
139  case token::tokenType::VERBATIMSTRING: return "verbatim";
140  case token::tokenType::VARIABLE: return "variable";
141  case token::tokenType::COMPOUND: return "compound";
142  case token::tokenType::ERROR: return "error";
143 
144  default:
145  break;
146  }
147 
148  return "unknown(" + std::to_string(int(type_)) + ")";
149 }
150 
151 
152 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
153 
155 {
156  tok.reset();
157  return is.read(tok);
158 }
159 
160 
162 {
163  switch (tok.type_)
164  {
165  case token::tokenType::UNDEFINED:
166  os << "UNDEFINED";
168  << "Undefined token" << endl;
169  break;
170 
171  case token::tokenType::FLAG:
172  // Swallow the flag
173  break;
174 
175  case token::tokenType::PUNCTUATION:
176  os << tok.data_.punctuationVal;
177  break;
178 
179  case token::tokenType::BOOL:
180  case token::tokenType::LABEL:
181  os << tok.data_.labelVal;
182  break;
183 
184  case token::tokenType::FLOAT_SCALAR:
185  os << tok.data_.floatVal;
186  break;
187 
188  case token::tokenType::DOUBLE_SCALAR:
189  os << tok.data_.doubleVal;
190  break;
191 
192  case token::tokenType::WORD:
193  os << *tok.data_.wordPtr;
194  break;
195 
196  case token::tokenType::STRING:
197  case token::tokenType::VERBATIMSTRING:
198  os << *tok.data_.stringPtr;
199  break;
200 
201  case token::tokenType::VARIABLE:
202  // Behaviour differs according to stream type
203  os.write(tok);
204  break;
205 
206  case token::tokenType::COMPOUND:
207  os << *tok.data_.compoundPtr;
208  break;
209 
210  case token::tokenType::ERROR:
211  os << "ERROR";
213  << "Error token" << endl;
214  break;
215 
216  default:
217  os << "UNKNOWN";
219  << "Unknown token" << endl;
220  }
221 
222  os.check(FUNCTION_NAME);
223  return os;
224 }
225 
226 
227 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
228 {
229  return os << char(pt);
230 }
231 
232 
234 {
235  return os << char(pt);
236 }
237 
238 
240 {
241  os << ct.type() << token::SPACE;
242  ct.write(os);
243 
244  return os;
245 }
246 
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 
250 ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
251 {
252  return printTokenInfo(os, ip.t_);
253 }
254 
255 
256 template<>
257 Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<token>& ip)
258 {
259  return printTokenInfo(os, ip.t_);
260 }
261 
262 
263 // ************************************************************************* //
token.H
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:456
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:143
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::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:599
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:392
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:228
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
Foam::token::compound::write
virtual void write(Ostream &os) const =0
Foam::token::compoundToken
const compound & compoundToken() const
Read access for compound token.
Definition: tokenI.H:627
Foam::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:428
Foam::token::compound::empty
bool empty() const
Definition: token.H:195
Foam::token::floatScalarToken
floatScalar floatScalarToken() const
Return float value.
Definition: tokenI.H:474
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:243
Foam::InfoProxy::t_
const T & t_
Definition: InfoProxy.H:55
SeriousErrorInFunction
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Definition: messageStream.H:272
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::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:558
Foam::token::type
tokenType type() const
Return the token type.
Definition: tokenI.H:295
Foam::token::name
word name() const
Return the name of the token type.
Definition: tokenIO.C:126
Foam::token::SPACE
Space [isspace].
Definition: token.H:112
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
Foam::token::doubleScalarToken
doubleScalar doubleScalarToken() const
Return double value.
Definition: tokenI.H:492
Foam::token::token
constexpr token() noexcept
Construct null, initialized to an UNDEFINED token.
Definition: tokenI.H:95
Foam::token::flagToken
int flagToken() const
Return flag bitmask value.
Definition: tokenI.H:410
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::token::lineNumber
label lineNumber() const
The line number for the token.
Definition: tokenI.H:356
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
Foam::token::punctuationToken
punctuationToken
Standard punctuation tokens (a character)
Definition: token.H:109
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.