pub mod error;
pub mod latex;
pub mod lexer;
pub mod parser;
pub use error::EqError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EqOutcome {
pub latex: String,
pub degraded: Vec<String>,
}
pub fn convert(script: &str) -> Result<String, EqError> {
convert_with_degraded(script).map(|o| o.latex)
}
pub fn convert_with_degraded(script: &str) -> Result<EqOutcome, EqError> {
let tokens = lexer::lex(script);
let ast = parser::parse(&tokens)?;
let (latex, degraded) = latex::emit_with_degraded(&ast);
Ok(EqOutcome { latex, degraded })
}
#[cfg(test)]
mod tests {
use super::{convert, convert_with_degraded, EqError};
fn check(script: &str, expected: &str) {
assert_eq!(convert(script).unwrap(), expected, "script: {script:?}");
}
#[test]
fn fraction() {
check("1 over 2", "\\frac{1}{2}");
}
#[test]
fn nested_fraction() {
check("a over {b over c}", "\\frac{a}{\\frac{b}{c}}");
}
#[test]
fn atop() {
check("a atop b", "{a \\atop b}");
}
#[test]
fn sqrt_group() {
check("sqrt {x+1}", "\\sqrt{x+1}");
}
#[test]
fn root_n_of() {
check("root 3 of {x}", "\\sqrt[3]{x}");
}
#[test]
fn superscript_single() {
check("x^2", "x^2");
}
#[test]
fn superscript_multi_braced() {
check("x^{n+1}", "x^{n+1}");
}
#[test]
fn subscript_keyword_and_symbol() {
check("x sub i", "x_i");
check("x_i", "x_i");
}
#[test]
fn sum_with_bounds() {
check("sum from {i=1} to {n} i^2", "\\sum_{i=1}^n i^2");
}
#[test]
fn int_with_bounds_and_infty() {
check(
"int from 0 to infty e^{-x} dx",
"\\int_0^{\\infty}e^{-x}\\text{dx}",
);
}
#[test]
fn oint_command() {
check("oint", "\\oint");
}
#[test]
fn lim_with_bound() {
check("lim from {x rarrow 0}", "\\lim_{x \\rightarrow 0}");
}
#[test]
fn greek_lowercase() {
check("alpha beta gamma", "\\alpha \\beta \\gamma");
}
#[test]
fn greek_uppercase_forms() {
check("Gamma", "\\Gamma");
check("GAMMA", "\\Gamma");
}
#[test]
fn matrix_2x2() {
check(
"matrix{a & b # c & d}",
"\\begin{matrix}a & b \\\\ c & d\\end{matrix}",
);
}
#[test]
fn pmatrix_2x2() {
check(
"pmatrix{1 & 0 # 0 & 1}",
"\\begin{pmatrix}1 & 0 \\\\ 0 & 1\\end{pmatrix}",
);
}
#[test]
fn cases_environment() {
check(
"cases{x & x>0 # 0 & x leq 0}",
"\\begin{cases}x & x>0 \\\\ 0 & x \\leq 0\\end{cases}",
);
}
#[test]
fn decoration_bar_hat_vec() {
check("bar x", "\\bar{x}");
check("hat y", "\\hat{y}");
check("vec v", "\\vec{v}");
}
#[test]
fn decoration_dot_tilde() {
check("dot x", "\\dot{x}");
check("tilde a", "\\tilde{a}");
}
#[test]
fn left_right_delimiters() {
check("left ( a over b right )", "\\left( \\frac{a}{b} \\right)");
}
#[test]
fn left_right_brace_delimiters() {
check("left { x right }", "\\left\\{ x \\right\\}");
}
#[test]
fn binom() {
check("binom {n} {k}", "\\binom{n}{k}");
}
#[test]
fn font_switches() {
check("it x", "\\mathit{x}");
check("rm d", "\\mathrm{d}");
check("bold v", "\\mathbf{v}");
}
#[test]
fn operators_and_relations() {
check("a times b", "a \\times b");
check("a div b", "a \\div b");
check("a leq b", "a \\leq b");
check("a geq b", "a \\geq b");
check("a neq b", "a \\neq b");
check("a approx b", "a \\approx b");
check("a pm b", "a \\pm b");
check("a cdot b", "a \\cdot b");
}
#[test]
fn set_and_logic_symbols() {
check("x in A", "x \\in A");
check("x notin A", "x \\notin A");
check("A subset B", "A \\subset B");
check("A cup B", "A \\cup B");
check("A cap B", "A \\cap B");
check("forall x", "\\forall x");
check("exist y", "\\exists y");
check("therefore p", "\\therefore p");
check("because q", "\\because q");
}
#[test]
fn dots_and_arrows() {
check("cdots", "\\cdots");
check("ldots", "\\ldots");
check("a rarrow b", "a \\rightarrow b");
check("a larrow b", "a \\leftarrow b");
}
#[test]
fn thin_and_normal_space() {
check("a ` b", "a\\,b");
check("a ~ b", "a\\ b");
}
#[test]
fn unknown_command_passthrough() {
check("foobar", "\\text{foobar}");
}
#[test]
fn unknown_mixed_with_known() {
check("alpha widget", "\\alpha \\text{widget}");
}
#[test]
fn single_letter_identifier_stays_italic() {
check("x", "x");
}
#[test]
fn empty_input_is_empty_output() {
check("", "");
}
#[test]
fn whitespace_only_is_empty() {
check(" \t\n ", "");
}
#[test]
fn unbalanced_open_brace_errors() {
assert_eq!(convert("{ x"), Err(EqError::UnbalancedBrace));
}
#[test]
fn unbalanced_close_brace_errors() {
assert_eq!(convert("x }"), Err(EqError::UnbalancedBrace));
}
#[test]
fn deeply_nested_balanced_ok() {
check("{{{x}}}", "x");
}
#[test]
fn plain_arithmetic_passthrough() {
check("a + b - c = d", "a+b-c=d");
}
#[test]
fn uppercase_over_is_fraction() {
check("1 OVER 2", "\\frac{1}{2}");
}
#[test]
fn uppercase_sqrt_is_radical() {
check("SQRT {x}", "\\sqrt{x}");
}
#[test]
fn uppercase_times_is_command() {
check("TIMES", "\\times");
}
#[test]
fn uppercase_left_right_delimiters() {
check("LEFT ( x RIGHT )", "\\left( x \\right)");
}
#[test]
fn mixed_case_keywords_resolve() {
check("1 Over 2", "\\frac{1}{2}");
check("Sqrt {x}", "\\sqrt{x}");
check("a Times b", "a \\times b");
}
#[test]
fn eq01_corpus_pattern_uses_real_commands() {
let latex =
convert("최저입찰가격 TIMES LEFT ( 최저입찰가격 over 해당입찰가격 RIGHT )").unwrap();
assert!(latex.contains("\\times"), "expected \\times in {latex:?}");
assert!(latex.contains("\\left("), "expected \\left( in {latex:?}");
assert!(latex.contains("\\right)"), "expected \\right) in {latex:?}");
assert!(latex.contains("\\frac{"), "expected \\frac in {latex:?}");
assert!(
!latex.contains("\\text{TIMES}"),
"TIMES still degraded: {latex:?}"
);
assert!(
!latex.contains("\\text{LEFT}"),
"LEFT still degraded: {latex:?}"
);
assert!(
!latex.contains("\\text{RIGHT}"),
"RIGHT still degraded: {latex:?}"
);
}
#[test]
fn mixed_case_non_command_passes_through_as_text() {
check("Widget", "\\text{Widget}");
check("fooBar", "\\text{fooBar}");
}
#[test]
fn known_command_does_not_degrade() {
let out = convert_with_degraded("TIMES").unwrap();
assert_eq!(out.latex, "\\times");
assert!(out.degraded.is_empty(), "degraded should be empty: {out:?}");
}
#[test]
fn unknown_command_like_word_is_reported_degraded() {
let out = convert_with_degraded("FOOBAR").unwrap();
assert_eq!(out.latex, "\\text{FOOBAR}");
assert_eq!(out.degraded, vec!["FOOBAR".to_string()]);
}
#[test]
fn single_letter_and_labels_not_degraded() {
let out = convert_with_degraded("x").unwrap();
assert!(out.degraded.is_empty());
let out = convert_with_degraded("x1").unwrap();
assert!(
out.degraded.is_empty(),
"x1 is a label, not degraded: {out:?}"
);
}
#[test]
fn degraded_list_collects_multiple_tokens() {
let out = convert_with_degraded("FOO QUUX baz").unwrap();
assert_eq!(
out.degraded,
vec!["FOO".to_string(), "QUUX".to_string(), "baz".to_string()]
);
}
}