tokenIO.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 // based on OpenFOAM stream, with some modifications/simplifications
21 // to be tailored to our needs
22 
23 #include "error.hpp"
24 #include "token.hpp"
25 #include "iIstream.hpp"
26 #include "iOstream.hpp"
27 
28 namespace pFlow
29 {
30 
31 template<class OS>
32 static OS& printTokenInfo(OS& os, const token& tok)
33 {
34  os << "on line " << tok.lineNumber() << ": ";
35 
36  switch (tok.type())
37  {
38  case token::tokenType::UNDEFINED:
39  os << "UNDEFINED";
41  << "Undefined token" << endl;
42  break;
43 
44  case token::tokenType::FLAG:
45  // Swallow the flag
46  break;
47 
48  case token::tokenType::PUNCTUATION:
49  os << tok.pToken();
50  break;
51 
52  case token::tokenType::BOOL:
53  case token::tokenType::INT64:
54  os << tok.int64Token();
55  break;
56 
57  case token::tokenType::FLOAT:
58  os << tok.floatToken();
59  break;
60 
61  case token::tokenType::DOUBLE:
62  os <<tok.doubleToken();
63  break;
64 
65  // Different behaviour for (serial/parallel) streams: preserve types
66  case token::tokenType::DIRECTIVE:
67  case token::tokenType::VARIABLE:
68  //case token::tokenType::VERBATIMSTRING:
69  os << tok.stringToken();
70  break;
71 
72  case token::tokenType::WORD:
73  os << tok.wordToken();
74  break;
75 
76  case token::tokenType::STRING:
77  os << tok.stringToken();
78  break;
79 
80  case token::tokenType::ERROR:
81  os << "ERROR";
83  << "Error token" << endl;
84  break;
85 
86  default:
87  os << "UNKNOWN";
89  << "Unknown token" << endl;
90  }
91 
92  return os;
93 }
94 
95 } // pFlow
96 
97 
98 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
99 
101 :
102  token()
103 {
104  is.read(*this);
105 }
106 
107 
108 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
109 
111 {
112  switch (type_)
113  {
114  case token::tokenType::UNDEFINED: return "undefined";
115  case token::tokenType::BOOL: return "bool";
116  case token::tokenType::FLAG: return "flag";
117  case token::tokenType::PUNCTUATION: return "punctuation";
118  case token::tokenType::INT64: return "int64/int32";
119  case token::tokenType::FLOAT: return "float";
120  case token::tokenType::DOUBLE: return "double";
121  case token::tokenType::WORD: return "word";
122  case token::tokenType::DIRECTIVE: return "directive";
123  case token::tokenType::STRING: return "string";
124  //case token::tokenType::VERBATIM: return "verbatim";
125  case token::tokenType::VARIABLE: return "variable";
126  //case token::tokenType::COMPOUND: return "compound";
127  case token::tokenType::ERROR: return "error";
128 
129  default:
130  break;
131  }
132 
133  return "unknown(" + std::to_string(int(type_)) + ")";
134 }
135 
136 
137 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
138 
140 {
141  tok.reset();
142  return is.read(tok);
143 }
144 
145 
147 {
148  switch (tok.type_)
149  {
150  case token::tokenType::UNDEFINED:
151  os << "UNDEFINED";
153  << "Undefined token" << endl;
154  break;
155 
156  case token::tokenType::FLAG:
157  // Swallow the flag
158  break;
159 
160  case token::tokenType::PUNCTUATION:
161  os << tok.pToken();
162  break;
163 
164  case token::tokenType::BOOL:
165  case token::tokenType::INT64:
166  os << tok.int64Token();
167  break;
168 
169  case token::tokenType::FLOAT:
170  os << tok.floatToken();
171  break;
172 
173  case token::tokenType::DOUBLE:
174  os <<tok.doubleToken();
175  break;
176 
177  // Different behaviour for (serial/parallel) streams: preserve types
178  case token::tokenType::DIRECTIVE:
179  case token::tokenType::VARIABLE:
180  //case token::tokenType::VERBATIMSTRING:
181  os.write(tok);
182  break;
183 
184  case token::tokenType::WORD:
185  os << tok.wordToken();
186  break;
187 
188  case token::tokenType::STRING:
189  os << tok.stringToken();
190  break;
191 
192  case token::tokenType::ERROR:
193  os << "ERROR";
195  << "Error token" << endl;
196  break;
197 
198  default:
199  os << "UNKNOWN";
201  << "Unknown token" << endl;
202  }
203 
204  os.check(FUNCTION_NAME);
205  return os;
206 }
207 
209 {
210  return os << char(pt);
211 }
212 
213 std::ostream& pFlow::operator<<(std::ostream& os, const token::punctuationToken& pt)
214 {
215  return os << char(pt);
216 }
217 
218 std::ostream& pFlow::operator<<(std::ostream& os, const token& tok)
219 {
220  return printTokenInfo(os, tok);
221 }
222 
223 
225 {
226  return printTokenInfo(os, *this);
227 }
228 
229 std::ostream& pFlow::token::printInfo(std::ostream& os)const
230 {
231  return printTokenInfo(os, *this);
232 }
233 
234 
pFlow::iOstream::write
virtual bool write(const token &tok)=0
pFlow::token::type
tokenType type() const
Definition: tokenI.hpp:284
pFlow::iIstream::read
virtual iIstream & read(token &)=0
pFlow::token
Definition: token.hpp:42
pFlow::token::pToken
punctuationToken pToken() const
Definition: tokenI.hpp:452
iIstream.hpp
pFlow::printTokenInfo
static OS & printTokenInfo(OS &os, const token &tok)
Definition: tokenIO.cpp:32
warningInFunction
#define warningInFunction
Definition: error.hpp:55
pFlow::token::punctuationToken
punctuationToken
Definition: token.hpp:81
pFlow::token::stringToken
const word & stringToken() const
Definition: tokenI.hpp:624
token.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
pFlow::token::int64Token
int64 int64Token() const
Definition: tokenI.hpp:484
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow
Definition: demComponent.hpp:28
pFlow::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.cpp:42
pFlow::token::reset
void reset()
Definition: tokenI.hpp:245
pFlow::token::token
constexpr token() noexcept
Definition: tokenI.hpp:89
pFlow::token::doubleToken
double doubleToken() const
Definition: tokenI.hpp:524
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::token::printInfo
iOstream & printInfo(iOstream &os) const
Definition: tokenIO.cpp:224
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::token::floatToken
float floatToken() const
Definition: tokenI.hpp:506
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
iOstream.hpp
pFlow::token::name
word name() const
Definition: tokenIO.cpp:110
pFlow::token::type_
tokenType type_
Definition: token.hpp:182
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::token::wordToken
const word & wordToken() const
Definition: tokenI.hpp:600
pFlow::token::lineNumber
int32 lineNumber() const
Definition: tokenI.hpp:360
error.hpp