use std::path::{Path, PathBuf};
use std::process::ExitCode;
const USAGE: &str = "\
\n USAGE: texrs [OPTIONS] [FILE[.tex]]... [COMMANDS]
texrs [OPTIONS] \\FIRST-LINE // the arguments are the input
texrs [OPTIONS] &FMT ARGS // with a named format
texrs // no arguments: the prompt
── TEX OPTIONS ────────────────────────────────────────
-interaction=MODE
// batchmode, nonstopmode, scrollmode or errorstopmode
-jobname=NAME
// Set the job name
-output-directory=DIR
// Write files in DIR
-progname=NAME
// Set the program name
-fmt=NAME
// Use a named format
-ini
// Be initex
-halt-on-error
// Stop at the first error
-file-line-error, -no-file-line-error
// file:line:error style messages
-recorder
// Record the files read
-8bit
// Write 8-bit characters as themselves
── RUNNING ────────────────────────────────────────────
--repl
// Start the interactive prompt
--jobs=N
// Compile N documents at once (default: one per core)
--no-cache
// Compile this run rather than reading the bytecode cache
--aot
// Compile the document to a standalone native executable
── LOOKING INSIDE ─────────────────────────────────────
--dump-tokens
// Print the mouth's token stream and exit
--disasm
// Print the lowered fusevm bytecode and exit
--tiers
// Run it, then report which fusevm tier took its bytecode
── EDITORS ────────────────────────────────────────────
--lsp
// Speak the Language Server Protocol over stdio
--dap
// Speak the Debug Adapter Protocol over stdio
── CACHE ──────────────────────────────────────────────
--cache-stats
// Say what the bytecode cache holds and where
--cache-clear
// Delete it
── DOCUMENTS ──────────────────────────────────────────
-X new [DIR] // Make one (Texrs.toml + index.tex)
-X init // Make one here, named after this directory
-X build [--profile P] // Build the document this directory is in
-X watch [--profile P] // Rebuild it whenever an input changes
-X show // Say what the document is and can produce
-X dump [--profile P] // Build to stdout, writing nothing
-X bundle fetch URL // Download a bundle into the cache
-X bundle list // Say which bundles have been fetched
--profile NAME // Which output to build
--interval MS // How often -X watch looks (default 250)
── SYSTEM ─────────────────────────────────────────────
-h, --help // Print this
--version // Print the version banner
";
fn main() -> ExitCode {
struct Stats;
impl Drop for Stats {
fn drop(&mut self) {
if std::env::var_os("TEXRS_PRELEX_STATS").is_some() {
prelex_stats();
}
}
}
let _stats = Stats;
let args: Vec<String> = std::env::args().skip(1).collect();
if args.first().map(String::as_str) == Some("-X") {
return run_document(&args[1..]);
}
let cli = match texrs::cli::parse(&args) {
Ok(c) => c,
Err(e) => {
eprintln!("{e}");
return ExitCode::from(1);
}
};
match cli.mode {
texrs::cli::Mode::Help => {
texrs::banner::print_banner(true);
print!("{USAGE}");
return ExitCode::SUCCESS;
}
texrs::cli::Mode::Version => {
println!("{}", texrs::banner::version_banner());
return ExitCode::SUCCESS;
}
texrs::cli::Mode::Repl => {
return match texrs::repl::run() {
Ok(()) => ExitCode::SUCCESS,
Err(e) => fail(&e),
}
}
texrs::cli::Mode::Lsp => {
return match texrs::lsp::run() {
Ok(()) => ExitCode::SUCCESS,
Err(e) => fail(&e),
}
}
texrs::cli::Mode::Dap => {
return match texrs::dap::run() {
Ok(()) => ExitCode::SUCCESS,
Err(e) => fail(&e),
}
}
texrs::cli::Mode::CacheStats => return cache_stats(),
texrs::cli::Mode::CacheClear => return cache_clear(),
texrs::cli::Mode::Run => {}
}
let (dump_tokens, disasm, aot, tiers, no_cache, jobs) = (
cli.dump_tokens,
cli.disasm,
cli.aot,
cli.tiers,
cli.no_cache,
cli.jobs,
);
let mut paths = cli.files.clone();
if paths.is_empty() {
if let Some(src) = cli.command_line_source() {
return run_command_line(&src, cli.interaction);
}
}
if paths.is_empty() {
return match texrs::repl::run() {
Ok(()) => ExitCode::SUCCESS,
Err(e) => fail(&e),
};
}
if paths.len() > 1 && !dump_tokens && !disasm && !tiers {
return run_many(&paths, no_cache, jobs);
}
let path = paths.remove(0);
let mut inputs = texrs::io::ProviderStack::new();
inputs.push(Box::new(
texrs::io::FilesystemProvider::with_roots(Vec::new()).with_primary(&path),
));
let src = match texrs::io::InputProvider::input_open_primary(&mut inputs) {
texrs::io::OpenResult::Ok(input) => input.content,
texrs::io::OpenResult::NotAvailable => {
eprintln!("texrs: {path}: no such file");
return ExitCode::from(1);
}
texrs::io::OpenResult::Err(e) => {
eprintln!("texrs: {path}: {e}");
return ExitCode::from(1);
}
};
if dump_tokens {
let cats = texrs::catcode::CatTable::new();
let mut lx = texrs::lexer::Lexer::new(&src);
while let Some(t) = lx.next_token(&cats) {
println!("{t:?}");
}
return ExitCode::SUCCESS;
}
if disasm {
let compiled = match no_cache {
true => texrs::compile(&src),
false => texrs::compile_cached(std::path::Path::new(&path), &src),
};
return match compiled {
Ok(chunk) => {
print!("{}", chunk.disassemble());
ExitCode::SUCCESS
}
Err(e) => fail(&e.0),
};
}
if aot {
let out = std::path::Path::new(&path).with_extension("");
return match texrs::aot::compile_executable(&path, &out) {
Ok(p) => {
println!("{}", p.display());
ExitCode::SUCCESS
}
Err(e) => fail(&e),
};
}
if tiers {
return match texrs::tiers::report(&src) {
Ok(r) => {
println!("{r}");
ExitCode::SUCCESS
}
Err(e) => fail(&e),
};
}
let trailing = cli
.command_line_source()
.filter(|_| !texrs::source_ends_run(&src));
if let Some(extra) = trailing {
return run_with_trailing(&path, &src, &extra, cli.interaction);
}
let run = match no_cache {
true => texrs::run_messages(&src),
false => texrs::run_messages_cached(std::path::Path::new(&path), &src),
};
match run {
Ok(msgs) => {
if cli.interaction.prints() {
println!("{}", file_line(&path, &msgs, texrs::source_ends_run(&src)));
}
ExitCode::SUCCESS
}
Err(e) => fail(&e.0),
}
}
fn run_many(paths: &[String], no_cache: bool, jobs: Option<usize>) -> ExitCode {
let workers = jobs
.unwrap_or_else(|| {
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
})
.max(1)
.min(paths.len());
let next = std::sync::atomic::AtomicUsize::new(0);
let results: Vec<std::sync::Mutex<Option<Result<String, String>>>> = (0..paths.len())
.map(|_| std::sync::Mutex::new(None))
.collect();
std::thread::scope(|scope| {
for _ in 0..workers {
scope.spawn(|| {
loop {
let i = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
if i >= paths.len() {
break;
}
*results[i].lock().expect("result slot") =
Some(one_document(&paths[i], no_cache));
}
});
}
});
let mut code = ExitCode::SUCCESS;
let mut failed = false;
for (i, slot) in results.iter().enumerate() {
match slot.lock().expect("result slot").take() {
Some(Ok(line)) => println!("{line}"),
Some(Err(e)) => {
eprintln!("! {e}.");
failed = true;
}
None => {
eprintln!("texrs: {}: not run", paths[i]);
failed = true;
}
}
}
if failed {
code = ExitCode::from(1);
}
code
}
fn one_document(path: &str, no_cache: bool) -> Result<String, String> {
let src = std::fs::read_to_string(path).map_err(|e| format!("{path}: {e}"))?;
let run = match no_cache {
true => texrs::run_messages(&src),
false => texrs::run_messages_cached(Path::new(path), &src),
};
let msgs = run.map_err(|e| e.0)?;
let body = match msgs.is_empty() {
true => String::new(),
false => format!(" {msgs}"),
};
Ok(format!("(./{path}{body} )"))
}
fn run_document(args: &[String]) -> ExitCode {
let Some(command) = args.first().map(String::as_str) else {
eprint!("{USAGE}");
return ExitCode::from(1);
};
match command {
"new" | "init" => {
let dir = match command {
"init" => PathBuf::from("."),
_ => args
.get(1)
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(".")),
};
let name = std::fs::canonicalize(&dir)
.ok()
.as_deref()
.and_then(Path::file_name)
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| "document".to_string());
match texrs::document::scaffold(&dir, &name) {
Ok(path) => {
println!("wrote {}", path.display());
ExitCode::SUCCESS
}
Err(e) => fail(&e),
}
}
"bundle" => match args.get(1).map(String::as_str) {
Some("fetch") => {
let Some(url) = args.get(2) else {
return fail("`-X bundle fetch` needs a URL");
};
match texrs::geturl::fetch(url) {
Ok(got) => {
println!("fetched {} bytes → {}", got.bytes, got.path.display());
println!(
"name it in Texrs.toml as: bundle = \"sha256:{}\"",
got.digest
);
ExitCode::SUCCESS
}
Err(e) => fail(&e),
}
}
Some("list") => {
let fetched = texrs::geturl::fetched();
if fetched.is_empty() {
println!("no bundles fetched");
}
for digest in fetched {
match texrs::geturl::path_for(&digest) {
Some(path) => println!("sha256:{digest} {}", path.display()),
None => println!("sha256:{digest}"),
}
}
ExitCode::SUCCESS
}
other => {
eprintln!(
"texrs: `-X bundle` takes fetch or list, not {}",
other.unwrap_or("nothing")
);
ExitCode::from(1)
}
},
"show" => {
let here = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
match texrs::document::Document::find_from(here) {
Ok(document) => {
print!("{}", document.show());
ExitCode::SUCCESS
}
Err(e) => fail(&e),
}
}
"build" | "watch" | "dump" => {
let mut profile: Option<String> = None;
let mut interval = texrs::document::WATCH_INTERVAL;
let mut rest = args[1..].iter();
while let Some(arg) = rest.next() {
match arg.as_str() {
"--profile" => match rest.next() {
Some(name) => profile = Some(name.clone()),
None => return fail("--profile needs a name"),
},
"--interval" => match rest.next().and_then(|ms| ms.parse().ok()) {
Some(ms) => interval = std::time::Duration::from_millis(ms),
None => return fail("--interval needs a number of milliseconds"),
},
other => return fail(&format!("unknown argument: {other}")),
}
}
let here = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
let document = match texrs::document::Document::find_from(here) {
Ok(d) => d,
Err(e) => return fail(&e),
};
if command == "dump" {
return match document.dump(profile.as_deref()) {
Ok(text) => {
print!("{text}");
if !text.ends_with('\n') {
println!();
}
ExitCode::SUCCESS
}
Err(e) => fail(&e),
};
}
if command == "build" {
return match document.build(profile.as_deref()) {
Ok(built) => {
println!(
"built {} from {} input(s) → {}",
built.profile,
built.inputs.len(),
built.path.display()
);
ExitCode::SUCCESS
}
Err(e) => fail(&e),
};
}
let mut status = texrs::status::TexStatus::new();
match document.watch(profile.as_deref(), interval, &mut status, &|| false) {
Ok(_) => ExitCode::SUCCESS,
Err(e) => fail(&e),
}
}
other => match external_command(other, &args[1..]) {
Some(code) => code,
None => {
eprintln!("texrs: unknown document command: {other}");
eprint!("{USAGE}");
ExitCode::from(1)
}
},
}
}
fn external_command(name: &str, args: &[String]) -> Option<ExitCode> {
let program = format!("texrs-{name}");
match std::process::Command::new(&program).args(args).status() {
Ok(status) => Some(match status.code() {
Some(0) => ExitCode::SUCCESS,
Some(code) => ExitCode::from(code.min(255) as u8),
None => ExitCode::from(1),
}),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
Err(e) => {
eprintln!("texrs: {program}: {e}");
Some(ExitCode::from(1))
}
}
}
fn cache_stats() -> ExitCode {
let path = texrs::script_cache::default_cache_path();
if !texrs::script_cache::cache_enabled() {
println!("cache: off (TEXRS_CACHE)");
println!("would be: {}", path.display());
return ExitCode::SUCCESS;
}
let Ok(cache) = texrs::script_cache::ScriptCache::open(&path) else {
eprintln!("texrs: cannot open {}", path.display());
return ExitCode::from(1);
};
let size = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
println!("cache: {}", path.display());
println!("documents: {}", cache.len());
println!("size: {size} bytes");
ExitCode::SUCCESS
}
fn cache_clear() -> ExitCode {
let path = texrs::script_cache::default_cache_path();
let Ok(cache) = texrs::script_cache::ScriptCache::open(&path) else {
eprintln!("texrs: cannot open {}", path.display());
return ExitCode::from(1);
};
match cache.clear() {
Ok(()) => {
println!("cleared {}", path.display());
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("texrs: {}: {e}", path.display());
ExitCode::from(1)
}
}
}
fn file_line(path: &str, msgs: &str, ended: bool) -> String {
let body = match msgs.is_empty() {
true => String::new(),
false => format!(" {msgs}"),
};
let close = match ended {
true => " )",
false => ")",
};
format!("(./{path}{body}{close}")
}
fn run_with_trailing(
path: &str,
src: &str,
extra: &str,
interaction: texrs::cli::Interaction,
) -> ExitCode {
let from_file = match texrs::run_messages_list(src) {
Ok(m) => m,
Err(e) => return fail(&e.0),
};
let all = match texrs::run_messages_list(&format!("{src}\n{extra}")) {
Ok(m) => m,
Err(e) => return fail(&e.0),
};
if interaction.prints() {
let after = all[from_file.len().min(all.len())..].join(" ");
let line = file_line(path, &from_file.join(" "), false);
match after.is_empty() {
true => println!("{line}"),
false => println!("{line} {after}"),
}
}
ExitCode::SUCCESS
}
fn run_command_line(src: &str, interaction: texrs::cli::Interaction) -> ExitCode {
match texrs::run_messages(src) {
Ok(msgs) => {
if interaction.prints() && !msgs.is_empty() {
println!("{msgs}");
}
ExitCode::SUCCESS
}
Err(e) => fail(&e.0),
}
}
fn fail(reason: &str) -> ExitCode {
eprintln!("! {reason}.");
ExitCode::from(1)
}
pub fn prelex_stats() {
use std::sync::atomic::Ordering;
eprintln!(
"prelex calls={} chars={} drop_gen={} drop_exhaust={}",
texrs::parallel::PRELEX_CALLS.load(Ordering::Relaxed),
texrs::parallel::PRELEX_CHARS.load(Ordering::Relaxed),
texrs::parallel::DROP_GEN.load(Ordering::Relaxed),
texrs::parallel::DROP_EXHAUST.load(Ordering::Relaxed)
);
}