2026-07-25 21:16:21 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cctype>
|
|
|
|
|
|
2026-07-18 18:48:23 +02:00
|
|
|
#include "target.h"
|
|
|
|
|
#include "log.h"
|
|
|
|
|
#include "show.h"
|
|
|
|
|
//#include "text.h"
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
void Target::add_transform(const std::string& original, const std::string& transformed)
|
2026-07-18 18:48:23 +02:00
|
|
|
{
|
|
|
|
|
m_transforms.push_back({original, transformed});
|
|
|
|
|
}
|
|
|
|
|
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
void Target::add_transforms(const std::string& transforms)
|
2026-07-18 18:48:23 +02:00
|
|
|
{
|
|
|
|
|
add_transforms(parse_transforms(transforms));
|
|
|
|
|
}
|
|
|
|
|
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
void Target::add_transforms(const string_pairs& transforms)
|
2026-07-18 18:48:23 +02:00
|
|
|
{
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
for (const auto& [old_str, new_str] : transforms) {
|
2026-07-18 18:48:23 +02:00
|
|
|
add_transform(old_str, new_str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
void Target::transform(katom_list& katoms) const
|
2026-07-18 18:48:23 +02:00
|
|
|
{
|
|
|
|
|
(void)K::log(3);
|
|
|
|
|
std::for_each(
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
katoms.begin(), katoms.end(),
|
|
|
|
|
[this] (Katom& k) {
|
2026-07-18 18:48:23 +02:00
|
|
|
// std::cout << "transform: " << k << "\n";
|
|
|
|
|
if (k.m_type != katom_t::literal) {
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
for (const auto& [a, b] : this->m_transforms) {
|
2026-07-18 18:48:23 +02:00
|
|
|
// std::cout << " " << a << right_arrow << b << "\n";
|
|
|
|
|
k.m_text = string_replace(k.m_text, a, b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, std::string>>
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
parse_transforms(const std::string& transform_string)
|
2026-07-18 18:48:23 +02:00
|
|
|
{
|
|
|
|
|
(void)K::log(3);
|
|
|
|
|
if (trim(transform_string).empty()) return {};
|
|
|
|
|
auto transforms = regex_split(transform_string, std::regex(R"(\s*\|\s*)"), true);
|
|
|
|
|
std::vector<std::pair<std::string, std::string>> result;
|
|
|
|
|
std::transform(transforms.begin(), transforms.end(), std::back_inserter(result),
|
|
|
|
|
[] (std::string s) {
|
|
|
|
|
strings_t v = word_split(s);
|
|
|
|
|
if (v.size() < 2) return std::pair(std::string{}, std::string{});
|
|
|
|
|
return std::pair(v[0], v[1]);
|
|
|
|
|
});
|
|
|
|
|
// Remove empty pairs
|
|
|
|
|
result.erase(std::remove_if(result.begin(), result.end(),
|
|
|
|
|
[](const auto& p) { return p.first.empty(); }), result.end());
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
void Target::add_escapes(const std::string& escape_spec)
|
2026-07-18 18:48:23 +02:00
|
|
|
{
|
|
|
|
|
auto words = word_split(escape_spec);
|
|
|
|
|
for (size_t i = 0; i + 1 < words.size(); i += 2) {
|
|
|
|
|
m_escapes.push_back({words[i], words[i+1]});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Target::escape_marker(const std::string& ch)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream ss {};
|
|
|
|
|
ss << "KTESC";
|
|
|
|
|
for (unsigned char c : ch)
|
|
|
|
|
ss << std::hex << std::setfill('0') << std::setw(4) << (int)c;
|
|
|
|
|
ss << "KTESC";
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Target::escape_text(std::string text) const
|
|
|
|
|
{
|
|
|
|
|
for (const auto& [ch, repl] : m_escapes) {
|
|
|
|
|
text = string_replace(text, ch, escape_marker(ch));
|
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Target::unescape_text(std::string text) const
|
|
|
|
|
{
|
|
|
|
|
// Restore KTESC markers to original characters (for programmatic use)
|
2026-07-25 21:16:21 +02:00
|
|
|
return ktesc_resolve(text);
|
2026-07-18 18:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string Target::resolve_escapes(std::string text) const
|
|
|
|
|
{
|
2026-07-25 21:16:21 +02:00
|
|
|
// Target-declared escapes first (marker -> declared replacement), then
|
|
|
|
|
// the generic decode for the remaining markers (marker -> the character
|
|
|
|
|
// itself: quoted Klammertext specials and literal-span content).
|
2026-07-18 18:48:23 +02:00
|
|
|
for (const auto& [ch, repl] : m_escapes) {
|
|
|
|
|
text = string_replace(text, escape_marker(ch), repl);
|
|
|
|
|
}
|
2026-07-25 21:16:21 +02:00
|
|
|
return ktesc_resolve(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string ktesc_resolve(std::string text)
|
|
|
|
|
{
|
|
|
|
|
// Hand-rolled scan: no std::regex here, this runs over document-sized
|
|
|
|
|
// strings.
|
|
|
|
|
static const std::string tag = "KTESC";
|
|
|
|
|
size_t pos = 0;
|
|
|
|
|
while ((pos = text.find(tag, pos)) != std::string::npos) {
|
|
|
|
|
size_t start = pos + tag.size();
|
|
|
|
|
size_t close = text.find(tag, start);
|
|
|
|
|
if (close == std::string::npos) break;
|
|
|
|
|
size_t len = close - start;
|
|
|
|
|
bool is_hex = len > 0 && len % 4 == 0 &&
|
|
|
|
|
std::all_of(text.begin() + start, text.begin() + close,
|
|
|
|
|
[](unsigned char c) { return std::isxdigit(c) != 0; });
|
|
|
|
|
if (!is_hex) {
|
|
|
|
|
// Not a marker body; the closing tag may open a real marker.
|
|
|
|
|
pos = start;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
std::string chars {};
|
|
|
|
|
for (size_t i = start; i < close; i += 4)
|
|
|
|
|
chars += (char)std::stoi(text.substr(i, 4), nullptr, 16);
|
|
|
|
|
text.replace(pos, close + tag.size() - pos, chars);
|
|
|
|
|
pos += chars.size();
|
|
|
|
|
}
|
2026-07-18 18:48:23 +02:00
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-25 21:16:21 +02:00
|
|
|
std::string hide_structural_characters(const std::string& s)
|
|
|
|
|
{
|
|
|
|
|
std::string result {};
|
|
|
|
|
for (char c : s) {
|
|
|
|
|
if (c == '@' || c == '|' || c == '#' || c == '^' || c == ':' || c == '*')
|
|
|
|
|
result += Target::escape_marker(std::string(1, c));
|
|
|
|
|
else
|
|
|
|
|
result += c;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
void Target::add_after_apply(const std::string& function_specs)
|
2026-07-18 18:48:23 +02:00
|
|
|
{
|
Klammerset: the @@@klammerset construct, its search path, and const correctness
The @@@klammerset system command formally declares a klammerset — a
named, logically related group of klammer definitions — with an
operative, idempotent declaration (:requires and :files load in order
at the declaration point, relative to the declaring file). A bare
symbol given to ktext -k, kdesc --input, or :requires resolves to
x/x.k on the search path: the document's directory, then
KLAMMERTEXT_KLAMMERSETS, then KLAMMERTEXT_HOME; kdesc --klammerset
lists the available sets. sks/sks.k is the first declared klammerset,
so `-k sks` loads the SKS by name. The engine's lookup classes were
renamed *_set → *_registry to keep the two concepts apart, and the
whole C++ tree now follows standard const-correctness conventions.
tst/ gains klammerset_test.sh (18 cases).
(from dev 64b1abf23e56)
2026-07-30 23:50:07 +02:00
|
|
|
for (const auto& f : regex_split(function_specs, std::regex(R"(\s+;\s+)"), true)) {
|
2026-07-18 18:48:23 +02:00
|
|
|
// msg() << "Add " << m_name << " after-apply: " << f << "\n";
|
|
|
|
|
m_after_apply.push_back(f);
|
|
|
|
|
}
|
|
|
|
|
}
|