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-2019 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 
70  os << *t.functionNameTokenPtr_;
71  break;
72 
73  case token::VARIABLE:
74  os << *t.variableTokenPtr_;
75  break;
76 
77  case token::STRING:
78  os << *t.stringTokenPtr_;
79  break;
80 
82  os << *t.verbatimStringTokenPtr_;
83  break;
84 
85  case token::LABEL:
86  os << t.labelToken_;
87  break;
88 
90  os << t.floatScalarToken_;
91  break;
92 
94  os << t.doubleScalarToken_;
95  break;
96 
99  break;
100 
101  case token::COMPOUND:
102  os << *t.compoundTokenPtr_;
103  break;
104 
105  case token::ERROR:
106  os << "ERROR";
108  << "Error token" << endl;
109  break;
110 
111  default:
112  os << "UNKNOWN";
114  << "Unknown token"
115  << endl;
116  }
117 
118  // Check state of stream
119  os.check("Ostream& operator<<(Ostream&, const token&)");
120 
121  return os;
122 }
123 
124 
125 ostream& Foam::operator<<(ostream& os, const token::punctuationToken& pt)
126 {
127  return os << char(pt);
128 }
129 
130 
132 {
133  return os << char(pt);
134 }
135 
136 
138 {
139  os << ct.type() << token::SPACE;
140  ct.write(os);
141 
142  return os;
143 }
144 
145 
146 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
147 
148 ostream& Foam::operator<<(ostream& os, const InfoProxy<token>& ip)
149 {
150  const token& t = ip.t_;
151 
152  os << "on line " << t.lineNumber();
153 
154  switch (t.type())
155  {
156  case token::UNDEFINED:
157  os << " an undefined token";
158  break;
159 
160  case token::PUNCTUATION:
161  os << " the punctuation token " << '\'' << t.pToken() << '\'';
162  break;
163 
164  case token::WORD:
165  os << " the word " << '\'' << t.wordToken() << '\'';
166  break;
167 
168  case token::STRING:
169  os << " the string " << t.stringToken();
170  break;
171 
173  os << " the verbatim string " << t.verbatimStringToken();
174  break;
175 
176  case token::FUNCTIONNAME:
177  os << " the functionName " << t.functionNameToken();
178  break;
179 
180  case token::VARIABLE:
181  os << " the variable " << t.variableToken();
182  break;
183 
184  case token::LABEL:
185  os << " the label " << t.labelToken();
186  break;
187 
188  case token::FLOAT_SCALAR:
189  os << " the floatScalar " << t.floatScalarToken();
190  break;
191 
193  os << " the doubleScalar " << t.doubleScalarToken();
194  break;
195 
197  os << " the longDoubleScalar " << t.longDoubleScalarToken();
198  break;
199 
200  case token::COMPOUND:
201  {
202  if (t.compoundToken().empty())
203  {
204  os << " the empty compound of type "
205  << t.compoundToken().type();
206  }
207  else
208  {
209  os << " the compound of type "
210  << t.compoundToken().type();
211  }
212  }
213  break;
214 
215  case token::ERROR:
216  os << " an error";
217  break;
218 
219  default:
220  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
221  }
222 
223  return os;
224 }
225 
226 
227 template<>
229 {
230  const token& t = ip.t_;
231 
232  os << "on line " << t.lineNumber();
233 
234  switch (t.type())
235  {
236  case token::UNDEFINED:
237  os << " an undefined token";
238  break;
239 
240  case token::PUNCTUATION:
241  os << " the punctuation token " << '\'' << t.pToken() << '\'';
242  break;
243 
244  case token::WORD:
245  os << " the word " << '\'' << t.wordToken() << '\'';
246  break;
247 
248  case token::STRING:
249  os << " the string " << t.stringToken();
250  break;
251 
253  os << " the verbatim string " << t.verbatimStringToken();
254  break;
255 
256  case token::FUNCTIONNAME:
257  os << " the functionName " << t.functionNameToken();
258  break;
259 
260  case token::VARIABLE:
261  os << " the variable " << t.variableToken();
262  break;
263 
264  case token::LABEL:
265  os << " the label " << t.labelToken();
266  break;
267 
268  case token::FLOAT_SCALAR:
269  os << " the floatScalar " << t.floatScalarToken();
270  break;
271 
273  os << " the doubleScalar " << t.doubleScalarToken();
274  break;
275 
277  os << " the longDoubleScalar " << t.longDoubleScalarToken();
278  break;
279 
280  case token::COMPOUND:
281  {
282  if (t.compoundToken().empty())
283  {
284  os << " the empty compound of type "
285  << t.compoundToken().type();
286  }
287  else
288  {
289  os << " the compound of type "
290  << t.compoundToken().type();
291  }
292  }
293  break;
294 
295  case token::ERROR:
296  os << " an error";
297  break;
298 
299  default:
300  os << " an unknown token type " << '\'' << int(t.type()) << '\'';
301  }
302 
303  return os;
304 }
305 
306 
307 // ************************************************************************* //
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:92
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:50
const T & t_
Definition: InfoProxy.H:53
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
virtual Istream & read(token &)=0
Return next token from stream.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Abstract base class for complex tokens.
Definition: token.H:130
virtual void write(Ostream &) const =0
bool empty() const
Definition: token.H:180
A token holds items read from Istream.
Definition: token.H:73
longDoubleScalar longDoubleScalarToken() const
Definition: tokenI.H:453
label labelToken_
Definition: token.H:256
const variable & variableToken() const
Definition: tokenI.H:302
punctuationToken punctuationToken_
Definition: token.H:250
@ LABEL
Definition: token.H:88
@ ERROR
Definition: token.H:94
@ VARIABLE
Definition: token.H:85
@ WORD
Definition: token.H:83
@ UNDEFINED
Definition: token.H:80
@ COMPOUND
Definition: token.H:92
@ FLOAT_SCALAR
Definition: token.H:89
@ DOUBLE_SCALAR
Definition: token.H:90
@ LONG_DOUBLE_SCALAR
Definition: token.H:91
@ VERBATIMSTRING
Definition: token.H:87
@ FUNCTIONNAME
Definition: token.H:84
@ STRING
Definition: token.H:86
@ PUNCTUATION
Definition: token.H:82
verbatimString * verbatimStringTokenPtr_
Definition: token.H:255
compound * compoundTokenPtr_
Definition: token.H:260
functionName * functionNameTokenPtr_
Definition: token.H:252
floatScalar floatScalarToken_
Definition: token.H:257
const functionName & functionNameToken() const
Definition: tokenI.H:284
variable * variableTokenPtr_
Definition: token.H:253
floatScalar floatScalarToken() const
Definition: tokenI.H:415
punctuationToken
Standard punctuation tokens.
Definition: token.H:99
const string & stringToken() const
Definition: tokenI.H:320
punctuationToken pToken() const
Definition: tokenI.H:248
label labelToken() const
Definition: tokenI.H:397
string * stringTokenPtr_
Definition: token.H:254
tokenType type() const
Definition: tokenI.H:218
const compound & compoundToken() const
Definition: tokenI.H:525
doubleScalar doubleScalarToken() const
Definition: tokenI.H:434
doubleScalar doubleScalarToken_
Definition: token.H:258
word * wordTokenPtr_
Definition: token.H:251
token()
Construct null.
Definition: tokenI.H:74
const word & wordToken() const
Definition: tokenI.H:266
const verbatimString & verbatimStringToken() const
Definition: tokenI.H:338
label lineNumber() const
Definition: tokenI.H:539
longDoubleScalar * longDoubleScalarTokenPtr_
Definition: token.H:259
#define WarningInFunction
Report a warning using Foam::Warning.
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Istream & operator>>(Istream &, directionInfo &)
Ostream & operator<<(Ostream &, const ensightPart &)