Skip to content

jeanbenitez/logical-expression-parser

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logical Expression Parser (Fork)

This is a logical expression parser for JavaScript, it can parse a logical expression into a AST object and evaluates the result using your token checking function.

Supported logical operators

  1. OR
  2. AND
  3. () Parentheses

How it works

  1. The parser parse and tokenize the expression, for example one of your function requires REGISTED AND (SPECIAL OR INVITED)
  2. Parser then will pass REGISTED, SPECIAL and INVITED into your token checking function to get a boolean result
  3. Finaly the parser will evaluates the final result

Example

const LEP = require('@jeanbenitez/lep');

const requirements = 'REGISTED AND (SPECIAL OR INVITED)';
const listA = ['REGISTED', 'INVITED'];
const listB = ['SPECIAL', 'EXPERT'];

const resultA = LEP.parse(requirements, t => listA.includes(t));
const resultB = LEP.parse(requirements, t => listB.includes(t));
console.log({ resultA, resultB });
// { resultA: true, resultB: false }

// Getting AST only
const ast = LEP.ast(requirements);
console.log({ ast });
/*
  {
    ast: ExpNode {
      op: 'AND',
      left: ExpNode {
        op: 'LITERAL',
        literal: 'REGISTED'
      },
      right: ExpNode {
        op: 'OR',
        left: [ExpNode],
        right: [ExpNode]
      }
    }
  }
*/

About

Logical expression parser written in JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%