UNPKG

numenor

Version:

Customizable, safe evaluator of JavaScript-like expressions.

210 lines (110 loc) 11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExpressionParser = void 0; var _Parser2 = require("./Parser"); var _Lexer = require("./Lexer"); var _Value = require("./Parser/Parselet/Value"); var _Assignment = require("./Parser/Parselet/Assignment"); var _BinaryOperator = require("./Parser/Parselet/BinaryOperator"); var Precedence = _interopRequireWildcard(require("./Parser/Precedence")); var _Sequence = require("./Parser/Parselet/Sequence"); var _Conditional = require("./Parser/Parselet/Conditional"); var _UnaryOperator = require("./Parser/Parselet/UnaryOperator"); var _MemberAccess = require("./Parser/Parselet/MemberAccess"); var _Call = require("./Parser/Parselet/Call"); var _Group = require("./Parser/Parselet/Group"); var _ArrayLiteral = require("./Parser/Parselet/ArrayLiteral"); var _ObjectLiteral = require("./Parser/Parselet/ObjectLiteral"); var _Await = require("./Parser/Parselet/Await"); var _Lambda = require("./Parser/Parselet/Lambda"); var _Spread = require("./Parser/Parselet/Spread"); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } var ExpressionParser = /*#__PURE__*/ function (_Parser) { _inherits(ExpressionParser, _Parser); function ExpressionParser(lexer) { var _this; _classCallCheck(this, ExpressionParser); _this = _possibleConstructorReturn(this, _getPrototypeOf(ExpressionParser).call(this, lexer)); _this.ignore(_Lexer.TokenType.Whitespace); _this.ignore(_Lexer.TokenType.LineTerminator); _this.setInfix(_Lexer.TokenType.Comma, _Sequence.Sequence); _this.setInfix(_Lexer.TokenType.Eq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.Eq)); _this.setInfix(_Lexer.TokenType.PlusEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.Plus)); _this.setInfix(_Lexer.TokenType.MinusEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.Minus)); _this.setInfix(_Lexer.TokenType.StarEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.Star)); _this.setInfix(_Lexer.TokenType.StarStarEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.StarStar)); _this.setInfix(_Lexer.TokenType.SlashEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.Slash)); _this.setInfix(_Lexer.TokenType.PercentEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.Percent)); _this.setInfix(_Lexer.TokenType.AmpEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.Amp)); _this.setInfix(_Lexer.TokenType.PipeEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.Pipe)); _this.setInfix(_Lexer.TokenType.CaretEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.Caret)); _this.setInfix(_Lexer.TokenType.TildeEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.Tilde)); _this.setInfix(_Lexer.TokenType.QuestionQuestionEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.QuestionQuestion)); _this.setInfix(_Lexer.TokenType.LtLtEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.LtLt)); _this.setInfix(_Lexer.TokenType.GtGtEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.GtGt)); _this.setInfix(_Lexer.TokenType.GtGtGtEq, (0, _Assignment.makeAssignmentParselet)(_Lexer.TokenType.GtGtGt)); _this.setInfix(_Lexer.TokenType.Question, _Conditional.Conditional); _this.setInfix(_Lexer.TokenType.QuestionQuestion, _Conditional.NullCoalesce); _this.setInfix(_Lexer.TokenType.PipePipe, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.PipePipe, Precedence.LogicalOr)); _this.setInfix(_Lexer.TokenType.AmpAmp, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.AmpAmp, Precedence.LogicalAnd)); _this.setInfix(_Lexer.TokenType.Pipe, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.Pipe, Precedence.BitwiseOr)); _this.setInfix(_Lexer.TokenType.Caret, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.Caret, Precedence.BitwiseXor)); _this.setInfix(_Lexer.TokenType.Amp, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.Amp, Precedence.BitwiseAnd)); _this.setInfix(_Lexer.TokenType.EqEq, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.EqEq, Precedence.Equality)); _this.setInfix(_Lexer.TokenType.BangEq, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.BangEq, Precedence.Equality)); _this.setInfix(_Lexer.TokenType.EqEqEq, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.EqEqEq, Precedence.Equality)); _this.setInfix(_Lexer.TokenType.BangEqEq, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.BangEqEq, Precedence.Equality)); _this.setInfix(_Lexer.TokenType.Lt, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.Lt, Precedence.Relational)); _this.setInfix(_Lexer.TokenType.LtEq, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.LtEq, Precedence.Relational)); _this.setInfix(_Lexer.TokenType.Gt, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.Gt, Precedence.Relational)); _this.setInfix(_Lexer.TokenType.GtEq, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.GtEq, Precedence.Relational)); _this.setInfix(_Lexer.TokenType.In, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.In, Precedence.Relational)); _this.setInfix(_Lexer.TokenType.LtLt, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.LtLt, Precedence.BitwiseShift)); _this.setInfix(_Lexer.TokenType.GtGt, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.GtGt, Precedence.BitwiseShift)); _this.setInfix(_Lexer.TokenType.GtGtGt, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.GtGtGt, Precedence.BitwiseShift)); _this.setInfix(_Lexer.TokenType.Plus, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.Plus, Precedence.Additive)); _this.setInfix(_Lexer.TokenType.Minus, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.Minus, Precedence.Additive)); _this.setInfix(_Lexer.TokenType.Star, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.Star, Precedence.Multiplicative)); _this.setInfix(_Lexer.TokenType.Slash, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.Slash, Precedence.Multiplicative)); _this.setInfix(_Lexer.TokenType.Percent, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.Percent, Precedence.Multiplicative)); _this.setInfix(_Lexer.TokenType.StarStar, (0, _BinaryOperator.makeBinaryOperatorParselet)(_Lexer.TokenType.StarStar, Precedence.Power, _BinaryOperator.RightAssociative)); _this.setPrefix(_Lexer.TokenType.Await, _Await.Await); _this.setPrefix(_Lexer.TokenType.Bang, (0, _UnaryOperator.makePrefixOperatorParselet)(_Lexer.TokenType.Bang)); _this.setPrefix(_Lexer.TokenType.Tilde, (0, _UnaryOperator.makePrefixOperatorParselet)(_Lexer.TokenType.Tilde)); _this.setPrefix(_Lexer.TokenType.Plus, (0, _UnaryOperator.makePrefixOperatorParselet)(_Lexer.TokenType.Plus)); _this.setPrefix(_Lexer.TokenType.Minus, (0, _UnaryOperator.makePrefixOperatorParselet)(_Lexer.TokenType.Minus)); _this.setPrefix(_Lexer.TokenType.PlusPlus, (0, _UnaryOperator.makePrefixAccessMutatorParselet)()); _this.setPrefix(_Lexer.TokenType.MinusMinus, (0, _UnaryOperator.makePrefixAccessMutatorParselet)()); _this.setInfix(_Lexer.TokenType.PlusPlus, (0, _UnaryOperator.makePostfixAccessMutatorParselet)(_Lexer.TokenType.PlusPlus)); _this.setInfix(_Lexer.TokenType.MinusMinus, (0, _UnaryOperator.makePostfixAccessMutatorParselet)(_Lexer.TokenType.MinusMinus)); _this.setInfix(_Lexer.TokenType.Dot, _MemberAccess.MemberAccess); _this.setInfix(_Lexer.TokenType.LBracket, _MemberAccess.ComputedMemberAccess); _this.setInfix(_Lexer.TokenType.LParen, _Call.Call); _this.setInfix(_Lexer.TokenType.QuestionDot, _Conditional.NullConditional); _this.setPrefix(_Lexer.TokenType.Ellipsis, _Spread.Spread); _this.setInfix(_Lexer.TokenType.RightArrow, _Lambda.Lambda); _this.setPrefix(_Lexer.TokenType.LParen, _Group.Group); _this.setPrefix(_Lexer.TokenType.Identifier, _Value.Identifier); _this.setPrefix(_Lexer.TokenType.NumberLiteral, _Value.NumberLiteral); _this.setPrefix(_Lexer.TokenType.StringLiteral, _Value.StringLiteral); _this.setPrefix(_Lexer.TokenType.BooleanLiteral, _Value.BooleanLiteral); _this.setPrefix(_Lexer.TokenType.NullLiteral, _Value.NullLiteral); _this.setPrefix(_Lexer.TokenType.UndefinedLiteral, _Value.UndefinedLiteral); _this.setPrefix(_Lexer.TokenType.LBracket, _ArrayLiteral.ArrayLiteral); _this.setPrefix(_Lexer.TokenType.LBrace, _ObjectLiteral.ObjectLiteral); return _this; } return ExpressionParser; }(_Parser2.Parser); exports.ExpressionParser = ExpressionParser;