Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement custom commands with 0 arguments #334

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions latex2mmlc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ categories = ["science"]

[dependencies]
bumpalo = "3.17.0"
no-panic = "0.1.33"
phf = { version = "0.11.3", features = ["macros"] }
strum_macros = "0.26.4"

[dev-dependencies]
insta = { version = "1.41.1", features = ["default", "ron"] }
serde = { version = "1.0.217", features = ["derive"] }
lazy_static = "1.5.0"
regex = "1.11.1"
similar = "2.7.0"
3 changes: 3 additions & 0 deletions latex2mmlc/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ impl<'arena> NodeList<'arena> {
}
}

// NodeList is sync, because we don't allow mutation through shared references.
unsafe impl<'arena> Sync for NodeList<'arena> {}

#[cfg(test)]
impl<'arena> Serialize for NodeList<'arena> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Expand Down
9 changes: 9 additions & 0 deletions latex2mmlc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ pub enum Node<'arena> {
tf: MathVariant,
content: &'arena Node<'arena>,
},
PredefinedNode(&'static Node<'static>),
}

impl PartialEq for &'static Node<'static> {
fn eq(&self, other: &&'static Node<'static>) -> bool {
std::ptr::eq(*self, *other)
}
}

const INDENT: &str = " ";
Expand Down Expand Up @@ -178,6 +185,7 @@ impl MathMLEmitter {
| Node::ColumnSeparator
| Node::RowSeparator
| Node::TextTransform { .. }
| Node::PredefinedNode(_)
) {
// Get the base indent out of the way.
new_line_and_indent(&mut self.s, base_indent);
Expand Down Expand Up @@ -516,6 +524,7 @@ impl MathMLEmitter {
pushln!(&mut self.s, base_indent, "</mtable>");
}
Node::ColumnSeparator | Node::RowSeparator => (),
Node::PredefinedNode(node) => self.emit(node, base_indent),
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions latex2mmlc/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ast::Node;
use crate::attribute::{FracAttr, MathVariant, OpAttr, Size, Style, TextTransform};
use crate::ops::{self, Op};
use crate::token::Token;
Expand Down Expand Up @@ -68,6 +69,10 @@ static COMMANDS: phf::Map<&'static str, Token> = phf::phf_map! {
"Pi" => Token::UprightLetter('Π'),
"Pr" => Token::Function("Pr"),
"Psi" => Token::UprightLetter('Ψ'),
"RR" => Token::PredefinedNode(&Node::TextTransform {
tf: MathVariant::Transform(TextTransform::DoubleStruck),
content: &Node::SingleLetterIdent('R', false),
}),
"Re" => Token::Letter('ℜ'),
"Rho" => Token::UprightLetter('Ρ'),
"Rightarrow" => Token::Operator(ops::RIGHTWARDS_DOUBLE_ARROW),
Expand Down Expand Up @@ -146,7 +151,7 @@ static COMMANDS: phf::Map<&'static str, Token> = phf::phf_map! {
"blacksquare" => Token::Letter(ops::BLACK_SQUARE),
"bm" => Token::Transform(MathVariant::Transform(TextTransform::BoldItalic)),
"boldsymbol" => Token::Transform(MathVariant::Transform(TextTransform::BoldItalic)),
"bot" => Token::Operator(ops::UP_TACK),
"bot" => Token::Letter(ops::UP_TACK),
"botdoteq" => Token::Operator(ops::EQUALS_SIGN_WITH_DOT_BELOW),
"boxbox" => Token::Operator(ops::SQUARED_SQUARE),
"boxbslash" => Token::Operator(ops::SQUARED_FALLING_DIAGONAL_SLASH),
Expand Down Expand Up @@ -195,6 +200,10 @@ static COMMANDS: phf::Map<&'static str, Token> = phf::phf_map! {
"curlywedge" => Token::Operator(ops::CURLY_LOGICAL_AND),
"curvearrowleft" => Token::Operator(ops::ANTICLOCKWISE_TOP_SEMICIRCLE_ARROW),
"curvearrowright" => Token::Operator(ops::CLOCKWISE_TOP_SEMICIRCLE_ARROW),
"d" => Token::PredefinedNode(&Node::TextTransform {
tf: MathVariant::Normal,
content: &Node::SingleLetterIdent('d', false),
}),
"dag" => Token::Letter('†'),
"dagger" => Token::Letter('†'),
"daleth" => Token::Letter('ℸ'),
Expand Down Expand Up @@ -448,7 +457,7 @@ static COMMANDS: phf::Map<&'static str, Token> = phf::phf_map! {
"overset" => Token::Overset,
"parallel" => Token::Operator(ops::PARALLEL_TO),
"partial" => Token::Letter(ops::PARTIAL_DIFFERENTIAL),
"perp" => Token::Operator(ops::UP_TACK),
"perp" => Token::Operator(ops::PERPENDICULAR),
"phi" => Token::Letter('ϕ'),
"pi" => Token::Letter('π'),
"pm" => Token::Operator(ops::PLUS_MINUS_SIGN),
Expand Down Expand Up @@ -562,7 +571,7 @@ static COMMANDS: phf::Map<&'static str, Token> = phf::phf_map! {
"tilde" => Token::OverUnder(ops::TILDE, true, Some(OpAttr::StretchyFalse)),
"times" => Token::Operator(ops::MULTIPLICATION_SIGN),
"to" => Token::Operator(ops::RIGHTWARDS_ARROW),
"top" => Token::Operator(ops::DOWN_TACK),
"top" => Token::Letter(ops::DOWN_TACK),
"triangle" => Token::Letter('△'),
"triangledown" => Token::Operator(ops::WHITE_DOWN_POINTING_TRIANGLE),
"triangleleft" => Token::Operator(ops::WHITE_LEFT_POINTING_TRIANGLE),
Expand Down
4 changes: 4 additions & 0 deletions latex2mmlc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub(crate) mod lexer;
pub(crate) mod ops;
pub(crate) mod parse;
pub mod token;

pub use ast::MathMLEmitter;
pub use error::{LatexErrKind, LatexError};

Expand Down Expand Up @@ -313,6 +314,9 @@ mod tests {
),
("middle_bracket", r"\left(\frac12\middle]\frac12\right)"),
("left_right_different_stretch", r"\left/\frac12\right)"),
("d_command", r"\d"),
("d_command_nested", r"\mathit{x\d x}"),
("RR_command", r"\RR"),
];

for (name, problem) in problems.into_iter() {
Expand Down
5 changes: 3 additions & 2 deletions latex2mmlc/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ pub(crate) const SQUARED_TIMES: Op = Op('⊠');
pub(crate) const SQUARED_DOT_OPERATOR: Op = Op('⊡');
pub(crate) const RIGHT_TACK: Op = Op('⊢');
pub(crate) const LEFT_TACK: Op = Op('⊣');
pub(crate) const DOWN_TACK: Op = Op('⊤');
pub(crate) const UP_TACK: Op = Op('⊥');
pub(crate) const DOWN_TACK: char = '⊤';
pub(crate) const UP_TACK: char = '⊥';
// pub(crate) const ASSERTION: Op = Op('⊦');
// pub(crate) const MODELS: Op = Op('⊧');
pub(crate) const TRUE: Op = Op('⊨');
Expand Down Expand Up @@ -538,6 +538,7 @@ pub(crate) const BLACK_STAR: char = '★';
//
// Unicode Block: Miscellaneous Mathematical Symbols-A
//
pub(crate) const PERPENDICULAR: Op = Op('⟂');
pub(crate) const MATHEMATICAL_LEFT_WHITE_SQUARE_BRACKET: &ParenOp =
&ParenOp('⟦', false, Stretchy::Always);
pub(crate) const MATHEMATICAL_RIGHT_WHITE_SQUARE_BRACKET: &ParenOp =
Expand Down
1 change: 1 addition & 0 deletions latex2mmlc/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ where
Token::End | Token::Right | Token::GroupEnd => {
return Err(LatexError(loc, LatexErrKind::UnexpectedClose(cur_token)))
}
Token::PredefinedNode(node) => Node::PredefinedNode(node),
};
Ok(self.commit(node))
}
Expand Down
7 changes: 7 additions & 0 deletions latex2mmlc/src/snapshots/latex2mmlc__tests__RR_command.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: latex2mmlc/src/lib.rs
expression: "\\RR"
---
<math>
<mi>ℝ</mi>
</math>
7 changes: 7 additions & 0 deletions latex2mmlc/src/snapshots/latex2mmlc__tests__d_command.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: latex2mmlc/src/lib.rs
expression: "\\d"
---
<math>
<mi mathvariant="normal">d</mi>
</math>
11 changes: 11 additions & 0 deletions latex2mmlc/src/snapshots/latex2mmlc__tests__d_command_nested.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: latex2mmlc/src/lib.rs
expression: "\\mathit{x\\d x}"
---
<math>
<mrow>
<mi>𝑥</mi>
<mi mathvariant="normal">d</mi>
<mi>𝑥</mi>
</mrow>
</math>
2 changes: 2 additions & 0 deletions latex2mmlc/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::mem::discriminant;

use strum_macros::AsRefStr;

use crate::ast::Node;
use crate::attribute::{FracAttr, MathVariant, OpAttr, Size, Style, TextTransform};
use crate::ops::{Op, ParenOp};

Expand Down Expand Up @@ -94,6 +95,7 @@ pub enum Token<'source> {
#[strum(serialize = r"\mathstrut")]
Mathstrut,
Style(Style),
PredefinedNode(&'static Node<'static>),
UnknownCommand(&'source str),
}

Expand Down
2 changes: 1 addition & 1 deletion latex2mmlc/tests/snapshots/wiki_test__wiki068.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: latex2mmlc/tests/wiki_test.rs
expression: "\\perp, \\angle, \\sphericalangle, \\measuredangle, 45^\\circ"
---
<math>
<mo></mo>
<mo></mo>
<mo>,</mo>
<mi>∠</mi>
<mo>,</mo>
Expand Down
7 changes: 3 additions & 4 deletions latex2mmlc/tests/wiki_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::LazyLock;

use insta::assert_snapshot;
use lazy_static::lazy_static;
use regex::Regex;
// use similar::{ChangeTag, TextDiff};

Expand Down Expand Up @@ -268,9 +269,7 @@ fn wiki_test() {

/// Prettify HTML input
pub fn prettify(input: &str) -> String {
lazy_static! {
static ref OPEN_TAG: Regex = Regex::new("(?P<tag><[A-z])").unwrap();
}
static OPEN_TAG: LazyLock<Regex> = LazyLock::new(|| Regex::new("(?P<tag><[A-z])").unwrap());

// First get all tags on their own lines
let mut stage1 = input.to_string();
Expand Down