Skip to content

Commit 649baa6

Browse files
committed
fix #168
1 parent 5a266d1 commit 649baa6

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
### Bug Fixes
44

5+
- **([#168](https://github.com/cortex-js/compute-engine/issues/168))
6+
Absolute Value**: Fixed parsing of nested absolute value expressions that
7+
start with a double bar (e.g. `||3-5|-4|`), which previously produced an
8+
invalid structure instead of evaluating correctly.
9+
510
- **([#244](https://github.com/cortex-js/compute-engine/issues/244))
611
Serialization**: Fixed LaTeX and ASCIIMath serialization ambiguity for
712
negative bases and negated powers. Powers now render `(-2)^2` (instead of

src/compute-engine/latex-syntax/parse.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,30 @@ export class _Parser implements Parser {
14051405
this.skipSpace();
14061406
let body = this.parseExpression();
14071407
this.skipSpace();
1408-
if (!this.matchBoundary()) {
1408+
const boundary = this._boundaries[this._boundaries.length - 1]?.tokens;
1409+
const matchedBoundary = this.matchBoundary();
1410+
const sameTrigger =
1411+
(typeof def.openTrigger === 'string' &&
1412+
typeof def.closeTrigger === 'string' &&
1413+
def.openTrigger === def.closeTrigger) ||
1414+
(Array.isArray(def.openTrigger) &&
1415+
Array.isArray(def.closeTrigger) &&
1416+
def.openTrigger.length === def.closeTrigger.length &&
1417+
def.openTrigger.every((tok, i) => tok === def.closeTrigger[i]));
1418+
if (matchedBoundary && isEmptySequence(body) && sameTrigger && boundary) {
1419+
// If the open/close delimiter are identical and the body is empty,
1420+
// we may have consumed an inner delimiter (e.g. "||3-5|-4|").
1421+
// Retry parsing without the boundary and look for the closing delimiter.
1422+
this.index = bodyStart;
1423+
this.skipSpace();
1424+
body = this.parseExpression();
1425+
this.skipSpace();
1426+
if (!this.matchAll(boundary)) {
1427+
this.index = start;
1428+
if (!this.atEnd) continue;
1429+
return null;
1430+
}
1431+
} else if (!matchedBoundary) {
14091432
// We couldn't parse the body up to the closing delimiter.
14101433
// This could be a case where the boundary of the enclosure is
14111434
// ambiguous, i.e. `|(a+|b|+c)|`. Attempt to parse without the boundary

test/compute-engine/latex-syntax/matchfix.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ describe('MATCHFIX abs and norm', () => {
110110
simplify = |a| + 3
111111
`));
112112

113+
test('||3-5|-4|', () => {
114+
const expr = engine.parse('||3-5|-4|');
115+
expect(expr.isValid).toBe(true);
116+
expect(expr.evaluate().toString()).toBe('2');
117+
});
118+
113119
test('||a||', () =>
114120
expect(check('||a||')).toMatchInlineSnapshot(`["Norm", "a"]`));
115121
test('||a||+|b|', () =>

0 commit comments

Comments
 (0)