Home

Awesome

mr-parser

Build Status npm Coverage Status Stability

js-standard-style

Parses mathematical expressions and outputs an AST

<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> <!-- END doctoc generated TOC please keep comment here to allow auto update -->

Description

mr-parser is a lightweight mathematical parser inspired by math.js expression parser, unlike math.js expression parser the AST nodes contain no info on how to compile a node which makes the parser modular

why?

I felt that I had to make another parser because:

Grammar

program          : block (; block)*

block            : assignment

assignment       : ternary
                 | symbol `=` assignment

ternary          : logicalOR
                 | logicalOR `?` ternary `:` ternary

logicalOR        : logicalXOR
                 | logicalXOR (`||`,`or`) logicalOR

logicalXOR       : logicalAND
                 : logicalAND `xor` logicalXOR

logicalAND       : bitwiseOR
                 | bitwiseOR (`&&`,`and`) logicalAND

bitwiseOR        : bitwiseXOR
                 | bitwiseXOR `|` bitwiseOR

bitwiseXOR       : bitwiseAND
                 | bitwiseAND `^|` bitwiseXOR

bitwiseAND       : relational
                 | relational `&` bitwiseAND

relational       : shift
                 | shift (`!=` | `==` | `>` | '<' | '<=' |'>=') shift)

shift            : additive
                 | additive (`>>` | `<<` | `>>>`) shift

additive         : multiplicative
                 | multiplicative (`+` | `-`) additive

multiplicative   : unary
                 | unary (`*` | `/` | `%`) multiplicative
                 | unary symbol

unary            : pow
                 | (`-` | `+` | `~`) unary

pow              : factorial
                 | factorial (`^`, '**') unary

factorial        : symbol
                 | symbol (`!`)

symbol           : symbolToken
                 | symbolToken functionCall
                 | string

functionCall     : `(` `)`
                 | `(` ternary (, ternary)* `)`

string           : `'` (character)* `'`
                 : `"` (character)* `"`
                 | array

array            : `[` `]`
                 | `[` assignment (, assignment)* `]`
                 | number

number           : number-token
                 | parentheses

parentheses      : `(` assignment `)`
                 : end

end              : null-token

Install

$ npm install --save mr-parser

Usage

var Parser = require('mr-parser').Parser;
Parser.parse('1 * (2 + 3) * 4')

// returns
{
  blocks:[{
    op: '*',
    args: [{
      value: '1',
      valueType: 'number'
    }, {
      op: '*',
      args: [{
        op: '+',
        args: [
          { value: '2', valueType: 'number' },
          { value: '3', valueType: 'number' }
        ]
      }, {
        value: '4',
        valueType: 'number'
      }]
    }]
  }]
} 

Parser.parse('2x')

// returns
{
  blocks: [{
    op: '*',
    args: [
      { value: '2', valueType: 'number' },
      { name: 'x' }
    ]
  }]
}

API

Parser = require('mr-parser').Parser

Parser.parse(expression)

params

returns

2015 MIT © Mauricio Poppe