Giter Club home page Giter Club logo

expression-parser's Introduction

expression-parser

Customizable Infix to Postfix Converter/Resolver


npm (scoped)

expression-parser is yet another infix to postfix/reverse polish notation converter. I created it because I needed the ability to parse expressions with symbols/identifiers/variables of different formats.

Highlights

Supports the following expression tokens:

  • basic binary operators (e.g. +,-,*,/,%,^)
  • unary negation operator (e.g. -10)
  • parenthesis for grouping

Supports the following operands:

  • Integers (e.g. -10,1,0,1,2,3,...)
  • Floating point numbers (e.g. 3.14159)
  • Symbols in the form if singe upper/lower case letters (e.g. a+b-c)
  • Custom symbom/variable patters (specify your own regular expression)

Detects the following expression errors:

  • Too many operands
  • Missing operands
  • Missing opening parenthesis
  • Missing closing parenthesis

Is thoughrouly unit tested.

Installation

npm install @estilles/expression-parser

Usage

Importing

import { parse, resolve, toString, toArray } from '@estilles/expression-parser'

or

const { parse, resolve, toString, toArray } = require('@estilles/expression-parser')

API

parse()

The parse() function takes a mathematica expression, tokenizes and parses it, then returns a parsed expression object that represents the expression in postfix or reversed polish notation (RPN).

The resulting postfix/RPN expression can be visualized by converting it to a string using toString(parsedExpression) or an array of tokens using toArray(parsedExpression) (see below).

The optional symbolPattern is a regular expression used to parse symbols/variables within the expression. The default symbolPattern is /^([a-zA-Z])/. It will accept single upper or lower case letters. If you want to create your symbolPattern, the regular expression must: (a) start with a begin anchor ^, and (b) capture the name of the symbol/variable in the first capture group.

parse(expression : String[, symbolPattern : Regex]) => parsedExpression

e.g.
parse('1+2*3/4')
parse('(a+b)^2*c')
parse('$(instance:var1) + $(instance:var2)', /^\$\(((?:[^:$)]+):(?:[^)$]+))\)/)

toString()

Converts a parsed expression to a string

toString(parsedExpression) => string

e.g.
toString(parse('1+2*3/4')) => '1 2 3 * 4 / 1 +'
toString(parse('(a+b)^2*c')) => 'a b + 2 ^ c *'

toArray()

Converts a parsed expression to an array of tokens

toArray(parsedExpression) => array

e.g.
toString(parse('1+2*3/4')) => ['1', '2', '3', '*', '4', '/', '1', '+']
toString(parse('(a+b)^2*c')) => ['a', 'b', '+', '2', '^', 'c', '*']

resolve(parsedExpression[, values]) -> result

Takes a parsed expression and an optional dictionary of symbol -> values, evaluates the expression and returns the result.

resolve(parsedExpression[, values]) => result

e.g.
resolve(parse('1+2*3/4')) => 2.5
resolve(parse('(a+b)^2*c'), { a: 1, b: 2, c: 3}) => 27

expression-parser's People

Contributors

estilles avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.