LRDE Tiger Compiler  1.34a $Id: 7fef12e1f5fa43449d667a0eec1d837c40fc1202 $
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
tweast.hxx
Go to the documentation of this file.
1 
6 #ifndef PARSE_TWEAST_HXX
7 # define PARSE_TWEAST_HXX
8 
9 # include <algorithm>
10 
11 # include <boost/algorithm/string/replace.hpp>
12 # include <misc/algorithm.hh>
13 
14 # include <misc/error.hh>
15 # include <parse/tweast.hh>
16 
17 namespace parse
18 {
19 
20  template <typename T>
21  T&
22  Tweast::append_(unsigned&, T& data) const
23  {
24  return data;
25  }
26 
27  template <typename T>
28  Tweast&
29  Tweast::operator<<(const T& t)
30  {
31  input_ << append_(count_, t);
32  return *this;
33  }
34 
35  template <typename T>
36  T*
37  Tweast::take(unsigned s)
38  {
39  T* t = nullptr;
40  try
41  {
42  t = MetavarMap<T>::take_(s);
43  }
44  catch (std::range_error& e)
45  {
46  // Print the contents of the Tweast before dying.
47  misc::error error;
48  error << e.what() << std::endl;
49  error << *this;
50  error.ice_here();
51  }
52  return t;
53  }
54 
55  template <typename T>
56  void
57  Tweast::move_metavars_(Tweast& tweast, std::string& input)
58  {
59  typedef MetavarMap<T> metavars_type;
60  for (const typename metavars_type::map_type::value_type& var :
61  tweast.metavars_type::map_)
62  {
63  // Append the variable from VAR to the enclosing Tweast.
64  unsigned old_num = var.first;
65  // Warning, this is not thread-safe.
66  unsigned new_num = count_;
67  T* data = var.second;
68  metavars_type::map_[new_num] = data;
69  ++count_;
70 
71  // Rename metavariables according to the numbering scheme
72  // within the input string.
73  std::string old_str = metavars_type::show(old_num);
74  std::string new_str = metavars_type::show(new_num);
75  // FIXME: This is inefficient, since the string is viewed
76  // each time a metavariable is processed. Better store
77  // each (old_num, new_num) pair in a map, and process
78  // the string in a single pass.
79  boost::algorithm::replace_first(input,
80  old_str, new_str);
81  }
82  tweast.metavars_type::map_.clear();
83  }
84 
85 }
86 
87 #endif // !PARSE_TWEAST_HXX