summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/parse/tweast.hxx
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
commit967be9e750221ab2ab783f95df79bb26d290a45e (patch)
tree6802900a5e975f9f68b169f0f503f040056d6952 /tiger-compiler/src/parse/tweast.hxx
add: added projectsHEADmain
Diffstat (limited to 'tiger-compiler/src/parse/tweast.hxx')
-rw-r--r--tiger-compiler/src/parse/tweast.hxx75
1 files changed, 75 insertions, 0 deletions
diff --git a/tiger-compiler/src/parse/tweast.hxx b/tiger-compiler/src/parse/tweast.hxx
new file mode 100644
index 0000000..3cb9592
--- /dev/null
+++ b/tiger-compiler/src/parse/tweast.hxx
@@ -0,0 +1,75 @@
+/**
+ ** \file parse/tweast.hxx
+ ** \brief implements inline methods of parse/tweast.hh
+ */
+
+#pragma once
+
+#include <algorithm>
+
+#include <boost/algorithm/string/replace.hpp>
+
+#include <misc/algorithm.hh>
+#include <misc/error.hh>
+#include <parse/tweast.hh>
+
+namespace parse
+{
+ template <typename T> T& Tweast::append_(unsigned&, T& data) const
+ {
+ return data;
+ }
+
+ template <typename T> Tweast& Tweast::operator<<(const T& t)
+ {
+ input_ << append_(count_, t);
+ return *this;
+ }
+
+ template <typename T> T* Tweast::take(unsigned s)
+ {
+ T* t = nullptr;
+ try
+ {
+ t = MetavarMap<T>::take_(s);
+ }
+ catch (const std::range_error& e)
+ {
+ // Print the contents of the Tweast before dying.
+ misc::error error;
+ error << e.what() << std::endl;
+ error << *this;
+ error.ice_here();
+ }
+ return t;
+ }
+
+ template <typename T>
+ void Tweast::move_metavars_(Tweast& tweast, std::string& input)
+ {
+ using metavars_type = MetavarMap<T>;
+ for (const typename metavars_type::map_type::value_type& var :
+ tweast.metavars_type::map_)
+ {
+ // Append the variable from VAR to the enclosing Tweast.
+ unsigned old_num = var.first;
+ // Warning, this is not thread-safe.
+ unsigned new_num = count_;
+ T* data = var.second;
+ metavars_type::map_[new_num] = data;
+ ++count_;
+
+ // Rename metavariables according to the numbering scheme
+ // within the input string.
+ std::string old_str = metavars_type::show(old_num);
+ std::string new_str = metavars_type::show(new_num);
+ // FIXME: This is inefficient, since the string is viewed
+ // each time a metavariable is processed. Better store
+ // each (old_num, new_num) pair in a map, and process
+ // the string in a single pass.
+ boost::algorithm::replace_first(input, old_str, new_str);
+ }
+ tweast.metavars_type::map_.clear();
+ }
+
+} // namespace parse