Support nested if-prefixed conditionals, char, and SIVP math/preamble commands
This commit is contained in:
1 parent
64f4446113
commit
c1a7dedd6a
4 files changed
+149
-8
No files matched your search
@@ -82,6 +82,7 @@ const PRELUDE: &str = r#"
|
||||
\def\midrule{\texrratablerule}
|
||||
\def\bottomrule{\texrratablerule}
|
||||
\def\&{\texrraampersand}
|
||||
\def\%{\char37}
|
||||
\def\thetable{1}
|
||||
\def\thefigure{1}
|
||||
\def\arabic#1{0}
|
||||
@@ -89,13 +90,20 @@ const PRELUDE: &str = r#"
|
||||
\def\Roman#1{I}
|
||||
\def\alph#1{a}
|
||||
\def\Alph#1{A}
|
||||
\def\@ifpackageloaded#1#2#3{#2}
|
||||
\def\@ifclassloaded#1#2#3{#2}
|
||||
\def\@ifundefined#1#2#3{#3}
|
||||
\long\def\@ifpackageloaded#1#2#3{#2}
|
||||
\long\def\@ifclassloaded#1#2#3{#2}
|
||||
\long\def\@ifundefined#1#2#3{#3}
|
||||
\def\settowidth#1#2{}
|
||||
\def\pagenumbering#1{}
|
||||
\def\newtheorem#1#2{}
|
||||
\def\theoremstyle#1{}
|
||||
\def\newtheoremstyle#1#2#3#4#5#6#7#8#9{}
|
||||
\def\setcitestyle#1{}
|
||||
\newskip\bibsep
|
||||
\newskip\bibhang
|
||||
\def\allowdisplaybreaks{}
|
||||
\def\@ifstar#1#2{#2}
|
||||
\long\def\g@addto@macro#1#2{}
|
||||
\def\frenchspacing{}
|
||||
\def\nonfrenchspacing{}
|
||||
\def\markboth#1#2{}
|
||||
@@ -130,6 +138,50 @@ const PRELUDE: &str = r#"
|
||||
\def\substack#1{\begin{smallmatrix}#1\end{smallmatrix}}
|
||||
\def\mathord#1{\texrramathclassstart{ordinary}#1\texrramathclassend}
|
||||
\def\mathop#1{\texrramathclassstart{operator}#1\texrramathclassend}
|
||||
\def\max{\mathop{\mathrm{max}}}
|
||||
|
||||
|
||||
\def\resizebox#1#2#3{#3}
|
||||
\def\scalebox#1#2{#2}
|
||||
\def\rotatebox#1#2{#2}
|
||||
\def\hsize{\textwidth}
|
||||
\def\odot{\mathbin{\circ}}
|
||||
\def\otimes{\mathbin{\times}}
|
||||
\def\oplus{\mathbin{+}}
|
||||
\def\ominus{\mathbin{-}}
|
||||
\def\circ{\mathbin{\char176}}
|
||||
\def\bullet{\mathbin{\char183}}
|
||||
\def\ast{\mathbin{*}}
|
||||
\def\star{\mathbin{*}}
|
||||
\def\mid{\mathrel{|}}
|
||||
\def\vert{|}
|
||||
\def\Vert{\|}
|
||||
\def\setminus{\mathbin{\backslash}}
|
||||
\def\backslash{\char92}
|
||||
\def\langle{\mathopen{\char60}}
|
||||
\def\rangle{\mathclose{\char62}}
|
||||
\def\lfloor{\mathopen{\char91}}
|
||||
\def\rfloor{\mathclose{\char93}}
|
||||
\def\lceil{\mathopen{\char123}}
|
||||
\def\rceil{\mathclose{\char125}}
|
||||
|
||||
\def\min{\mathop{\mathrm{min}}}
|
||||
\def\sup{\mathop{\mathrm{sup}}}
|
||||
\def\inf{\mathop{\mathrm{inf}}}
|
||||
\def\lim{\mathop{\mathrm{lim}}}
|
||||
\def\det{\mathop{\mathrm{det}}}
|
||||
\def\log{\mathop{\mathrm{log}}}
|
||||
\def\ln{\mathop{\mathrm{ln}}}
|
||||
\def\exp{\mathop{\mathrm{exp}}}
|
||||
\def\sin{\mathop{\mathrm{sin}}}
|
||||
\def\cos{\mathop{\mathrm{cos}}}
|
||||
\def\tan{\mathop{\mathrm{tan}}}
|
||||
\def\arg{\mathop{\mathrm{arg}}}
|
||||
\def\deg{\mathop{\mathrm{deg}}}
|
||||
\def\dim{\mathop{\mathrm{dim}}}
|
||||
\def\ker{\mathop{\mathrm{ker}}}
|
||||
\def\gcd{\mathop{\mathrm{gcd}}}
|
||||
\def\hom{\mathop{\mathrm{hom}}}
|
||||
\def\mathbin#1{\texrramathclassstart{binary}#1\texrramathclassend}
|
||||
\def\mathrel#1{\texrramathclassstart{relation}#1\texrramathclassend}
|
||||
\def\mathopen#1{\texrramathclassstart{opening}#1\texrramathclassend}
|
||||
@@ -364,7 +416,7 @@ const PRELUDE: &str = r#"
|
||||
\def\nobreakspace{ }
|
||||
\def\ { }
|
||||
\def~{ }
|
||||
\def\AtBeginDocument#1{#1}
|
||||
\long\def\AtBeginDocument#1{#1}
|
||||
\def\newgeometry#1{\texrrageometry{#1}}
|
||||
\def\fancyheadoffset#1{}
|
||||
\def\lhead#1{\fancyhead[L]{#1}}
|
||||
|
||||
@@ -3006,6 +3006,25 @@ fn define_engine_macro(
|
||||
);
|
||||
}
|
||||
|
||||
fn char_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
) -> texlang::prelude::Result<()> {
|
||||
let code = i32::parse(input)?;
|
||||
let character = u32::try_from(code)
|
||||
.ok()
|
||||
.and_then(char::from_u32)
|
||||
.ok_or_else(|| {
|
||||
input.fatal_error(error::SimpleTokenError::new(
|
||||
token,
|
||||
format!("\\char expects a valid character code, got {code}"),
|
||||
))
|
||||
})?;
|
||||
let character_token = token::Token::new_other(character, token.trace_key());
|
||||
input.as_mut().expansions_mut().push(character_token);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn newlength_primitive(
|
||||
token: token::Token,
|
||||
input: &mut vm::ExecutionInput<CompilerState>,
|
||||
@@ -4977,6 +4996,18 @@ fn is_builtin_package(package: &str) -> bool {
|
||||
| "xcolor"
|
||||
| "color"
|
||||
| "graphics"
|
||||
| "manyfoot"
|
||||
| "algorithm"
|
||||
| "algorithmicx"
|
||||
| "algpseudocode"
|
||||
| "balance"
|
||||
| "listings"
|
||||
| "fix-cm"
|
||||
| "apacite"
|
||||
| "setspace"
|
||||
| "threeparttable"
|
||||
| "appendix"
|
||||
| "vruler"
|
||||
| "amsbsy"
|
||||
| "bm"
|
||||
| "mathrsfs"
|
||||
@@ -7131,6 +7162,10 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
|
||||
texlang_stdlib::input::get_read::<16, CompilerState>()
|
||||
.with_tag(engine_prefix::global_assignment_tag()),
|
||||
);
|
||||
built_ins.insert(
|
||||
"char",
|
||||
texlang::command::BuiltIn::new_execution(char_primitive),
|
||||
);
|
||||
built_ins.insert(
|
||||
"chardef",
|
||||
texlang_stdlib::chardef::get_chardef::<CompilerState>()
|
||||
|
||||
@@ -1703,3 +1703,25 @@ fn tex_conditionals_choose_branches() {
|
||||
assert!(!svg.contains("OTHER"));
|
||||
assert!(!svg.contains(">FULL</text>"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_newif_conditionals_skip_correctly() {
|
||||
let source = r"\documentclass{article}
|
||||
\begin{document}
|
||||
\newif\ifouter
|
||||
\newif\ifinner
|
||||
\outerfalse
|
||||
\ifouter
|
||||
\ifinner INNER-TRUE\else INNER-FALSE\fi
|
||||
OUTER-TRUE
|
||||
\else
|
||||
OUTER-FALSE
|
||||
\fi
|
||||
\end{document}";
|
||||
let value = fresh_preview(source, "nested-newif.tex");
|
||||
assert_eq!(value["status"], "ok");
|
||||
let svg = page_svg(&value, 0);
|
||||
assert!(svg.contains(">OUTER-F</text>"));
|
||||
assert!(!svg.contains("OUTER-TRUE"));
|
||||
assert!(!svg.contains(">INNER-F</text>"));
|
||||
}
|
||||
+36
-4
@@ -159,7 +159,15 @@ fn false_case<S: HasComponent<Component>>(
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
if tag == Some(input.state().component().tags.if_tag) {
|
||||
let is_if_prefixed = match command_ref {
|
||||
token::CommandRef::ControlSequence(name) => input
|
||||
.vm()
|
||||
.cs_name_interner()
|
||||
.resolve(*name)
|
||||
.is_some_and(|name| name.starts_with("if")),
|
||||
_ => false,
|
||||
};
|
||||
if tag == Some(input.state().component().tags.if_tag) || is_if_prefixed {
|
||||
depth += 1;
|
||||
}
|
||||
if tag == Some(input.state().component().tags.fi_tag) {
|
||||
@@ -331,7 +339,15 @@ fn if_case_primitive_fn<S: HasComponent<Component>>(
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
if tag == Some(input.state().component().tags.if_tag) {
|
||||
let is_if_prefixed = match command_ref {
|
||||
token::CommandRef::ControlSequence(name) => input
|
||||
.vm()
|
||||
.cs_name_interner()
|
||||
.resolve(*name)
|
||||
.is_some_and(|name| name.starts_with("if")),
|
||||
_ => false,
|
||||
};
|
||||
if tag == Some(input.state().component().tags.if_tag) || is_if_prefixed {
|
||||
depth += 1;
|
||||
}
|
||||
if tag == Some(input.state().component().tags.fi_tag) {
|
||||
@@ -402,7 +418,15 @@ fn or_primitive_fn<S: HasComponent<Component>>(
|
||||
let token = input.unexpanded().next_or_err(OrEndOfInputError {})?;
|
||||
if let token::Value::CommandRef(command_ref) = &token.value() {
|
||||
let tag = input.commands_map().get_tag(command_ref);
|
||||
if tag == Some(input.state().component().tags.if_tag) {
|
||||
let is_if_prefixed = match command_ref {
|
||||
token::CommandRef::ControlSequence(name) => input
|
||||
.vm()
|
||||
.cs_name_interner()
|
||||
.resolve(*name)
|
||||
.is_some_and(|name| name.starts_with("if")),
|
||||
_ => false,
|
||||
};
|
||||
if tag == Some(input.state().component().tags.if_tag) || is_if_prefixed {
|
||||
depth += 1;
|
||||
}
|
||||
if tag == Some(input.state().component().tags.fi_tag) {
|
||||
@@ -463,7 +487,15 @@ fn else_primitive_fn<S: HasComponent<Component>>(
|
||||
let token = input.unexpanded().next_or_err(ElseEndOfInputError {})?;
|
||||
if let token::Value::CommandRef(command_ref) = &token.value() {
|
||||
let tag = input.commands_map().get_tag(command_ref);
|
||||
if tag == Some(input.state().component().tags.if_tag) {
|
||||
let is_if_prefixed = match command_ref {
|
||||
token::CommandRef::ControlSequence(name) => input
|
||||
.vm()
|
||||
.cs_name_interner()
|
||||
.resolve(*name)
|
||||
.is_some_and(|name| name.starts_with("if")),
|
||||
_ => false,
|
||||
};
|
||||
if tag == Some(input.state().component().tags.if_tag) || is_if_prefixed {
|
||||
depth += 1;
|
||||
}
|
||||
if tag == Some(input.state().component().tags.fi_tag) {
|
||||
|
||||
Reference in new issue
Block a user