@evergis/condition
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -1,33 +0,31 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const OperationAccept_1 = require("./OperationAccept"); | ||
const Token_1 = require("./Token"); | ||
exports.AcceptTree = new OperationAccept_1.OperationAccept('', Token_1.TokenType.Null, [ | ||
new OperationAccept_1.OperationAccept('>', Token_1.TokenType.ArGr, [ | ||
new OperationAccept_1.OperationAccept('=', Token_1.TokenType.ArGre), | ||
import { OperationAccept } from './OperationAccept'; | ||
import { TokenType } from './Token'; | ||
export const AcceptTree = new OperationAccept('', TokenType.Null, [ | ||
new OperationAccept('>', TokenType.ArGr, [ | ||
new OperationAccept('=', TokenType.ArGre), | ||
]), | ||
new OperationAccept_1.OperationAccept('<', Token_1.TokenType.ArLs, [ | ||
new OperationAccept_1.OperationAccept('=', Token_1.TokenType.ArLse), | ||
new OperationAccept('<', TokenType.ArLs, [ | ||
new OperationAccept('=', TokenType.ArLse), | ||
]), | ||
new OperationAccept_1.OperationAccept('!', Token_1.TokenType.BoolNot, [ | ||
new OperationAccept_1.OperationAccept('=', Token_1.TokenType.ArNotEq), | ||
new OperationAccept('!', TokenType.BoolNot, [ | ||
new OperationAccept('=', TokenType.ArNotEq), | ||
]), | ||
new OperationAccept_1.OperationAccept('&', Token_1.TokenType.Null, [ | ||
new OperationAccept_1.OperationAccept('&', Token_1.TokenType.BoolAnd), | ||
new OperationAccept('&', TokenType.Null, [ | ||
new OperationAccept('&', TokenType.BoolAnd), | ||
]), | ||
new OperationAccept_1.OperationAccept('=', Token_1.TokenType.ArEq, [ | ||
new OperationAccept_1.OperationAccept('=', Token_1.TokenType.ArEq), | ||
new OperationAccept('=', TokenType.ArEq, [ | ||
new OperationAccept('=', TokenType.ArEq), | ||
]), | ||
new OperationAccept_1.OperationAccept('|', Token_1.TokenType.Null, [ | ||
new OperationAccept_1.OperationAccept('|', Token_1.TokenType.BoolOr), | ||
new OperationAccept('|', TokenType.Null, [ | ||
new OperationAccept('|', TokenType.BoolOr), | ||
]), | ||
new OperationAccept_1.OperationAccept('+', Token_1.TokenType.ArPlus), | ||
new OperationAccept_1.OperationAccept('-', Token_1.TokenType.ArMinus), | ||
new OperationAccept_1.OperationAccept('*', Token_1.TokenType.ArMul), | ||
new OperationAccept_1.OperationAccept('/', Token_1.TokenType.ArDiv), | ||
new OperationAccept_1.OperationAccept('(', Token_1.TokenType.ExprOpen), | ||
new OperationAccept_1.OperationAccept(')', Token_1.TokenType.ExprClose), | ||
new OperationAccept_1.OperationAccept('[', Token_1.TokenType.ArrayOpen), | ||
new OperationAccept_1.OperationAccept(']', Token_1.TokenType.ArrayClose), | ||
new OperationAccept_1.OperationAccept(',', Token_1.TokenType.Separator), | ||
new OperationAccept('+', TokenType.ArPlus), | ||
new OperationAccept('-', TokenType.ArMinus), | ||
new OperationAccept('*', TokenType.ArMul), | ||
new OperationAccept('/', TokenType.ArDiv), | ||
new OperationAccept('(', TokenType.ExprOpen), | ||
new OperationAccept(')', TokenType.ExprClose), | ||
new OperationAccept('[', TokenType.ArrayOpen), | ||
new OperationAccept(']', TokenType.ArrayClose), | ||
new OperationAccept(',', TokenType.Separator), | ||
]); |
@@ -1,4 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class Char { | ||
export class Char { | ||
static isChar(char) { | ||
@@ -47,3 +45,2 @@ return typeof char === 'string' && char.length === 1; | ||
} | ||
exports.Char = Char; | ||
Char.LEFT_BRACKET = '{'; | ||
@@ -50,0 +47,0 @@ Char.SINGLE_QUOTE = "'"; |
@@ -1,8 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Token_1 = require("./Token"); | ||
const ExpressionVisitor_1 = require("./ExpressionVisitor"); | ||
import { TokenType } from './Token'; | ||
import { ExpressionVisitor } from './ExpressionVisitor'; | ||
class ConditionEvaluatorError extends Error { | ||
} | ||
class ConditionEvaluator extends ExpressionVisitor_1.ExpressionVisitor { | ||
export class ConditionEvaluator extends ExpressionVisitor { | ||
constructor(attributes) { | ||
@@ -15,3 +13,3 @@ super(); | ||
const { x, y } = this.acceptLeafs(node); | ||
return node.value === Token_1.TokenType.BoolAnd ? x && y : x || y; | ||
return node.value === TokenType.BoolAnd ? x && y : x || y; | ||
} | ||
@@ -22,13 +20,13 @@ visitOpNode(node) { | ||
switch (node.value) { | ||
case Token_1.TokenType.ArEq: | ||
case TokenType.ArEq: | ||
return x === y; | ||
case Token_1.TokenType.ArNotEq: | ||
case TokenType.ArNotEq: | ||
return x !== y; | ||
case Token_1.TokenType.ArGre: | ||
case TokenType.ArGre: | ||
return x >= y; | ||
case Token_1.TokenType.ArLse: | ||
case TokenType.ArLse: | ||
return x <= y; | ||
case Token_1.TokenType.ArGr: | ||
case TokenType.ArGr: | ||
return x > y; | ||
case Token_1.TokenType.ArLs: | ||
case TokenType.ArLs: | ||
return x < y; | ||
@@ -56,2 +54,1 @@ default: | ||
} | ||
exports.ConditionEvaluator = ConditionEvaluator; |
@@ -1,13 +0,11 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ExpressionTokenizer_1 = require("./ExpressionTokenizer"); | ||
const ExpressionAst_1 = require("./ExpressionAst"); | ||
const ConditionVisitor_1 = require("./ConditionVisitor"); | ||
const isObject_1 = require("./isObject"); | ||
exports.isBoolResult = (result) => isObject_1.isObject(result) && 'boolOperations' in result; | ||
exports.isExpression = (result) => isObject_1.isObject(result) && 'attribute' in result; | ||
class ConditionInterpreter { | ||
import { ExpressionTokenizer, } from './ExpressionTokenizer'; | ||
import { ExpressionAst } from './ExpressionAst'; | ||
import { ConditionVisitor, } from './ConditionVisitor'; | ||
import { isObject } from './isObject'; | ||
export const isBoolResult = (result) => isObject(result) && 'boolOperations' in result; | ||
export const isExpression = (result) => isObject(result) && 'attribute' in result; | ||
export class ConditionInterpreter { | ||
constructor(condition) { | ||
this.result = null; | ||
this.tokenizer = new ExpressionTokenizer_1.ExpressionTokenizer(condition); | ||
this.tokenizer = new ExpressionTokenizer(condition); | ||
} | ||
@@ -20,8 +18,8 @@ get value() { | ||
const tokens = this.tokenizer.tokenize(); | ||
const ast = new ExpressionAst_1.ExpressionAst(tokens).value; | ||
const result = ast && ast.accept(new ConditionVisitor_1.ConditionVisitor()); | ||
if (exports.isBoolResult(result)) { | ||
const ast = new ExpressionAst(tokens).value; | ||
const result = ast && ast.accept(new ConditionVisitor()); | ||
if (isBoolResult(result)) { | ||
this.result = result; | ||
} | ||
else if (exports.isExpression(result)) { | ||
else if (isExpression(result)) { | ||
this.result = { | ||
@@ -45,2 +43,1 @@ boolOperations: [], | ||
} | ||
exports.ConditionInterpreter = ConditionInterpreter; |
@@ -1,6 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ExpressionNode_1 = require("./ExpressionNode"); | ||
const ExpressionVisitor_1 = require("./ExpressionVisitor"); | ||
class ConditionVisitor extends ExpressionVisitor_1.ExpressionVisitor { | ||
import { BoolOperationNode, } from './ExpressionNode'; | ||
import { ExpressionVisitor, ExpressionVisitorError } from './ExpressionVisitor'; | ||
export class ConditionVisitor extends ExpressionVisitor { | ||
visitBoolOpNode(node) { | ||
@@ -10,4 +8,4 @@ super.visitBoolOpNode(node); | ||
const rightExpression = node.right && node.right.accept(this); | ||
if (node.left instanceof ExpressionNode_1.BoolOperationNode && | ||
node.right instanceof ExpressionNode_1.BoolOperationNode) { | ||
if (node.left instanceof BoolOperationNode && | ||
node.right instanceof BoolOperationNode) { | ||
this.checkBoolOperations(node.value, [ | ||
@@ -29,3 +27,3 @@ ...leftExpression.boolOperations, | ||
} | ||
if (node.right instanceof ExpressionNode_1.BoolOperationNode) { | ||
if (node.right instanceof BoolOperationNode) { | ||
this.checkBoolOperations(node.value, rightExpression.boolOperations); | ||
@@ -37,3 +35,3 @@ return { | ||
} | ||
if (node.left instanceof ExpressionNode_1.BoolOperationNode) { | ||
if (node.left instanceof BoolOperationNode) { | ||
this.checkBoolOperations(node.value, leftExpression.boolOperations); | ||
@@ -67,6 +65,5 @@ return { | ||
if (operations.some(op => op !== baseOperation)) { | ||
throw new ExpressionVisitor_1.ExpressionVisitorError('bool operations should be the same'); | ||
throw new ExpressionVisitorError('bool operations should be the same'); | ||
} | ||
} | ||
} | ||
exports.ConditionVisitor = ConditionVisitor; |
@@ -1,11 +0,9 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ExpressionAst_1 = require("./ExpressionAst"); | ||
const ConditionEvaluator_1 = require("./ConditionEvaluator"); | ||
const ExpressionTokenizer_1 = require("./ExpressionTokenizer"); | ||
function evaluateCondition(condition, attributes) { | ||
import { ExpressionAst } from './ExpressionAst'; | ||
import { ConditionEvaluator } from './ConditionEvaluator'; | ||
import { ExpressionTokenizer } from './ExpressionTokenizer'; | ||
export function evaluateCondition(condition, attributes) { | ||
try { | ||
const tokens = new ExpressionTokenizer_1.ExpressionTokenizer(condition).tokenize(); | ||
const ast = new ExpressionAst_1.ExpressionAst(tokens).value; | ||
return ast && ast.accept(new ConditionEvaluator_1.ConditionEvaluator(attributes)); | ||
const tokens = new ExpressionTokenizer(condition).tokenize(); | ||
const ast = new ExpressionAst(tokens).value; | ||
return ast && ast.accept(new ConditionEvaluator(attributes)); | ||
} | ||
@@ -16,2 +14,1 @@ catch { | ||
} | ||
exports.evaluateCondition = evaluateCondition; |
@@ -1,6 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ExpressionNode_1 = require("./ExpressionNode"); | ||
const shuntingYard_1 = require("./shuntingYard"); | ||
class ExpressionAst { | ||
import { AttributeNode, BoolOperationNode, OperationNode, ValueNode, } from './ExpressionNode'; | ||
import { shuntingYard } from './shuntingYard'; | ||
export class ExpressionAst { | ||
constructor(tokens) { | ||
@@ -15,14 +13,14 @@ this.tokens = tokens; | ||
} | ||
this.tokens = shuntingYard_1.shuntingYard(this.tokens); | ||
this.tokens = shuntingYard(this.tokens); | ||
const stack = []; | ||
for (; this.currentPosition < this.tokens.length; this.nextToken()) { | ||
if (ExpressionNode_1.ValueNode.is(this.currentToken)) { | ||
stack.push(new ExpressionNode_1.ValueNode(this.currentToken.value)); | ||
if (ValueNode.is(this.currentToken)) { | ||
stack.push(new ValueNode(this.currentToken.value)); | ||
} | ||
if (ExpressionNode_1.AttributeNode.is(this.currentToken) && | ||
if (AttributeNode.is(this.currentToken) && | ||
typeof this.currentToken.value === 'string') { | ||
stack.push(new ExpressionNode_1.AttributeNode(this.currentToken.value)); | ||
stack.push(new AttributeNode(this.currentToken.value)); | ||
} | ||
if (ExpressionNode_1.OperationNode.is(this.currentToken)) { | ||
const node = new ExpressionNode_1.OperationNode(this.currentToken.type); | ||
if (OperationNode.is(this.currentToken)) { | ||
const node = new OperationNode(this.currentToken.type); | ||
node.right = stack.pop() || null; | ||
@@ -32,4 +30,4 @@ node.left = stack.pop() || null; | ||
} | ||
if (ExpressionNode_1.BoolOperationNode.is(this.currentToken)) { | ||
const node = new ExpressionNode_1.BoolOperationNode(this.currentToken.type); | ||
if (BoolOperationNode.is(this.currentToken)) { | ||
const node = new BoolOperationNode(this.currentToken.type); | ||
node.right = stack.pop() || null; | ||
@@ -50,2 +48,1 @@ node.left = stack.pop() || null; | ||
} | ||
exports.ExpressionAst = ExpressionAst; |
@@ -1,5 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Token_1 = require("./Token"); | ||
class ExpressionNode { | ||
import { TokenType } from './Token'; | ||
export class ExpressionNode { | ||
constructor(value = null, left = null, right = null) { | ||
@@ -17,4 +15,3 @@ this.value = value; | ||
} | ||
exports.ExpressionNode = ExpressionNode; | ||
class ValueNode extends ExpressionNode { | ||
export class ValueNode extends ExpressionNode { | ||
static is(token) { | ||
@@ -24,10 +21,9 @@ return ValueNode.TOKENS.includes(token.type); | ||
} | ||
exports.ValueNode = ValueNode; | ||
ValueNode.TOKENS = [ | ||
Token_1.TokenType.String, | ||
Token_1.TokenType.Number, | ||
Token_1.TokenType.DynamicIdentifier, | ||
Token_1.TokenType.Date, | ||
TokenType.String, | ||
TokenType.Number, | ||
TokenType.DynamicIdentifier, | ||
TokenType.Date, | ||
]; | ||
class AttributeNode extends ExpressionNode { | ||
export class AttributeNode extends ExpressionNode { | ||
static is(token) { | ||
@@ -37,5 +33,4 @@ return AttributeNode.TOKENS.includes(token.type); | ||
} | ||
exports.AttributeNode = AttributeNode; | ||
AttributeNode.TOKENS = [Token_1.TokenType.Identifier]; | ||
class OperationNode extends ExpressionNode { | ||
AttributeNode.TOKENS = [TokenType.Identifier]; | ||
export class OperationNode extends ExpressionNode { | ||
static is(token) { | ||
@@ -45,12 +40,11 @@ return OperationNode.TOKENS.includes(token.type); | ||
} | ||
exports.OperationNode = OperationNode; | ||
OperationNode.TOKENS = [ | ||
Token_1.TokenType.ArEq, | ||
Token_1.TokenType.ArNotEq, | ||
Token_1.TokenType.ArGr, | ||
Token_1.TokenType.ArGre, | ||
Token_1.TokenType.ArLs, | ||
Token_1.TokenType.ArLse, | ||
TokenType.ArEq, | ||
TokenType.ArNotEq, | ||
TokenType.ArGr, | ||
TokenType.ArGre, | ||
TokenType.ArLs, | ||
TokenType.ArLse, | ||
]; | ||
class BoolOperationNode extends ExpressionNode { | ||
export class BoolOperationNode extends ExpressionNode { | ||
static is(token) { | ||
@@ -60,3 +54,2 @@ return BoolOperationNode.TOKENS.includes(token.type); | ||
} | ||
exports.BoolOperationNode = BoolOperationNode; | ||
BoolOperationNode.TOKENS = [Token_1.TokenType.BoolAnd, Token_1.TokenType.BoolOr]; | ||
BoolOperationNode.TOKENS = [TokenType.BoolAnd, TokenType.BoolOr]; |
@@ -1,8 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const AcceptTree_1 = require("./AcceptTree"); | ||
const Char_1 = require("./Char"); | ||
const Token_1 = require("./Token"); | ||
import { AcceptTree } from './AcceptTree'; | ||
import { Char } from './Char'; | ||
import { Token, TokenType } from './Token'; | ||
const TOKENIZER_MAX_SIZE = 1000; | ||
class ExpressionTokenizer { | ||
export class ExpressionTokenizer { | ||
constructor(input) { | ||
@@ -15,3 +13,3 @@ this.currentPosition = 0; | ||
const isEndOfString = this.tokens.length && | ||
this.tokens[this.tokens.length - 1].type === Token_1.TokenType.EndOfString; | ||
this.tokens[this.tokens.length - 1].type === TokenType.EndOfString; | ||
if (isEndOfString) { | ||
@@ -26,3 +24,3 @@ return this.tokens; | ||
this.tokens.push(token); | ||
if (token.type === Token_1.TokenType.EndOfString || | ||
if (token.type === TokenType.EndOfString || | ||
this.tokens.length > TOKENIZER_MAX_SIZE) { | ||
@@ -39,27 +37,27 @@ break; | ||
for (; this.currentPosition < this.input.length; this.nextSymbol()) { | ||
if (Char_1.Char.isWhiteSpace(this.current)) { | ||
if (Char.isWhiteSpace(this.current)) { | ||
continue; | ||
} | ||
const acceptOp = AcceptTree_1.AcceptTree.accept(this.current); | ||
const acceptOp = AcceptTree.accept(this.current); | ||
if (acceptOp !== null) { | ||
return this.captureSpecial(acceptOp); | ||
} | ||
if (Char_1.Char.isDigit(this.current)) { | ||
if (Char.isDigit(this.current)) { | ||
return this.captureNumber(); | ||
} | ||
if (Char_1.Char.isLetter(this.current) || Char_1.Char.isUnderscore(this.current)) { | ||
if (Char.isLetter(this.current) || Char.isUnderscore(this.current)) { | ||
return this.captureIdentifier(); | ||
} | ||
if (this.current === Char_1.Char.LEFT_BRACKET) { | ||
if (this.current === Char.LEFT_BRACKET) { | ||
return this.captureDynamicIdentifier(); | ||
} | ||
if (this.current === Char_1.Char.DOUBLE_QUOTE || | ||
this.current === Char_1.Char.SINGLE_QUOTE) { | ||
if (this.current === Char.DOUBLE_QUOTE || | ||
this.current === Char.SINGLE_QUOTE) { | ||
return this.captureString(); | ||
} | ||
if (this.current === Char_1.Char.HASH_SYMBOL) { | ||
if (this.current === Char.HASH_SYMBOL) { | ||
return this.captureDate(); | ||
} | ||
} | ||
return new Token_1.Token(Token_1.TokenType.EndOfString); | ||
return new Token(TokenType.EndOfString); | ||
} | ||
@@ -76,3 +74,3 @@ nextSymbol() { | ||
const next = op.accept(this.current); | ||
if (next === null || next.tokenType == Token_1.TokenType.Null) { | ||
if (next === null || next.tokenType == TokenType.Null) { | ||
break; | ||
@@ -84,10 +82,10 @@ } | ||
if (this.current && | ||
Char_1.Char.isDigit(this.current) && | ||
op.tokenType === Token_1.TokenType.ArMinus) { | ||
return new Token_1.Token(Token_1.TokenType.Number, -(this.captureNumber().value || 0)); | ||
Char.isDigit(this.current) && | ||
op.tokenType === TokenType.ArMinus) { | ||
return new Token(TokenType.Number, -(this.captureNumber().value || 0)); | ||
} | ||
if (op.tokenType === Token_1.TokenType.Null) { | ||
if (op.tokenType === TokenType.Null) { | ||
throw new ExpressionTokenizerError(`Unexpected token '${op.value}'`); | ||
} | ||
return new Token_1.Token(op.tokenType); | ||
return new Token(op.tokenType); | ||
} | ||
@@ -98,10 +96,10 @@ captureNumber() { | ||
for (; this.currentPosition < this.input.length; this.nextSymbol()) { | ||
if ((!Char_1.Char.isDigit(this.current) || | ||
if ((!Char.isDigit(this.current) || | ||
this.currentPosition === this.input.length) && | ||
this.current !== '.') { | ||
return new Token_1.Token(Token_1.TokenType.Number, parseFloat(result)); | ||
return new Token(TokenType.Number, parseFloat(result)); | ||
} | ||
result += this.current; | ||
} | ||
return new Token_1.Token(Token_1.TokenType.Number, parseFloat(result)); | ||
return new Token(TokenType.Number, parseFloat(result)); | ||
} | ||
@@ -111,4 +109,4 @@ captureIdentifier() { | ||
for (; this.currentPosition < this.input.length; this.nextSymbol()) { | ||
if (!Char_1.Char.isLetterOrDigit(this.current) && | ||
!Char_1.Char.isUnderscore(this.current)) { | ||
if (!Char.isLetterOrDigit(this.current) && | ||
!Char.isUnderscore(this.current)) { | ||
break; | ||
@@ -119,11 +117,11 @@ } | ||
if (result == 'true' || result == 'True' || result == 'TRUE') { | ||
return new Token_1.Token(Token_1.TokenType.Terminal, true); | ||
return new Token(TokenType.Terminal, true); | ||
} | ||
if (result == 'false' || result == 'False' || result == 'FALSE') { | ||
return new Token_1.Token(Token_1.TokenType.Terminal, false); | ||
return new Token(TokenType.Terminal, false); | ||
} | ||
if (result == 'in' || result == 'IN') { | ||
return new Token_1.Token(Token_1.TokenType.ArIn); | ||
return new Token(TokenType.ArIn); | ||
} | ||
return new Token_1.Token(Token_1.TokenType.Identifier, result); | ||
return new Token(TokenType.Identifier, result); | ||
} | ||
@@ -134,3 +132,3 @@ captureDynamicIdentifier() { | ||
for (; this.currentPosition < this.input.length; this.nextSymbol()) { | ||
if (this.current == Char_1.Char.RIGHT_BRACKET) { | ||
if (this.current == Char.RIGHT_BRACKET) { | ||
this.nextSymbol(); | ||
@@ -142,5 +140,5 @@ break; | ||
if (result == 'null' || result == 'Null' || result == 'NULL') { | ||
return new Token_1.Token(Token_1.TokenType.DynamicIdentifier, null); | ||
return new Token(TokenType.DynamicIdentifier, null); | ||
} | ||
return new Token_1.Token(Token_1.TokenType.DynamicIdentifier, result); | ||
return new Token(TokenType.DynamicIdentifier, result); | ||
} | ||
@@ -153,3 +151,3 @@ captureString() { | ||
for (; this.currentPosition < this.input.length; this.nextSymbol()) { | ||
if (!escaping && this.current === Char_1.Char.BACK_SLASH) { | ||
if (!escaping && this.current === Char.BACK_SLASH) { | ||
escaping = true; | ||
@@ -165,3 +163,3 @@ continue; | ||
} | ||
return new Token_1.Token(Token_1.TokenType.String, result); | ||
return new Token(TokenType.String, result); | ||
} | ||
@@ -172,10 +170,8 @@ captureDate() { | ||
if (typeof stringToken.value === 'string') { | ||
return new Token_1.Token(Token_1.TokenType.Date, stringToken.value); | ||
return new Token(TokenType.Date, stringToken.value); | ||
} | ||
return new Token_1.Token(Token_1.TokenType.Date, null); | ||
return new Token(TokenType.Date, null); | ||
} | ||
} | ||
exports.ExpressionTokenizer = ExpressionTokenizer; | ||
class ExpressionTokenizerError extends Error { | ||
export class ExpressionTokenizerError extends Error { | ||
} | ||
exports.ExpressionTokenizerError = ExpressionTokenizerError; |
@@ -1,19 +0,16 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ExpressionNode_1 = require("./ExpressionNode"); | ||
class ExpressionVisitorError extends Error { | ||
import { AttributeNode, BoolOperationNode, OperationNode, ValueNode, } from './ExpressionNode'; | ||
export class ExpressionVisitorError extends Error { | ||
} | ||
exports.ExpressionVisitorError = ExpressionVisitorError; | ||
class ExpressionVisitor { | ||
export class ExpressionVisitor { | ||
visit(node) { | ||
if (node instanceof ExpressionNode_1.BoolOperationNode) { | ||
if (node instanceof BoolOperationNode) { | ||
return this.visitBoolOpNode(node); | ||
} | ||
if (node instanceof ExpressionNode_1.OperationNode) { | ||
if (node instanceof OperationNode) { | ||
return this.visitOpNode(node); | ||
} | ||
if (node instanceof ExpressionNode_1.ValueNode) { | ||
if (node instanceof ValueNode) { | ||
return this.visitValueNode(node); | ||
} | ||
if (node instanceof ExpressionNode_1.AttributeNode) { | ||
if (node instanceof AttributeNode) { | ||
return this.visitAttributeNode(node); | ||
@@ -35,8 +32,8 @@ } | ||
checkBoolOpNode(node) { | ||
if (!(node.left instanceof ExpressionNode_1.OperationNode || | ||
node.left instanceof ExpressionNode_1.BoolOperationNode)) { | ||
if (!(node.left instanceof OperationNode || | ||
node.left instanceof BoolOperationNode)) { | ||
throw new ExpressionVisitorError('left node of bool operation invalid'); | ||
} | ||
if (!(node.right instanceof ExpressionNode_1.OperationNode || | ||
node.right instanceof ExpressionNode_1.BoolOperationNode)) { | ||
if (!(node.right instanceof OperationNode || | ||
node.right instanceof BoolOperationNode)) { | ||
throw new ExpressionVisitorError('right node of bool operation invalid'); | ||
@@ -46,6 +43,6 @@ } | ||
checkOpNode(node) { | ||
if (!(node.left instanceof ExpressionNode_1.AttributeNode)) { | ||
if (!(node.left instanceof AttributeNode)) { | ||
throw new ExpressionVisitorError('left node of expression not attribute'); | ||
} | ||
if (!(node.right instanceof ExpressionNode_1.ValueNode)) { | ||
if (!(node.right instanceof ValueNode)) { | ||
throw new ExpressionVisitorError('right node of expression not value'); | ||
@@ -60,2 +57,1 @@ } | ||
} | ||
exports.ExpressionVisitor = ExpressionVisitor; |
@@ -1,34 +0,12 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var AcceptTree_1 = require("./AcceptTree"); | ||
exports.AcceptTree = AcceptTree_1.AcceptTree; | ||
var Char_1 = require("./Char"); | ||
exports.Char = Char_1.Char; | ||
var ConditionEvaluator_1 = require("./ConditionEvaluator"); | ||
exports.ConditionEvaluator = ConditionEvaluator_1.ConditionEvaluator; | ||
var ConditionInterpreter_1 = require("./ConditionInterpreter"); | ||
exports.ConditionInterpreter = ConditionInterpreter_1.ConditionInterpreter; | ||
exports.isBoolResult = ConditionInterpreter_1.isBoolResult; | ||
exports.isExpression = ConditionInterpreter_1.isExpression; | ||
var ConditionVisitor_1 = require("./ConditionVisitor"); | ||
exports.ConditionVisitor = ConditionVisitor_1.ConditionVisitor; | ||
var evaluateCondition_1 = require("./evaluateCondition"); | ||
exports.evaluateCondition = evaluateCondition_1.evaluateCondition; | ||
var ExpressionAst_1 = require("./ExpressionAst"); | ||
exports.ExpressionAst = ExpressionAst_1.ExpressionAst; | ||
var ExpressionNode_1 = require("./ExpressionNode"); | ||
exports.ExpressionNode = ExpressionNode_1.ExpressionNode; | ||
exports.ValueNode = ExpressionNode_1.ValueNode; | ||
exports.OperationNode = ExpressionNode_1.OperationNode; | ||
exports.BoolOperationNode = ExpressionNode_1.BoolOperationNode; | ||
exports.AttributeNode = ExpressionNode_1.AttributeNode; | ||
var ExpressionVisitor_1 = require("./ExpressionVisitor"); | ||
exports.ExpressionVisitor = ExpressionVisitor_1.ExpressionVisitor; | ||
exports.ExpressionVisitorError = ExpressionVisitor_1.ExpressionVisitorError; | ||
var OperationAccept_1 = require("./OperationAccept"); | ||
exports.OperationAccept = OperationAccept_1.OperationAccept; | ||
var OperationPrecedence_1 = require("./OperationPrecedence"); | ||
exports.OperationPrecedence = OperationPrecedence_1.OperationPrecedence; | ||
var Token_1 = require("./Token"); | ||
exports.Token = Token_1.Token; | ||
exports.TokenType = Token_1.TokenType; | ||
export { AcceptTree } from './AcceptTree'; | ||
export { Char } from './Char'; | ||
export { ConditionEvaluator } from './ConditionEvaluator'; | ||
export { ConditionInterpreter, isBoolResult, isExpression, } from './ConditionInterpreter'; | ||
export { ConditionVisitor, } from './ConditionVisitor'; | ||
export { evaluateCondition } from './evaluateCondition'; | ||
export { ExpressionAst } from './ExpressionAst'; | ||
export { ExpressionNode, ValueNode, OperationNode, BoolOperationNode, AttributeNode, } from './ExpressionNode'; | ||
export { ExpressionVisitor, ExpressionVisitorError } from './ExpressionVisitor'; | ||
export { OperationAccept } from './OperationAccept'; | ||
export { OperationPrecedence } from './OperationPrecedence'; | ||
export { Token, TokenType } from './Token'; |
@@ -1,6 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isObject(value) { | ||
export function isObject(value) { | ||
return typeof value === 'object' && value !== null && !Array.isArray(value); | ||
} | ||
exports.isObject = isObject; |
@@ -1,4 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class OperationAccept { | ||
export class OperationAccept { | ||
constructor(value, type, operations = []) { | ||
@@ -14,2 +12,1 @@ this.acceptMap = new Map(); | ||
} | ||
exports.OperationAccept = OperationAccept; |
@@ -1,18 +0,16 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Token_1 = require("./Token"); | ||
exports.OperationPrecedence = new Map(); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ExprOpen, 0); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ExprClose, 0); | ||
exports.OperationPrecedence.set(Token_1.TokenType.BoolAnd, 1); | ||
exports.OperationPrecedence.set(Token_1.TokenType.BoolOr, 1); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ArEq, 2); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ArNotEq, 2); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ArGr, 2); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ArGre, 2); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ArLs, 2); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ArLse, 2); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ArPlus, 3); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ArMinus, 3); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ArMul, 4); | ||
exports.OperationPrecedence.set(Token_1.TokenType.ArDiv, 4); | ||
import { TokenType } from './Token'; | ||
export const OperationPrecedence = new Map(); | ||
OperationPrecedence.set(TokenType.ExprOpen, 0); | ||
OperationPrecedence.set(TokenType.ExprClose, 0); | ||
OperationPrecedence.set(TokenType.BoolAnd, 1); | ||
OperationPrecedence.set(TokenType.BoolOr, 1); | ||
OperationPrecedence.set(TokenType.ArEq, 2); | ||
OperationPrecedence.set(TokenType.ArNotEq, 2); | ||
OperationPrecedence.set(TokenType.ArGr, 2); | ||
OperationPrecedence.set(TokenType.ArGre, 2); | ||
OperationPrecedence.set(TokenType.ArLs, 2); | ||
OperationPrecedence.set(TokenType.ArLse, 2); | ||
OperationPrecedence.set(TokenType.ArPlus, 3); | ||
OperationPrecedence.set(TokenType.ArMinus, 3); | ||
OperationPrecedence.set(TokenType.ArMul, 4); | ||
OperationPrecedence.set(TokenType.ArDiv, 4); |
@@ -1,15 +0,13 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Token_1 = require("./Token"); | ||
const ExpressionNode_1 = require("./ExpressionNode"); | ||
const OperationPrecedence_1 = require("./OperationPrecedence"); | ||
import { TokenType } from './Token'; | ||
import { AttributeNode, BoolOperationNode, OperationNode, ValueNode, } from './ExpressionNode'; | ||
import { OperationPrecedence } from './OperationPrecedence'; | ||
const last = (el) => el[el.length - 1]; | ||
const isOperand = (token) => ExpressionNode_1.ValueNode.is(token) || ExpressionNode_1.AttributeNode.is(token); | ||
const isOperation = (token) => ExpressionNode_1.OperationNode.is(token) || ExpressionNode_1.BoolOperationNode.is(token); | ||
const isHigherPrecedence = (tokenOne, tokenTwo) => OperationPrecedence_1.OperationPrecedence.get(tokenOne.type) > | ||
OperationPrecedence_1.OperationPrecedence.get(tokenTwo.type); | ||
const isOperand = (token) => ValueNode.is(token) || AttributeNode.is(token); | ||
const isOperation = (token) => OperationNode.is(token) || BoolOperationNode.is(token); | ||
const isHigherPrecedence = (tokenOne, tokenTwo) => OperationPrecedence.get(tokenOne.type) > | ||
OperationPrecedence.get(tokenTwo.type); | ||
/* | ||
https://en.wikipedia.org/wiki/Shunting-yard_algorithm | ||
*/ | ||
function shuntingYard(tokens) { | ||
export function shuntingYard(tokens) { | ||
let output = []; | ||
@@ -24,3 +22,3 @@ const operators = []; | ||
isHigherPrecedence(last(operators), token) && | ||
last(operators).type !== Token_1.TokenType.ExprOpen) { | ||
last(operators).type !== TokenType.ExprOpen) { | ||
output.push(operators.pop()); | ||
@@ -30,10 +28,10 @@ } | ||
} | ||
if (token.type === Token_1.TokenType.ExprOpen) { | ||
if (token.type === TokenType.ExprOpen) { | ||
operators.push(token); | ||
} | ||
if (token.type === Token_1.TokenType.ExprClose) { | ||
while (last(operators) && last(operators).type !== Token_1.TokenType.ExprOpen) { | ||
if (token.type === TokenType.ExprClose) { | ||
while (last(operators) && last(operators).type !== TokenType.ExprOpen) { | ||
output.push(operators.pop()); | ||
} | ||
if (last(operators).type === Token_1.TokenType.ExprOpen) { | ||
if (last(operators).type === TokenType.ExprOpen) { | ||
operators.pop(); | ||
@@ -48,2 +46,1 @@ } | ||
} | ||
exports.shuntingYard = shuntingYard; |
@@ -1,4 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var TokenType; | ||
export var TokenType; | ||
(function (TokenType) { | ||
@@ -32,4 +30,4 @@ TokenType[TokenType["Number"] = 0] = "Number"; | ||
TokenType[TokenType["Separator"] = 26] = "Separator"; | ||
})(TokenType = exports.TokenType || (exports.TokenType = {})); | ||
class Token { | ||
})(TokenType || (TokenType = {})); | ||
export class Token { | ||
constructor(type, value = null) { | ||
@@ -40,2 +38,1 @@ this.type = type; | ||
} | ||
exports.Token = Token; |
{ | ||
"name": "@evergis/condition", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Condition parser for spcore expressions", | ||
@@ -5,0 +5,0 @@ "author": "Kirill Protasov <kprotasov@everpoint.ru>", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33924
884