boolean-expressions
Version:
Boolean expression parser and evaluator. Makes use of the Ohm grammar package with grammar rules found [here](https://github.com/avadavat/boolean-expressions/blob/master/src/grammar/grammarRules.ts).
37 lines (31 loc) • 1.06 kB
text/typescript
import { grammar } from "ohm-js";
import { grammarRules } from "./grammar";
import { extractVariables } from "./util";
import { createSemantics } from "./evaluator";
class BooleanExpressions {
private variableNames: string[];
private evaluateFunc: (variables: string[]) => boolean;
constructor(exp: string) {
const truthGrammar = grammar(grammarRules);
const matchResult = truthGrammar.match(exp);
if (matchResult.failed()) {
throw new Error(`Parse failed ${matchResult.shortMessage}`);
}
this.variableNames = extractVariables(exp);
this.evaluateFunc = createSemantics(truthGrammar, matchResult);
}
/**
* Returns a list of all the unique variable names in the boolean expression.
*/
getVariableNames(): string[] {
return this.variableNames;
}
/**
* Evaluates the given boolean expression with the given variables set to true
* and the omitted variables set to false.
*/
evaluate(variables: string[]): boolean {
return this.evaluateFunc(variables);
}
}
export default BooleanExpressions;