Skip to content

Commit

Permalink
Merge pull request #274 from mgreminger/unintended-function-name-fix
Browse files Browse the repository at this point in the history
fix: provide better parser error handling for mathrm in expression
  • Loading branch information
mgreminger authored Sep 16, 2024
2 parents 49a8157 + 067f203 commit aa264b1
Show file tree
Hide file tree
Showing 9 changed files with 1,130 additions and 865 deletions.
15 changes: 12 additions & 3 deletions src/cells/MathField.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SvelteComponent } from "svelte";
import type MathFieldElement from "../MathField.svelte";

import { parseLatex } from "../parser/LatexToSympy";
import type { Statement, FieldTypes, DataTableInfo } from "../parser/types";
Expand All @@ -12,7 +12,7 @@ export class MathField {
parsingError = true;
parsingErrorMessage = "Invalid Syntax";
statement: Statement | null = null;
element: SvelteComponent | null = null;
element: MathFieldElement | null = null;
pendingNewLatex = false;
newLatex:string;

Expand All @@ -25,7 +25,7 @@ export class MathField {
setPendingLatex(): void {
if (this.pendingNewLatex && this.element) {
this.element.setLatex(this.newLatex, false);
this.pendingNewLatex = false;
this.pendingNewLatex = false; // needed to prevent the unlikely scenario where an immediateUpdate leads to an infinite loop
}
}

Expand All @@ -39,5 +39,14 @@ export class MathField {
this.parsingError = result.parsingError;
this.parsingErrorMessage = result.parsingErrorMessage;
this.statement = result.statement;

if (result.immediateUpdate && this.element) {
let startingPosition = this.element.getMathField().position;
this.setPendingLatex();
if (startingPosition > this.element.getMathField().lastOffset) {
startingPosition = this.element.getMathField().lastOffset;
}
this.element.getMathField().position = startingPosition;
}
}
}
1 change: 1 addition & 0 deletions src/parser/LatexLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ LTE: '\\le';
GTE: '\\ge';

COMMA: ',';
DECIMAL_POINT: '.';

CARET_SINGLE_CHAR_NUMBER: '^' [0-9];
CARET_SINGLE_CHAR_ID: '^' [a-zA-Z];
Expand Down
782 changes: 393 additions & 389 deletions src/parser/LatexLexer.ts

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/parser/LatexParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ expr: <assoc=right> id CARET_SINGLE_CHAR_ID_UNDERSCORE_SUBSCRIPT #exp
| expr integral_cmd #missingMultiplication
| expr derivative_cmd #missingMultiplication
| expr n_derivative_cmd #missingMultiplication
| CMD_PLACEHOLDER (L_BRACE R_BRACE)? #emptyPlaceholder
| CMD_PLACEHOLDER (L_BRACE R_BRACE)? #emptyPlaceholder
| (CMD_MATHRM L_BRACE expr R_BRACE)? (DECIMAL_POINT | number)? CMD_MATHRM L_BRACE expr R_BRACE (DECIMAL_POINT | number)? #removeOperatorFont
;


Expand Down
Loading

0 comments on commit aa264b1

Please sign in to comment.