Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: provide better parser error handling for mathrm in expression #274

Merged
merged 6 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading