Skip to content

Commit

Permalink
fix(lexer): test number of macro arguments before reading them (#85)
Browse files Browse the repository at this point in the history
* fix(lexer): test num of macro arguments before reading them

* fix: env definition changed
  • Loading branch information
Myriad-Dreamin committed Dec 28, 2023
1 parent cb855d7 commit 9f8052e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions crates/mitex-lexer/src/macro_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,13 +768,18 @@ impl<'a> MacroEngine<'a> {
Some((name, action, m))
}

// todo: insufficient macro arguments
fn read_macro_args(
ctx: &mut StreamContext<'a>,
num_args: u8,
opt: Option<Vec<Tok<'a>>>,
) -> Option<Vec<Vec<Tok<'a>>>> {
let mut args = Vec::with_capacity(num_args as usize);

if num_args == 0 {
return Some(args);
}

let mut num_of_read: u8 = 0;
loop {
match ctx.peek_not_trivia() {
Expand Down
15 changes: 12 additions & 3 deletions crates/mitex-lexer/tests/expand_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ fn get_macro(input: &str, macro_name: &str) -> String {

#[test]
fn ignoring_unimplemented() {
assert_snapshot!(r#"\AtEndOfClass{code}"#, @r###"\AtEndOfClass{code}"###);
assert_snapshot!(tokens(r#"\AtEndOfClass{code}"#), @r###"
CommandName(Generic)("\\AtEndOfClass")
Left(Curly)("{")
Word("code")
Right(Curly)("}")
"###);
}

#[test]
Expand Down Expand Up @@ -166,6 +171,9 @@ fn declare_macro() {

#[test]
fn subst_macro() {
// Description: zero arguments
assert_snapshot!(tokens(r#"\newcommand{\f}{f}\f"#), @r###"Word("f")"###);
// Description: reversed order of tokens
assert_snapshot!(tokens(r#"\newcommand{\f}[2]{#1f(#2)}\f\hat xy"#), @r###"
CommandName(Generic)("\\hat")
Word("f")
Expand All @@ -174,9 +182,10 @@ fn subst_macro() {
Right(Paren)(")")
Word("y")
"###);
assert_snapshot!(tokens(r#"\newenvironment{f}[2]{begin}{end}\begin{f}test\end{f}"#), @r###"
// Description: environment with macro
assert_snapshot!(tokens(r#"\newenvironment{f}{begin}{end}\begin{f}test\end{f}"#), @r###"
Word("begin")
Word("st")
Word("test")
Word("end")
"###);
}
Expand Down

0 comments on commit 9f8052e

Please sign in to comment.