1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
28 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 // Clear any allocated storage (word or string)
36 inline void token::clear()
42 else if (type_ == STRING || type_ == VERBATIMSTRING)
44 delete stringTokenPtr_;
46 else if (type_ == COMPOUND)
48 if (compoundTokenPtr_->okToDelete())
50 delete compoundTokenPtr_;
54 compoundTokenPtr_->refCount::operator--();
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
72 inline token::token(const token& t)
75 lineNumber_(t.lineNumber_)
79 case token::UNDEFINED:
83 punctuationToken_ = t.punctuationToken_;
87 wordTokenPtr_ = new word(*t.wordTokenPtr_);
92 stringTokenPtr_ = new string(*t.stringTokenPtr_);
96 labelToken_ = t.labelToken_;
100 floatScalarToken_ = t.floatScalarToken_;
104 doubleScalarToken_ = t.doubleScalarToken_;
108 compoundTokenPtr_ = t.compoundTokenPtr_;
109 compoundTokenPtr_->refCount::operator++();
117 // Construct punctuation character token
118 inline token::token(punctuationToken p, label lineNumber)
121 punctuationToken_(p),
122 lineNumber_(lineNumber)
125 // Construct word token
126 inline token::token(const word& w, label lineNumber)
129 wordTokenPtr_(new word(w)),
130 lineNumber_(lineNumber)
133 // Construct string token
134 inline token::token(const string& s, label lineNumber)
137 stringTokenPtr_(new string(s)),
138 lineNumber_(lineNumber)
141 // Construct label token
142 inline token::token(const label l, label lineNumber)
146 lineNumber_(lineNumber)
149 // Construct floatScalar token
150 inline token::token(const floatScalar s, label lineNumber)
153 floatScalarToken_(s),
154 lineNumber_(lineNumber)
157 // Construct doubleScalar token
158 inline token::token(const doubleScalar s, label lineNumber)
160 type_(DOUBLE_SCALAR),
161 doubleScalarToken_(s),
162 lineNumber_(lineNumber)
166 // * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * //
168 // Delete token clearing the storage used by word or string
169 inline token::~token()
175 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
177 inline token::tokenType token::type() const
182 inline token::tokenType& token::type()
187 inline bool token::good() const
189 return (type_ != ERROR && type_ != UNDEFINED);
192 inline bool token::undefined() const
194 return (type_ == UNDEFINED);
197 inline bool token::error() const
199 return (type_ == ERROR);
202 inline bool token::isPunctuation() const
204 return (type_ == PUNCTUATION);
207 inline token::punctuationToken token::pToken() const
209 if (type_ == PUNCTUATION)
211 return punctuationToken_;
215 parseError("punctuation character");
220 inline bool token::isWord() const
222 return (type_ == WORD);
225 inline const word& token::wordToken() const
229 return *wordTokenPtr_;
238 inline bool token::isString() const
240 return (type_ == STRING || type_ == VERBATIMSTRING);
243 inline const string& token::stringToken() const
245 if (type_ == STRING || type_ == VERBATIMSTRING)
247 return *stringTokenPtr_;
251 parseError("string");
256 inline bool token::isLabel() const
258 return (type_ == LABEL);
261 inline label token::labelToken() const
274 inline bool token::isFloatScalar() const
276 return (type_ == FLOAT_SCALAR);
279 inline floatScalar token::floatScalarToken() const
281 if (type_ == FLOAT_SCALAR)
283 return floatScalarToken_;
287 parseError("floatScalar");
293 inline bool token::isDoubleScalar() const
295 return (type_ == DOUBLE_SCALAR);
298 inline doubleScalar token::doubleScalarToken() const
300 if (type_ == DOUBLE_SCALAR)
302 return doubleScalarToken_;
306 parseError("doubleScalar");
312 inline bool token::isScalar() const
314 return (type_ == FLOAT_SCALAR || type_ == DOUBLE_SCALAR);
317 inline scalar token::scalarToken() const
319 if (type_ == FLOAT_SCALAR)
321 return floatScalarToken_;
323 else if (type_ == DOUBLE_SCALAR)
325 return doubleScalarToken_;
329 parseError("scalar");
334 inline bool token::isNumber() const
336 return (type_ == LABEL || isScalar());
339 inline scalar token::number() const
347 return scalarToken();
351 parseError("number (label or scalar)");
356 inline bool token::isCompound() const
358 return (type_ == COMPOUND);
361 inline const token::compound& token::compoundToken() const
363 if (type_ == COMPOUND)
365 return *compoundTokenPtr_;
369 parseError("compound");
370 return *compoundTokenPtr_;
375 inline label token::lineNumber() const
380 inline label& token::lineNumber()
386 inline void token::setBad()
393 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
395 inline void token::operator=(const token& t)
402 case token::UNDEFINED:
406 punctuationToken_ = t.punctuationToken_;
410 wordTokenPtr_ = new word(*t.wordTokenPtr_);
415 stringTokenPtr_ = new string(*t.stringTokenPtr_);
419 labelToken_ = t.labelToken_;
423 floatScalarToken_ = t.floatScalarToken_;
427 doubleScalarToken_ = t.doubleScalarToken_;
431 compoundTokenPtr_ = t.compoundTokenPtr_;
432 compoundTokenPtr_->refCount::operator++();
439 lineNumber_ = t.lineNumber_;
442 inline void token::operator=(const punctuationToken p)
446 punctuationToken_ = p;
449 inline void token::operator=(word* wPtr)
453 wordTokenPtr_ = wPtr;
456 inline void token::operator=(const word& w)
458 operator=(new word(w));
461 inline void token::operator=(string* sPtr)
465 stringTokenPtr_ = sPtr;
468 inline void token::operator=(const string& s)
470 operator=(new string(s));
473 inline void token::operator=(const label l)
480 inline void token::operator=(const floatScalar s)
483 type_ = FLOAT_SCALAR;
484 floatScalarToken_ = s;
487 inline void token::operator=(const doubleScalar s)
490 type_ = DOUBLE_SCALAR;
491 doubleScalarToken_ = s;
494 inline void token::operator=(token::compound* cPtr)
498 compoundTokenPtr_ = cPtr;
502 inline bool token::operator==(const token& t) const
504 if (type_ != t.type_)
511 case token::UNDEFINED:
515 return punctuationToken_ == t.punctuationToken_;
518 return *wordTokenPtr_ == *t.wordTokenPtr_;
522 return *stringTokenPtr_ == *t.stringTokenPtr_;
525 return labelToken_ == t.labelToken_;
528 return equal(floatScalarToken_, t.floatScalarToken_);
531 return equal(doubleScalarToken_, t.doubleScalarToken_);
534 return compoundTokenPtr_ == t.compoundTokenPtr_;
543 inline bool token::operator==(const punctuationToken p) const
545 return (type_ == PUNCTUATION && punctuationToken_ == p);
548 inline bool token::operator==(const word& w) const
550 return (type_ == WORD && wordToken() == w);
553 inline bool token::operator==(const string& s) const
555 return ((type_ == STRING || type_ == VERBATIMSTRING) && stringToken() == s);
558 inline bool token::operator==(const label l) const
560 return (type_ == LABEL && labelToken_ == l);
563 inline bool token::operator==(const floatScalar s) const
565 return (type_ == FLOAT_SCALAR && equal(floatScalarToken_, s));
568 inline bool token::operator==(const doubleScalar s) const
570 return (type_ == DOUBLE_SCALAR && equal(doubleScalarToken_, s));
573 inline bool token::operator!=(const token& t) const
575 return !operator==(t);
578 inline bool token::operator!=(const punctuationToken p) const
580 return !operator==(p);
583 inline bool token::operator!=(const word& w) const
585 return !operator==(w);
588 inline bool token::operator!=(const string& s) const
590 return !operator==(s);
593 inline bool token::operator!=(const floatScalar s) const
595 return !operator==(s);
598 inline bool token::operator!=(const doubleScalar s) const
600 return !operator==(s);
603 inline bool token::operator!=(const label l) const
605 return !operator==(l);
609 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
611 } // End namespace Foam
613 // ************************************************************************* //