Skip to content

Commit

Permalink
Fix parse erron when using inline math in cases* environment in inlin…
Browse files Browse the repository at this point in the history
…e math
  • Loading branch information
PHPirates committed Dec 25, 2024
1 parent 2c7b791 commit 2b8238b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 10 additions & 1 deletion src/nl/hannahsten/texifyidea/grammar/LatexLexer.flex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -470,6 +474,11 @@ END_IFS=\\fi
{CLOSE_BRACE} { yypopState(); return CLOSE_BRACE; }
}

<ENVIRONMENT_INSIDE_INLINE_MATH> {
"$" { yypushState(NESTED_INLINE_MATH); return INLINE_MATH_START; }
{END_TOKEN} { yypopState(); return END_TOKEN; }
}

<INLINE_MATH_LATEX> {
{ROBUST_INLINE_MATH_END} { yypopState(); return INLINE_MATH_END; }
}
Expand Down
5 changes: 5 additions & 0 deletions test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 2b8238b

Please sign in to comment.