From 62099438eb1411d69f0a95eeb67977d5771563b7 Mon Sep 17 00:00:00 2001 From: postaid Date: Wed, 23 Oct 2024 12:53:15 +0500 Subject: [PATCH] replace "group" with "math_group" in math_token grammar --- .../tests/parse.test.ts | 46 ++++++++++++++++++- .../grammars/latex.pegjs | 2 +- packages/unified-latex/libs/unified-latex.ts | 6 ++- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/packages/unified-latex-util-parse/tests/parse.test.ts b/packages/unified-latex-util-parse/tests/parse.test.ts index adf3ba88..2bda2779 100644 --- a/packages/unified-latex-util-parse/tests/parse.test.ts +++ b/packages/unified-latex-util-parse/tests/parse.test.ts @@ -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 */ @@ -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; } @@ -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: '}', + }), + ])), + ], + }]); + + }); }); diff --git a/packages/unified-latex-util-pegjs/grammars/latex.pegjs b/packages/unified-latex-util-pegjs/grammars/latex.pegjs index ea703117..aa200bdc 100644 --- a/packages/unified-latex-util-pegjs/grammars/latex.pegjs +++ b/packages/unified-latex-util-pegjs/grammars/latex.pegjs @@ -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* { diff --git a/packages/unified-latex/libs/unified-latex.ts b/packages/unified-latex/libs/unified-latex.ts index f17ffe77..d902280d 100644 --- a/packages/unified-latex/libs/unified-latex.ts +++ b/packages/unified-latex/libs/unified-latex.ts @@ -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); };