{"id":17129466,"url":"https://github.com/rkirsling/formula-parser","last_synced_at":"2025-04-13T07:16:14.193Z","repository":{"id":57240882,"uuid":"41607337","full_name":"rkirsling/formula-parser","owner":"rkirsling","description":"A parser class for simple formulae.","archived":false,"fork":false,"pushed_at":"2017-02-21T16:24:41.000Z","size":20,"stargazers_count":11,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-26T23:35:17.919Z","etag":null,"topics":["ast","formula","operator-precedence","parser"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rkirsling.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-29T21:08:16.000Z","updated_at":"2025-03-02T18:41:06.000Z","dependencies_parsed_at":"2022-09-07T23:13:04.573Z","dependency_job_id":null,"html_url":"https://github.com/rkirsling/formula-parser","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkirsling%2Fformula-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkirsling%2Fformula-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkirsling%2Fformula-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rkirsling%2Fformula-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rkirsling","download_url":"https://codeload.github.com/rkirsling/formula-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248442626,"owners_count":21104228,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","formula","operator-precedence","parser"],"created_at":"2024-10-14T19:09:43.361Z","updated_at":"2025-04-13T07:16:14.131Z","avatar_url":"https://github.com/rkirsling.png","language":"JavaScript","readme":"# FormulaParser\n\n[![NPM version](http://img.shields.io/npm/v/formula-parser.svg?style=flat)](https://npmjs.org/package/formula-parser)\n[![MIT license](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)\n\nA parser class for simple formulae, like those of algebra and propositional logic.  \nProduces [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree)s in JSON format.\n\nThe algorithm is a fully-immutable JavaScript adaptation of\n[precedence climbing](http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm#climbing).\n\n## Install\n\n```sh\nnpm install formula-parser\n```\n\nES module:\n```js\nimport FormulaParser from 'formula-parser';\n```\n\nNode:\n```js\nconst FormulaParser = require('formula-parser');\n```\n\nBrowser:\n```html\n\u003cscript src=\"node_modules/formula-parser/dist/formula-parser.js\"\u003e\u003c/script\u003e\n```\n\n## Usage\n\n### Background\n\n`FormulaParser` is a parser class for _operator-precedence languages_, i.e.,\n[context-free languages](https://en.wikipedia.org/wiki/Context-free_grammar)\nwhich have only variables, (prefix) unary operators, and (infix) binary operators.\n\nThis restriction means that the grammar for a parser instance is wholly specified\nby the operator definitions (and a key with which to label variable nodes).\n\n### Creating a parser instance\n\nAs the [`algebraParser`](examples/algebraParser.js) example demonstrates,\nan _operator definition_ is an object like the following:\n```js\n{ symbol: '+', key: 'plus', precedence: 1, associativity: 'left' }\n```\nIt specifies a `symbol`, a `key` for its AST node,\na `precedence` level, and (for binaries) an `associativity` direction.\n\nOnce the definitions are assembled, creating a parser instance is straightforward:\n```js\nconst algebraParser = new FormulaParser(variableKey, unaries, binaries);\n```\n\n### Parsing\n\nAfter creating a `FormulaParser` instance, calling its `parse` method will produce an AST for a formula:\n```js\nalgebraParser.parse('(a + b * c) ^ -d');\n```\n→\n```json\n{ \"exp\": [\n  { \"plus\": [\n    { \"var\": \"a\" },\n    { \"mult\": [\n      { \"var\": \"b\" },\n      { \"var\": \"c\" }\n    ]}\n  ]},\n  { \"neg\": { \"var\": \"d\" } }\n]}\n```\n\n## Limitations (presumed and actual)\n\n### Constants\n\nTechnically, constants aren't supported\u0026mdash;the leaves of the formula are all treated as variables,\nthe values of which are to be evaluated at some post-parse stage.\n\nThat said, since a \"variable\" for present purposes is any alphanumeric string (including underscores),\n`'true'`, `'PI'`, and even `'3'` will all be happily parsed as such.\n(_Of course, numbers in decimal notation will fail._)\n\n### Function symbols\n\nFunction symbols aren't explicitly supported either, but they can be simulated by operator symbols.\nSpecifying `sin` as a unary symbol will accept `sin x` or `sin(x)`,\nwhile specifying `mod` as a binary symbol will accept `x mod y`.\n\n### Operator position\n\nUnfortunately, this one's hard and fast:  \nUnary symbols _must_ be in prefix notation and binary symbols _must_ be in infix notation.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frkirsling%2Fformula-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frkirsling%2Fformula-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frkirsling%2Fformula-parser/lists"}