Files
klammertext/mac/katom.h
T
ack e4f1307aa9 Characters that killed a render, what may sit next to a klammer, and ref/
A character written in the ^...^ mnemonic form -- ^oe^, ^zz^, ^hello^ --
used to kill ktext outright: no message, no output, and an exit status
that looked like a crash because it was one.  Only the forms that happen
to spell hexadecimal digits escaped, and they were right by luck rather
than by reading.  Any caret form that names no character is now a located
error that quotes what was written.

A klammer may now sit inside square brackets and braces as it already
could inside parentheses, and may be followed by the punctuation that
ends a sentence: [@i word@], {@b word@}, @i word@'s.  The rule is a fixed
list of characters, because none of them can occur inside a klammer name
and so none of them is ambiguous next to a delimiter.  A letter still
cannot: "x@" is the named closing delimiter, so a letter before "@" has
two readings and no rule can separate them.

The abbreviations an author writes -- the hyphen form @i-word, a klammer
glued to its neighbouring punctuation -- are now recognised as author
conveniences only.  They are not applied to @@ definitions or @@@ system
commands, which are written by someone who needs no such shorthand and
whose forms are already whole.

Also fixed, each of them silent: an exported shell function in the
environment broke every @eval in the document; a line break written with
@nl was destroyed when it passed through a klammer that generates a
table, printing as \textbackslash{}newline in the finished PDF; and
"kdesc --coverage" shortened its file column by comparing the spelling of
a path rather than the path, so a klammer set reached by a symbolic link
showed a full path where its neighbours showed a short one.

New in the distribution: ref/, a small klammerset that describes the
Klammermachine's own tables in Klammertext.  It currently provides the
character tables -- the diacritics and the extended Latin letters -- as
klammers that render to any target, so the documentation of what can be
written cannot drift from what the machine accepts.  It is early, and
will grow.

The Klammertext taxonomy now has a section on targets, describes the
@@@klammerset system command alongside the other three, and states the
precedence of "^" -- what a caret means before a letter, a digit, a
punctuation character, and a quote, in one ordered list.

(from dev 6594c7ff8c1f)
2026-09-07 22:37:05 +02:00

215 lines
6.5 KiB
C++

#pragma once
#include <limits>
#include <memory>
#include "ktype.h"
#include "locator.h"
#include "util.h"
class Katom
{
public:
static size_t index;
Katom(const std::string& src, katom_t type, Locator loc);
// Copy constructor
Katom(const Katom& other)
: m_index(other.m_index)
, m_text(other.m_text)
, m_src(other.m_src)
, m_loc(other.m_loc)
, m_type(other.m_type)
, m_initial_type(other.m_initial_type)
, m_display(other.m_display)
, m_unparsed(other.m_unparsed)
, m_deferred(other.m_deferred)
{}
// Copy assignment operator
Katom& operator=(const Katom& other) {
if (this != &other) {
m_index = other.m_index;
m_text = other.m_text;
m_src = other.m_src;
m_loc = other.m_loc;
m_type = other.m_type;
m_initial_type = other.m_initial_type;
m_display = other.m_display;
m_unparsed = other.m_unparsed;
m_deferred = other.m_deferred;
}
return *this;
}
static inline bool show_index;
static inline bool show_type;
static inline bool show_id;
static inline bool show_whitespace;
static inline bool show_all;
static inline bool show_replaced;
static inline bool show_ignored;
bool is_whitespace() const {
return m_initial_type == katom_t::space
|| m_initial_type == katom_t::newline
|| m_type == katom_t::ws_added; }
bool is_word() const { return m_initial_type == katom_t::word; }
bool is_text() const { return m_initial_type == katom_t::text; }
//bool is_text() { return m_initial_type == katom_t::text; }
bool is_literal() const { return m_type == katom_t::literal; }
bool is_active() const {
return m_type != katom_t::ignored
&& m_type != katom_t::replaced; };
bool is_nonascii() const {
return m_type == katom_t::nonascii; };
size_t m_index;
std::string m_text{};
std::string m_src{};
Locator m_loc;
katom_t m_type;
katom_t m_initial_type;
std::string m_display {};
// Set when the word matched no katom type and fell back to katom_t::word.
// The warning is deferred to warn_unparsed_katoms(), after removal and
// literal marking, so removed text (comments, #[...]# blocks) never warns.
bool m_unparsed {};
// Inside an unresolved @cond span, and therefore INERT: the passes with
// observable effects -- @eval and @read -- skip it, so a branch that is
// never selected never runs anything. @cond is a non-strict special form
// (doc/cond_evaluation_order.md), and this flag is the mechanism; the flag
// is not the semantics. Cleared on the selected branch when the @cond is
// resolved in the apply fold, which then processes that branch normally.
bool m_deferred {};
};
bool active(const std::vector<Katom>& katoms);
int active_count(const std::vector<Katom>& katoms);
void restore_initial_type(std::vector<Katom>::iterator begin, std::vector<Katom>::iterator end);
void modify_type(katom_t new_type, std::vector<Katom>::iterator begin, std::vector<Katom>::iterator end);
void modify_type(katom_t old_type, katom_t new_type,
std::vector<Katom>::iterator begin, std::vector<Katom>::iterator end);
void ignore_whitespace(std::vector<Katom>::iterator& begin, std::vector<Katom>& katoms);
std::vector<Katom>::iterator after_whitespace(std::vector<Katom>::iterator begin);
std::vector<Katom> text_katoms(
std::vector<Katom>::iterator& begin, std::vector<Katom>::iterator& end);
std::string as_string(std::vector<Katom>::const_iterator begin, std::vector<Katom>::const_iterator end, bool strip_whitespace);
std::string as_string(const std::vector<Katom>& katoms, bool strip_whitespace);
std::vector<Katom> trim(std::vector<Katom>& katoms, std::set<katom_t> trim_types = {katom_t::space, katom_t::newline});
std::vector<Katom> trim(const std::vector<Katom>& katoms, bool trim_inactive = false);
std::vector<std::vector<Katom>> bar_split(std::vector<Katom>::iterator kbegin, std::vector<Katom>::iterator kend);
void process_whitespace_modifiers(std::vector<Katom>& katoms);
std::vector<Katom> trim_whitespace(std::vector<Katom> katoms);
// escape_backslash() and unescape_backslash() (the ___BS___ hack) were
// removed. Backslash is now handled by the general target escape mechanism
// via the :escape parameter on @@@target.
inline bool is_bar(const Katom& k) {
return k.m_type == katom_t::bar;
}
inline bool is_nonascii(const Katom& k) {
return k.m_type == katom_t::nonascii;
}
inline bool is_newline(const Katom& k) {
return k.m_type == katom_t::newline;
}
inline bool is_ignore_rest(const Katom& k) {
return k.m_type == katom_t::ignore_rest;
}
inline bool is_ignore_line(const Katom& k) {
return k.m_type == katom_t::ignore_line;
}
inline void mark_as_replaced(Katom& k) {
k.m_type = katom_t::replaced;
}
inline void mark_as_literal(Katom& k) {
k.m_type = katom_t::literal;
}
inline void mark_as_ignored(Katom& k) {
k.m_type = katom_t::ignored;
}
inline bool begin_read(Katom& k) {
return k.m_type == katom_t::read_begin;
}
inline bool begin_ignore(const Katom& k) {
return k.m_type == katom_t::ignore_begin;
}
inline bool end_ignore(const Katom& k) {
return k.m_type == katom_t::ignore_end;
}
inline bool begin_literal(const Katom& k) {
return k.m_type == katom_t::literal_begin;
}
inline bool end_literal(const Katom& k) {
return k.m_type == katom_t::literal_end;
}
inline bool begin_eval(const Katom& k) {
return k.m_type == katom_t::eval_begin;
}
inline bool begin_cond(const Katom& k) {
return k.m_type == katom_t::cond_begin;
}
inline bool begin_klammer_def(const Katom& k) {
return k.m_type == katom_t::define_begin;
}
inline bool end_klammer_def(const Katom& k) {
return k.m_type == katom_t::define_end;
}
inline bool begin_klammer_apply(const Katom& k) {
return k.m_type == katom_t::apply_begin;
}
inline bool end_klammer_apply(const Katom& k) {
return k.m_type == katom_t::apply_end;
}
inline bool begin_machine_def(const Katom& k) {
return k.m_type == katom_t::machine_begin;
}
inline bool end_machine_def(const Katom& k) {
return k.m_type == katom_t::machine_end;
}
inline bool begin_apply(const Katom& k)
{
const std::set<katom_t> opens {
katom_t::read_begin,
katom_t::eval_begin,
katom_t::cond_begin,
katom_t::apply_begin};
return opens.contains(k.m_type);
}
inline bool end_apply(const Katom& k)
{
return k.m_type == katom_t::apply_end;
}