From 2b8238b2ef1961b76573695996d7420e8c4a0204 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Wed, 25 Dec 2024 11:29:53 +0100 Subject: [PATCH] Fix parse erron when using inline math in cases* environment in inline math --- CHANGELOG.md | 2 ++ src/nl/hannahsten/texifyidea/grammar/LatexLexer.flex | 11 ++++++++++- test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae4085ee6..77cf50898 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ ### Fixed * Fix parse error when using parentheses in a group in a key value command argument +* Fix parse erron when using inline math in cases* environment in inline math +* ## [0.9.10-alpha.4] - 2024-12-21 diff --git a/src/nl/hannahsten/texifyidea/grammar/LatexLexer.flex b/src/nl/hannahsten/texifyidea/grammar/LatexLexer.flex index 035ef11e0..9c8db69e8 100644 --- a/src/nl/hannahsten/texifyidea/grammar/LatexLexer.flex +++ b/src/nl/hannahsten/texifyidea/grammar/LatexLexer.flex @@ -135,7 +135,7 @@ START_IFS=\\if | \\ifcat | \\ifx | \\ifcase | \\ifnum | \\ifodd | \\ifhmode | \\ ELSE=\\else END_IFS=\\fi -%states INLINE_MATH INLINE_MATH_LATEX DISPLAY_MATH TEXT_INSIDE_INLINE_MATH NESTED_INLINE_MATH PARTIAL_DEFINITION +%states INLINE_MATH INLINE_MATH_LATEX DISPLAY_MATH TEXT_INSIDE_INLINE_MATH NESTED_INLINE_MATH PARTIAL_DEFINITION ENVIRONMENT_INSIDE_INLINE_MATH %states NEW_ENVIRONMENT_DEFINITION_NAME NEW_ENVIRONMENT_DEFINITION NEW_ENVIRONMENT_SKIP_BRACE NEW_ENVIRONMENT_DEFINITION_END NEW_DOCUMENT_ENV_DEFINITION_NAME NEW_DOCUMENT_ENV_DEFINITION_ARGS_SPEC NEW_COMMAND_DEFINITION_PARAM1 NEW_COMMAND_DEFINITION_PARAM2 // latex3 has some special syntax @@ -462,6 +462,10 @@ END_IFS=\\fi // When already in inline math, when encountering a \text command we need to switch out of the math state // because if we encounter another $, then it will be an inline_math_start, not an inline_math_end \\text { yypushState(TEXT_INSIDE_INLINE_MATH); return COMMAND_TOKEN; } + // Similarly, environments like cases* from mathtools have text as their second column, which can have inline math + // We cannot use a single token for inline math start and end because the parser will try to parse the one that should be an 'end' as a 'start' + // Therefore, to make sure that we cannot have a START \begin{cases*} END ... START \end{cases*} END, we use a separate state + {BEGIN_TOKEN} { yypushState(ENVIRONMENT_INSIDE_INLINE_MATH); return BEGIN_TOKEN; } } // When in a \text in inline math, either start nested inline math or close the \text @@ -470,6 +474,11 @@ END_IFS=\\fi {CLOSE_BRACE} { yypopState(); return CLOSE_BRACE; } } + { + "$" { yypushState(NESTED_INLINE_MATH); return INLINE_MATH_START; } + {END_TOKEN} { yypopState(); return END_TOKEN; } +} + { {ROBUST_INLINE_MATH_END} { yypopState(); return INLINE_MATH_END; } } diff --git a/test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt b/test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt index a3f2378b4..8ffddf928 100644 --- a/test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt +++ b/test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt @@ -50,6 +50,11 @@ class LatexParserTest : BasePlatformTestCase() { LatexFileType, """ $ math \text{ text $\xi$ text } math$ + + $\begin{cases*} + 1 & if $ p \equiv 1 \pmod 4$ \\ + -1 & if $ p \equiv 3 \pmod 4$ + \end{cases*}$ a """.trimIndent() ) myFixture.checkHighlighting()