From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- tiger-compiler/src/parse/tweast.hxx | 75 +++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tiger-compiler/src/parse/tweast.hxx (limited to 'tiger-compiler/src/parse/tweast.hxx') 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 + +#include + +#include +#include +#include + +namespace parse +{ + template T& Tweast::append_(unsigned&, T& data) const + { + return data; + } + + template Tweast& Tweast::operator<<(const T& t) + { + input_ << append_(count_, t); + return *this; + } + + template T* Tweast::take(unsigned s) + { + T* t = nullptr; + try + { + t = MetavarMap::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 + void Tweast::move_metavars_(Tweast& tweast, std::string& input) + { + using metavars_type = MetavarMap; + 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 -- cgit v1.2.3