Skip to content

Commit

Permalink
feat(cli): initialize compile arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Dec 28, 2023
1 parent cb855d7 commit faefbfa
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ members = ["crates/*"]
[workspace.dependencies]

once_cell = "1"
anyhow = "1"

fxhash = "0.2.1"
ecow = "0.2.0"
Expand Down
3 changes: 3 additions & 0 deletions crates/mitex-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ repository.workspace = true
[dependencies]

mitex-spec = { path = "../mitex-spec" }
mitex-parser = { path = "../mitex-parser" }
mitex = { path = "../mitex" }

serde.workspace = true
serde_json.workspace = true
anyhow.workspace = true

[lints]
workspace = true
68 changes: 67 additions & 1 deletion crates/mitex-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
use std::path::Path;

use anyhow::Context;
use serde::{Deserialize, Serialize};

fn main() {
const HELP: &str = r#"usage: mitex <command> [args...]
commands:
generate
Generate artifacts for the mitex compiler.
compile <input> [output]
Compile the input file to a mitex document.
syntax <input> [output]
Parse the input file and print the syntax tree.
help
Show this help message.
"#;

fn main() -> anyhow::Result<()> {
let args = std::env::args().collect::<Vec<_>>();
let contains_help = args
.iter()
.any(|s| s == "help" || s == "--help" || s == "-h");
if contains_help {
println!("{HELP}");
return Ok(());
}
match args.get(1).map(|s| s.as_str()) {
None | Some("generate") => generate(),
Some("compile") | Some("syntax") => {
let is_ast = args[1] == "syntax";
let input_path = args.get(2).expect("missing input path");
let output_path = args.get(3).cloned().unwrap_or_else(|| {
let mut output_path = Path::new(input_path).to_owned();
output_path.set_extension("typ");
output_path.to_str().unwrap().to_owned()
});
compile(input_path, &output_path, is_ast)
}
_ => panic!("invalid command"),
}
}

fn generate() -> anyhow::Result<()> {
// typst query --root . .\packages\latex-spec\mod.typ "<mitex-packages>"
let project_root = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let project_root = std::path::Path::new(&project_root)
Expand Down Expand Up @@ -65,4 +109,26 @@ fn main() {
let spec: mitex_spec::CommandSpec = json_spec.into();

std::fs::write(target_dir.join("spec/default.rkyv"), spec.to_bytes()).unwrap();

Ok(())
}

fn compile(input_path: &str, output_path: &str, is_ast: bool) -> anyhow::Result<()> {
let input = std::fs::read_to_string(input_path)
.with_context(|| format!("failed to read input file: {input_path}"))?;

let output = if !is_ast {
mitex::convert_text(&input, None).map_err(|e| anyhow::anyhow!("{}", e))
} else {
Ok(format!(
"{:#?}",
mitex_parser::parse(&input, mitex::DEFAULT_SPEC.clone())
))
};

let output = output.with_context(|| format!("failed to convert input file: {input_path}"))?;

std::fs::write(output_path, output)?;

Ok(())
}
2 changes: 1 addition & 1 deletion crates/mitex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ pub fn convert_math_no_macro(input: &str, spec: Option<CommandSpec>) -> Result<S
convert_math_inner(input, spec, parse_without_macro)
}

static DEFAULT_SPEC: once_cell::sync::Lazy<CommandSpec> = once_cell::sync::Lazy::new(|| {
pub static DEFAULT_SPEC: once_cell::sync::Lazy<CommandSpec> = once_cell::sync::Lazy::new(|| {
CommandSpec::from_bytes(include_bytes!(
"../../../target/mitex-artifacts/spec/default.rkyv"
))
Expand Down

0 comments on commit faefbfa

Please sign in to comment.