gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
Parser.h
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef PARSER_H
7 #define PARSER_H
8 
9 #include <algorithm>
10 #include <map>
11 #include <string>
12 #include <vector>
13 
15 int gmsh_yylex();
16 void gmsh_yyflush();
17 
19 public:
20  bool list;
21  std::vector<double> value;
22  gmsh_yysymbol() : list(false) {}
23 };
24 
25 struct TwoChar {
26  char *char1, *char2;
27 };
28 
29 // classes for Struct
30 class Struct {
31 public:
32  Struct() {}
33  Struct(int tag, std::map<std::string, std::vector<double> > &fopt,
34  std::map<std::string, std::vector<std::string> > &copt,
35  int member_ValMax)
36  : _tag(tag), _member_ValMax(member_ValMax), _fopt(fopt), _copt(copt)
37  {
38  }
39  ~Struct() {}
40 
41  int append(int tag, std::map<std::string, std::vector<double> > &fopt,
42  std::map<std::string, std::vector<std::string> > &copt,
43  int member_ValMax)
44  {
45  if(tag >= 0) _tag = tag;
47  _fopt.insert(fopt.begin(), fopt.end());
48  _copt.insert(copt.begin(), copt.end());
49  return _tag;
50  }
51 
52  inline int getTag() const { return _tag; }
53 
54  int getMember(std::string &key_member, double &out, int index) const
55  {
56  auto it = _fopt.find(key_member);
57  if(it != _fopt.end()) {
58  if(index < (int)it->second.size()) {
59  out = it->second[index];
60  return 0;
61  }
62  else {
63  out = 0.;
64  return 2; // Error: Index out of range
65  }
66  }
67  else {
68  out = 0.;
69  return 1; // Error: Unknown member of Struct
70  }
71  }
72 
73  int getMember(std::string &key_member, const std::string *&out,
74  int index) const
75  {
76  auto it = _copt.find(key_member);
77  if(it != _copt.end()) {
78  if(index < (int)it->second.size()) {
79  out = &it->second[index];
80  return 0;
81  }
82  else {
83  out = nullptr;
84  return 2; // Error: Index out of range
85  }
86  }
87  else {
88  out = nullptr;
89  return 1; // Error: Unknown member of Struct
90  }
91  }
92 
93  int getMember_Dim(std::string &key_member, int &out) const
94  {
95  auto it = _fopt.find(key_member);
96  if(it != _fopt.end()) {
97  out = it->second.size();
98  return 0;
99  }
100  else {
101  auto it = _copt.find(key_member);
102  if(it != _copt.end()) {
103  out = it->second.size();
104  return 0;
105  }
106  else {
107  out = 0;
108  return 1; // Error: Unknown member of Struct
109  }
110  }
111  }
112 
113  int getMember_Vector(std::string &key_member,
114  const std::vector<double> *&out_vector) const
115  {
116  auto it = _fopt.find(key_member);
117  if(it != _fopt.end()) {
118  out_vector = &it->second;
119  return 0;
120  }
121  else {
122  out_vector = nullptr;
123  return 1; // Error: Unknown member of Struct
124  }
125  }
126 
127  int getMember_Vector(std::string &key_member,
128  const std::vector<std::string> *&out_vector) const
129  {
130  auto it = _copt.find(key_member);
131  if(it != _copt.end()) {
132  out_vector = &it->second;
133  return 0;
134  }
135  else {
136  out_vector = nullptr;
137  return 1; // Error: Unknown member of Struct
138  }
139  }
140 
141  int getMember_ValMax() const { return _member_ValMax; }
142 
143  void sprint(std::string &str, const std::string &struct_name,
144  const std::string &struct_namespace) const
145  {
146  str = "Struct ";
147  if(struct_namespace.size()) str += struct_namespace + "::";
148  str += struct_name + " [ ";
149  bool flag_comma = false;
150  for(auto it_attrib = _fopt.begin(); it_attrib != _fopt.end(); ++it_attrib) {
151  if(!flag_comma && it_attrib != _fopt.begin()) flag_comma = true;
152  if(flag_comma) str += ", ";
153  str += it_attrib->first + " ";
154  char tmp[32];
155  if(it_attrib->second.size() > 1) str += "{ ";
156  for(std::size_t i = 0; i < it_attrib->second.size(); i++) {
157  if(i) str += ", ";
158  sprintf(tmp, "%g", it_attrib->second[i]);
159  str += tmp;
160  }
161  if(it_attrib->second.size() > 1) str += "}";
162  }
163  for(auto it_attrib = _copt.begin(); it_attrib != _copt.end(); ++it_attrib) {
164  if(!flag_comma && it_attrib != _copt.begin()) flag_comma = true;
165  if(flag_comma) str += ", ";
166  str += it_attrib->first + " ";
167  if(it_attrib->second.size() > 1) str += "Str[{ ";
168  for(std::size_t i = 0; i < it_attrib->second.size(); i++) {
169  if(i) str += ", ";
170  str += "\"" + it_attrib->second[i] + "\"";
171  }
172  if(it_attrib->second.size() > 1) str += "}]";
173  }
174  str += " ];\n";
175  }
176 
177 private:
179  std::map<std::string, std::vector<double> > _fopt;
180  std::map<std::string, std::vector<std::string> > _copt;
181 };
182 
183 template <class K, class T> class Map {
184 public:
185  Map() {}
186  ~Map() {}
187 
188  T *Find(K key)
189  {
190  typename std::map<K, T>::iterator it;
191  if((it = _map.find(key)) != _map.end())
192  return &it->second;
193  else
194  return nullptr;
195  }
196 
197  const T *Find(K key) const
198  {
199  typename std::map<K, T>::const_iterator it;
200  if((it = _map.find(key)) != _map.end())
201  return &it->second;
202  else
203  return nullptr;
204  }
205 
206  inline T &operator[](K key) { return _map[key]; }
207  inline std::map<K, T> &get() { return _map; }
208  inline const std::map<K, T> &get() const { return _map; }
209  inline int count(const std::string key) const { return _map.count(key); }
210  inline int size() const { return _map.size(); }
211  void clear() { _map.clear(); }
212 
213 public:
214  std::map<K, T> _map;
215 };
216 
217 typedef std::map<std::string, Struct> Map_string_Struct;
218 
219 class Structs : public Map<std::string, Struct> {
220 public:
221  Structs() { _max_tag = 0; }
222  ~Structs() {}
223 
224  int defStruct(std::string &struct_name,
225  std::map<std::string, std::vector<double> > &fopt,
226  std::map<std::string, std::vector<std::string> > &copt,
227  int member_ValMax, bool append = false)
228  {
229  int tag;
230  auto it = fopt.find("Tag");
231  if(it != fopt.end()) {
232  tag = (int)it->second[0]; // Tag forced
233  _max_tag = std::max(_max_tag, tag);
234  }
235  else {
236  tag = (!append) ? ++_max_tag : -1; // Tag auto
237  if(!append) fopt["Tag"].push_back((double)tag);
238  }
239  if(!append)
240  (*this)[struct_name] = Struct(tag, fopt, copt, member_ValMax);
241  else
242  (*this)[struct_name].append(tag, fopt, copt, member_ValMax);
243  return tag;
244  }
245 
246  int get_key_struct_from_tag(int tag, const std::string *&key_struct) const
247  {
248  Map_string_Struct::const_iterator it_st;
249  for(it_st = this->get().begin(); it_st != this->get().end(); ++it_st)
250  if(it_st->second.getTag() == tag) break;
251  if(it_st == this->get().end()) return 2; // 2: Error: Unknown Struct
252  key_struct = &it_st->first;
253  return 0; // 0: no error
254  }
255 
256  void sprint(std::vector<std::string> &strs_out,
257  const std::string &struct_namespace) const
258  {
259  std::string str;
260  for(auto it_st = this->get().begin(); it_st != this->get().end(); ++it_st) {
261  it_st->second.sprint(str, it_st->first, struct_namespace);
262  strs_out.insert(strs_out.end(), str);
263  }
264  }
265 
266 private:
267  int _max_tag;
268 };
269 
270 typedef std::map<std::string, Structs> Map_string_Structs;
271 
272 class NameSpaces : public Map<std::string, Structs> {
273 public:
276 
277  int defStruct(std::string &key_namespace, std::string &key_name,
278  std::map<std::string, std::vector<double> > &fopt,
279  std::map<std::string, std::vector<std::string> > &copt,
280  int &tag_out, int member_ValMax, bool append = false)
281  {
282  Structs *structs_P = &(*this)[key_namespace];
283  if(structs_P->count(key_name)) {
284  if(!append) {
285  tag_out = (*structs_P)[key_name].getTag();
286  return 1; // 1: Error: Redefinition of Struct
287  }
288  }
289  else if(append)
290  append = false; // non-existing Struct
291  tag_out = structs_P->defStruct(key_name, fopt, copt, member_ValMax, append);
292  return 0; // 0: no error
293  }
294 
295  int getTag(std::string &key_namespace, std::string &key_name,
296  double &out) const
297  {
298  const Structs *structs_P = this->Find(key_namespace);
299  const Struct *struct_P = (structs_P) ? structs_P->Find(key_name) : nullptr;
300  if(structs_P && struct_P) { out = (double)struct_P->getTag(); }
301  else {
302  out = 0.;
303  return 1; // 1: Error: Unknown Struct
304  }
305  return 0; // 0: no error
306  }
307 
308  int getMember(std::string &key_namespace, std::string &key_name,
309  std::string &key_member, double &out, int index = 0) const
310  {
311  const Structs *structs_P = this->Find(key_namespace);
312  const Struct *struct_P = (structs_P) ? structs_P->Find(key_name) : nullptr;
313  if(structs_P && struct_P) {
314  switch(struct_P->getMember(key_member, out, index)) {
315  case 0: break;
316  case 1: out = 0.; return 2; // 2: Error: Unknown member of Struct
317  case 2: out = 0.; return 3; // 3: // Error: Index out of range
318  }
319  }
320  else {
321  out = 0.;
322  return 1; // 1: Error: Unknown Struct
323  }
324  return 0; // 0: no error
325  }
326 
327  int getMember(std::string &key_namespace, std::string &key_name,
328  std::string &key_member, const std::string *&out,
329  int index = 0) const
330  {
331  const Structs *structs_P = this->Find(key_namespace);
332  const Struct *struct_P = (structs_P) ? structs_P->Find(key_name) : nullptr;
333  if(structs_P && struct_P) {
334  switch(struct_P->getMember(key_member, out, index)) {
335  case 0: break;
336  case 1: out = nullptr; return 2; // 2: Error: Unknown member of Struct
337  case 2: out = nullptr; return 3; // 3: // Error: Index out of range
338  }
339  }
340  else {
341  out = nullptr;
342  return 1; // 1: Error: Unknown Struct
343  }
344  return 0; // 0: no error
345  }
346 
347  int getMember_Dim(std::string &key_namespace, std::string &key_name,
348  std::string &key_member, int &out) const
349  {
350  const Structs *structs_P = this->Find(key_namespace);
351  const Struct *struct_P = (structs_P) ? structs_P->Find(key_name) : nullptr;
352  if(structs_P && struct_P) {
353  switch(struct_P->getMember_Dim(key_member, out)) {
354  case 0: break;
355  case 1: out = 0; return 2; // 2: Error: Unknown member of Struct
356  }
357  }
358  else {
359  out = 0;
360  return 1; // 1: Error: Unknown Struct
361  }
362  return 0; // 0: no error
363  }
364 
365  int getMember_Vector(std::string &key_namespace, std::string &key_name,
366  std::string &key_member,
367  const std::vector<double> *&out_vector) const
368  {
369  const Structs *structs_P = this->Find(key_namespace);
370  const Struct *struct_P = (structs_P) ? structs_P->Find(key_name) : nullptr;
371  if(structs_P && struct_P) {
372  switch(struct_P->getMember_Vector(key_member, out_vector)) {
373  case 0: break;
374  case 1:
375  out_vector = nullptr;
376  return 2; // 2: Error: Unknown member of Struct
377  }
378  }
379  else {
380  out_vector = nullptr;
381  return 1; // 1: Error: Unknown Struct
382  }
383  return 0; // 0: no error
384  }
385 
386  int getMember_Vector(std::string &key_namespace, std::string &key_name,
387  std::string &key_member,
388  const std::vector<std::string> *&out_vector) const
389  {
390  const Structs *structs_P = this->Find(key_namespace);
391  const Struct *struct_P = (structs_P) ? structs_P->Find(key_name) : nullptr;
392  if(structs_P && struct_P) {
393  switch(struct_P->getMember_Vector(key_member, out_vector)) {
394  case 0: break;
395  case 1:
396  out_vector = nullptr;
397  return 2; // 2: Error: Unknown member of Struct
398  }
399  }
400  else {
401  out_vector = nullptr;
402  return 1; // 1: Error: Unknown Struct
403  }
404  return 0; // 0: no error
405  }
406 
407  int get_key_struct_from_tag(std::string &key_namespace, int tag,
408  const std::string *&key_struct) const
409  {
410  const Structs *structs_P = this->Find(key_namespace);
411  if(structs_P != nullptr)
412  return structs_P->get_key_struct_from_tag(tag, key_struct);
413  else
414  return 1; // 1: Error: Unknown NameSpace
415  }
416 
417  int getMember_ValMax(std::string &key_namespace, std::string &key_name)
418  {
419  const Structs *structs_P = this->Find(key_namespace);
420  const Struct *struct_P = (structs_P) ? structs_P->Find(key_name) : nullptr;
421  return (structs_P && struct_P) ? struct_P->getMember_ValMax() : -1;
422  }
423 
424  void sprint(std::vector<std::string> &strs_out) const
425  {
426  std::vector<std::string> strs;
427  for(auto it_ns = this->get().begin(); it_ns != this->get().end(); ++it_ns) {
428  strs.clear();
429  it_ns->second.sprint(strs, it_ns->first);
430  strs_out.insert(strs_out.end(), strs.begin(), strs.end());
431  }
432  }
433 };
434 
435 // global parser variables that need to be exported
436 extern FILE *gmsh_yyin;
437 extern int gmsh_yylineno;
438 extern char *gmsh_yytext;
439 extern int gmsh_yyviewindex;
440 extern std::string gmsh_yyname;
441 extern int gmsh_yyerrorstate;
442 extern std::map<std::string, gmsh_yysymbol> gmsh_yysymbols;
443 extern std::map<std::string, std::vector<std::string> > gmsh_yystringsymbols;
444 extern std::string gmsh_yyfactory;
446 
447 void PrintParserSymbols(bool help, std::vector<std::string> &vec);
448 
449 #endif
struct_namespace
static std::string struct_namespace
Definition: Gmsh.tab.cpp:663
Struct::sprint
void sprint(std::string &str, const std::string &struct_name, const std::string &struct_namespace) const
Definition: Parser.h:143
gmsh_yyerrorstate
int gmsh_yyerrorstate
Definition: Gmsh.tab.cpp:641
Map::size
int size() const
Definition: Parser.h:210
NameSpaces::getMember_Vector
int getMember_Vector(std::string &key_namespace, std::string &key_name, std::string &key_member, const std::vector< double > *&out_vector) const
Definition: Parser.h:365
Struct::getMember_Dim
int getMember_Dim(std::string &key_member, int &out) const
Definition: Parser.h:93
Structs
Definition: Parser.h:219
TwoChar::char1
char * char1
Definition: Parser.h:26
Struct::getMember
int getMember(std::string &key_member, double &out, int index) const
Definition: Parser.h:54
TwoChar
Definition: Parser.h:25
gmsh_yyfactory
std::string gmsh_yyfactory
Definition: Gmsh.tab.cpp:645
Map::count
int count(const std::string key) const
Definition: Parser.h:209
gmsh_yysymbols
std::map< std::string, gmsh_yysymbol > gmsh_yysymbols
Definition: Gmsh.tab.cpp:643
Structs::_max_tag
int _max_tag
Definition: Parser.h:267
gmsh_yysymbol::list
bool list
Definition: Parser.h:20
TwoChar::char2
char * char2
Definition: Parser.h:26
Struct::~Struct
~Struct()
Definition: Parser.h:39
Struct::getMember_Vector
int getMember_Vector(std::string &key_member, const std::vector< double > *&out_vector) const
Definition: Parser.h:113
Struct::Struct
Struct()
Definition: Parser.h:32
Struct::getTag
int getTag() const
Definition: Parser.h:52
Struct::Struct
Struct(int tag, std::map< std::string, std::vector< double > > &fopt, std::map< std::string, std::vector< std::string > > &copt, int member_ValMax)
Definition: Parser.h:33
NameSpaces::get_key_struct_from_tag
int get_key_struct_from_tag(std::string &key_namespace, int tag, const std::string *&key_struct) const
Definition: Parser.h:407
Struct
Definition: Parser.h:30
Structs::~Structs
~Structs()
Definition: Parser.h:222
NameSpaces::getTag
int getTag(std::string &key_namespace, std::string &key_name, double &out) const
Definition: Parser.h:295
Map::get
const std::map< K, T > & get() const
Definition: Parser.h:208
PrintParserSymbols
void PrintParserSymbols(bool help, std::vector< std::string > &vec)
Definition: Gmsh.tab.cpp:15027
Struct::getMember_Vector
int getMember_Vector(std::string &key_member, const std::vector< std::string > *&out_vector) const
Definition: Parser.h:127
Map::operator[]
T & operator[](K key)
Definition: Parser.h:206
Struct::_member_ValMax
int _member_ValMax
Definition: Parser.h:178
Struct::append
int append(int tag, std::map< std::string, std::vector< double > > &fopt, std::map< std::string, std::vector< std::string > > &copt, int member_ValMax)
Definition: Parser.h:41
Structs::Structs
Structs()
Definition: Parser.h:221
Structs::defStruct
int defStruct(std::string &struct_name, std::map< std::string, std::vector< double > > &fopt, std::map< std::string, std::vector< std::string > > &copt, int member_ValMax, bool append=false)
Definition: Parser.h:224
gmsh_yyin
FILE * gmsh_yyin
gmsh_yysymbol
Definition: Parser.h:18
NameSpaces
Definition: Parser.h:272
struct_name
static std::string struct_name
Definition: Gmsh.tab.cpp:663
Map_string_Struct
std::map< std::string, Struct > Map_string_Struct
Definition: Parser.h:217
NameSpaces::getMember_ValMax
int getMember_ValMax(std::string &key_namespace, std::string &key_name)
Definition: Parser.h:417
Struct::_tag
int _tag
Definition: Parser.h:178
NameSpaces::getMember
int getMember(std::string &key_namespace, std::string &key_name, std::string &key_member, double &out, int index=0) const
Definition: Parser.h:308
Struct::getMember_ValMax
int getMember_ValMax() const
Definition: Parser.h:141
Map::~Map
~Map()
Definition: Parser.h:186
NameSpaces::defStruct
int defStruct(std::string &key_namespace, std::string &key_name, std::map< std::string, std::vector< double > > &fopt, std::map< std::string, std::vector< std::string > > &copt, int &tag_out, int member_ValMax, bool append=false)
Definition: Parser.h:277
gmsh_yylineno
int gmsh_yylineno
Structs::sprint
void sprint(std::vector< std::string > &strs_out, const std::string &struct_namespace) const
Definition: Parser.h:256
Structs::get_key_struct_from_tag
int get_key_struct_from_tag(int tag, const std::string *&key_struct) const
Definition: Parser.h:246
gmsh_yyname
std::string gmsh_yyname
Definition: Gmsh.tab.cpp:640
gmsh_yynamespaces
NameSpaces gmsh_yynamespaces
Definition: Gmsh.tab.cpp:646
Map::Find
const T * Find(K key) const
Definition: Parser.h:197
NameSpaces::getMember
int getMember(std::string &key_namespace, std::string &key_name, std::string &key_member, const std::string *&out, int index=0) const
Definition: Parser.h:327
Map::get
std::map< K, T > & get()
Definition: Parser.h:207
Struct::_fopt
std::map< std::string, std::vector< double > > _fopt
Definition: Parser.h:179
NameSpaces::~NameSpaces
~NameSpaces()
Definition: Parser.h:275
Map::clear
void clear()
Definition: Parser.h:211
gmsh_yysymbol::value
std::vector< double > value
Definition: Parser.h:21
NameSpaces::sprint
void sprint(std::vector< std::string > &strs_out) const
Definition: Parser.h:424
gmsh_yytext
char * gmsh_yytext
NameSpaces::getMember_Dim
int getMember_Dim(std::string &key_namespace, std::string &key_name, std::string &key_member, int &out) const
Definition: Parser.h:347
gmsh_yyflush
void gmsh_yyflush()
gmsh_yyviewindex
int gmsh_yyviewindex
Definition: Gmsh.tab.cpp:642
member_ValMax
static int member_ValMax
Definition: Gmsh.tab.cpp:668
gmsh_yyparse
int gmsh_yyparse()
Struct::_copt
std::map< std::string, std::vector< std::string > > _copt
Definition: Parser.h:180
gmsh_yysymbol::gmsh_yysymbol
gmsh_yysymbol()
Definition: Parser.h:22
Map_string_Structs
std::map< std::string, Structs > Map_string_Structs
Definition: Parser.h:270
gmsh_yystringsymbols
std::map< std::string, std::vector< std::string > > gmsh_yystringsymbols
Definition: Gmsh.tab.cpp:644
Struct::getMember
int getMember(std::string &key_member, const std::string *&out, int index) const
Definition: Parser.h:73
Map::Find
T * Find(K key)
Definition: Parser.h:188
Map::_map
std::map< K, T > _map
Definition: Parser.h:214
Map::Map
Map()
Definition: Parser.h:185
gmsh_yylex
int gmsh_yylex()
NameSpaces::NameSpaces
NameSpaces()
Definition: Parser.h:274
NameSpaces::getMember_Vector
int getMember_Vector(std::string &key_namespace, std::string &key_name, std::string &key_member, const std::vector< std::string > *&out_vector) const
Definition: Parser.h:386
Map
Definition: Parser.h:183