Skip to content

Avoid crash when a known command's required arg ends a group#190

Open
SAY-5 wants to merge 1 commit into
alvinwan:masterfrom
SAY-5:fix-label-missing-arg-crash
Open

Avoid crash when a known command's required arg ends a group#190
SAY-5 wants to merge 1 commit into
alvinwan:masterfrom
SAY-5:fix-label-missing-arg-crash

Conversation

@SAY-5

@SAY-5 SAY-5 commented Jun 18, 2026

Copy link
Copy Markdown

Summary

  • Fixes Error parsing #189. TexSoup(r"{\label}") raised TypeError: Malformed argument because \label has a known one-required-argument signature, and when its argument is absent at the end of a group the parser consumed the closing } as the argument, leaving the group unterminated.
  • read_arg_required now skips the fallback "use the next token as the argument" branch when that next token is a closing delimiter (} or ]), so a missing required argument is simply left empty instead of swallowing the delimiter.

The reduced case is {\label}, but the same crash hit the original \tikzmath{\label = 1;} / \node[...] {\label} document in the issue.

Validation

  • python3 -m pytest (190 passed)
  • New regression test test_known_command_missing_arg_at_group_end fails on the current parser and passes with this change.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
@lorenzo25832

Copy link
Copy Markdown

This does not seem to fully fix it:

In [90]: TexSoup(r"\foreach \label in {1,2,3,4}{}")
Out[90]: \foreach \label{ in }{1,2,3,4}{}

In [91]: TexSoup(r"\foreach \i in {1,2,3,4}{}")
Out[91]: \foreach \i in {1,2,3,4}{}

@SAY-5

SAY-5 commented Jul 2, 2026

Copy link
Copy Markdown
Author

Thanks for testing. The two cases differ because \label has a known one-required-argument signature while \i does not, so only \label reaches the required-arg path. This PR stops that path from consuming the closing brace (the TypeError in #189), which is why {\label} no longer crashes. Your \foreach \label in {...} example is a separate issue: with no brace group present, the required-arg fallback (rule c in read_arg_required) takes the next token, here the text ' in ', as the argument. That is the documented behaviour for a missing brace arg, so declining to consume a bare text token would be a behaviour change beyond the crash fix. Happy to extend the PR to skip text tokens in that fallback for known commands if that is the direction you'd prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error parsing

2 participants