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 nested subscripts parsing #114 #117

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
46 changes: 44 additions & 2 deletions packages/unified-latex-util-parse/tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { trimRenderInfo } from "@unified-latex/unified-latex-util-render-info";
import * as Ast from "@unified-latex/unified-latex-types/index";
import { trim } from "@unified-latex/unified-latex-util-trim";
import { processLatexToAstViaUnified } from "@unified-latex/unified-latex";
import { PluginOptions as ParserPluginOptions } from "../libs/plugin-from-string";
import * as AstBuilder from "@unified-latex/unified-latex-builder";

/* eslint-env jest */

Expand All @@ -18,9 +20,9 @@ describe("unified-latex-util-parse", () => {
let value: string | undefined;
let file: VFile | undefined;

function strToNodes(str: string) {
function strToNodes(str: string, options?: ParserPluginOptions) {
value = str;
file = processLatexToAstViaUnified().processSync({ value });
file = processLatexToAstViaUnified(options).processSync({ value });
const root = trimRenderInfo(file.result as any) as Ast.Root;
return root.content;
}
Expand Down Expand Up @@ -73,4 +75,44 @@ describe("unified-latex-util-parse", () => {
trim(ast);
expect(ast).toEqual(targetAst);
});

it("nested math subscripts", () => {
let ast = strToNodes("{1_2}", {
mode: 'math',
});
expect(ast).toEqual([{
type: "group",
content: [
AstBuilder.s("1"),
AstBuilder.m("_", AstBuilder.args([
AstBuilder.arg([AstBuilder.s("2")], {
openMark: '{',
closeMark: '}',
}),
]), { escapeToken: "" }),
],
}]);
});

it("nested math single char arguments", () => {
const ast = strToNodes("{\\frac12}", {
mode: "math",
});
expect(ast).toEqual([{
type: "group",
content: [
AstBuilder.m('frac', AstBuilder.args([
AstBuilder.arg([AstBuilder.s("1")], {
openMark: '{',
closeMark: '}',
}),
AstBuilder.arg([AstBuilder.s("2")], {
openMark: '{',
closeMark: '}',
}),
])),
],
}]);

});
});
2 changes: 1 addition & 1 deletion packages/unified-latex-util-pegjs/grammars/latex.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ math_token "math token"
= special_macro
/ macro
/ full_comment
/ whitespace* x:group whitespace* { return x; }
/ whitespace* x:math_group whitespace* { return x; }
/ whitespace* x:alignment_tab whitespace* { return x; }
/ macro_parameter
/ whitespace* superscript whitespace* {
Expand Down
6 changes: 4 additions & 2 deletions packages/unified-latex/libs/unified-latex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export const processLatexViaUnified = (
* Use `unified()` to a string to an `Ast.Ast` and then return it. This function
* will not print/pretty-print the `Ast.Ast` back to a string.
*/
export const processLatexToAstViaUnified = () => {
return unified().use(unifiedLatexFromString).use(unifiedLatexAstComplier);
export const processLatexToAstViaUnified = (
options?: ParserPluginOptions
) => {
return unified().use(unifiedLatexFromString, options).use(unifiedLatexAstComplier);
};
Loading