Skip to content

Put environment parsing into a separate function #180

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
44 changes: 44 additions & 0 deletions latex2mmlc/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::{arena::{Arena, NodeList}, ast::Node, attribute::Align, error::{LatexError, LatexErrKind}, ops};


#[inline]
pub fn parse_env<'source>(
arena: &mut Arena<'source>,
name: &'source str,
content: NodeList,
loc: usize,
) -> Result<Node<'source>, LatexError<'source>> {
let node = match name {
"align" | "align*" | "aligned" => Node::Table(content, Align::Alternating),
"cases" => {
let content = arena.push(Node::Table(content, Align::Left));
Node::Fenced {
open: ops::LEFT_CURLY_BRACKET,
close: ops::NULL,
content,
style: None,
}
}
"matrix" => Node::Table(content, Align::Center),
matrix_variant @ ("pmatrix" | "bmatrix" | "vmatrix") => {
let content = arena.push(Node::Table(content, Align::Center));
let (open, close) = match matrix_variant {
"pmatrix" => (ops::LEFT_PARENTHESIS, ops::RIGHT_PARENTHESIS),
"bmatrix" => (ops::LEFT_SQUARE_BRACKET, ops::RIGHT_SQUARE_BRACKET),
"vmatrix" => (ops::VERTICAL_LINE, ops::VERTICAL_LINE),
// SAFETY: `matrix_variant` is one of the three strings above.
_ => unsafe { std::hint::unreachable_unchecked() },
};
Node::Fenced {
open,
close,
content,
style: None,
}
}
_ => {
return Err(LatexError(loc, LatexErrKind::UnknownEnvironment(name)));
}
};
Ok(node)
}
1 change: 1 addition & 0 deletions latex2mmlc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub mod ast;
pub mod attribute;
pub(crate) mod commands;
mod error;
mod env;
pub(crate) mod lexer;
pub(crate) mod ops;
pub(crate) mod parse;
Expand Down
39 changes: 4 additions & 35 deletions latex2mmlc/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use crate::{
StrReference,
},
ast::Node,
attribute::{Accent, Align, MathSpacing, MathVariant, OpAttr, Style, TextTransform},
attribute::{Accent, MathSpacing, MathVariant, OpAttr, Style, TextTransform},
commands::get_negated_op,
env::parse_env,
error::{LatexErrKind, LatexError, Place},
lexer::Lexer,
ops,
Expand Down Expand Up @@ -494,38 +495,7 @@ impl<'source> Parser<'source> {
let env_name = self.parse_text_group()?;
let env_content = self.parse_group(Token::End)?.finish();
let end_token_loc = self.next_token().location();
let node = match env_name {
"align" | "align*" | "aligned" => Node::Table(env_content, Align::Alternating),
"cases" => {
let content = self.commit(Node::Table(env_content, Align::Left));
Node::Fenced {
open: ops::LEFT_CURLY_BRACKET,
close: ops::NULL,
content,
style: None,
}
}
"matrix" => Node::Table(env_content, Align::Center),
matrix_variant @ ("pmatrix" | "bmatrix" | "vmatrix") => {
let content = self.commit(Node::Table(env_content, Align::Center));
let (open, close) = match matrix_variant {
"pmatrix" => (ops::LEFT_PARENTHESIS, ops::RIGHT_PARENTHESIS),
"bmatrix" => (ops::LEFT_SQUARE_BRACKET, ops::RIGHT_SQUARE_BRACKET),
"vmatrix" => (ops::VERTICAL_LINE, ops::VERTICAL_LINE),
// SAFETY: `matrix_variant` is one of the three strings above.
_ => unsafe { std::hint::unreachable_unchecked() },
};
Node::Fenced {
open,
close,
content,
style: None,
}
}
_ => {
return Err(LatexError(loc, LatexErrKind::UnknownEnvironment(env_name)));
}
};
let node = parse_env(&mut self.arena, env_name, env_content, loc)?;
self.check_lbrace()?;
let end_name = self.parse_text_group()?;
if end_name != env_name {
Expand All @@ -537,8 +507,7 @@ impl<'source> Parser<'source> {
},
));
}

node
node
}
Token::OperatorName => {
// TODO: Don't parse a node just to immediately destructure it.
Expand Down
Loading