UNPKG

expr-eval-fork

Version:

Mathematical expression evaluator fork with prototype pollution fix

2 lines (1 loc) 25.4 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).exprEval={})}(this,function(t){"use strict";var y="INUMBER",x="IOP1",w="IOP2",d="IOP3",g="IVAR",M="IVARNAME",v="IFUNCALL",E="IFUNDEF",m="IEXPR",A="IEXPREVAL",b="IMEMBER",k="IENDSTATEMENT",O="IARRAY";function T(t,e){this.type=t,this.value=null!=e?e:0}function u(t){return new T(x,t)}function c(t){return new T(w,t)}function j(t){return new T(d,t)}function C(t,i,o){var e,r,s,n,a,p=[];if(N(t))return S(t,o);for(var h=t.length,u=0;u<h;u++){var c=t[u],f=c.type;if(f===y||f===M)p.push(c.value);else if(f===w)r=p.pop(),e=p.pop(),"and"===c.value?p.push(!!e&&!!C(r,i,o)):"or"===c.value?p.push(!!e||!!C(r,i,o)):"="===c.value?(s=i.binaryOps[c.value],p.push(s(e,C(r,i,o),o))):(s=i.binaryOps[c.value],p.push(s(S(e,o),S(r,o))));else if(f===d)l=p.pop(),r=p.pop(),e=p.pop(),"?"===c.value?p.push(C(e?r:l,i,o)):(s=i.ternaryOps[c.value],p.push(s(S(e,o),S(r,o),S(l,o))));else if(f===g){if(/^__proto__|prototype|constructor$/.test(c.value))throw new Error("prototype access detected");if(c.value in i.functions)p.push(i.functions[c.value]);else if(c.value in i.unaryOps&&i.parser.isOperatorEnabled(c.value))p.push(i.unaryOps[c.value]);else{var l=o[c.value];if(void 0===l)throw new Error("undefined variable: "+c.value);p.push(l)}}else if(f===x)e=p.pop(),s=i.unaryOps[c.value],p.push(s(S(e,o)));else if(f===v){for(a=c.value,n=[];0<a--;)n.unshift(S(p.pop(),o));if(!(s=p.pop()).apply||!s.call)throw new Error(s+" is not a function");p.push(s.apply(void 0,n))}else if(f===E)p.push(function(){for(var s=p.pop(),n=[],t=c.value;0<t--;)n.unshift(p.pop());function e(){for(var t=Object.assign({},o),e=0,r=n.length;e<r;e++)t[n[e]]=arguments[e];return C(s,i,t)}var r=p.pop();return Object.defineProperty(e,"name",{value:r,writable:!1}),o[r]=e}());else if(f===m)p.push(function(e,r){return N(e)?e:{type:A,value:function(t){return C(e.value,r,t)}}}(c,i));else if(f===A)p.push(c);else if(f===b)e=p.pop(),p.push(e[c.value]);else if(f===k)p.pop();else{if(f!==O)throw new Error("invalid Expression");for(a=c.value,n=[];0<a--;)n.unshift(p.pop());p.push(n)}}if(1<p.length)throw new Error("invalid Expression (parity)");return 0===p[0]?0:S(p[0],o)}function N(t){return t&&t.type===A}function S(t,e){return N(t)?t.value(e):t}function f(t,e){for(var r,s,n,i,o,a,p=[],h=0;h<t.length;h++){var u=t[h],c=u.type;if(c===y)"number"==typeof u.value&&u.value<0?p.push("("+u.value+")"):Array.isArray(u.value)?p.push("["+u.value.map(U).join(", ")+"]"):p.push(U(u.value));else if(c===w)s=p.pop(),r=p.pop(),i=u.value,e?"^"===i?p.push("Math.pow("+r+", "+s+")"):"and"===i?p.push("(!!"+r+" && !!"+s+")"):"or"===i?p.push("(!!"+r+" || !!"+s+")"):"||"===i?p.push("(function(a,b){ return Array.isArray(a) && Array.isArray(b) ? a.concat(b) : String(a) + String(b); }(("+r+"),("+s+")))"):"=="===i?p.push("("+r+" === "+s+")"):"!="===i?p.push("("+r+" !== "+s+")"):"["===i?p.push(r+"[("+s+") | 0]"):p.push("("+r+" "+i+" "+s+")"):"["===i?p.push(r+"["+s+"]"):p.push("("+r+" "+i+" "+s+")");else if(c===d){if(n=p.pop(),s=p.pop(),r=p.pop(),"?"!==(i=u.value))throw new Error("invalid Expression");p.push("("+r+" ? "+s+" : "+n+")")}else if(c===g||c===M)p.push(u.value);else if(c===x)r=p.pop(),"-"===(i=u.value)||"+"===i?p.push("("+i+r+")"):e?"not"===i?p.push("(!"+r+")"):"!"===i?p.push("fac("+r+")"):p.push(i+"("+r+")"):"!"===i?p.push("("+r+"!)"):p.push("("+i+" "+r+")");else if(c===v){for(a=u.value,o=[];0<a--;)o.unshift(p.pop());i=p.pop(),p.push(i+"("+o.join(", ")+")")}else if(c===E){for(s=p.pop(),a=u.value,o=[];0<a--;)o.unshift(p.pop());r=p.pop(),e?p.push("("+r+" = function("+o.join(", ")+") { return "+s+" })"):p.push("("+r+"("+o.join(", ")+") = "+s+")")}else if(c===b)r=p.pop(),p.push(r+"."+u.value);else if(c===O){for(a=u.value,o=[];0<a--;)o.unshift(p.pop());p.push("["+o.join(", ")+"]")}else if(c===m)p.push("("+f(u.value,e)+")");else if(c!==k)throw new Error("invalid Expression")}return 1<p.length&&(p=e?[p.join(",")]:[p.join(";")]),String(p[0])}function U(t){return"string"==typeof t?JSON.stringify(t).replace(/\u2028/g,"\\u2028").replace(/\u2029/g,"\\u2029"):t}function a(t,e){for(var r=0;r<t.length;r++)if(t[r]===e)return!0;return!1}function p(t,e,r){for(var s=!!(r=r||{}).withMembers,n=null,i=0;i<t.length;i++){var o=t[i];o.type===g||o.type===M?s||a(e,o.value)?n=(null!==n&&(a(e,n)||e.push(n)),o.value):e.push(o.value):o.type===b&&s&&null!==n?n+="."+o.value:o.type===m?p(o.value,e,r):null!==n&&(a(e,n)||e.push(n),n=null)}null===n||a(e,n)||e.push(n)}function r(t,e){this.tokens=t,this.parser=e,this.unaryOps=e.unaryOps,this.binaryOps=e.binaryOps,this.ternaryOps=e.ternaryOps,this.functions=e.functions}T.prototype.toString=function(){switch(this.type){case y:case x:case w:case d:case g:case M:case k:return this.value;case v:return"CALL "+this.value;case E:return"DEF "+this.value;case O:return"ARRAY "+this.value;case b:return"."+this.value;default:return"Invalid Instruction"}},r.prototype.simplify=function(t){return new r(function t(e,r,s,n,i){for(var o,a,p,h,u=[],c=[],f=0;f<e.length;f++){var l=e[f],v=l.type;if(v===y||v===M)Array.isArray(l.value)?u.push.apply(u,t(l.value.map(function(t){return new T(y,t)}).concat(new T(O,l.value.length)),r,s,n,i)):u.push(l);else if(v===g&&i.hasOwnProperty(l.value))l=new T(y,i[l.value]),u.push(l);else if(v===w&&1<u.length)a=u.pop(),o=u.pop(),h=s[l.value],l=new T(y,h(o.value,a.value)),u.push(l);else if(v===d&&2<u.length)p=u.pop(),a=u.pop(),o=u.pop(),"?"===l.value?u.push((o.value?a:p).value):(h=n[l.value],l=new T(y,h(o.value,a.value,p.value)),u.push(l));else if(v===x&&0<u.length)o=u.pop(),h=r[l.value],l=new T(y,h(o.value)),u.push(l);else if(v===m){for(;0<u.length;)c.push(u.shift());c.push(new T(m,t(l.value,r,s,n,i)))}else if(v===b&&0<u.length)o=u.pop(),u.push(new T(y,o.value[l.value]));else{for(;0<u.length;)c.push(u.shift());c.push(l)}}for(;0<u.length;)c.push(u.shift());return c}(this.tokens,this.unaryOps,this.binaryOps,this.ternaryOps,t=t||{}),this.parser)},r.prototype.substitute=function(t,e){return e instanceof r||(e=this.parser.parse(String(e))),new r(function t(e,r,s){for(var n=[],i=0;i<e.length;i++){var o=e[i],a=o.type;if(a===g&&o.value===r)for(var p=0;p<s.tokens.length;p++){var h=(h=s.tokens[p]).type===x?u(h.value):h.type===w?c(h.value):h.type===d?j(h.value):new T(h.type,h.value);n.push(h)}else n.push(a===m?new T(m,t(o.value,r,s)):o)}return n}(this.tokens,t,e),this.parser)},r.prototype.evaluate=function(t){return C(this.tokens,this,t=t||{})},r.prototype.toString=function(){return f(this.tokens,!1)},r.prototype.symbols=function(t){var e=[];return p(this.tokens,e,t=t||{}),e},r.prototype.variables=function(t){var e=[],r=(p(this.tokens,e,t=t||{}),this.functions);return e.filter(function(t){return!(t in r)})},r.prototype.toJSFunction=function(t,e){var r=this,s=new Function(t,"with(this.functions) with (this.ternaryOps) with (this.binaryOps) with (this.unaryOps) { return "+f(this.simplify(e).tokens,!0)+"; }");return function(){return s.apply(r,arguments)}};var s="TEOF",h="TOP",l="TNUMBER",_="TSTRING",n="TPAREN",i="TBRACKET",o="TCOMMA",I="TNAME",P="TSEMICOLON";function q(t,e,r){this.type=t,this.value=e,this.index=r}function F(t,e){this.pos=0,this.current=null,this.unaryOps=t.unaryOps,this.binaryOps=t.binaryOps,this.ternaryOps=t.ternaryOps,this.consts=t.consts,this.expression=e,this.savedPosition=0,this.savedCurrent=null,this.options=t.options,this.parser=t}q.prototype.toString=function(){return this.type+": "+this.value},F.prototype.newToken=function(t,e,r){return new q(t,e,null!=r?r:this.pos)},F.prototype.save=function(){this.savedPosition=this.pos,this.savedCurrent=this.current},F.prototype.restore=function(){this.pos=this.savedPosition,this.current=this.savedCurrent},F.prototype.next=function(){return this.pos>=this.expression.length?this.newToken(s,"EOF"):this.isWhitespace()||this.isComment()?this.next():this.isRadixInteger()||this.isNumber()||this.isOperator()||this.isString()||this.isParen()||this.isBracket()||this.isComma()||this.isSemicolon()||this.isNamedOp()||this.isConst()||this.isName()?this.current:void this.parseError('Unknown character "'+this.expression.charAt(this.pos)+'"')},F.prototype.isString=function(){var t=!1,e=this.pos,r=this.expression.charAt(e);if("'"===r||'"'===r)for(var s=this.expression.indexOf(r,e+1);0<=s&&this.pos<this.expression.length;){if(this.pos=s+1,"\\"!==this.expression.charAt(s-1)){var n=this.expression.substring(e+1,s);this.current=this.newToken(_,this.unescape(n),e),t=!0;break}s=this.expression.indexOf(r,s+1)}return t},F.prototype.isParen=function(){var t=this.expression.charAt(this.pos);return("("===t||")"===t)&&(this.current=this.newToken(n,t),this.pos++,!0)},F.prototype.isBracket=function(){var t=this.expression.charAt(this.pos);return!("["!==t&&"]"!==t||!this.isOperatorEnabled("[")||(this.current=this.newToken(i,t),this.pos++,0))},F.prototype.isComma=function(){return","===this.expression.charAt(this.pos)&&(this.current=this.newToken(o,","),this.pos++,!0)},F.prototype.isSemicolon=function(){return";"===this.expression.charAt(this.pos)&&(this.current=this.newToken(P,";"),this.pos++,!0)},F.prototype.isConst=function(){for(var t=this.pos,e=t;e<this.expression.length;e++){var r=this.expression.charAt(e);if(r.toUpperCase()===r.toLowerCase()&&(e===this.pos||"_"!==r&&"."!==r&&(r<"0"||"9"<r)))break}if(t<e){t=this.expression.substring(t,e);if(t in this.consts)return this.current=this.newToken(l,this.consts[t]),this.pos+=t.length,!0}return!1},F.prototype.isNamedOp=function(){for(var t=this.pos,e=t;e<this.expression.length;e++){var r=this.expression.charAt(e);if(r.toUpperCase()===r.toLowerCase()&&(e===this.pos||"_"!==r&&(r<"0"||"9"<r)))break}if(t<e){t=this.expression.substring(t,e);if(this.isOperatorEnabled(t)&&(t in this.binaryOps||t in this.unaryOps||t in this.ternaryOps))return this.current=this.newToken(h,t),this.pos+=t.length,!0}return!1},F.prototype.isName=function(){for(var t=this.pos,e=t,r=!1;e<this.expression.length;e++){var s=this.expression.charAt(e);if(s.toUpperCase()===s.toLowerCase())if(e!==this.pos||"$"!==s&&"_"!==s){if(e===this.pos||!r||"_"!==s&&(s<"0"||"9"<s))break}else"_"===s&&(r=!0);else r=!0}return!!r&&(t=this.expression.substring(t,e),this.current=this.newToken(I,t),this.pos+=t.length,!0)},F.prototype.isWhitespace=function(){for(var t=!1,e=this.expression.charAt(this.pos);!(" "!==e&&"\t"!==e&&"\n"!==e&&"\r"!==e||(t=!0,this.pos++,this.pos>=this.expression.length));)e=this.expression.charAt(this.pos);return t};var B=/^[0-9a-f]{4}$/i;function R(t,e,r){this.parser=t,this.tokens=e,this.current=null,this.nextToken=null,this.next(),this.savedCurrent=null,this.savedNextToken=null,this.allowMemberAccess=!1!==r.allowMemberAccess}F.prototype.unescape=function(t){var e=t.indexOf("\\");if(e<0)return t;for(var r=t.substring(0,e);0<=e;){var s=t.charAt(++e);switch(s){case"'":r+="'";break;case'"':r+='"';break;case"\\":r+="\\";break;case"/":r+="/";break;case"b":r+="\b";break;case"f":r+="\f";break;case"n":r+="\n";break;case"r":r+="\r";break;case"t":r+="\t";break;case"u":var n=t.substring(e+1,e+5);B.test(n)||this.parseError("Illegal escape sequence: \\u"+n),r+=String.fromCharCode(parseInt(n,16)),e+=4;break;default:throw this.parseError('Illegal escape sequence: "\\'+s+'"')}++e;var i=t.indexOf("\\",e);r+=t.substring(e,i<0?t.length:i),e=i}return r},F.prototype.isComment=function(){return"/"===this.expression.charAt(this.pos)&&"*"===this.expression.charAt(this.pos+1)&&(this.pos=this.expression.indexOf("*/",this.pos)+2,1===this.pos&&(this.pos=this.expression.length),!0)},F.prototype.isRadixInteger=function(){var t,e,r=this.pos;if(r>=this.expression.length-2||"0"!==this.expression.charAt(r))return!1;if("x"===this.expression.charAt(++r))t=16,e=/^[0-9a-f]$/i;else{if("b"!==this.expression.charAt(r))return!1;t=2,e=/^[01]$/i}for(var s=!1,n=++r;r<this.expression.length;){var i=this.expression.charAt(r);if(!e.test(i))break;r++,s=!0}return s&&(this.current=this.newToken(l,parseInt(this.expression.substring(n,r),t)),this.pos=r),s},F.prototype.isNumber=function(){for(var t,e=!1,r=this.pos,s=r,n=r,i=!1,o=!1;r<this.expression.length&&("0"<=(t=this.expression.charAt(r))&&t<="9"||!i&&"."===t);)"."===t?i=!0:o=!0,r++,e=o;if(e&&(n=r),"e"===t||"E"===t){r++;for(var a=!0,p=!1;r<this.expression.length;){if(t=this.expression.charAt(r),!a||"+"!==t&&"-"!==t){if(!("0"<=t&&t<="9"))break;a=!(p=!0)}else a=!1;r++}p||(r=n)}return e?(this.current=this.newToken(l,parseFloat(this.expression.substring(s,r))),this.pos=r):this.pos=n,e},F.prototype.isOperator=function(){var t=this.pos,e=this.expression.charAt(this.pos);if("+"===e||"-"===e||"*"===e||"/"===e||"%"===e||"^"===e||"?"===e||":"===e||"."===e)this.current=this.newToken(h,e);else if("∙"===e||"•"===e)this.current=this.newToken(h,"*");else if(">"===e)"="===this.expression.charAt(this.pos+1)?(this.current=this.newToken(h,">="),this.pos++):this.current=this.newToken(h,">");else if("<"===e)"="===this.expression.charAt(this.pos+1)?(this.current=this.newToken(h,"<="),this.pos++):this.current=this.newToken(h,"<");else if("|"===e){if("|"!==this.expression.charAt(this.pos+1))return!1;this.current=this.newToken(h,"||"),this.pos++}else if("="===e)"="===this.expression.charAt(this.pos+1)?(this.current=this.newToken(h,"=="),this.pos++):this.current=this.newToken(h,e);else{if("!"!==e)return!1;"="===this.expression.charAt(this.pos+1)?(this.current=this.newToken(h,"!="),this.pos++):this.current=this.newToken(h,e)}return this.pos++,!!this.isOperatorEnabled(this.current.value)||(this.pos=t,!1)},F.prototype.isOperatorEnabled=function(t){return this.parser.isOperatorEnabled(t)},F.prototype.getCoordinates=function(){for(var t,e=0,r=-1;e++,t=this.pos-r,0<=(r=this.expression.indexOf("\n",r+1))&&r<this.pos;);return{line:e,column:t}},F.prototype.parseError=function(t){var e=this.getCoordinates();throw new Error("parse error ["+e.line+":"+e.column+"]: "+t)},R.prototype.next=function(){return this.current=this.nextToken,this.nextToken=this.tokens.next()},R.prototype.tokenMatches=function(t,e){return void 0===e||(Array.isArray(e)?a(e,t.value):"function"==typeof e?e(t):t.value===e)},R.prototype.save=function(){this.savedCurrent=this.current,this.savedNextToken=this.nextToken,this.tokens.save()},R.prototype.restore=function(){this.tokens.restore(),this.current=this.savedCurrent,this.nextToken=this.savedNextToken},R.prototype.accept=function(t,e){return!(this.nextToken.type!==t||!this.tokenMatches(this.nextToken,e)||(this.next(),0))},R.prototype.expect=function(t,e){var r;if(!this.accept(t,e))throw r=this.tokens.getCoordinates(),new Error("parse error ["+r.line+":"+r.column+"]: Expected "+(e||t))},R.prototype.parseAtom=function(t){var e,r=this.tokens.unaryOps;if(this.accept(I)||this.accept(h,function(t){return t.value in r}))t.push(new T(g,this.current.value));else if(this.accept(l))t.push(new T(y,this.current.value));else if(this.accept(_))t.push(new T(y,this.current.value));else if(this.accept(n,"("))this.parseExpression(t),this.expect(n,")");else{if(!this.accept(i,"["))throw new Error("unexpected "+this.nextToken);this.accept(i,"]")?t.push(new T(O,0)):(e=this.parseArrayList(t),t.push(new T(O,e)))}},R.prototype.parseExpression=function(t){var e=[];this.parseUntilEndStatement(t,e)||(this.parseVariableAssignmentExpression(e),this.parseUntilEndStatement(t,e))||this.pushExpression(t,e)},R.prototype.pushExpression=function(t,e){for(var r=0,s=e.length;r<s;r++)t.push(e[r])},R.prototype.parseUntilEndStatement=function(t,e){return!!this.accept(P)&&(!this.nextToken||this.nextToken.type===s||this.nextToken.type===n&&")"===this.nextToken.value||e.push(new T(k)),this.nextToken.type!==s&&this.parseExpression(e),t.push(new T(m,e)),!0)},R.prototype.parseArrayList=function(t){for(var e=0;!this.accept(i,"]");)for(this.parseExpression(t),++e;this.accept(o);)this.parseExpression(t),++e;return e},R.prototype.parseVariableAssignmentExpression=function(t){for(this.parseConditionalExpression(t);this.accept(h,"=");){var e=t.pop(),r=[],s=t.length-1;if(e.type===v){if(!this.tokens.isOperatorEnabled("()="))throw new Error("function definition is not permitted");for(var n=0,i=e.value+1;n<i;n++){var o=s-n;t[o].type===g&&(t[o]=new T(M,t[o].value))}this.parseVariableAssignmentExpression(r),t.push(new T(m,r)),t.push(new T(E,e.value))}else{if(e.type!==g&&e.type!==b)throw new Error("expected variable for assignment");this.parseVariableAssignmentExpression(r),t.push(new T(M,e.value)),t.push(new T(m,r)),t.push(c("="))}}},R.prototype.parseConditionalExpression=function(t){for(this.parseOrExpression(t);this.accept(h,"?");){var e=[],r=[];this.parseConditionalExpression(e),this.expect(h,":"),this.parseConditionalExpression(r),t.push(new T(m,e)),t.push(new T(m,r)),t.push(j("?"))}},R.prototype.parseOrExpression=function(t){for(this.parseAndExpression(t);this.accept(h,"or");){var e=[];this.parseAndExpression(e),t.push(new T(m,e)),t.push(c("or"))}},R.prototype.parseAndExpression=function(t){for(this.parseComparison(t);this.accept(h,"and");){var e=[];this.parseComparison(e),t.push(new T(m,e)),t.push(c("and"))}};var V=["==","!=","<","<=",">=",">","in"],$=(R.prototype.parseComparison=function(t){for(this.parseAddSub(t);this.accept(h,V);){var e=this.current;this.parseAddSub(t),t.push(c(e.value))}},["+","-","||"]),D=(R.prototype.parseAddSub=function(t){for(this.parseTerm(t);this.accept(h,$);){var e=this.current;this.parseTerm(t),t.push(c(e.value))}},["*","/","%"]);function G(t,e){return Number(t)+Number(e)}function J(t,e){return t-e}function W(t,e){return t*e}function X(t,e){return t/e}function Y(t,e){return t%e}function K(t,e){return Array.isArray(t)&&Array.isArray(e)?t.concat(e):""+t+e}function z(t,e){return t===e}function H(t,e){return t!==e}function Q(t,e){return e<t}function Z(t,e){return t<e}function tt(t,e){return e<=t}function et(t,e){return t<=e}function rt(t,e){return Boolean(t&&e)}function st(t,e){return Boolean(t||e)}function nt(t,e){return a(e,t)}function it(t){return(Math.exp(t)-Math.exp(-t))/2}function ot(t){return(Math.exp(t)+Math.exp(-t))/2}function at(t){return t===1/0?1:t===-1/0?-1:(Math.exp(t)-Math.exp(-t))/(Math.exp(t)+Math.exp(-t))}function pt(t){return t===-1/0?t:Math.log(t+Math.sqrt(t*t+1))}function ht(t){return Math.log(t+Math.sqrt(t*t-1))}function ut(t){return Math.log((1+t)/(1-t))/2}function ct(t){return Math.log(t)*Math.LOG10E}function ft(t){return-t}function lt(t){return!t}function vt(t){return t<0?Math.ceil(t):Math.floor(t)}function yt(t){return Math.random()*(t||1)}function xt(t){return dt(t+1)}R.prototype.parseTerm=function(t){for(this.parseFactor(t);this.accept(h,D);){var e=this.current;this.parseFactor(t),t.push(c(e.value))}},R.prototype.parseFactor=function(t){var e=this.tokens.unaryOps;if(this.save(),this.accept(h,function(t){return t.value in e})){if("-"!==this.current.value&&"+"!==this.current.value){if(this.nextToken.type===n&&"("===this.nextToken.value)return this.restore(),void this.parseExponential(t);if(this.nextToken.type===P||this.nextToken.type===o||this.nextToken.type===s||this.nextToken.type===n&&")"===this.nextToken.value)return this.restore(),void this.parseAtom(t)}var r=this.current;this.parseFactor(t),t.push(u(r.value))}else this.parseExponential(t)},R.prototype.parseExponential=function(t){for(this.parsePostfixExpression(t);this.accept(h,"^");)this.parseFactor(t),t.push(c("^"))},R.prototype.parsePostfixExpression=function(t){for(this.parseFunctionCall(t);this.accept(h,"!");)t.push(u("!"))},R.prototype.parseFunctionCall=function(t){var e,r=this.tokens.unaryOps;if(this.accept(h,function(t){return t.value in r})){var s=this.current;this.parseAtom(t),t.push(u(s.value))}else for(this.parseMemberExpression(t);this.accept(n,"(");)this.accept(n,")")?t.push(new T(v,0)):(e=this.parseArgumentList(t),t.push(new T(v,e)))},R.prototype.parseArgumentList=function(t){for(var e=0;!this.accept(n,")");)for(this.parseExpression(t),++e;this.accept(o);)this.parseExpression(t),++e;return e},R.prototype.parseMemberExpression=function(t){for(this.parseAtom(t);this.accept(h,".")||this.accept(i,"[");){var e=this.current;if("."===e.value){if(!this.allowMemberAccess)throw new Error('unexpected ".", member access is not permitted');this.expect(I),t.push(new T(b,this.current.value))}else{if("["!==e.value)throw new Error("unexpected symbol: "+e.value);if(!this.tokens.isOperatorEnabled("["))throw new Error('unexpected "[]", arrays are disabled');this.parseExpression(t),this.expect(i,"]"),t.push(c("["))}}};var wt=4.7421875,L=[.9999999999999971,57.15623566586292,-59.59796035547549,14.136097974741746,-.4919138160976202,3399464998481189e-20,4652362892704858e-20,-9837447530487956e-20,.0001580887032249125,-.00021026444172410488,.00021743961811521265,-.0001643181065367639,8441822398385275e-20,-26190838401581408e-21,36899182659531625e-22];function dt(t){var e,r,s,n;if(e=t,isFinite(e)&&e===Math.round(e)){if(t<=0)return isFinite(t)?1/0:NaN;if(171<t)return 1/0;for(var i=t-2,o=t-1;1<i;)o*=i,i--;return o=0===o?1:o}if(t<.5)return Math.PI/(Math.sin(Math.PI*t)*dt(1-t));if(171.35<=t)return 1/0;if(85<t)return n=(s=(r=(e=t*t)*t)*t)*t,Math.sqrt(2*Math.PI/t)*Math.pow(t/Math.E,t)*(1+1/(12*t)+1/(288*e)-139/(51840*r)-571/(2488320*s)+163879/(209018880*n)+5246819/(75246796800*n*t));--t;for(var a=L[0],p=1;p<L.length;++p)a+=L[p]/(t+p);return e=t+wt+.5,Math.sqrt(2*Math.PI)*Math.pow(e,t+.5)*Math.exp(-e)*a}function gt(t){return(Array.isArray(t)?t:String(t)).length}function Mt(){for(var t=0,e=0,r=0;r<arguments.length;r++){var s,n=Math.abs(arguments[r]);e<n?(t=t*(s=e/n)*s+1,e=n):t+=0<n?(s=n/e)*s:n}return e===1/0?1/0:e*Math.sqrt(t)}function Et(t,e,r){return t?e:r}function mt(t,e){return void 0===e||0==+e?Math.round(t):(t=+t,e=-+e,isNaN(t)||"number"!=typeof e||e%1!=0?NaN:(t=t.toString().split("e"),+((t=(t=Math.round(+(t[0]+"e"+(t[1]?+t[1]-e:-e)))).toString().split("e"))[0]+"e"+(t[1]?+t[1]+e:e))))}function At(t,e,r){return r&&(r[t]=e),e}function bt(t,e){return t[0|e]}function kt(t){return 1===arguments.length&&Array.isArray(t)?Math.max.apply(Math,t):Math.max.apply(Math,arguments)}function Ot(t){return 1===arguments.length&&Array.isArray(t)?Math.min.apply(Math,t):Math.min.apply(Math,arguments)}function Tt(r,t){if("function"!=typeof r)throw new Error("First argument to map is not a function");if(Array.isArray(t))return t.map(function(t,e){return r(t,e)});throw new Error("Second argument to map is not an array")}function Ct(s,t,e){if("function"!=typeof s)throw new Error("First argument to fold is not a function");if(Array.isArray(e))return e.reduce(function(t,e,r){return s(t,e,r)},t);throw new Error("Second argument to fold is not an array")}function Nt(r,t){if("function"!=typeof r)throw new Error("First argument to filter is not a function");if(Array.isArray(t))return t.filter(function(t,e){return r(t,e)});throw new Error("Second argument to filter is not an array")}function St(t,e){if(Array.isArray(e)||"string"==typeof e)return e.indexOf(t);throw new Error("Second argument to indexOf is not a string or array")}function It(t,e){if(Array.isArray(e))return e.join(t);throw new Error("Second argument to join is not an array")}function Pt(t){return(0<t)-(t<0)||+t}var Ft=1/3;function Rt(t){return t<0?-Math.pow(-t,Ft):Math.pow(t,Ft)}function Lt(t){return Math.exp(t)-1}function jt(t){return Math.log(1+t)}function Ut(t){return Math.log(t)/Math.LN2}function _t(t){if(Array.isArray(t))return t.reduce(function(t,e){return t+Number(e)},0);throw new Error("Sum argument is not an array")}function e(t){this.options=t||{},this.unaryOps={sin:Math.sin,cos:Math.cos,tan:Math.tan,asin:Math.asin,acos:Math.acos,atan:Math.atan,sinh:Math.sinh||it,cosh:Math.cosh||ot,tanh:Math.tanh||at,asinh:Math.asinh||pt,acosh:Math.acosh||ht,atanh:Math.atanh||ut,sqrt:Math.sqrt,cbrt:Math.cbrt||Rt,log:Math.log,log2:Math.log2||Ut,ln:Math.log,lg:Math.log10||ct,log10:Math.log10||ct,expm1:Math.expm1||Lt,log1p:Math.log1p||jt,abs:Math.abs,ceil:Math.ceil,floor:Math.floor,round:Math.round,trunc:Math.trunc||vt,"-":ft,"+":Number,exp:Math.exp,not:lt,length:gt,"!":xt,sign:Math.sign||Pt},this.binaryOps={"+":G,"-":J,"*":W,"/":X,"%":Y,"^":Math.pow,"||":K,"==":z,"!=":H,">":Q,"<":Z,">=":tt,"<=":et,and:rt,or:st,in:nt,"=":At,"[":bt},this.ternaryOps={"?":Et},this.functions={random:yt,fac:xt,min:Ot,max:kt,hypot:Math.hypot||Mt,pyt:Math.hypot||Mt,pow:Math.pow,atan2:Math.atan2,if:Et,gamma:dt,roundTo:mt,map:Tt,fold:Ct,filter:Nt,indexOf:St,join:It,sum:_t},this.consts={E:Math.E,PI:Math.PI,true:!0,false:!1}}e.prototype.parse=function(t){var e=[],t=new R(this,new F(this,t),{allowMemberAccess:this.options.allowMemberAccess});return t.parseExpression(e),t.expect(s,"EOF"),new r(e,this)},e.prototype.evaluate=function(t,e){return this.parse(t).evaluate(e)};var qt=new e,Bt=(e.parse=function(t){return qt.parse(t)},e.evaluate=function(t,e){return qt.parse(t).evaluate(e)},{"+":"add","-":"subtract","*":"multiply","/":"divide","%":"remainder","^":"power","!":"factorial","<":"comparison",">":"comparison","<=":"comparison",">=":"comparison","==":"comparison","!=":"comparison","||":"concatenate",and:"logical",or:"logical",not:"logical","?":"conditional",":":"conditional","=":"assignment","[":"array","()=":"fndef"});e.prototype.isOperatorEnabled=function(t){var t=Bt.hasOwnProperty(t=t)?Bt[t]:t,e=this.options.operators||{};return!(t in e&&!e[t])};var Vt={Parser:e,Expression:r};t.Expression=r,t.Parser=e,t.default=Vt,Object.defineProperty(t,"__esModule",{value:!0})});