2026-07-18 18:48:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
|
|
|
|
// #include <filesystem>
|
|
|
|
|
#include <source_location>
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
#include "file.h"
|
|
|
|
|
|
|
|
|
|
inline std::string klammertext_home_var = "KLAMMERTEXT_HOME";
|
|
|
|
|
|
|
|
|
|
class Locator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit Locator(const std::source_location location =
|
|
|
|
|
std::source_location::current())
|
|
|
|
|
// std::source_location::file_name() can return nullptr on some
|
|
|
|
|
// toolchains (e.g. Homebrew GCC on macOS); guard against
|
|
|
|
|
// fs::path(nullptr) -> strlen(NULL).
|
|
|
|
|
: m_filename(location.file_name()
|
2026-09-25 00:32:55 +02:00
|
|
|
? fs::absolute(location.file_name()).generic_string() : std::string{})
|
2026-07-18 18:48:23 +02:00
|
|
|
, m_line(int(location.line()))
|
|
|
|
|
, m_chr(int(location.column()))
|
|
|
|
|
{};
|
2026-07-30 23:50:07 +02:00
|
|
|
Locator(const fs::path& filename, int line, int chr);
|
2026-08-23 20:48:26 +02:00
|
|
|
// "No location": for errors with no document position -- command-line
|
|
|
|
|
// argument errors, lookups made on the command's behalf. NOT the same
|
|
|
|
|
// as Locator(): the default constructor captures the C++ CALL SITE via
|
|
|
|
|
// std::source_location (and a defaulted Locator parameter captures the
|
|
|
|
|
// CALLER), which is what leaked "argv.cpp, line 597" into user-facing
|
|
|
|
|
// errors (found by the error gallery, 2026-08-22). Locator() is for
|
|
|
|
|
// logging; errors either carry a real document location or this.
|
|
|
|
|
static Locator none() { return Locator(fs::path{}, -1, -1); }
|
2026-08-25 23:30:55 +02:00
|
|
|
// The inverse of str(): "[<file>:<line>.<column>]" back to a Locator.
|
|
|
|
|
// A klammer implementation receives its position as the STATE value
|
|
|
|
|
// K_loc, which is that string; without this it can only pass a
|
|
|
|
|
// default-constructed Locator, which captures the C++ source location
|
|
|
|
|
// and prints a file no user can act on. Anything unparseable gives
|
|
|
|
|
// none() -- a missing location beats a wrong one.
|
|
|
|
|
static Locator from_string(const std::string& s);
|
2026-07-18 18:48:23 +02:00
|
|
|
std::string str(bool relative = false) const;
|
|
|
|
|
std::string desc(bool relative = false) const;
|
|
|
|
|
std::string abbrev(bool include_chr=true) const;
|
|
|
|
|
|
|
|
|
|
std::string m_filename {};
|
|
|
|
|
int m_line;
|
|
|
|
|
int m_chr;
|
|
|
|
|
//std::string m_desc {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::ostream &nformat(std::ostream &os);
|
|
|
|
|
|
|
|
|
|
std::string locator_range(
|
|
|
|
|
const Locator& start_loc, const Locator& end_loc,
|
|
|
|
|
std::map<std::string, std::string>& relpath);
|
|
|
|
|
|
|
|
|
|
Locator current_locator(
|
|
|
|
|
const std::source_location location = std::source_location::current());
|
|
|
|
|
|
|
|
|
|
std::string locator_summary(std::vector<Locator> locators);
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
std::string showloc(Locator loc=Locator()) {
|
|
|
|
|
return loc.abbrev(false) + " ";
|
|
|
|
|
}
|
|
|
|
|
|