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)
170 lines
5.0 KiB
C++
170 lines
5.0 KiB
C++
#include <iostream>
|
|
#include <iomanip>
|
|
#include <algorithm>
|
|
#include <utility>
|
|
|
|
#include "locator.h"
|
|
#include "ktype.h"
|
|
#include "log.h"
|
|
#include "file.h"
|
|
#include "show.h"
|
|
#include "util.h"
|
|
|
|
/*
|
|
bool is_error(katom_t t)
|
|
{
|
|
return
|
|
t == katom_t::undefined ||
|
|
t == katom_t::no_matching_end ||
|
|
t == katom_t::no_matching_begin ||
|
|
t == katom_t::karg_error ||
|
|
t == katom_t::klammer_definition_type_error ||
|
|
t == katom_t::eval_error;
|
|
}
|
|
*/
|
|
|
|
bool is_active(katom_t type)
|
|
{
|
|
return type != katom_t::ignored
|
|
&& type != katom_t::replaced;
|
|
}
|
|
|
|
bool is_printable(katom_t type)
|
|
{
|
|
return printable_katom_types.find(type) != printable_katom_types.end();
|
|
}
|
|
|
|
bool is_deftype(katom_t type)
|
|
{
|
|
return
|
|
type == katom_t::klammer_definition ||
|
|
type == katom_t::klammer_instance ||
|
|
type == katom_t::klammer_override ||
|
|
type == katom_t::klammer_default;
|
|
}
|
|
|
|
std::string pattern_display(Ktype t)
|
|
{
|
|
std::string pat{};
|
|
if (t.m_type == katom_t::space) {
|
|
pat = "<space>";
|
|
} else if (t.m_type == katom_t::newline) {
|
|
pat = "\\n";
|
|
} else if (t.m_type == katom_t::ws_space) {
|
|
pat = "#+ or #+<number>";
|
|
} else if (t.m_type == katom_t::ws_newline) {
|
|
pat = "#/ or #/<number>";
|
|
} else if (t.m_type == katom_t::special) {
|
|
pat = "^@, ^|, ^#, ^:, or ^^";
|
|
} else if (t.m_type == katom_t::apply_end) {
|
|
pat = "@ or <name>@";
|
|
} else if (t.m_type == katom_t::define_end) {
|
|
pat = "@@ or <name>@@";
|
|
} else if (t.m_type == katom_t::machine_end) {
|
|
pat = "@@@ or <name>@@@";
|
|
} else if (t.m_type == katom_t::ws_added) {
|
|
pat = "<space> or \\n";
|
|
} else if (t.m_type == katom_t::word) {
|
|
pat = "<only-letters>";
|
|
} else if (t.m_type == katom_t::text) {
|
|
pat = "<no-special-chars>";
|
|
} else if (t.m_type == katom_t::nonascii) {
|
|
pat = "^<code> or ^<code>^";
|
|
} else {
|
|
pat = t.m_pattern;
|
|
pat = string_replace(pat, definition_name, "<name>");
|
|
pat = string_replace(pat, "\\", "");
|
|
}
|
|
return pat;
|
|
}
|
|
|
|
std::string regex_display(Ktype t)
|
|
{
|
|
std::string rgx{};
|
|
if (t.m_type == katom_t::space) {
|
|
rgx = "<space>";
|
|
} else if (t.m_type == katom_t::newline) {
|
|
rgx = "\\n";
|
|
} else {
|
|
rgx = t.m_pattern;
|
|
}
|
|
return rgx;
|
|
}
|
|
|
|
Ktype find_katom_type(katom_t type)
|
|
{
|
|
for (auto t : katom_types) {
|
|
if (t.m_type == type) {
|
|
return t;
|
|
}
|
|
}
|
|
throw Internal_error("Unknown katom_t: " + std::to_string((int)type));
|
|
}
|
|
|
|
void describe_katoms(bool show_regex)
|
|
{
|
|
size_t name_w = 0;
|
|
size_t pattern_w = 0;
|
|
size_t regex_w = 0;
|
|
size_t description_w = 0;
|
|
std::stringstream ss {};
|
|
ss << R"(
|
|
A "katom" is an individual element in the Klammertext input text.
|
|
Each katom has a type, listed below. You can see how Klammertext
|
|
divides up text into katoms with the "kdiag" command.)" << "\n\n";
|
|
if (show_regex) {
|
|
ss << R"(The "Regex" column is the argument to the std::regex C++ function.)";
|
|
} else {
|
|
ss << R"(In the katom patterns, "<name>" is a word that begins with a letter
|
|
and only contains letters, numbers, or the underscore (_) or period (.) characters. )";
|
|
}
|
|
ss << "A <number> is an integer greater than or equal to 1. "
|
|
"A <code> is one of the non-ASCII codes that are displayed by the command "
|
|
"\"kdesc -c\".";
|
|
|
|
for (auto t : katom_types) {
|
|
name_w = std::max(name_w, t.m_name.size());
|
|
pattern_w = std::max(pattern_w, pattern_display(t).size());
|
|
regex_w = std::max(regex_w, regex_display(t).size());
|
|
description_w = std::max(description_w, t.m_description.size());
|
|
}
|
|
pattern_w++;
|
|
regex_w++;
|
|
|
|
std::cout << justify(ss.str()) << "\n\n"
|
|
<< std::setfill(' ') << boldblack << std::right << std::setw(6) << "Index"
|
|
<< std::left
|
|
<< std::setw(name_w) << " Name" << " ";
|
|
if (show_regex) {
|
|
std::cout << std::setw(regex_w) << " Regex" << " ";
|
|
} else {
|
|
std::cout << std::setw(pattern_w) << " Pattern" << " ";
|
|
}
|
|
std::cout << std::setw(description_w) << "Description"
|
|
<< "\n" << black;
|
|
|
|
for (auto typ : katom_type_display_order) {
|
|
auto t = find_katom_type(typ);
|
|
std::cout << " " << std::right << std::setw(3)
|
|
<< static_cast<int>(t.m_type) << " "
|
|
<< std::left
|
|
<< std::setw(name_w) << t.m_name << " ";
|
|
if (show_regex) {
|
|
std::cout << std::setw(regex_w) << regex_display(t) << " ";
|
|
} else {
|
|
std::cout << std::setw(pattern_w) << pattern_display(t) << " ";
|
|
}
|
|
|
|
std::cout << std::setw(description_w) << t.m_description << "\n";
|
|
}
|
|
std::cout << "\n";
|
|
}
|
|
|
|
|
|
std::string type_to_name(katom_t type)
|
|
{
|
|
auto var = std::find_if(katom_types.begin(), katom_types.end(),
|
|
[&] (Ktype t) { return t.m_type == type; });
|
|
return var->m_name;
|
|
}
|