Skip to content

Commit

Permalink
fix(math-parser): add empty exponent support (#1688)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zitrone44 authored Jul 25, 2024
1 parent 2bec80e commit 5c5a81b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ mulFactor : expo // higher presedence!
| mulFactor ' '* expo
;

expo : expo ' '* EXP ' '* SUB? factor
expo : expo ' '* EXP ' '* SUB? factor
| expo unicode_expo
| factor
;

factor : LEFT? OPENING_ROUND_BRACKET ' '* expr ' '* RIGHT? CLOSING_ROUND_BRACKET
| OPENING_CURLY_BRACKET ' '* expr ' '* CLOSING_CURLY_BRACKET
| EMPTY_CURLY_BRACKETS
| (NUMBER|VAR)
;

Expand Down Expand Up @@ -63,6 +64,7 @@ OPENING_ROUND_BRACKET: '(';
CLOSING_ROUND_BRACKET: ')';
OPENING_CURLY_BRACKET: '{';
CLOSING_CURLY_BRACKET: '}';
EMPTY_CURLY_BRACKETS: '{}';
OPENING_SQUARE_BRACKET: '[';
CLOSING_SQUARE_BRACKET: ']';
LEFT: '\\left';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class AstBuilder(val eq: MathParser.EqContext) {
factor.expr() !== null -> buildExpr(factor.expr())
factor.NUMBER() !== null -> Num(germanFormat.parse(factor.NUMBER().text.replace("{,}", ",")))
factor.VAR() !== null -> Var(factor.VAR().text)
factor.EMPTY_CURLY_BRACKETS() !== null -> Num(1)
else -> throw IllegalArgumentException("not a legal factor: ${factor.text}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,16 @@ internal class MathParserHelperTest {
)
}

@Test
fun parseEmptyExp() {
assertEquals(
Ast(
Operation(Operator.ADD, Operation(Operator.EXP, Num(2), Num(1)), Operation(Operator.EXP, Num(2), Num(1)))
),
MathParserHelper.parse("2^{}+2^{}")
)
}

@Test
fun parseLatexRad() {
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,14 @@ internal class SemanticAstComparatorTest {
)
)
}

@Test
fun emptyExponentTest() {
assertTrue(
semanticAstComparator.compare(
MathParserHelper.parse("a^{}"),
MathParserHelper.parse("a")
)
)
}
}

0 comments on commit 5c5a81b

Please sign in to comment.