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

[EXPERIMENT] Create a recursive descent version of KAS' parser #1746

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
138 changes: 138 additions & 0 deletions packages/kas/src/__tests__/__snapshots__/lexer.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lexer should lex LaTeX tokens 1`] = `
[
{
"kind": "(",
},
{
"kind": "FRAC",
},
{
"kind": "{",
},
{
"kind": "INT",
"value": "1",
},
{
"kind": "}",
},
{
"kind": "{",
},
{
"kind": "VAR",
"value": "x",
},
{
"kind": "}",
},
{
"kind": "-",
},
{
"kind": "INT",
"value": "1",
},
{
"kind": ")",
},
{
"kind": "EOF",
},
]
`;

exports[`lexer should lex different numbers 1`] = `
[
{
"kind": "INT",
"value": "1",
},
{
"kind": "+",
},
{
"kind": "-",
},
{
"kind": "INT",
"value": "2",
},
{
"kind": "+",
},
{
"kind": "FLOAT",
"value": "3.14",
},
{
"kind": "EOF",
},
]
`;

exports[`lexer should lex simple tokens 1`] = `
[
{
"kind": "INT",
"value": "1",
},
{
"kind": "+",
},
{
"kind": "INT",
"value": "2",
},
{
"kind": "VAR",
"value": "x",
},
{
"kind": "VAR",
"value": "y",
},
{
"kind": "EOF",
},
]
`;

exports[`lexer should lex with options 1`] = `
[
{
"kind": "FUNC",
"value": "f",
},
{
"kind": "(",
},
{
"kind": "VAR",
"value": "x",
},
{
"kind": ")",
},
{
"kind": "SIGN",
"value": "=",
},
{
"kind": "CONST",
"value": "e",
},
{
"kind": "^",
},
{
"kind": "VAR",
"value": "x",
},
{
"kind": "EOF",
},
]
`;
27 changes: 27 additions & 0 deletions packages/kas/src/__tests__/lexer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {lex} from "../lexer";

describe("lexer", () => {
it("should lex simple tokens", () => {
const tokens = lex("1+2xy");

expect(tokens).toMatchSnapshot();
});

it("should lex LaTeX tokens", () => {
const tokens = lex("\\left(\\frac{1}{x}\u22121\\right)");

expect(tokens).toMatchSnapshot();
});

it("should lex different numbers", () => {
const tokens = lex("1+-2+3.14");

expect(tokens).toMatchSnapshot();
});

it("should lex with options", () => {
const tokens = lex("f(x)=e^x", {functions: ["f"]});

expect(tokens).toMatchSnapshot();
});
});
Loading
Loading