From 2567f2a78795c2430e371d07c1bad5390f53d230 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 1 Jun 2019 13:25:02 +0900 Subject: [PATCH] feat: add source_location for error message generation. --- toml/source_location.hpp | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 toml/source_location.hpp diff --git a/toml/source_location.hpp b/toml/source_location.hpp new file mode 100644 index 0000000..48396a0 --- /dev/null +++ b/toml/source_location.hpp @@ -0,0 +1,82 @@ +// Copyright Toru Niina 2019. +// Distributed under the MIT License. +#ifndef TOML11_SOURCE_LOCATION_HPP +#define TOML11_SOURCE_LOCATION_HPP +#include "region.hpp" +#include + +namespace toml +{ + +// A struct to contain location in a toml file. +// The interface imitates std::experimental::source_location, +// but not completely the same. +// +// It would be constructed by toml::value. It can be used to generate +// user-defined error messages. +// +// - std::uint_least32_t line() const noexcept +// - returns the line number where the region is on. +// - std::uint_least32_t column() const noexcept +// - returns the column number where the region starts. +// - std::uint_least32_t region() const noexcept +// - returns the size of the region. +// +// +-- line() +-- region of interest (region() == 9) +// v .---+---. +// 12 | value = "foo bar" +// ^ +// +-- column() +// +// - std::string const& file_name() const noexcept; +// - name of the file. +// - std::string const& line_str() const noexcept; +// - the whole line that contains the region of interest. +// +struct source_location +{ + public: + + source_location() + : line_num_(0), column_num_(0), region_size_(0), + file_name_("unknown file"), line_str_("") + {} + + explicit source_location(const detail::region_base* reg) + : line_num_(0), column_num_(0), region_size_(0), + file_name_("unknown file"), line_str_("") + { + if(reg) + { + line_num_ = std::stoul(reg->line_num()); + column_num_ = reg->before() + 1; + region_size_ = reg->size(); + file_name_ = reg->name(); + line_str_ = reg->line(); + } + } + + ~source_location() = default; + source_location(source_location const&) = default; + source_location(source_location &&) = default; + source_location& operator=(source_location const&) = default; + source_location& operator=(source_location &&) = default; + + std::uint_least32_t line() const noexcept {return line_num_;} + std::uint_least32_t column() const noexcept {return column_num_;} + std::uint_least32_t region() const noexcept {return region_size_;} + + std::string const& file_name() const noexcept {return file_name_;} + std::string const& line_str() const noexcept {return line_str_;} + + private: + + std::uint_least32_t line_num_; + std::uint_least32_t column_num_; + std::uint_least32_t region_size_; + std::string file_name_; + std::string line_str_; +}; + +} // toml +#endif// TOML11_SOURCE_LOCATION_HPP