An output policy for the three commands, and @cond as a true special form
A snapshot of the development tree. The substantial changes since the last one:
COMMAND OUTPUT POLICY. The three commands display text in exactly three cases,
and each owns a stream: LOGGING under "-v" greater than 0 and an ERROR before
termination go to STDERR; OUTPUT THE USER ASKED FOR goes to STDOUT. For ktext
that output is a document, so "ktext doc.kt -d | ..." is now safe -- logging
used to share the stream and land inside the document. A bare command prints
its usage and succeeds rather than failing. Colour is emitted only to a
terminal, per stream, and NO_COLOR is honoured.
"-v 1" reports every decision whose outcome you could not have read off your own
input: the klammerset that was loaded and from which file, a font's directory, a
":files" name's file, how "-o" was expanded. Higher levels are the trace.
The commands no longer warn and continue: an anomaly is an error, described with
its location. Two exceptions remain, each for a stated reason -- a condition
that is expected and temporary by design, and a judgment that is a heuristic
rather than exact.
@cond IS NOW A TRUE SPECIAL FORM, resolved at APPLICATION time rather than when
the file is read. Two consequences for a writer:
* a state variable reaches the predicate. "@@@state Flag :value true @@@
@cond *Flag* | T | F @" renders "T"; it used to see the literal "*Flag*" and
silently take the false branch. The document now behaves like a klammer
body, whose arguments are bound before its conditionals are decided.
* nothing in a discarded branch happens -- it is not read, not evaluated, not
expanded. An @eval in the branch not taken used to run anyway.
Its predicate relation is total and strict: true, True, 1; false, False, 0, and
empty; anything else is an error at the @cond rather than silently false.
@eval REACHING OUTSIDE. ":shell" and ":haskell" now keep the command's standard
error out of the document (it appears under "-v 1") and treat a nonzero exit as
an error naming what the command reported. A command that exits nonzero on
purpose -- "grep" finding no match -- says so with "|| true".
KLAMMER SETS. Several combine: "--klammersets a b c" loads all three in the
order given, sharing one namespace, with the definition modes deciding
collisions. "none" means none and may not be combined with other symbols. A
klammerset with symbol X is declared in a file X/X.k, which is what lets two
sets require the same third set without loading it twice.
TESTS. Four new suites: the kdiag command's interface, the @eval primitive's
contract with the outside world, and verbosity at both tiers. Three suites
that could not run on macOS at all now do.
Assembled from dev commit 6c8ee6c22fca.
This commit is contained in:
174
com/kdesc.cpp
174
com/kdesc.cpp
@@ -16,18 +16,18 @@ static void font_usage()
|
||||
{
|
||||
std::cout <<
|
||||
"Font maintenance commands:\n"
|
||||
" --font List the available fonts\n"
|
||||
" --font list The same\n"
|
||||
" --font samples <output-dir> Write a sample page of the available fonts\n"
|
||||
" to <output-dir>/index.html\n"
|
||||
" --font List the available fonts\n"
|
||||
" --font list The same\n"
|
||||
" --font samples <output-dir> Write a sample page of the available fonts\n"
|
||||
" to <output-dir>/index.html\n"
|
||||
" --font samples <input-dir> <output-dir>\n"
|
||||
" Write a sample page for the (not yet\n"
|
||||
" installed) font files under <input-dir>\n"
|
||||
" --font install <input-dir> Install the font files found under\n"
|
||||
" <input-dir> into the user font directory\n"
|
||||
" Write a sample page for the (not yet\n"
|
||||
" installed) font files under <input-dir>\n"
|
||||
" --font install <input-dir> Install the font files found under\n"
|
||||
" <input-dir> into the user font directory\n"
|
||||
" --font install <input-dir> <output-dir>\n"
|
||||
" Install into <output-dir> instead\n"
|
||||
" --font help This description\n"
|
||||
" Install into <output-dir> instead\n"
|
||||
" --font help This description\n"
|
||||
"\n"
|
||||
"Fonts are searched in the directories of the KLAMMERTEXT_FONTS\n"
|
||||
"environment variable (colon-separated; default $HOME/.klammertext/fonts)\n"
|
||||
@@ -38,11 +38,11 @@ static void coverage_usage()
|
||||
{
|
||||
std::cout <<
|
||||
"Coverage commands:\n"
|
||||
" --coverage Report the targets each klammer covers\n"
|
||||
" --coverage all Also name the file each klammer is written\n"
|
||||
" in, and show every \"needs attention\"\n"
|
||||
" category, including the empty ones\n"
|
||||
" --coverage help This description\n"
|
||||
" --coverage Report the targets each klammer covers\n"
|
||||
" --coverage all Also name the file each klammer is written\n"
|
||||
" in, and show every \"needs attention\"\n"
|
||||
" category, including the empty ones\n"
|
||||
" --coverage help This description\n"
|
||||
"\n"
|
||||
"Coverage is a fact about a klammer: the targets it can render to. It is\n"
|
||||
"DERIVED where the definitions determine it (a general body of klammer\n"
|
||||
@@ -60,10 +60,9 @@ static void katom_usage()
|
||||
{
|
||||
std::cout <<
|
||||
"Katom commands:\n"
|
||||
" --katoms List the katom types\n"
|
||||
" --katoms full The same, with the regex each type is\n"
|
||||
" matched by\n"
|
||||
" --katoms help This description\n";
|
||||
" --katoms List the katom types\n"
|
||||
" --katoms full The list, with the regex syntax used for a march\n"
|
||||
" --katoms help This description\n";
|
||||
}
|
||||
|
||||
// The words of a variadic option, with the empty ones dropped.
|
||||
@@ -82,33 +81,47 @@ static void klammerset_usage()
|
||||
{
|
||||
std::cout <<
|
||||
"Klammerset commands:\n"
|
||||
" --klammerset List the klammersets on the search path\n"
|
||||
" --klammerset list The same\n"
|
||||
" --klammerset help This description\n"
|
||||
" --klammersets List the klammersets on the search path\n"
|
||||
" --klammersets <symbols> Load the klammersets specified by <symbols>\n"
|
||||
" --klammersets help This description\n"
|
||||
"\n"
|
||||
"Without an argument, all available klammersets are listed. With one or more\n"
|
||||
"klammerset symbols, those klammersets are loaded for analysis by the -k, -t,\n"
|
||||
"--argtypes, --optionsets, and --coverage arguments.\n\n"
|
||||
"SEVERAL SETS COMBINE. The symbols are a list, loaded in the order given, and\n"
|
||||
"the klammers of all of them share ONE flat namespace -- membership in a set is\n"
|
||||
"provenance, not containment. Where two sets define the same klammer, the\n"
|
||||
"definition modes decide: \":\" is an error if the name is already defined,\n"
|
||||
"\":::\" replaces the earlier definition (with a warning), and \"::::\" yields to\n"
|
||||
"any later one. So a set of house overrides is loaded after the set it\n"
|
||||
"adjusts, and a set of defaults before it.\n\n"
|
||||
"The Standard Klammer Set is loaded by default. If the <symbols> argument is\n"
|
||||
"\"none\", no klammerset is loaded.\n\n"
|
||||
"A klammerset symbol x names the declaration file x/x.k, searched for in:\n"
|
||||
"the current directory (for ktext, the input document's directory), the\n"
|
||||
"KLAMMERTEXT_KLAMMERSETS directories (colon-separated; default\n"
|
||||
"$HOME/.klammertext/klammersets), and $KLAMMERTEXT_HOME. The first hit\n"
|
||||
"1) the current directory (for ktext, the input document's directory); 2) the\n"
|
||||
"KLAMMERTEXT_KLAMMERSETS directories (colon-separated; the default is\n"
|
||||
"$HOME/.klammertext/klammersets); and 3) $KLAMMERTEXT_HOME. The first hit\n"
|
||||
"wins, so a document-local klammerset shadows an installed one, which\n"
|
||||
"shadows a distributed one.\n";
|
||||
}
|
||||
|
||||
static void klammerset_command(const strings_t& words)
|
||||
static void klammerset_command(Machine& machine, const strings_t& words)
|
||||
{
|
||||
std::string verb = words.empty() ? "list" : words[0];
|
||||
if (verb == "list") {
|
||||
if (words.empty()) {
|
||||
std::cout << boldblack << "Klammersets\n" << black
|
||||
<< describe_klammerset_search(fs::current_path().string());
|
||||
} else if (verb == "help") {
|
||||
} else if (words[0] == "help") {
|
||||
klammerset_usage();
|
||||
} else {
|
||||
std::cout << "Unrecognized klammerset command: --klammerset "
|
||||
<< join(words, " ") << "\n\n";
|
||||
klammerset_usage();
|
||||
// "none" is not filtered out here: load_klammersets() is the single
|
||||
// place that decides what the word means, including that it may not be
|
||||
// combined with other symbols. Deciding it twice is how kdesc came to
|
||||
// accept "--klammersets none sks" and silently load neither.
|
||||
load_klammersets(machine, words);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void font_command(const strings_t& words)
|
||||
{
|
||||
std::string verb = words.empty() ? "list" : words[0];
|
||||
@@ -147,21 +160,24 @@ int main(int argc, char* argv[])
|
||||
// regexes and the coverage file column are words of their own options
|
||||
// rather than verbosity levels.
|
||||
args.var("k", "Show klammers. With <text>, only those whose name or description contains <text>.", "text");
|
||||
args.flag("t", "Show the targets for Klammertext output");
|
||||
args.flag("c", "Show the codes for accented vowels and other special characters");
|
||||
args.opt("i", "Input filename. If given, it is used instead of the SKS.", "filename", "", "'text'");
|
||||
args.flag("t", "Show the targets for Klammertext output");
|
||||
args.opt("i", "Input filename to analyze containing klammer and other definitions", "filename", "", "'text'");
|
||||
args.var("klammersets", "List the available klammersets, or load one or more for analysis. Enter \"--klammersets help\" for details.", "symbols");
|
||||
args.var("font", "List installed fonts. Enter \"--font help\" for font maintenance commands.");
|
||||
args.flag("argtypes", "Show the klammer argument types");
|
||||
args.var("katoms", "Show the katom types. Enter \"--katoms help\" for details.");
|
||||
args.flag("rewrite", "Show the katom rewrite patterns");
|
||||
args.flag("optionsets", "Show the option sets declared by the input");
|
||||
args.var("coverage", "Show the targets each klammer covers. Enter \"--coverage help\" for details.");
|
||||
args.var("klammerset", "List the klammersets on the search path. Enter \"--klammerset help\" for details.");
|
||||
args.opt("v", "'verbosity'", "n", "0", "'verbosity'");
|
||||
|
||||
if (show_usage(argc, argv)) {
|
||||
// A bare command is a REQUEST for information, not a failure:
|
||||
// usage goes to stdout (it is the result being asked for) and the
|
||||
// exit status is 0, so "kdesc && echo ok" reports what happened.
|
||||
args.usage(file_basename(argv[0]));
|
||||
exit(1);
|
||||
exit(0);
|
||||
}
|
||||
auto p = [&](const std::string& name) { return args.get(name) == "true"; };
|
||||
|
||||
@@ -208,11 +224,45 @@ int main(int argc, char* argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The --klammerset subcommands operate on the search path
|
||||
// (filesystem enumeration) and load no klammer set.
|
||||
if (args.given("klammerset")) {
|
||||
klammerset_command(option_words(args, "klammerset"));
|
||||
return 0;
|
||||
Machine M;
|
||||
// Same rule as kdiag: a command that specifies no target evaluates
|
||||
// under the GENERAL target (Andy, 2026-08-15). kdesc has no target
|
||||
// argument either -- it describes what a klammer set provides for ALL
|
||||
// targets -- and a designer's ".k" holding a top-level @eval hit the
|
||||
// identical "Variable "K_target" not defined".
|
||||
M.m_state.set("K_target", Target_registry::general_name);
|
||||
|
||||
strings_t input_filenames = resolve_filename_list(args.get("i"));
|
||||
if (verbose_level > 0 && !input_filenames.empty()) {
|
||||
std::cout << "input_filenames: " << input_filenames << "\n";
|
||||
}
|
||||
|
||||
if (!input_filenames.empty()) {
|
||||
M.m_state.set(
|
||||
"K_input_dir", absolute_pathname(file_directory(input_filenames[0])));
|
||||
} else {
|
||||
M.m_state.set("K_input_dir", fs::current_path().string());
|
||||
}
|
||||
|
||||
// --klammersets both lists and loads: with no words it enumerates the
|
||||
// search path, with symbols it loads them for the analyses below.
|
||||
if (args.given("klammersets")) {
|
||||
klammerset_command(M, option_words(args, "klammersets"));
|
||||
} else { // By default, SKS is loaded for analysis
|
||||
M.read(fs::path(M.m_state.value("KLAMMERTEXT_HOME") + "/sks/sks.k"));
|
||||
}
|
||||
|
||||
// Snapshot BEFORE the input is read. The distinction is not "declared
|
||||
// by a klammerset" but "loaded as context": an input file may itself
|
||||
// declare @@@klammerset -- that is how a designer writes one -- and
|
||||
// its klammers are still the ones being asked about.
|
||||
strings_t context_files = M.m_klammersets.loaded_files();
|
||||
|
||||
for (auto fname : input_filenames) {
|
||||
if (verbose_level > 0) {
|
||||
std::cout << "Read " << fname << "\n";
|
||||
}
|
||||
M.read(fs::path(absolute_pathname(fname)));
|
||||
}
|
||||
|
||||
// --coverage is validated here, before a klammer set is read, so a
|
||||
@@ -236,41 +286,31 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
}
|
||||
|
||||
Machine M;
|
||||
|
||||
strings_t input_filenames = resolve_filename_list(args.get("i"));
|
||||
if (verbose_level > 0) {
|
||||
std::cout << "input_filenames: " << input_filenames << "\n";
|
||||
}
|
||||
if (input_filenames.empty()) {
|
||||
M.read(fs::path(M.m_state.value("KLAMMERTEXT_HOME") + "/sks/sks.k"));
|
||||
} else {
|
||||
for (auto fname : input_filenames) {
|
||||
if (is_klammerset_symbol(fname)) {
|
||||
// A bare symbol resolves on the klammerset search
|
||||
// path; kdesc has no input document, so the local
|
||||
// stage is the cwd.
|
||||
fname = resolve_klammerset_symbol(
|
||||
fname, fs::current_path().string(), Locator()).string();
|
||||
}
|
||||
if (verbose_level > 0) {
|
||||
std::cout << "Read " << fname << "\n";
|
||||
}
|
||||
M.read(fs::path(absolute_pathname(fname)));
|
||||
}
|
||||
strings_t own_klammers_only {};
|
||||
if (!input_filenames.empty()) {
|
||||
own_klammers_only = context_files;
|
||||
}
|
||||
|
||||
if (p("t")) {
|
||||
std::cout << boldblack << "Targets\n" << black << M.m_targets.describe(2, true);
|
||||
std::cout << boldblack << "Targets\n" << black
|
||||
<< M.m_targets.describe(2, true, own_klammers_only);
|
||||
}
|
||||
|
||||
// "-k <text>" searches names AND descriptions, case-insensitively,
|
||||
// with whitespace collapsed on both sides. An empty listing for a
|
||||
// search that was actually made is reported: silence would read as a
|
||||
// broken command. It is not an error -- finding nothing is a result.
|
||||
// With an input file, the klammersets are loaded so the input can be
|
||||
// ANALYSED against them, but they are not what the user is asking
|
||||
// about: a designer wants the klammers this file defines, and a reader
|
||||
// of an unfamiliar document wants the custom klammers it carries. So
|
||||
// the listing is restricted to klammers with a definition outside the
|
||||
// klammerset files. Without -i there is nothing else to show, so the
|
||||
// klammerset itself is the answer and no filter applies.
|
||||
if (args.given("k")) {
|
||||
std::string search = join(option_words(args, "k"), " ");
|
||||
std::string listing = M.m_klammers.describe(2, search);
|
||||
std::string listing = M.m_klammers.describe(2, search, own_klammers_only);
|
||||
if (listing.empty() && !search.empty()) {
|
||||
std::cout << "No klammer names or descriptions contained "
|
||||
<< q_(collapse_whitespace(search)) << ".\n";
|
||||
@@ -280,15 +320,15 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
|
||||
if (p("optionsets")) {
|
||||
std::cout << boldblack << "Option sets\n" << black << M.m_option_sets.describe(2);
|
||||
std::cout << boldblack << "Option sets\n" << black
|
||||
<< M.m_option_sets.describe(2, own_klammers_only);
|
||||
}
|
||||
|
||||
// Analysis only: klammer_coverage() reads the registry and modifies
|
||||
// nothing, so what a document renders to is unaffected by asking.
|
||||
if (args.given("coverage")) {
|
||||
report_coverage(M, klammer_coverage(M), coverage_all, std::cout);
|
||||
report_coverage(M, klammer_coverage(M, own_klammers_only), coverage_all, std::cout);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Error& e) {
|
||||
// Nonzero, as ktext does: a command that prints an error and exits 0
|
||||
|
||||
118
com/kdiag.cpp
118
com/kdiag.cpp
@@ -3,6 +3,7 @@
|
||||
#include <map>
|
||||
|
||||
#include "util.h"
|
||||
#include "check.h"
|
||||
#include "argument_set.h"
|
||||
#include "argv.h"
|
||||
#include "argument.h"
|
||||
@@ -19,7 +20,11 @@ int main(int argc, char* argv[])
|
||||
set_verbose_level(argc, argv);
|
||||
|
||||
Argv args {};
|
||||
args.req("input", "Klammertext input text", "'text'");
|
||||
// Optional: "kdiag --machine" with no input shows the Machine's initial
|
||||
// state, which is a question about the machine rather than about any
|
||||
// document -- and the one thing a programmer wants before feeding it
|
||||
// anything.
|
||||
args.req("input", "Klammertext input text", "'text'", false);
|
||||
args.flag("type", "Show katom types in subscript");
|
||||
args.flag("index", "Show the list index of the katom");
|
||||
args.flag("text", "Show text katoms with selected attributes");
|
||||
@@ -37,13 +42,27 @@ int main(int argc, char* argv[])
|
||||
args.flag("literal", "Process literal: ^'...'^");
|
||||
args.flag("ignore", "Process ignored: #, ##, #[...]#");
|
||||
args.flag("ws", "Process whitespace: #-, #+, #/");
|
||||
args.flag("klammer", "Process klammer definitions: @@<name> ... @@");
|
||||
args.flag("process", "Process all");
|
||||
args.flag("klammer", "Register klammer definitions: @@<name> ... @@. Their katoms "
|
||||
"are consumed, so they appear only with --replaced.");
|
||||
args.flag("system", "Register machine definitions: @@@target, @@@argtype, @@@state, "
|
||||
"@@@klammerset. Their katoms are consumed, so they appear only "
|
||||
"with --replaced. The two tiers are ORDERED: a klammer names a "
|
||||
"target, so --klammer alone cannot register a klammer whose "
|
||||
"target the input itself declares. That is not an error here: "
|
||||
"the definition is skipped and its katoms stay visible, which is "
|
||||
"the report.");
|
||||
args.flag("process", "Process all, both tiers of definition included");
|
||||
args.flag("check", "Check every klammer application in the input and in the "
|
||||
"body of every defined klammer");
|
||||
args.flag("machine", "Show the state of the Klammermachine after all processing");
|
||||
args.opt("v", "'verbosity'", "degree", "0", "'verbosity'");
|
||||
|
||||
if (show_usage(argc, argv)) {
|
||||
// A bare command is a REQUEST for information, not a failure:
|
||||
// usage goes to stdout (it is the result being asked for) and the
|
||||
// exit status is 0, so "kdiag && echo ok" reports what happened.
|
||||
args.usage(file_basename(argv[0]));
|
||||
exit(1);
|
||||
exit(0);
|
||||
}
|
||||
auto p = [&](const std::string& name) { return args.get(name) == "true"; };
|
||||
|
||||
@@ -57,19 +76,48 @@ int main(int argc, char* argv[])
|
||||
show_rewrite_rules = true;
|
||||
}
|
||||
|
||||
// kdiag deliberately has no --klammersets: a klammerset is reached here
|
||||
// the way the input reaches anything else, with "@read sks/sks.k @".
|
||||
// The symbol mechanism is a higher-level convenience, and a debugger
|
||||
// must not depend on the machinery it debugs -- if symbol resolution
|
||||
// breaks, kdiag must still run, and the two paths to the same file can
|
||||
// then be compared.
|
||||
Machine machine;
|
||||
// kdiag has no target argument, and evaluating needs one: Eval::eval
|
||||
// re-reads its result under K_target, so with the variable unset every
|
||||
// @eval died with "Variable "K_target" not defined" -- an error naming
|
||||
// something the user never wrote. A command that does not specify a
|
||||
// target evaluates under the GENERAL target (Andy, 2026-08-15), which
|
||||
// is also what kdiag means: it loads no klammerset, so nothing
|
||||
// target-specific is in scope to begin with.
|
||||
machine.m_state.set("K_target", Target_registry::general_name);
|
||||
machine.m_state.set("K_input_dir", fs::current_path().string());
|
||||
std::string input = args.as_string("input");
|
||||
std::string command = construct_command_pathname(argv[0]);
|
||||
katom_list katoms {};
|
||||
if (p("process")) {
|
||||
katoms = machine.process(input, command);
|
||||
} else {
|
||||
katoms = machine.process(
|
||||
//args.as_string("input"), construct_command_pathname(argv[0]),
|
||||
input, command,
|
||||
p("nonascii"), p("literal"), p("ignore"), p("ws"), p("klammer"),
|
||||
p("eval"), p("cond"), p("read"));
|
||||
}
|
||||
// ONE authoritative katom list. process() returns the katoms but
|
||||
// registers nothing -- read() is what appends to m_katoms and extracts
|
||||
// the definitions -- so kdiag does that itself, in the order read()
|
||||
// uses, and then displays m_katoms. Displaying its own copy instead
|
||||
// would show the definition katoms unconsumed, because extraction
|
||||
// marks them "replaced" in the Machine's list, not in a copy.
|
||||
bool all = p("process");
|
||||
katom_list processed = all
|
||||
? machine.process(input, command)
|
||||
: machine.process(input, command,
|
||||
p("nonascii"), p("literal"), p("ignore"), p("ws"),
|
||||
p("klammer"), p("eval"), p("cond"), p("read"));
|
||||
machine.m_katoms.insert(
|
||||
machine.m_katoms.end(), processed.begin(), processed.end());
|
||||
// Machine definitions first: a klammer definition names a target, so
|
||||
// the target must be registered before the klammer that uses it.
|
||||
// Tolerant: a definition that cannot be registered because the other
|
||||
// tier was not run is skipped, not fatal -- its katoms stay visible.
|
||||
// kdiag dissects structure and typing, which means allowing modes of
|
||||
// processing that would be errors in ktext.
|
||||
if (all || p("system")) machine.extract_machine_definitions(true);
|
||||
if (all || p("klammer")) machine.extract_klammer_definitions(true);
|
||||
katom_list& katoms = machine.m_katoms;
|
||||
|
||||
if (p("spans")) {
|
||||
describe_spans(katoms);
|
||||
} else {
|
||||
@@ -79,7 +127,9 @@ int main(int argc, char* argv[])
|
||||
if (p("text")) std::cout << kall;
|
||||
if (p("all")) std::cout << kall << kreplaced << kignored;
|
||||
if (p("replaced")) std::cout << kreplaced;
|
||||
std::cout << katoms << "\n";
|
||||
if (!p("check")) {
|
||||
std::cout << katoms << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (p("args")) {
|
||||
@@ -105,11 +155,45 @@ int main(int argc, char* argv[])
|
||||
std::cout << std::right << std::setw(label_width) << "rest:" << " " << rest << "\n";
|
||||
}
|
||||
}
|
||||
int check_errors = 0;
|
||||
if (p("check")) {
|
||||
check_errors = report_diagnostics(check_machine(machine, katoms, "*"), std::cout);
|
||||
// The commonest cause of a wall of "not defined" is that nothing
|
||||
// was registered. The advice belongs here rather than in the
|
||||
// catch block: --check REPORTS, it does not throw, so an exception
|
||||
// handler never sees this case. The condition is the CAUSE, not a
|
||||
// proxy for it.
|
||||
if (check_errors > 0 && machine.m_klammers.m_klammers.empty()) {
|
||||
std::cout << "\nNo klammers are registered, so every application is "
|
||||
"undefined.\nkdiag loads no klammerset: read one in the input, "
|
||||
"as \"@read sks/sks.k @\",\nand add --process (or --system "
|
||||
"--klammer --read) to register what it defines.\n";
|
||||
}
|
||||
}
|
||||
// Last, and after everything else: the state shown is the result of
|
||||
// whatever the other arguments did. This is why --check defers its
|
||||
// exit status rather than returning as soon as it finds errors.
|
||||
if (p("machine")) {
|
||||
std::cout << machine;
|
||||
}
|
||||
if (check_errors > 0) {
|
||||
std::cout << black;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Error& e) {
|
||||
// The advice must name a way in that kdiag actually has. It named
|
||||
// "--klammersets sks" until 2026-08-15, a flag the 2026-08-14 redesign
|
||||
// deliberately removed from this command -- a debugger must not depend
|
||||
// on the machinery it debugs -- so the one remedy offered was one that
|
||||
// errors. Same wording as the --check hint above, for the same reason.
|
||||
std::string advice = "";
|
||||
if (e.m_type == "target")
|
||||
advice = "To include the Standard Klammer Set, add flag \"--sks\".";
|
||||
if (e.m_type == "target" || e.m_type == "definition") {
|
||||
advice = "kdiag loads no klammerset: read one in the input, as "
|
||||
"\"@read sks/sks.k @\", and add --process to register what it "
|
||||
"defines.";
|
||||
}
|
||||
e.print_message(advice);
|
||||
// Nonzero, as ktext does: a command that prints an error and exits 0
|
||||
// reports success, and a script cannot tell the difference.
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <iostream>
|
||||
#include "error.h"
|
||||
#include "check.h"
|
||||
#include "command.h"
|
||||
#include "log.h"
|
||||
#include "argv.h"
|
||||
@@ -22,18 +21,20 @@ int main(int argc, char* argv[])
|
||||
Target_registry::general_name, "'word'");
|
||||
args.opt("o", "Output basename; meaning and default defined by target.", "basename",
|
||||
"", "'word'");
|
||||
args.opt("k", "Klammerset symbol (resolved on the klammerset search path) or the pathname of a klammerset definition file; default is the Standard Klammer Set. With a value of \"none\", no klammerset is loaded.",
|
||||
"pathname", "", "'word'");
|
||||
args.flag("d", "Display the output to the screen, rather than writing files.");
|
||||
args.flag("m", "Show the Klammermachine state at the beginning of processing.");
|
||||
args.flag("check", "Check every klammer application in the input and in the "
|
||||
"body of every defined klammer, report all problems found, and exit "
|
||||
"without producing output.");
|
||||
args.flag("d", "Display the output to the screen, rather than writing a file.");
|
||||
args.opt("klammersets", "Klammersets to load, as a list of symbols resolved on the "
|
||||
"klammerset search path. Several combine: they are loaded in the order given "
|
||||
"and share one namespace. The default is the Standard Klammer Set; "
|
||||
"\"none\" loads none.",
|
||||
"symbols", "", "'list'");
|
||||
args.opt("v", "'verbosity'", "degree", "0", "'verbosity'");
|
||||
|
||||
if (show_usage(argc, argv)) {
|
||||
// A bare command is a REQUEST for information, not a failure:
|
||||
// usage goes to stdout (it is the result being asked for) and the
|
||||
// exit status is 0, so "ktext && echo ok" reports what happened.
|
||||
args.usage(file_basename(argv[0]));
|
||||
exit(1);
|
||||
exit(0);
|
||||
}
|
||||
args.parse(argc, argv);
|
||||
if (verbose_level > 0) {
|
||||
@@ -56,19 +57,6 @@ int main(int argc, char* argv[])
|
||||
parse_args(input_filenames, args.as_string("t"),
|
||||
expand_tilde(args.as_string("o")), args.as_bool("d"));
|
||||
|
||||
// --check produces no output, so it needs neither a target nor -d.
|
||||
// With no -t it checks every defined target, which is the more useful
|
||||
// default here: "does this document hold together at all?"
|
||||
bool check_only = args.as_bool("check");
|
||||
|
||||
if (*(output_filename.end() - 1) == '*'
|
||||
&& !display_only && !check_only) {
|
||||
throw Argument_error(
|
||||
"You must specify an output target or "
|
||||
"display the results with the \"-d\" flag.",
|
||||
Locator());
|
||||
}
|
||||
|
||||
Machine M;
|
||||
M.m_state.open_frame("ktext");
|
||||
M.m_state.set("K_target", target);
|
||||
@@ -89,20 +77,7 @@ int main(int argc, char* argv[])
|
||||
M.m_state.set("K_input_dir", fs::current_path().string());
|
||||
}
|
||||
|
||||
std::string klammerset_filename = args.as_string("k");
|
||||
if (klammerset_filename != "none") {
|
||||
if (klammerset_filename.empty()) {
|
||||
klammerset_filename = M.m_state.value("KLAMMERTEXT_HOME") + "/sks/sks.k";
|
||||
} else if (is_klammerset_symbol(klammerset_filename)) {
|
||||
// A bare symbol resolves on the klammerset search path;
|
||||
// the local stage is the input document's directory (the
|
||||
// cwd when the input is a string).
|
||||
klammerset_filename = resolve_klammerset_symbol(
|
||||
klammerset_filename, M.m_state.value("K_input_dir"), Locator()).string();
|
||||
}
|
||||
K::log(1, "Reading klammerset filename: " + klammerset_filename);
|
||||
M.read(fs::path(klammerset_filename));
|
||||
}
|
||||
load_klammersets(M, word_split(args.as_string("klammersets")));
|
||||
|
||||
if (!input_text.empty()) {
|
||||
M.read(input_text + "\n");
|
||||
@@ -111,30 +86,17 @@ int main(int argc, char* argv[])
|
||||
M.read(fs::path(p));
|
||||
}
|
||||
|
||||
if (args.as_bool("m")) {
|
||||
std::cout << M << "\n";
|
||||
}
|
||||
|
||||
// --check reports statically, before anything is applied: all problems
|
||||
// at once, including ones in @cond branches that are not selected and
|
||||
// in klammer bodies that this render would never reach.
|
||||
if (check_only) {
|
||||
int errors = report_diagnostics(check_machine(M, target), std::cout);
|
||||
return errors > 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
std::string result = trim(M.apply(target));
|
||||
if (display_only && !result.empty()) {
|
||||
std::cout << result << "\n";
|
||||
} else if (!result.empty()) {
|
||||
msg() << "Output filename: " << output_filename << "\n";
|
||||
string_to_file(output_filename, result + "\n");
|
||||
(void)K::log(1, "Wrote file: " + output_filename);
|
||||
}
|
||||
}
|
||||
catch (Error& err) {
|
||||
err.print_message();
|
||||
std::cout << "\n";
|
||||
std::cerr << "\n";
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user