use super::*;
impl EqParser {
pub(super) fn parse_command(&mut self, cmd: &str) -> EqNode {
self.parse_command_with_tail(cmd, true)
}
pub(super) fn parse_command_atom(&mut self, cmd: &str) -> EqNode {
self.parse_command_with_tail(cmd, false)
}
pub(super) fn parse_command_with_tail(&mut self, cmd: &str, attach_scripts: bool) -> EqNode {
self.depth += 1;
if self.depth > MAX_EQ_DEPTH {
self.depth -= 1;
return EqNode::Empty;
}
let node = self.parse_command_inner(cmd, attach_scripts);
self.depth -= 1;
node
}
pub(super) fn parse_command_inner(&mut self, cmd: &str, attach_scripts: bool) -> EqNode {
let explicit_root = cmd
.strip_prefix('\\')
.filter(|name| name.eq_ignore_ascii_case("sqrt") || name.eq_ignore_ascii_case("root"));
let cmd = explicit_root.unwrap_or(cmd);
let cmd_upper = cmd.to_ascii_uppercase();
let cu = cmd_upper.as_str();
match classify_command(cmd) {
EqCommandClass::InfixDiscard => EqNode::Empty,
EqCommandClass::LatexFraction => self.parse_latex_fraction(),
EqCommandClass::RomanText => {
let body = self.parse_single_or_group();
EqNode::FontStyle {
style: FontStyleKind::Roman,
body: Box::new(body),
}
}
EqCommandClass::Phantom => {
self.parse_single_or_group();
EqNode::Text(" ".to_string())
}
EqCommandClass::LatexSpacing => {
let space = lookup_symbol(cu).unwrap_or(" ");
EqNode::Text(space.to_string())
}
EqCommandClass::Overset => {
let over = self.parse_single_or_group();
let base = self.parse_single_or_group();
EqNode::Superscript {
base: Box::new(base),
sup: Box::new(over),
}
}
EqCommandClass::Underset => {
let under = self.parse_single_or_group();
let base = self.parse_single_or_group();
EqNode::Subscript {
base: Box::new(base),
sub: Box::new(under),
}
}
EqCommandClass::BeginEnv => self.parse_latex_environment(),
EqCommandClass::EndEnv => {
self.skip_brace_arg();
EqNode::Empty
}
EqCommandClass::Sqrt => {
self.parse_sqrt(explicit_root.is_some() || !self.document_terms)
}
EqCommandClass::IntegralNolimits => {
let symbol = lookup_symbol(cu)
.or_else(|| lookup_symbol(cmd))
.unwrap_or("∫")
.to_string();
let node = EqNode::MathSymbol(symbol);
if self.document_terms {
let (sub, sup) = self.parse_operator_scripts();
match (sub, sup) {
(Some(sub), Some(sup)) => EqNode::SubSup {
base: Box::new(node),
sub,
sup,
},
(Some(sub), None) => EqNode::Subscript {
base: Box::new(node),
sub,
},
(None, Some(sup)) => EqNode::Superscript {
base: Box::new(node),
sup,
},
(None, None) => node,
}
} else {
self.finish_command_atom(node, attach_scripts)
}
}
EqCommandClass::SmallOperator => {
let symbol = lookup_symbol(cu).unwrap_or("?").to_string();
self.finish_command_atom(EqNode::MathSymbol(symbol), attach_scripts)
}
EqCommandClass::BigOperator => {
let symbol = if is_big_operator(cu) {
lookup_symbol(cu).unwrap_or("?").to_string()
} else {
lookup_symbol(cmd).unwrap_or("?").to_string()
};
self.parse_big_op(symbol)
}
EqCommandClass::Limit => self.parse_limit(cmd == "Lim"),
EqCommandClass::Matrix => self.parse_matrix(matrix_style(cu)),
EqCommandClass::Column if self.document_terms => self.parse_column(pile_align(cu)),
EqCommandClass::Column => self.parse_command_fallback(cmd, attach_scripts),
EqCommandClass::Cases => self.parse_cases(),
EqCommandClass::EqAlign => self.parse_eqalign(),
EqCommandClass::Pile => self.parse_pile(pile_align(cu)),
EqCommandClass::LeftDelim => {
let node = self.parse_left_right();
self.finish_command_atom(node, attach_scripts)
}
EqCommandClass::RightDiscard => EqNode::Empty,
EqCommandClass::Rel => {
let is_buildrel = cu == "BUILDREL";
let arrow_node = self.parse_element_atom();
let arrow = match &arrow_node {
EqNode::MathSymbol(s) => s.clone(),
EqNode::Symbol(s) => s.clone(),
EqNode::Text(s) => s.clone(),
_ => "→".to_string(),
};
let over = self.parse_single_or_group();
let under = if !is_buildrel {
Some(Box::new(self.parse_single_or_group()))
} else {
None
};
EqNode::Rel {
arrow,
over: Box::new(over),
under,
}
}
EqCommandClass::LongDiv => {
let divisor = self.parse_single_or_group();
let quotient = self.parse_single_or_group();
let body = self.parse_single_or_group();
EqNode::LongDiv {
divisor: Box::new(divisor),
quotient: Box::new(quotient),
body: Box::new(body),
}
}
EqCommandClass::Ladder => self.parse_matrix(if cu == "SLADDER" {
MatrixStyle::ShortLadder
} else {
MatrixStyle::Ladder
}),
EqCommandClass::Benzene => EqNode::MathSymbol("⌬".to_string()),
EqCommandClass::Bigg => self.parse_element(),
EqCommandClass::Choose => {
let body = self.parse_single_or_group();
EqNode::Binomial {
top: Box::new(EqNode::Empty),
bottom: Box::new(body),
}
}
EqCommandClass::Binom => {
let top = self.parse_single_or_group();
let bottom = self.parse_single_or_group();
EqNode::Binomial {
top: Box::new(top),
bottom: Box::new(bottom),
}
}
EqCommandClass::Color => self.parse_color(),
EqCommandClass::LeftScript => {
if self.document_terms {
self.invalid_document_script = true;
return EqNode::Empty;
}
let script = self.parse_single_or_group();
let body = self.parse_single_or_group();
if cu == "LSUB" {
EqNode::Subscript {
base: Box::new(body),
sub: Box::new(script),
}
} else {
EqNode::Superscript {
base: Box::new(body),
sup: Box::new(script),
}
}
}
EqCommandClass::Sup | EqCommandClass::Sub => {
if self.document_terms {
self.invalid_document_script = true;
return EqNode::Empty;
}
let body = self.parse_single_or_group();
self.finish_command_atom(body, attach_scripts)
}
EqCommandClass::Fallback => self.parse_command_fallback(cmd, attach_scripts),
}
}
pub(super) fn parse_command_fallback(&mut self, cmd: &str, attach_scripts: bool) -> EqNode {
let roman_literal = self.document_terms
&& self
.pos
.checked_sub(1)
.and_then(|index| self.tokens.get(index))
.is_some_and(|token| token.roman_literal);
let cmd_lower = cmd.to_ascii_lowercase();
if cmd_lower == "not" {
return self.parse_hancom_not();
}
if cmd == r"\not" {
return EqNode::Decoration {
kind: symbols::DecoKind::StrikeThrough,
body: Box::new(self.parse_single_or_group()),
};
}
if let Some(&deco) = DECORATIONS
.get(cmd)
.or_else(|| DECORATIONS.get(cmd_lower.as_str()))
{
let body = self.parse_single_or_group();
let body = self.finish_command_atom(body, attach_scripts);
return EqNode::Decoration {
kind: deco,
body: Box::new(body),
};
}
if let Some(&style) = FONT_STYLES
.get(cmd)
.or_else(|| FONT_STYLES.get(cmd_lower.as_str()))
{
if matches!(cmd_lower.as_str(), "rm" | "it") {
self.font_state = Some(style);
return EqNode::FontStyle {
style,
body: Box::new(EqNode::Empty),
};
}
let mut leading = Vec::new();
while !self.document_terms
&& self.current_type() == TokenType::Whitespace
&& matches!(self.current_value(), "`" | "~")
{
leading.push(super::atoms::whitespace_node(self.current_value()));
self.pos += 1;
}
let body = if self.current_type() == TokenType::Command
&& is_structure_command(&self.current_value().to_ascii_uppercase())
{
EqNode::Empty
} else {
self.parse_single_or_group()
};
let styled = EqNode::FontStyle {
style,
body: Box::new(body),
};
if leading.is_empty() {
return styled;
}
leading.push(styled);
return EqNode::Row(leading);
}
if let Some(symbol) = lookup_symbol(cmd) {
let node = EqNode::MathSymbol(symbol.to_string());
return self.finish_command_atom(node, attach_scripts);
}
if is_function(cmd) {
let func_name = lookup_function(cmd).unwrap_or(cmd).to_string();
if attach_scripts
&& self.current_type() == TokenType::Whitespace
&& self.current_value() == "`"
{
let after_space_is_script = self.tokens.get(self.pos + 1).is_some_and(|t| {
t.ty == TokenType::Subscript || t.ty == TokenType::Superscript
});
if !after_space_is_script {
self.pos += 1;
}
}
let node = EqNode::Function(func_name);
return self.finish_command_atom(node, attach_scripts);
}
let node = self.styled_literal(EqNode::Text(cmd.to_string()), roman_literal);
self.finish_command_atom(node, attach_scripts)
}
}