token.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-2024 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 "token.H"
27 
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
29 
30 namespace Foam
31 {
33 
37 
38  const word token::typeNames_[] =
39  {
40  "undefined",
41  "char",
42  "word",
43  "functionName",
44  "variable",
45  "string",
46  "verbatimString",
47  "int32_t",
48  "int64_t",
49  "uint32_t",
50  "uint64_t",
51  "floatScalar",
52  "doubleScalar",
53  "longDoubleScalar",
54  "compound",
55  "error"
56  };
57 }
58 
59 
60 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
61 
62 void Foam::token::parseError(const char* expected) const
63 {
64  cerr<< "Parse error, expected a " << expected
65  << ", found \n " << info() << std::endl;
66 }
67 
68 
69 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
70 
72 {}
73 
74 
75 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
76 
78 (
79  const word& compoundType,
80  Istream& is
81 )
82 {
83  IstreamConstructorTable::iterator cstrIter =
84  IstreamConstructorTablePtr_->find(compoundType);
85 
86  if (cstrIter == IstreamConstructorTablePtr_->end())
87  {
89  << "Unknown compound type " << compoundType << nl << nl
90  << "Valid compound types:" << endl
91  << IstreamConstructorTablePtr_->sortedToc()
92  << abort(FatalIOError);
93  }
94 
95  return autoPtr<Foam::token::compound>(cstrIter()(is));
96 }
97 
98 
99 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100 
102 {
103  if (type_ == token::UNDEFINED)
104  {
105  return typeNames_[0];
106  }
107  else if (type_ == token::COMPOUND)
108  {
109  return compoundTokenPtr_->type();
110  }
111  else
112  {
113  return typeNames_[type_ - token::PUNCTUATION + 1];
114  }
115 }
116 
117 
119 {
120  return
121  (
122  IstreamConstructorTablePtr_
123  && IstreamConstructorTablePtr_->found(name)
124  );
125 }
126 
127 
129 {
130  if (type_ == COMPOUND)
131  {
132  if (compoundTokenPtr_->empty())
133  {
135  << "compound has already been transferred from token\n "
136  << info() << abort(FatalIOError);
137  }
138  else
139  {
140  compoundTokenPtr_->empty() = true;
141  }
142 
143  return *compoundTokenPtr_;
144  }
145  else
146  {
147  parseError("compound");
148  return *compoundTokenPtr_;
149  }
150 }
151 
152 
153 // ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
Abstract base class for complex tokens.
Definition: token.H:133
static autoPtr< compound > New(const word &type, Istream &)
Select null constructed.
Definition: token.C:78
static bool isCompound(const word &name)
Return true if name is a compound type.
Definition: token.C:118
virtual ~compound()
Destructor.
Definition: token.C:71
compound & transferCompoundToken(const Istream &is)
Definition: token.C:128
@ UNDEFINED
Definition: token.H:80
@ COMPOUND
Definition: token.H:95
@ PUNCTUATION
Definition: token.H:82
InfoProxy< token > info() const
Return info proxy.
Definition: token.H:422
const word & typeName() const
Return the type name of the token.
Definition: token.C:101
static token undefinedToken
Static undefined token.
Definition: token.H:240
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
Namespace for OpenFOAM.
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
errorManip< error > abort(error &err)
Definition: errorManip.H:131
token::compound tokenCompound
Definition: token.C:34
defineTypeNameAndDebug(combustionModel, 0)
IOerror FatalIOError
static const char nl
Definition: Ostream.H:266