Nektar++
ParseUtils.hpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ParseUtils.hpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: This file contains various parsing utilities, primarily used
33 // by SpatialDomains to process input files.
34 //
35 //
36 ////////////////////////////////////////////////////////////////////////////////
37 #ifndef NEKTAR_LIBUTILITIES_PARSEUTILS_HPP
38 #define NEKTAR_LIBUTILITIES_PARSEUTILS_HPP
39 
41 #include <vector>
42 
43 #include <boost/version.hpp>
45 #if( BOOST_VERSION / 100 % 1000 >= 36 )
46 #include <boost/spirit/include/classic_core.hpp>
47 #include <boost/spirit/include/classic_push_back_actor.hpp>
48 
49 using namespace boost::spirit::classic;
50 #else
51 #include <boost/spirit/core.hpp>
52 #include <boost/spirit/actor/push_back_actor.hpp>
53 
54 using namespace boost::spirit;
55 #endif
56 
57 namespace Nektar
58 {
59  class ParseUtils
60  {
61  public:
62  static bool ParseRealAssignment(const char *const str, std::string &symbol, NekDouble &value)
63  {
64  SymbolFunctor symbolFunctor(&symbol);
65  ValueFunctor valueFunctor(&value);
66 
67  return parse(str,
68  // Begin grammar
69  (
70  lexeme_d[alpha_p >> *alnum_p][symbolFunctor] >> "=" >> real_p[valueFunctor]
71  )
72  ,
73  // End grammar
74 
75  space_p).full;
76  }
77 
78  static bool GenerateSeqVector(const char *const str, std::vector<unsigned int> &vec)
79  {
80  // Functors used to parse the sequence.
81  fctor1 functor1(&vec);
82  fctor2 functor2(&vec);
83 
84  return parse(str,
85  // Begin grammar
86  (
87  uint_p[functor1] >> !('-' >> uint_p[functor2]) >>
88  *(',' >> uint_p[functor1] >> !('-' >> uint_p[functor2]))
89  )
90  ,
91  // End grammar
92 
93  space_p).full;
94  }
95 
96  static bool GenerateOrderedVector(const char *const str, std::vector<unsigned int> &vec)
97  {
98  // Functors used to parse the sequence.
99  fctor1 functor1(&vec);
100 
101  return parse(str,
102  // Begin grammar
103  (
104  uint_p[functor1] >> *(',' >> uint_p[functor1])
105  )
106  ,
107  // End grammar
108 
109  space_p).full;
110  }
111 
112  static bool GenerateOrderedVector(const char *const str, std::vector<NekDouble> &vec)
113  {
114  // Functors used to parse the sequence.
115  fctor4 functor4(&vec);
116 
117  return parse(str,
118  // Begin grammar
119  (
120  real_p[functor4] >> *(',' >> real_p[functor4])
121  )
122  ,
123  // End grammar
124  space_p).full;
125  }
126 
127  static bool GenerateUnOrderedVector(const char *const str, std::vector<NekDouble> &vec)
128  {
129  // Functors used to parse the sequence.
130  fctor5 functor5(&vec);
131 
132  return parse(str,
133  // Begin grammar
134  (
135  real_p[functor5] >> *(',' >> real_p[functor5])
136  )
137  ,
138  // End grammar
139  space_p).full;
140  }
141 
142  static bool GenerateOrderedStringVector(const char *const str, std::vector<std::string> &vec)
143  {
144  // Functors used to parse the sequence.
145  fctor3 functor3(&vec);
146 
147  return parse(str,
148  // Begin grammar
149  (
150  (+(print_p - ','))[functor3] >> *(',' >> (+(print_p - ','))[functor3])
151  )
152  ,
153  // End grammar
154 
155  space_p).full;
156  }
157 
158  private:
159 
161  {
162  SymbolFunctor(std::string *symbol):
163  m_symbol(symbol)
164  {
165  }
166 
167  void operator()(const char *beg, const char *end) const
168  {
169  m_symbol->assign(beg, end-beg);
170  }
171 
172  private:
173  std::string *m_symbol;
174  };
175 
177  {
179  m_value(value)
180  {
181  }
182 
183  void operator()(NekDouble val) const
184  {
185  *m_value = val;
186  }
187 
188  private:
190  };
191 
192  struct fctor1
193  {
194  fctor1(std::vector<unsigned int> *vec):
195  m_vector(vec)
196  {
197  }
198 
199  void operator()(unsigned int n) const
200  {
201 #ifdef NOTREQUIRED //SJS: I do not think we need this check
202  if (!m_vector->empty())
203  {
204  unsigned int prevElem = m_vector->back();
205 
206  if (n > prevElem)
207  {
208  m_vector->push_back(n);
209  }
210  }
211  else
212 #endif
213  {
214  m_vector->push_back(n);
215  }
216  }
217 
218  private:
219  std::vector<unsigned int> *m_vector;
220  fctor1();
221  };
222 
223  struct fctor2
224  {
225  fctor2(std::vector<unsigned int> *vec):
226  m_vector(vec)
227  {
228  }
229 
230  void operator()(unsigned int n) const
231  {
232  unsigned int prevElem = m_vector->back();
233 
234  for (unsigned int i=prevElem+1; i<=n; ++i)
235  {
236  m_vector->push_back(i);
237  }
238  }
239 
240  private:
241  std::vector<unsigned int> *m_vector;
242  };
243 
244  struct fctor3
245  {
246  fctor3(std::vector<std::string> *vec):
247  m_vector(vec)
248  {
249  }
250 
251  void operator()(char const* first, char const* last) const
252  {
253  m_vector->push_back(std::string(first, last));
254  }
255 
256  private:
257  std::vector<std::string> *m_vector;
258  };
259 
260  // Probably should template fctor1 if that is possible?
261  struct fctor4
262  {
263  fctor4(std::vector<NekDouble> *vec):
264  m_vector(vec)
265  {
266  }
267 
268  void operator()(NekDouble n) const
269  {
270  if (!m_vector->empty())
271  {
272  NekDouble prevElem = m_vector->back();
273 
274  if (n > prevElem)
275  {
276  m_vector->push_back(n);
277  }
278  }
279  else
280  {
281  m_vector->push_back(n);
282  }
283  }
284 
285  private:
286  std::vector<NekDouble> *m_vector;
287  fctor4();
288  };
289 
290  struct fctor5
291  {
292  fctor5(std::vector<NekDouble> *vec):
293  m_vector(vec)
294  {
295  }
296 
297  void operator()(NekDouble n) const
298  {
299  m_vector->push_back(n);
300  }
301 
302  private:
303  std::vector<NekDouble> *m_vector;
304  fctor5();
305  };
306 
307  };
308 }
309 
310 #endif //NEKTAR_LIBUTILITIES_PARSEUTILS_HPP
static bool GenerateOrderedStringVector(const char *const str, std::vector< std::string > &vec)
Definition: ParseUtils.hpp:142
fctor5(std::vector< NekDouble > *vec)
Definition: ParseUtils.hpp:292
static bool GenerateOrderedVector(const char *const str, std::vector< unsigned int > &vec)
Definition: ParseUtils.hpp:96
fctor1(std::vector< unsigned int > *vec)
Definition: ParseUtils.hpp:194
std::vector< unsigned int > * m_vector
Definition: ParseUtils.hpp:241
static bool GenerateSeqVector(const char *const str, std::vector< unsigned int > &vec)
Definition: ParseUtils.hpp:78
void operator()(const char *beg, const char *end) const
Definition: ParseUtils.hpp:167
void operator()(NekDouble n) const
Definition: ParseUtils.hpp:268
std::vector< std::string > * m_vector
Definition: ParseUtils.hpp:257
void operator()(unsigned int n) const
Definition: ParseUtils.hpp:199
static bool ParseRealAssignment(const char *const str, std::string &symbol, NekDouble &value)
Definition: ParseUtils.hpp:62
fctor3(std::vector< std::string > *vec)
Definition: ParseUtils.hpp:246
double NekDouble
void operator()(char const *first, char const *last) const
Definition: ParseUtils.hpp:251
void operator()(NekDouble n) const
Definition: ParseUtils.hpp:297
fctor2(std::vector< unsigned int > *vec)
Definition: ParseUtils.hpp:225
SymbolFunctor(std::string *symbol)
Definition: ParseUtils.hpp:162
std::vector< unsigned int > * m_vector
Definition: ParseUtils.hpp:219
static bool GenerateOrderedVector(const char *const str, std::vector< NekDouble > &vec)
Definition: ParseUtils.hpp:112
std::vector< NekDouble > * m_vector
Definition: ParseUtils.hpp:286
static bool GenerateUnOrderedVector(const char *const str, std::vector< NekDouble > &vec)
Definition: ParseUtils.hpp:127
void operator()(unsigned int n) const
Definition: ParseUtils.hpp:230
void operator()(NekDouble val) const
Definition: ParseUtils.hpp:183
fctor4(std::vector< NekDouble > *vec)
Definition: ParseUtils.hpp:263
std::vector< NekDouble > * m_vector
Definition: ParseUtils.hpp:303