tokenIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include "token.H"
28 
29 #include "IOstreams.H"
30 #include "scalar.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
35 :
36  type_(UNDEFINED)
37 {
38  is.read(*this);
39 }
40 
41 
42 // * * * * * * * * * * * * IOstream operators * * * * * * * * * * * * * * * //
43 
45 {
46  t.clear();
47  return is.read(t);
48 }
49 
50 
52 {
53  switch (t.type_)
54  {
55  case token::UNDEFINED:
56  os << "UNDEFINED";
58  << "Undefined token" << endl;
59  break;
60 
61  case token::PUNCTUATION:
62  os << t.punctuationToken_;
63  break;
64 
65  case token::WORD:
66  os << *t.wordTokenPtr_;
67  break;
68 
69  case token::STRING:
71  os << *t.stringTokenPtr_;
72  break;
73 
74  case token::VARIABLE:
75  // Behaviour differs according to stream type
76  os.write(t);
77  break;
78 
79  case token::LABEL:
80  os << t.labelToken_;
81  break;
82 
84  os << t.floatScalarToken_;
85  break;
86 
88  os << t.doubleScalarToken_;
89  break;
90 
93  break;
94 
95  case token::COMPOUND:
96  os << *t.compoundTokenPtr_;
97  break;
98 
99  case token::ERROR:
100  os << "ERROR";
102  << "Error token" << endl;
103  break;
104 
105  default:
106  os << "UNKNOWN";
108  << "Unknown token"
109  << endl;
110  }
111 
112  // Check state of stream
113  os.check("Ostream& operator<<(Ostream&, const token&)");
114 
115  return os;
116 }
117 
118 
119 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
120 {
121  return os << char(pt);
122 }
123 
124 
126 {
127  return os << char(pt);
128 }
129 
130 
132 {
133  os << ct.type() << token::SPACE;
134  ct.write(os);
135 
136  return os;
137 }
138 
139 
140 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
141 
142 ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
143 {
144  const token& t = ip.t_;
145 
146  os << "on line " << t.lineNumber();
147 
148  switch (t.type())
149  {
150  case token::UNDEFINED:
151  os << " an undefined token";
152  break;
153 
154  case token::PUNCTUATION:
155  os << " the punctuation token " << '\'' << t.pToken() << '\'';
156  break;
157 
158  case token::WORD:
159  os << " the word " << '\'' << t.wordToken() << '\'';
160  break;
161 
162  case token::STRING:
163  os << " the string " << t.stringToken();
164  break;
165 
166  case token::VARIABLE:
167  os << " the variable " << t.stringToken();
168  break;
169 
171  os << " the verbatim string " << t.stringToken();
172  break;
173 
174  case token::LABEL:
175  os << " the label " << t.labelToken();
176  break;
177 
178  case token::FLOAT_SCALAR:
179  os << " the floatScalar " << t.floatScalarToken();
180  break;
181 
183  os << " the doubleScalar " << t.doubleScalarToken();
184  break;
185 
187  os << " the longDoubleScalar " << t.longDoubleScalarToken();
188  break;
189 
190  case token::COMPOUND:
191  {
192  if (t.compoundToken().empty())
193  {
194  os << " the empty compound of type "
195  << t.compoundToken().type();
196  }
197  else
198  {
199  os << " the compound of type "
200  << t.compoundToken().type();
201  }
202  }
203  break;
204 
205  case token::ERROR:
206  os << " an error";
207  break;
208 
209  default:
210  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
211  }
212 
213  return os;
214 }
215 
216 
217 template<>
218 Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<token>& ip)
219 {
220  const token& t = ip.t_;
221 
222  os << "on line " << t.lineNumber();
223 
224  switch (t.type())
225  {
226  case token::UNDEFINED:
227  os << " an undefined token";
228  break;
229 
230  case token::PUNCTUATION:
231  os << " the punctuation token " << '\'' << t.pToken() << '\'';
232  break;
233 
234  case token::WORD:
235  os << " the word " << '\'' << t.wordToken() << '\'';
236  break;
237 
238  case token::STRING:
239  os << " the string " << t.stringToken();
240  break;
241 
242  case token::VARIABLE:
243  os << " the variable " << t.stringToken();
244  break;
245 
247  os << " the verbatim string " << t.stringToken();
248  break;
249 
250  case token::LABEL:
251  os << " the label " << t.labelToken();
252  break;
253 
254  case token::FLOAT_SCALAR:
255  os << " the floatScalar " << t.floatScalarToken();
256  break;
257 
259  os << " the doubleScalar " << t.doubleScalarToken();
260  break;
261 
263  os << " the longDoubleScalar " << t.longDoubleScalarToken();
264  break;
265 
266  case token::COMPOUND:
267  {
268  if (t.compoundToken().empty())
269  {
270  os << " the empty compound of type "
271  << t.compoundToken().type();
272  }
273  else
274  {
275  os << " the compound of type "
276  << t.compoundToken().type();
277  }
278  }
279  break;
280 
281  case token::ERROR:
282  os << " an error";
283  break;
284 
285  default:
286  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
287  }
288 
289  return os;
290 }
291 
292 
293 // ************************************************************************* //
doubleScalar doubleScalarToken_
Definition: token.H:260
punctuationToken pToken() const
Definition: tokenI.H:217
longDoubleScalar * longDoubleScalarTokenPtr_
Definition: token.H:261
compound * compoundTokenPtr_
Definition: token.H:262
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
label labelToken_
Definition: token.H:258
const word & wordToken() const
Definition: tokenI.H:235
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:69
string * stringTokenPtr_
Definition: token.H:257
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
word * wordTokenPtr_
Definition: token.H:256
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Abstract base class for complex tokens.
Definition: token.H:123
floatScalar floatScalarToken() const
Definition: tokenI.H:294
virtual Istream & read(token &)=0
Return next token from stream.
Istream & operator>>(Istream &, directionInfo &)
punctuationToken
Standard punctuation tokens.
Definition: token.H:94
label lineNumber() const
Definition: tokenI.H:418
const compound & compoundToken() const
Definition: tokenI.H:404
bool empty() const
Definition: token.H:184
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
virtual void write(Ostream &) const =0
const string & stringToken() const
Definition: tokenI.H:258
longDoubleScalar longDoubleScalarToken() const
Definition: tokenI.H:332
floatScalar floatScalarToken_
Definition: token.H:259
#define WarningInFunction
Report a warning using Foam::Warning.
tokenType type() const
Definition: tokenI.H:187
punctuationToken punctuationToken_
Definition: token.H:255
Ostream & operator<<(Ostream &, const ensightPart &)
label labelToken() const
Definition: tokenI.H:276
token()
Construct null.
Definition: tokenI.H:62
virtual Ostream & write(const token &)=0
Write next token to stream.
doubleScalar doubleScalarToken() const
Definition: tokenI.H:313