0.6.3 • Published 7 months ago

asciimath-parser-nearley v0.6.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Asciimath Parser Nearley

Another asciimath parser based on nearley.

Usage

Install with a package manager.

pnpm install asciimath-parser-nearley

Import and create an instance of AsciiMath.

import { AsciiMath } from 'asciimath-parser-nearley'
const am = new AsciiMath({
  display: false,
  throws: false,
  symbols: {
    keywords: {/** ... */},
    /** ... */
  },
  replaceBeforeTokenizing: [],
})
console.log(am.toTex('sum_(n=1)^(+oo)1/n^2=pi^2/6'))
// \sum_{ n = 1 }^{ + \infty } \frac{ 1 }{ n^2 } = \frac{ \pi^2 }{ 6 }

Warning Some rules are not consistent with asciimath-parser. Please view the documentation below.

Differences

Asciimath Parser Core version is written in pure TypeScript, and we may fail to consider some edge cases, so please report any issue if you run into any error.

Asciimath Parser Nearley version is parsed with nearley grammar, so it is strict and you have to write correct formulas.

outputAM coreAM nearley
${\color{red} a}$color(red)(a)color"red" a
$\text{text}$text(text)text"text"
$a\hspace{12pt}b$a hspace(12pt) ba hspace"12pt" b
$\frac{\partial L}{\partial \sigma^2}$pp L (sigma^2)pp L sigma^2 or pp L (sigma^2)

Performance

Nearley version is about 40% slower than core version; the tokenization step take about half time.

String Literal

In Nearley version, text have to be quoted with " to skip tokenize, for example the argument of color, tex, and hspace command must be quoted. You can use escape sequences \" and \\ in a string literal.

Different params

  • symbols: Symbols

    In Nearley version, We removed the extConst param, and added symbols param:

    const am = new AsciiMath({
      symbols: {
        keyword: {
          dx: { tex: '{\\text{d}x}' },
          dy: { tex: '{\\text{d}y}' },
          dz: { tex: '{\\text{d}z}' },
          dt: { tex: '{\\text{d}t}' },
          ee: { tex: '\\text{e}' },
          ii: { tex: '\\text{i}' },
        },
        opOAB: {
          fbox: { tex: '\\fbox[ $2 ]{ $1 }' },
        },
      },
    })
  • throws: boolean

    If true, throws error when parser encounters any syntax error. If false, just output error message as a text string. Default is false.

New Grammar

Added new infix symbol over, atop and choose:

a over b,
a atop b,
a choose b

Added new symbol -- which will be transformed into \hline. You can draw a table like this

{:
--
|a|b|;
--
c, d;
--
:}

Verbatim environment (experimental)

verb"#include<stdio.h>
int main() {
  printf(\"hello, world!\n\");
  return 0;
}"

How to dev

We use moo as the tokenizer, but it does not seem to support ESM, so we downloaded the repo and built it locally.

The parser core is parser.ne. After changing the file, you should run the following code to compile it to grammar.js.

pnpm run nearley

Then the index.ts will export AsciiMath properly. You can use it like below, and the params are almost the same as asciimath-parser.

const am = new AsciiMath()
const tex = am.toTex('sum_(n=1)^(+oo)1/n^2=pi^2/6')
console.log(tex)
// \displaystyle{ \sum_{ n = 1 }^{ + \infty } \frac{ 1 }{ n^2 } = \frac{ \pi^2 }{ 6 } }

TODO

  • ==_a^b
  • multiline formulas
  • unicode support
  • escape backslashes in text like "\\"
  • vabatim environment
    verb "aaa
    bbb"
    translates to
    \begin{aligned}
    & \verb|aaa| \\
    & \verb|bbb|
    \end{aligned}
  • single paren case like ( and )
  • table syntax sugar
    table[
    a, b, c;
    d, e, f;
    ]
    is equal to
    {:
    --
    |a|b|c|
    --
    d, e, f;
    --
    :}
0.6.3

7 months ago

0.6.2

10 months ago

0.6.1

10 months ago

0.5.9

1 year ago

0.6.0

1 year ago

0.5.8

1 year ago

0.5.7

1 year ago