Comparing version 1.1.2 to 1.2.0
@@ -131,7 +131,14 @@ 'use strict'; | ||
* @param {number} precedence The precedence of the binary op (can be a float). Higher number = higher precedence | ||
* @param {boolean} [isRightAssociative=false] whether operator is right-associative | ||
* @returns {Jsep} | ||
*/ | ||
static addBinaryOp(op_name, precedence) { | ||
static addBinaryOp(op_name, precedence, isRightAssociative) { | ||
Jsep.max_binop_len = Math.max(op_name.length, Jsep.max_binop_len); | ||
Jsep.binary_ops[op_name] = precedence; | ||
if (isRightAssociative) { | ||
Jsep.right_associative.add(op_name); | ||
} | ||
else { | ||
Jsep.right_associative.delete(op_name); | ||
} | ||
return Jsep; | ||
@@ -206,2 +213,3 @@ } | ||
} | ||
Jsep.right_associative.delete(op_name); | ||
@@ -499,3 +507,3 @@ return Jsep; | ||
// precedence structure | ||
biop_info = { value: biop, prec: Jsep.binaryPrecedence(biop)}; | ||
biop_info = { value: biop, prec: Jsep.binaryPrecedence(biop), right_a: Jsep.right_associative.has(biop) }; | ||
@@ -519,3 +527,3 @@ right = this.gobbleToken(); | ||
biop_info = { value: biop, prec }; | ||
biop_info = { value: biop, prec, right_a: Jsep.right_associative.has(biop) }; | ||
@@ -525,3 +533,6 @@ cur_biop = biop; | ||
// Reduce: make a binary expression from the three topmost entries. | ||
while ((stack.length > 2) && (prec <= stack[stack.length - 2].prec)) { | ||
const comparePrev = prev => biop_info.right_a && prev.right_a | ||
? prec > prev.prec | ||
: prec <= prev.prec; | ||
while ((stack.length > 2) && comparePrev(stack[stack.length - 2])) { | ||
right = stack.pop(); | ||
@@ -1029,2 +1040,5 @@ biop = stack.pop().value; | ||
// sets specific binary_ops as right-associative | ||
right_associative: new Set(), | ||
// Additional valid identifier chars, apart from a-z, A-Z and 0-9 (except on the starting char) | ||
@@ -1031,0 +1045,0 @@ additional_identifier_chars: new Set(['$', '_']), |
@@ -1,2 +0,2 @@ | ||
"use strict";class e{static get version(){return"1.0.2"}static toString(){return"JavaScript Expression Parser (JSEP) v"+e.version}static addUnaryOp(t){return e.max_unop_len=Math.max(t.length,e.max_unop_len),e.unary_ops[t]=1,e}static addBinaryOp(t,r){return e.max_binop_len=Math.max(t.length,e.max_binop_len),e.binary_ops[t]=r,e}static addIdentifierChar(t){return e.additional_identifier_chars.add(t),e}static addLiteral(t,r){return e.literals[t]=r,e}static removeUnaryOp(t){return delete e.unary_ops[t],t.length===e.max_unop_len&&(e.max_unop_len=e.getMaxKeyLen(e.unary_ops)),e}static removeAllUnaryOps(){return e.unary_ops={},e.max_unop_len=0,e}static removeIdentifierChar(t){return e.additional_identifier_chars.delete(t),e}static removeBinaryOp(t){return delete e.binary_ops[t],t.length===e.max_binop_len&&(e.max_binop_len=e.getMaxKeyLen(e.binary_ops)),e}static removeAllBinaryOps(){return e.binary_ops={},e.max_binop_len=0,e}static removeLiteral(t){return delete e.literals[t],e}static removeAllLiterals(){return e.literals={},e}get char(){return this.expr.charAt(this.index)}get code(){return this.expr.charCodeAt(this.index)}constructor(e){this.expr=e,this.index=0}static parse(t){return new e(t).parse()}static getMaxKeyLen(e){return Math.max(0,...Object.keys(e).map((e=>e.length)))}static isDecimalDigit(e){return e>=48&&e<=57}static binaryPrecedence(t){return e.binary_ops[t]||0}static isIdentifierStart(t){return t>=65&&t<=90||t>=97&&t<=122||t>=128&&!e.binary_ops[String.fromCharCode(t)]||e.additional_identifier_chars.has(String.fromCharCode(t))}static isIdentifierPart(t){return e.isIdentifierStart(t)||e.isDecimalDigit(t)}throwError(e){const t=new Error(e+" at character "+this.index);throw t.index=this.index,t.description=e,t}runHook(t,r){if(e.hooks[t]){const i={context:this,node:r};return e.hooks.run(t,i),i.node}return r}searchHook(t){if(e.hooks[t]){const r={context:this};return e.hooks[t].find((function(e){return e.call(r.context,r),r.node})),r.node}}gobbleSpaces(){let t=this.code;for(;t===e.SPACE_CODE||t===e.TAB_CODE||t===e.LF_CODE||t===e.CR_CODE;)t=this.expr.charCodeAt(++this.index);this.runHook("gobble-spaces")}parse(){this.runHook("before-all");const t=this.gobbleExpressions(),r=1===t.length?t[0]:{type:e.COMPOUND,body:t};return this.runHook("after-all",r)}gobbleExpressions(t){let r,i,s=[];for(;this.index<this.expr.length;)if(r=this.code,r===e.SEMCOL_CODE||r===e.COMMA_CODE)this.index++;else if(i=this.gobbleExpression())s.push(i);else if(this.index<this.expr.length){if(r===t)break;this.throwError('Unexpected "'+this.char+'"')}return s}gobbleExpression(){const e=this.searchHook("gobble-expression")||this.gobbleBinaryExpression();return this.gobbleSpaces(),this.runHook("after-expression",e)}gobbleBinaryOp(){this.gobbleSpaces();let t=this.expr.substr(this.index,e.max_binop_len),r=t.length;for(;r>0;){if(e.binary_ops.hasOwnProperty(t)&&(!e.isIdentifierStart(this.code)||this.index+t.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+t.length))))return this.index+=r,t;t=t.substr(0,--r)}return!1}gobbleBinaryExpression(){let t,r,i,s,n,o,h,a,l;if(o=this.gobbleToken(),!o)return o;if(r=this.gobbleBinaryOp(),!r)return o;for(n={value:r,prec:e.binaryPrecedence(r)},h=this.gobbleToken(),h||this.throwError("Expected expression after "+r),s=[o,n,h];r=this.gobbleBinaryOp();){if(i=e.binaryPrecedence(r),0===i){this.index-=r.length;break}for(n={value:r,prec:i},l=r;s.length>2&&i<=s[s.length-2].prec;)h=s.pop(),r=s.pop().value,o=s.pop(),t={type:e.BINARY_EXP,operator:r,left:o,right:h},s.push(t);t=this.gobbleToken(),t||this.throwError("Expected expression after "+l),s.push(n,t)}for(a=s.length-1,t=s[a];a>1;)t={type:e.BINARY_EXP,operator:s[a-1].value,left:s[a-2],right:t},a-=2;return t}gobbleToken(){let t,r,i,s;if(this.gobbleSpaces(),s=this.searchHook("gobble-token"),s)return this.runHook("after-token",s);if(t=this.code,e.isDecimalDigit(t)||t===e.PERIOD_CODE)return this.gobbleNumericLiteral();if(t===e.SQUOTE_CODE||t===e.DQUOTE_CODE)s=this.gobbleStringLiteral();else if(t===e.OBRACK_CODE)s=this.gobbleArray();else{for(r=this.expr.substr(this.index,e.max_unop_len),i=r.length;i>0;){if(e.unary_ops.hasOwnProperty(r)&&(!e.isIdentifierStart(this.code)||this.index+r.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+r.length)))){this.index+=i;const t=this.gobbleToken();return t||this.throwError("missing unaryOp argument"),this.runHook("after-token",{type:e.UNARY_EXP,operator:r,argument:t,prefix:!0})}r=r.substr(0,--i)}e.isIdentifierStart(t)?(s=this.gobbleIdentifier(),e.literals.hasOwnProperty(s.name)?s={type:e.LITERAL,value:e.literals[s.name],raw:s.name}:s.name===e.this_str&&(s={type:e.THIS_EXP})):t===e.OPAREN_CODE&&(s=this.gobbleGroup())}return s?(s=this.gobbleTokenProperty(s),this.runHook("after-token",s)):this.runHook("after-token",!1)}gobbleTokenProperty(t){this.gobbleSpaces();let r=this.code;for(;r===e.PERIOD_CODE||r===e.OBRACK_CODE||r===e.OPAREN_CODE||r===e.QUMARK_CODE;){let i;if(r===e.QUMARK_CODE){if(this.expr.charCodeAt(this.index+1)!==e.PERIOD_CODE)break;i=!0,this.index+=2,this.gobbleSpaces(),r=this.code}this.index++,r===e.OBRACK_CODE?(t={type:e.MEMBER_EXP,computed:!0,object:t,property:this.gobbleExpression()},this.gobbleSpaces(),r=this.code,r!==e.CBRACK_CODE&&this.throwError("Unclosed ["),this.index++):r===e.OPAREN_CODE?t={type:e.CALL_EXP,arguments:this.gobbleArguments(e.CPAREN_CODE),callee:t}:(r===e.PERIOD_CODE||i)&&(i&&this.index--,this.gobbleSpaces(),t={type:e.MEMBER_EXP,computed:!1,object:t,property:this.gobbleIdentifier()}),i&&(t.optional=!0),this.gobbleSpaces(),r=this.code}return t}gobbleNumericLiteral(){let t,r,i="";for(;e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(this.code===e.PERIOD_CODE)for(i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(t=this.char,"e"===t||"E"===t){for(i+=this.expr.charAt(this.index++),t=this.char,"+"!==t&&"-"!==t||(i+=this.expr.charAt(this.index++));e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.expr.charCodeAt(this.index-1))||this.throwError("Expected exponent ("+i+this.char+")")}return r=this.code,e.isIdentifierStart(r)?this.throwError("Variable names cannot start with a number ("+i+this.char+")"):(r===e.PERIOD_CODE||1===i.length&&i.charCodeAt(0)===e.PERIOD_CODE)&&this.throwError("Unexpected period"),{type:e.LITERAL,value:parseFloat(i),raw:i}}gobbleStringLiteral(){let t="";const r=this.index,i=this.expr.charAt(this.index++);let s=!1;for(;this.index<this.expr.length;){let e=this.expr.charAt(this.index++);if(e===i){s=!0;break}if("\\"===e)switch(e=this.expr.charAt(this.index++),e){case"n":t+="\n";break;case"r":t+="\r";break;case"t":t+="\t";break;case"b":t+="\b";break;case"f":t+="\f";break;case"v":t+="\v";break;default:t+=e}else t+=e}return s||this.throwError('Unclosed quote after "'+t+'"'),{type:e.LITERAL,value:t,raw:this.expr.substring(r,this.index)}}gobbleIdentifier(){let t=this.code,r=this.index;for(e.isIdentifierStart(t)?this.index++:this.throwError("Unexpected "+this.char);this.index<this.expr.length&&(t=this.code,e.isIdentifierPart(t));)this.index++;return{type:e.IDENTIFIER,name:this.expr.slice(r,this.index)}}gobbleArguments(t){const r=[];let i=!1,s=0;for(;this.index<this.expr.length;){this.gobbleSpaces();let n=this.code;if(n===t){i=!0,this.index++,t===e.CPAREN_CODE&&s&&s>=r.length&&this.throwError("Unexpected token "+String.fromCharCode(t));break}if(n===e.COMMA_CODE){if(this.index++,s++,s!==r.length)if(t===e.CPAREN_CODE)this.throwError("Unexpected token ,");else if(t===e.CBRACK_CODE)for(let e=r.length;e<s;e++)r.push(null)}else if(r.length!==s&&0!==s)this.throwError("Expected comma");else{const t=this.gobbleExpression();t&&t.type!==e.COMPOUND||this.throwError("Expected comma"),r.push(t)}}return i||this.throwError("Expected "+String.fromCharCode(t)),r}gobbleGroup(){this.index++;let t=this.gobbleExpressions(e.CPAREN_CODE);if(this.code===e.CPAREN_CODE)return this.index++,1===t.length?t[0]:!!t.length&&{type:e.SEQUENCE_EXP,expressions:t};this.throwError("Unclosed (")}gobbleArray(){return this.index++,{type:e.ARRAY_EXP,elements:this.gobbleArguments(e.CBRACK_CODE)}}}const t=new class{add(e,t,r){if("string"!=typeof arguments[0])for(let e in arguments[0])this.add(e,arguments[0][e],arguments[1]);else(Array.isArray(e)?e:[e]).forEach((function(e){this[e]=this[e]||[],t&&this[e][r?"unshift":"push"](t)}),this)}run(e,t){this[e]=this[e]||[],this[e].forEach((function(e){e.call(t&&t.context?t.context:t,t)}))}};Object.assign(e,{hooks:t,plugins:new class{constructor(e){this.jsep=e,this.registered={}}register(...e){e.forEach((e=>{if("object"!=typeof e||!e.name||!e.init)throw new Error("Invalid JSEP plugin format");this.registered[e.name]||(e.init(this.jsep),this.registered[e.name]=e)}))}}(e),COMPOUND:"Compound",SEQUENCE_EXP:"SequenceExpression",IDENTIFIER:"Identifier",MEMBER_EXP:"MemberExpression",LITERAL:"Literal",THIS_EXP:"ThisExpression",CALL_EXP:"CallExpression",UNARY_EXP:"UnaryExpression",BINARY_EXP:"BinaryExpression",ARRAY_EXP:"ArrayExpression",TAB_CODE:9,LF_CODE:10,CR_CODE:13,SPACE_CODE:32,PERIOD_CODE:46,COMMA_CODE:44,SQUOTE_CODE:39,DQUOTE_CODE:34,OPAREN_CODE:40,CPAREN_CODE:41,OBRACK_CODE:91,CBRACK_CODE:93,QUMARK_CODE:63,SEMCOL_CODE:59,COLON_CODE:58,unary_ops:{"-":1,"!":1,"~":1,"+":1},binary_ops:{"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},additional_identifier_chars:new Set(["$","_"]),literals:{true:!0,false:!1,null:null},this_str:"this"}),e.max_unop_len=e.getMaxKeyLen(e.unary_ops),e.max_binop_len=e.getMaxKeyLen(e.binary_ops);const r=t=>new e(t).parse();Object.getOwnPropertyNames(e).forEach((t=>{void 0===r[t]&&"prototype"!==t&&(r[t]=e[t])})),r.Jsep=e;const i="ConditionalExpression";var s={name:"ternary",init(e){function t(e){for(;e.left&&":"===e.left.operator;)e=e.left;return e}e.hooks.add("after-expression",(function(r){if(r.node&&this.code===e.QUMARK_CODE){this.index++;const s=r.node,n=this.gobbleExpression();if(n||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const e=this.gobbleExpression();e||this.throwError("Expected expression"),r.node={type:"ConditionalExpression",test:s,consequent:n,alternate:e}}else if(":"===s.operator){n.right||this.throwError("Expected :");const e=t(n);s.right={type:"ConditionalExpression",test:s.right,consequent:e.left,alternate:e===n?e.right:{operator:":",left:e.right,right:n.right}},r.node=s}else if(":"===n.operator)!function(e,t){e.type=i,e.test=t,e.consequent=e.left,e.alternate=e.right,delete e.operator,delete e.left,delete e.right}(t(n),s),r.node=n;else if(n.alternate){let e=n.alternate;for(;e.alternate;)e=e.alternate;r.node={type:i,test:s,consequent:n,alternate:e.right},delete e.operator,delete e.right,Object.assign(e,e.left)}else this.throwError("Expected :");if(r.node.test&&r.node.test.operator&&e.binary_ops[r.node.test.operator]<.95){const e=r.node;r.node=e.test,r.node.right={type:i,test:e.test.right,consequent:e.consequent,alternate:e.alternate}}}}))}};r.plugins.register(s),module.exports=r; | ||
"use strict";class e{static get version(){return"1.0.2"}static toString(){return"JavaScript Expression Parser (JSEP) v"+e.version}static addUnaryOp(t){return e.max_unop_len=Math.max(t.length,e.max_unop_len),e.unary_ops[t]=1,e}static addBinaryOp(t,r,i){return e.max_binop_len=Math.max(t.length,e.max_binop_len),e.binary_ops[t]=r,i?e.right_associative.add(t):e.right_associative.delete(t),e}static addIdentifierChar(t){return e.additional_identifier_chars.add(t),e}static addLiteral(t,r){return e.literals[t]=r,e}static removeUnaryOp(t){return delete e.unary_ops[t],t.length===e.max_unop_len&&(e.max_unop_len=e.getMaxKeyLen(e.unary_ops)),e}static removeAllUnaryOps(){return e.unary_ops={},e.max_unop_len=0,e}static removeIdentifierChar(t){return e.additional_identifier_chars.delete(t),e}static removeBinaryOp(t){return delete e.binary_ops[t],t.length===e.max_binop_len&&(e.max_binop_len=e.getMaxKeyLen(e.binary_ops)),e.right_associative.delete(t),e}static removeAllBinaryOps(){return e.binary_ops={},e.max_binop_len=0,e}static removeLiteral(t){return delete e.literals[t],e}static removeAllLiterals(){return e.literals={},e}get char(){return this.expr.charAt(this.index)}get code(){return this.expr.charCodeAt(this.index)}constructor(e){this.expr=e,this.index=0}static parse(t){return new e(t).parse()}static getMaxKeyLen(e){return Math.max(0,...Object.keys(e).map((e=>e.length)))}static isDecimalDigit(e){return e>=48&&e<=57}static binaryPrecedence(t){return e.binary_ops[t]||0}static isIdentifierStart(t){return t>=65&&t<=90||t>=97&&t<=122||t>=128&&!e.binary_ops[String.fromCharCode(t)]||e.additional_identifier_chars.has(String.fromCharCode(t))}static isIdentifierPart(t){return e.isIdentifierStart(t)||e.isDecimalDigit(t)}throwError(e){const t=new Error(e+" at character "+this.index);throw t.index=this.index,t.description=e,t}runHook(t,r){if(e.hooks[t]){const i={context:this,node:r};return e.hooks.run(t,i),i.node}return r}searchHook(t){if(e.hooks[t]){const r={context:this};return e.hooks[t].find((function(e){return e.call(r.context,r),r.node})),r.node}}gobbleSpaces(){let t=this.code;for(;t===e.SPACE_CODE||t===e.TAB_CODE||t===e.LF_CODE||t===e.CR_CODE;)t=this.expr.charCodeAt(++this.index);this.runHook("gobble-spaces")}parse(){this.runHook("before-all");const t=this.gobbleExpressions(),r=1===t.length?t[0]:{type:e.COMPOUND,body:t};return this.runHook("after-all",r)}gobbleExpressions(t){let r,i,s=[];for(;this.index<this.expr.length;)if(r=this.code,r===e.SEMCOL_CODE||r===e.COMMA_CODE)this.index++;else if(i=this.gobbleExpression())s.push(i);else if(this.index<this.expr.length){if(r===t)break;this.throwError('Unexpected "'+this.char+'"')}return s}gobbleExpression(){const e=this.searchHook("gobble-expression")||this.gobbleBinaryExpression();return this.gobbleSpaces(),this.runHook("after-expression",e)}gobbleBinaryOp(){this.gobbleSpaces();let t=this.expr.substr(this.index,e.max_binop_len),r=t.length;for(;r>0;){if(e.binary_ops.hasOwnProperty(t)&&(!e.isIdentifierStart(this.code)||this.index+t.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+t.length))))return this.index+=r,t;t=t.substr(0,--r)}return!1}gobbleBinaryExpression(){let t,r,i,s,n,o,a,h,l;if(o=this.gobbleToken(),!o)return o;if(r=this.gobbleBinaryOp(),!r)return o;for(n={value:r,prec:e.binaryPrecedence(r),right_a:e.right_associative.has(r)},a=this.gobbleToken(),a||this.throwError("Expected expression after "+r),s=[o,n,a];r=this.gobbleBinaryOp();){if(i=e.binaryPrecedence(r),0===i){this.index-=r.length;break}n={value:r,prec:i,right_a:e.right_associative.has(r)},l=r;const h=e=>n.right_a&&e.right_a?i>e.prec:i<=e.prec;for(;s.length>2&&h(s[s.length-2]);)a=s.pop(),r=s.pop().value,o=s.pop(),t={type:e.BINARY_EXP,operator:r,left:o,right:a},s.push(t);t=this.gobbleToken(),t||this.throwError("Expected expression after "+l),s.push(n,t)}for(h=s.length-1,t=s[h];h>1;)t={type:e.BINARY_EXP,operator:s[h-1].value,left:s[h-2],right:t},h-=2;return t}gobbleToken(){let t,r,i,s;if(this.gobbleSpaces(),s=this.searchHook("gobble-token"),s)return this.runHook("after-token",s);if(t=this.code,e.isDecimalDigit(t)||t===e.PERIOD_CODE)return this.gobbleNumericLiteral();if(t===e.SQUOTE_CODE||t===e.DQUOTE_CODE)s=this.gobbleStringLiteral();else if(t===e.OBRACK_CODE)s=this.gobbleArray();else{for(r=this.expr.substr(this.index,e.max_unop_len),i=r.length;i>0;){if(e.unary_ops.hasOwnProperty(r)&&(!e.isIdentifierStart(this.code)||this.index+r.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+r.length)))){this.index+=i;const t=this.gobbleToken();return t||this.throwError("missing unaryOp argument"),this.runHook("after-token",{type:e.UNARY_EXP,operator:r,argument:t,prefix:!0})}r=r.substr(0,--i)}e.isIdentifierStart(t)?(s=this.gobbleIdentifier(),e.literals.hasOwnProperty(s.name)?s={type:e.LITERAL,value:e.literals[s.name],raw:s.name}:s.name===e.this_str&&(s={type:e.THIS_EXP})):t===e.OPAREN_CODE&&(s=this.gobbleGroup())}return s?(s=this.gobbleTokenProperty(s),this.runHook("after-token",s)):this.runHook("after-token",!1)}gobbleTokenProperty(t){this.gobbleSpaces();let r=this.code;for(;r===e.PERIOD_CODE||r===e.OBRACK_CODE||r===e.OPAREN_CODE||r===e.QUMARK_CODE;){let i;if(r===e.QUMARK_CODE){if(this.expr.charCodeAt(this.index+1)!==e.PERIOD_CODE)break;i=!0,this.index+=2,this.gobbleSpaces(),r=this.code}this.index++,r===e.OBRACK_CODE?(t={type:e.MEMBER_EXP,computed:!0,object:t,property:this.gobbleExpression()},this.gobbleSpaces(),r=this.code,r!==e.CBRACK_CODE&&this.throwError("Unclosed ["),this.index++):r===e.OPAREN_CODE?t={type:e.CALL_EXP,arguments:this.gobbleArguments(e.CPAREN_CODE),callee:t}:(r===e.PERIOD_CODE||i)&&(i&&this.index--,this.gobbleSpaces(),t={type:e.MEMBER_EXP,computed:!1,object:t,property:this.gobbleIdentifier()}),i&&(t.optional=!0),this.gobbleSpaces(),r=this.code}return t}gobbleNumericLiteral(){let t,r,i="";for(;e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(this.code===e.PERIOD_CODE)for(i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(t=this.char,"e"===t||"E"===t){for(i+=this.expr.charAt(this.index++),t=this.char,"+"!==t&&"-"!==t||(i+=this.expr.charAt(this.index++));e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.expr.charCodeAt(this.index-1))||this.throwError("Expected exponent ("+i+this.char+")")}return r=this.code,e.isIdentifierStart(r)?this.throwError("Variable names cannot start with a number ("+i+this.char+")"):(r===e.PERIOD_CODE||1===i.length&&i.charCodeAt(0)===e.PERIOD_CODE)&&this.throwError("Unexpected period"),{type:e.LITERAL,value:parseFloat(i),raw:i}}gobbleStringLiteral(){let t="";const r=this.index,i=this.expr.charAt(this.index++);let s=!1;for(;this.index<this.expr.length;){let e=this.expr.charAt(this.index++);if(e===i){s=!0;break}if("\\"===e)switch(e=this.expr.charAt(this.index++),e){case"n":t+="\n";break;case"r":t+="\r";break;case"t":t+="\t";break;case"b":t+="\b";break;case"f":t+="\f";break;case"v":t+="\v";break;default:t+=e}else t+=e}return s||this.throwError('Unclosed quote after "'+t+'"'),{type:e.LITERAL,value:t,raw:this.expr.substring(r,this.index)}}gobbleIdentifier(){let t=this.code,r=this.index;for(e.isIdentifierStart(t)?this.index++:this.throwError("Unexpected "+this.char);this.index<this.expr.length&&(t=this.code,e.isIdentifierPart(t));)this.index++;return{type:e.IDENTIFIER,name:this.expr.slice(r,this.index)}}gobbleArguments(t){const r=[];let i=!1,s=0;for(;this.index<this.expr.length;){this.gobbleSpaces();let n=this.code;if(n===t){i=!0,this.index++,t===e.CPAREN_CODE&&s&&s>=r.length&&this.throwError("Unexpected token "+String.fromCharCode(t));break}if(n===e.COMMA_CODE){if(this.index++,s++,s!==r.length)if(t===e.CPAREN_CODE)this.throwError("Unexpected token ,");else if(t===e.CBRACK_CODE)for(let e=r.length;e<s;e++)r.push(null)}else if(r.length!==s&&0!==s)this.throwError("Expected comma");else{const t=this.gobbleExpression();t&&t.type!==e.COMPOUND||this.throwError("Expected comma"),r.push(t)}}return i||this.throwError("Expected "+String.fromCharCode(t)),r}gobbleGroup(){this.index++;let t=this.gobbleExpressions(e.CPAREN_CODE);if(this.code===e.CPAREN_CODE)return this.index++,1===t.length?t[0]:!!t.length&&{type:e.SEQUENCE_EXP,expressions:t};this.throwError("Unclosed (")}gobbleArray(){return this.index++,{type:e.ARRAY_EXP,elements:this.gobbleArguments(e.CBRACK_CODE)}}}const t=new class{add(e,t,r){if("string"!=typeof arguments[0])for(let e in arguments[0])this.add(e,arguments[0][e],arguments[1]);else(Array.isArray(e)?e:[e]).forEach((function(e){this[e]=this[e]||[],t&&this[e][r?"unshift":"push"](t)}),this)}run(e,t){this[e]=this[e]||[],this[e].forEach((function(e){e.call(t&&t.context?t.context:t,t)}))}};Object.assign(e,{hooks:t,plugins:new class{constructor(e){this.jsep=e,this.registered={}}register(...e){e.forEach((e=>{if("object"!=typeof e||!e.name||!e.init)throw new Error("Invalid JSEP plugin format");this.registered[e.name]||(e.init(this.jsep),this.registered[e.name]=e)}))}}(e),COMPOUND:"Compound",SEQUENCE_EXP:"SequenceExpression",IDENTIFIER:"Identifier",MEMBER_EXP:"MemberExpression",LITERAL:"Literal",THIS_EXP:"ThisExpression",CALL_EXP:"CallExpression",UNARY_EXP:"UnaryExpression",BINARY_EXP:"BinaryExpression",ARRAY_EXP:"ArrayExpression",TAB_CODE:9,LF_CODE:10,CR_CODE:13,SPACE_CODE:32,PERIOD_CODE:46,COMMA_CODE:44,SQUOTE_CODE:39,DQUOTE_CODE:34,OPAREN_CODE:40,CPAREN_CODE:41,OBRACK_CODE:91,CBRACK_CODE:93,QUMARK_CODE:63,SEMCOL_CODE:59,COLON_CODE:58,unary_ops:{"-":1,"!":1,"~":1,"+":1},binary_ops:{"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},right_associative:new Set,additional_identifier_chars:new Set(["$","_"]),literals:{true:!0,false:!1,null:null},this_str:"this"}),e.max_unop_len=e.getMaxKeyLen(e.unary_ops),e.max_binop_len=e.getMaxKeyLen(e.binary_ops);const r=t=>new e(t).parse();Object.getOwnPropertyNames(e).forEach((t=>{void 0===r[t]&&"prototype"!==t&&(r[t]=e[t])})),r.Jsep=e;const i="ConditionalExpression";var s={name:"ternary",init(e){function t(e){for(;e.left&&":"===e.left.operator;)e=e.left;return e}e.hooks.add("after-expression",(function(r){if(r.node&&this.code===e.QUMARK_CODE){this.index++;const s=r.node,n=this.gobbleExpression();if(n||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const e=this.gobbleExpression();e||this.throwError("Expected expression"),r.node={type:"ConditionalExpression",test:s,consequent:n,alternate:e}}else if(":"===s.operator){n.right||this.throwError("Expected :");const e=t(n);s.right={type:"ConditionalExpression",test:s.right,consequent:e.left,alternate:e===n?e.right:{operator:":",left:e.right,right:n.right}},r.node=s}else if(":"===n.operator)!function(e,t){e.type=i,e.test=t,e.consequent=e.left,e.alternate=e.right,delete e.operator,delete e.left,delete e.right}(t(n),s),r.node=n;else if(n.alternate){let e=n.alternate;for(;e.alternate;)e=e.alternate;r.node={type:i,test:s,consequent:n,alternate:e.right},delete e.operator,delete e.right,Object.assign(e,e.left)}else this.throwError("Expected :");if(r.node.test&&r.node.test.operator&&e.binary_ops[r.node.test.operator]<.95){const e=r.node;r.node=e.test,r.node.right={type:i,test:e.test.right,consequent:e.consequent,alternate:e.alternate}}}}))}};r.plugins.register(s),module.exports=r; | ||
//# sourceMappingURL=jsep.cjs.min.js.map |
@@ -132,7 +132,14 @@ var jsep = (function () { | ||
* @param {number} precedence The precedence of the binary op (can be a float). Higher number = higher precedence | ||
* @param {boolean} [isRightAssociative=false] whether operator is right-associative | ||
* @returns {Jsep} | ||
*/ | ||
static addBinaryOp(op_name, precedence) { | ||
static addBinaryOp(op_name, precedence, isRightAssociative) { | ||
Jsep.max_binop_len = Math.max(op_name.length, Jsep.max_binop_len); | ||
Jsep.binary_ops[op_name] = precedence; | ||
if (isRightAssociative) { | ||
Jsep.right_associative.add(op_name); | ||
} | ||
else { | ||
Jsep.right_associative.delete(op_name); | ||
} | ||
return Jsep; | ||
@@ -207,2 +214,3 @@ } | ||
} | ||
Jsep.right_associative.delete(op_name); | ||
@@ -500,3 +508,3 @@ return Jsep; | ||
// precedence structure | ||
biop_info = { value: biop, prec: Jsep.binaryPrecedence(biop)}; | ||
biop_info = { value: biop, prec: Jsep.binaryPrecedence(biop), right_a: Jsep.right_associative.has(biop) }; | ||
@@ -520,3 +528,3 @@ right = this.gobbleToken(); | ||
biop_info = { value: biop, prec }; | ||
biop_info = { value: biop, prec, right_a: Jsep.right_associative.has(biop) }; | ||
@@ -526,3 +534,6 @@ cur_biop = biop; | ||
// Reduce: make a binary expression from the three topmost entries. | ||
while ((stack.length > 2) && (prec <= stack[stack.length - 2].prec)) { | ||
const comparePrev = prev => biop_info.right_a && prev.right_a | ||
? prec > prev.prec | ||
: prec <= prev.prec; | ||
while ((stack.length > 2) && comparePrev(stack[stack.length - 2])) { | ||
right = stack.pop(); | ||
@@ -1030,2 +1041,5 @@ biop = stack.pop().value; | ||
// sets specific binary_ops as right-associative | ||
right_associative: new Set(), | ||
// Additional valid identifier chars, apart from a-z, A-Z and 0-9 (except on the starting char) | ||
@@ -1032,0 +1046,0 @@ additional_identifier_chars: new Set(['$', '_']), |
@@ -1,2 +0,2 @@ | ||
var jsep=function(){"use strict";class e{static get version(){return"1.0.2"}static toString(){return"JavaScript Expression Parser (JSEP) v"+e.version}static addUnaryOp(t){return e.max_unop_len=Math.max(t.length,e.max_unop_len),e.unary_ops[t]=1,e}static addBinaryOp(t,r){return e.max_binop_len=Math.max(t.length,e.max_binop_len),e.binary_ops[t]=r,e}static addIdentifierChar(t){return e.additional_identifier_chars.add(t),e}static addLiteral(t,r){return e.literals[t]=r,e}static removeUnaryOp(t){return delete e.unary_ops[t],t.length===e.max_unop_len&&(e.max_unop_len=e.getMaxKeyLen(e.unary_ops)),e}static removeAllUnaryOps(){return e.unary_ops={},e.max_unop_len=0,e}static removeIdentifierChar(t){return e.additional_identifier_chars.delete(t),e}static removeBinaryOp(t){return delete e.binary_ops[t],t.length===e.max_binop_len&&(e.max_binop_len=e.getMaxKeyLen(e.binary_ops)),e}static removeAllBinaryOps(){return e.binary_ops={},e.max_binop_len=0,e}static removeLiteral(t){return delete e.literals[t],e}static removeAllLiterals(){return e.literals={},e}get char(){return this.expr.charAt(this.index)}get code(){return this.expr.charCodeAt(this.index)}constructor(e){this.expr=e,this.index=0}static parse(t){return new e(t).parse()}static getMaxKeyLen(e){return Math.max(0,...Object.keys(e).map((e=>e.length)))}static isDecimalDigit(e){return e>=48&&e<=57}static binaryPrecedence(t){return e.binary_ops[t]||0}static isIdentifierStart(t){return t>=65&&t<=90||t>=97&&t<=122||t>=128&&!e.binary_ops[String.fromCharCode(t)]||e.additional_identifier_chars.has(String.fromCharCode(t))}static isIdentifierPart(t){return e.isIdentifierStart(t)||e.isDecimalDigit(t)}throwError(e){const t=new Error(e+" at character "+this.index);throw t.index=this.index,t.description=e,t}runHook(t,r){if(e.hooks[t]){const i={context:this,node:r};return e.hooks.run(t,i),i.node}return r}searchHook(t){if(e.hooks[t]){const r={context:this};return e.hooks[t].find((function(e){return e.call(r.context,r),r.node})),r.node}}gobbleSpaces(){let t=this.code;for(;t===e.SPACE_CODE||t===e.TAB_CODE||t===e.LF_CODE||t===e.CR_CODE;)t=this.expr.charCodeAt(++this.index);this.runHook("gobble-spaces")}parse(){this.runHook("before-all");const t=this.gobbleExpressions(),r=1===t.length?t[0]:{type:e.COMPOUND,body:t};return this.runHook("after-all",r)}gobbleExpressions(t){let r,i,s=[];for(;this.index<this.expr.length;)if(r=this.code,r===e.SEMCOL_CODE||r===e.COMMA_CODE)this.index++;else if(i=this.gobbleExpression())s.push(i);else if(this.index<this.expr.length){if(r===t)break;this.throwError('Unexpected "'+this.char+'"')}return s}gobbleExpression(){const e=this.searchHook("gobble-expression")||this.gobbleBinaryExpression();return this.gobbleSpaces(),this.runHook("after-expression",e)}gobbleBinaryOp(){this.gobbleSpaces();let t=this.expr.substr(this.index,e.max_binop_len),r=t.length;for(;r>0;){if(e.binary_ops.hasOwnProperty(t)&&(!e.isIdentifierStart(this.code)||this.index+t.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+t.length))))return this.index+=r,t;t=t.substr(0,--r)}return!1}gobbleBinaryExpression(){let t,r,i,s,n,o,h,a,l;if(o=this.gobbleToken(),!o)return o;if(r=this.gobbleBinaryOp(),!r)return o;for(n={value:r,prec:e.binaryPrecedence(r)},h=this.gobbleToken(),h||this.throwError("Expected expression after "+r),s=[o,n,h];r=this.gobbleBinaryOp();){if(i=e.binaryPrecedence(r),0===i){this.index-=r.length;break}for(n={value:r,prec:i},l=r;s.length>2&&i<=s[s.length-2].prec;)h=s.pop(),r=s.pop().value,o=s.pop(),t={type:e.BINARY_EXP,operator:r,left:o,right:h},s.push(t);t=this.gobbleToken(),t||this.throwError("Expected expression after "+l),s.push(n,t)}for(a=s.length-1,t=s[a];a>1;)t={type:e.BINARY_EXP,operator:s[a-1].value,left:s[a-2],right:t},a-=2;return t}gobbleToken(){let t,r,i,s;if(this.gobbleSpaces(),s=this.searchHook("gobble-token"),s)return this.runHook("after-token",s);if(t=this.code,e.isDecimalDigit(t)||t===e.PERIOD_CODE)return this.gobbleNumericLiteral();if(t===e.SQUOTE_CODE||t===e.DQUOTE_CODE)s=this.gobbleStringLiteral();else if(t===e.OBRACK_CODE)s=this.gobbleArray();else{for(r=this.expr.substr(this.index,e.max_unop_len),i=r.length;i>0;){if(e.unary_ops.hasOwnProperty(r)&&(!e.isIdentifierStart(this.code)||this.index+r.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+r.length)))){this.index+=i;const t=this.gobbleToken();return t||this.throwError("missing unaryOp argument"),this.runHook("after-token",{type:e.UNARY_EXP,operator:r,argument:t,prefix:!0})}r=r.substr(0,--i)}e.isIdentifierStart(t)?(s=this.gobbleIdentifier(),e.literals.hasOwnProperty(s.name)?s={type:e.LITERAL,value:e.literals[s.name],raw:s.name}:s.name===e.this_str&&(s={type:e.THIS_EXP})):t===e.OPAREN_CODE&&(s=this.gobbleGroup())}return s?(s=this.gobbleTokenProperty(s),this.runHook("after-token",s)):this.runHook("after-token",!1)}gobbleTokenProperty(t){this.gobbleSpaces();let r=this.code;for(;r===e.PERIOD_CODE||r===e.OBRACK_CODE||r===e.OPAREN_CODE||r===e.QUMARK_CODE;){let i;if(r===e.QUMARK_CODE){if(this.expr.charCodeAt(this.index+1)!==e.PERIOD_CODE)break;i=!0,this.index+=2,this.gobbleSpaces(),r=this.code}this.index++,r===e.OBRACK_CODE?(t={type:e.MEMBER_EXP,computed:!0,object:t,property:this.gobbleExpression()},this.gobbleSpaces(),r=this.code,r!==e.CBRACK_CODE&&this.throwError("Unclosed ["),this.index++):r===e.OPAREN_CODE?t={type:e.CALL_EXP,arguments:this.gobbleArguments(e.CPAREN_CODE),callee:t}:(r===e.PERIOD_CODE||i)&&(i&&this.index--,this.gobbleSpaces(),t={type:e.MEMBER_EXP,computed:!1,object:t,property:this.gobbleIdentifier()}),i&&(t.optional=!0),this.gobbleSpaces(),r=this.code}return t}gobbleNumericLiteral(){let t,r,i="";for(;e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(this.code===e.PERIOD_CODE)for(i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(t=this.char,"e"===t||"E"===t){for(i+=this.expr.charAt(this.index++),t=this.char,"+"!==t&&"-"!==t||(i+=this.expr.charAt(this.index++));e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.expr.charCodeAt(this.index-1))||this.throwError("Expected exponent ("+i+this.char+")")}return r=this.code,e.isIdentifierStart(r)?this.throwError("Variable names cannot start with a number ("+i+this.char+")"):(r===e.PERIOD_CODE||1===i.length&&i.charCodeAt(0)===e.PERIOD_CODE)&&this.throwError("Unexpected period"),{type:e.LITERAL,value:parseFloat(i),raw:i}}gobbleStringLiteral(){let t="";const r=this.index,i=this.expr.charAt(this.index++);let s=!1;for(;this.index<this.expr.length;){let e=this.expr.charAt(this.index++);if(e===i){s=!0;break}if("\\"===e)switch(e=this.expr.charAt(this.index++),e){case"n":t+="\n";break;case"r":t+="\r";break;case"t":t+="\t";break;case"b":t+="\b";break;case"f":t+="\f";break;case"v":t+="\v";break;default:t+=e}else t+=e}return s||this.throwError('Unclosed quote after "'+t+'"'),{type:e.LITERAL,value:t,raw:this.expr.substring(r,this.index)}}gobbleIdentifier(){let t=this.code,r=this.index;for(e.isIdentifierStart(t)?this.index++:this.throwError("Unexpected "+this.char);this.index<this.expr.length&&(t=this.code,e.isIdentifierPart(t));)this.index++;return{type:e.IDENTIFIER,name:this.expr.slice(r,this.index)}}gobbleArguments(t){const r=[];let i=!1,s=0;for(;this.index<this.expr.length;){this.gobbleSpaces();let n=this.code;if(n===t){i=!0,this.index++,t===e.CPAREN_CODE&&s&&s>=r.length&&this.throwError("Unexpected token "+String.fromCharCode(t));break}if(n===e.COMMA_CODE){if(this.index++,s++,s!==r.length)if(t===e.CPAREN_CODE)this.throwError("Unexpected token ,");else if(t===e.CBRACK_CODE)for(let e=r.length;e<s;e++)r.push(null)}else if(r.length!==s&&0!==s)this.throwError("Expected comma");else{const t=this.gobbleExpression();t&&t.type!==e.COMPOUND||this.throwError("Expected comma"),r.push(t)}}return i||this.throwError("Expected "+String.fromCharCode(t)),r}gobbleGroup(){this.index++;let t=this.gobbleExpressions(e.CPAREN_CODE);if(this.code===e.CPAREN_CODE)return this.index++,1===t.length?t[0]:!!t.length&&{type:e.SEQUENCE_EXP,expressions:t};this.throwError("Unclosed (")}gobbleArray(){return this.index++,{type:e.ARRAY_EXP,elements:this.gobbleArguments(e.CBRACK_CODE)}}}const t=new class{add(e,t,r){if("string"!=typeof arguments[0])for(let e in arguments[0])this.add(e,arguments[0][e],arguments[1]);else(Array.isArray(e)?e:[e]).forEach((function(e){this[e]=this[e]||[],t&&this[e][r?"unshift":"push"](t)}),this)}run(e,t){this[e]=this[e]||[],this[e].forEach((function(e){e.call(t&&t.context?t.context:t,t)}))}};Object.assign(e,{hooks:t,plugins:new class{constructor(e){this.jsep=e,this.registered={}}register(...e){e.forEach((e=>{if("object"!=typeof e||!e.name||!e.init)throw new Error("Invalid JSEP plugin format");this.registered[e.name]||(e.init(this.jsep),this.registered[e.name]=e)}))}}(e),COMPOUND:"Compound",SEQUENCE_EXP:"SequenceExpression",IDENTIFIER:"Identifier",MEMBER_EXP:"MemberExpression",LITERAL:"Literal",THIS_EXP:"ThisExpression",CALL_EXP:"CallExpression",UNARY_EXP:"UnaryExpression",BINARY_EXP:"BinaryExpression",ARRAY_EXP:"ArrayExpression",TAB_CODE:9,LF_CODE:10,CR_CODE:13,SPACE_CODE:32,PERIOD_CODE:46,COMMA_CODE:44,SQUOTE_CODE:39,DQUOTE_CODE:34,OPAREN_CODE:40,CPAREN_CODE:41,OBRACK_CODE:91,CBRACK_CODE:93,QUMARK_CODE:63,SEMCOL_CODE:59,COLON_CODE:58,unary_ops:{"-":1,"!":1,"~":1,"+":1},binary_ops:{"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},additional_identifier_chars:new Set(["$","_"]),literals:{true:!0,false:!1,null:null},this_str:"this"}),e.max_unop_len=e.getMaxKeyLen(e.unary_ops),e.max_binop_len=e.getMaxKeyLen(e.binary_ops);const r=t=>new e(t).parse();Object.getOwnPropertyNames(e).forEach((t=>{void 0===r[t]&&"prototype"!==t&&(r[t]=e[t])})),r.Jsep=e;const i="ConditionalExpression";var s={name:"ternary",init(e){function t(e){for(;e.left&&":"===e.left.operator;)e=e.left;return e}e.hooks.add("after-expression",(function(r){if(r.node&&this.code===e.QUMARK_CODE){this.index++;const s=r.node,n=this.gobbleExpression();if(n||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const e=this.gobbleExpression();e||this.throwError("Expected expression"),r.node={type:i,test:s,consequent:n,alternate:e}}else if(":"===s.operator){n.right||this.throwError("Expected :");const e=t(n);s.right={type:i,test:s.right,consequent:e.left,alternate:e===n?e.right:{operator:":",left:e.right,right:n.right}},r.node=s}else if(":"===n.operator)!function(e,t){e.type=i,e.test=t,e.consequent=e.left,e.alternate=e.right,delete e.operator,delete e.left,delete e.right}(t(n),s),r.node=n;else if(n.alternate){let e=n.alternate;for(;e.alternate;)e=e.alternate;r.node={type:i,test:s,consequent:n,alternate:e.right},delete e.operator,delete e.right,Object.assign(e,e.left)}else this.throwError("Expected :");if(r.node.test&&r.node.test.operator&&e.binary_ops[r.node.test.operator]<.95){const e=r.node;r.node=e.test,r.node.right={type:i,test:e.test.right,consequent:e.consequent,alternate:e.alternate}}}}))}};return r.plugins.register(s),r}(); | ||
var jsep=function(){"use strict";class e{static get version(){return"1.0.2"}static toString(){return"JavaScript Expression Parser (JSEP) v"+e.version}static addUnaryOp(t){return e.max_unop_len=Math.max(t.length,e.max_unop_len),e.unary_ops[t]=1,e}static addBinaryOp(t,r,i){return e.max_binop_len=Math.max(t.length,e.max_binop_len),e.binary_ops[t]=r,i?e.right_associative.add(t):e.right_associative.delete(t),e}static addIdentifierChar(t){return e.additional_identifier_chars.add(t),e}static addLiteral(t,r){return e.literals[t]=r,e}static removeUnaryOp(t){return delete e.unary_ops[t],t.length===e.max_unop_len&&(e.max_unop_len=e.getMaxKeyLen(e.unary_ops)),e}static removeAllUnaryOps(){return e.unary_ops={},e.max_unop_len=0,e}static removeIdentifierChar(t){return e.additional_identifier_chars.delete(t),e}static removeBinaryOp(t){return delete e.binary_ops[t],t.length===e.max_binop_len&&(e.max_binop_len=e.getMaxKeyLen(e.binary_ops)),e.right_associative.delete(t),e}static removeAllBinaryOps(){return e.binary_ops={},e.max_binop_len=0,e}static removeLiteral(t){return delete e.literals[t],e}static removeAllLiterals(){return e.literals={},e}get char(){return this.expr.charAt(this.index)}get code(){return this.expr.charCodeAt(this.index)}constructor(e){this.expr=e,this.index=0}static parse(t){return new e(t).parse()}static getMaxKeyLen(e){return Math.max(0,...Object.keys(e).map((e=>e.length)))}static isDecimalDigit(e){return e>=48&&e<=57}static binaryPrecedence(t){return e.binary_ops[t]||0}static isIdentifierStart(t){return t>=65&&t<=90||t>=97&&t<=122||t>=128&&!e.binary_ops[String.fromCharCode(t)]||e.additional_identifier_chars.has(String.fromCharCode(t))}static isIdentifierPart(t){return e.isIdentifierStart(t)||e.isDecimalDigit(t)}throwError(e){const t=new Error(e+" at character "+this.index);throw t.index=this.index,t.description=e,t}runHook(t,r){if(e.hooks[t]){const i={context:this,node:r};return e.hooks.run(t,i),i.node}return r}searchHook(t){if(e.hooks[t]){const r={context:this};return e.hooks[t].find((function(e){return e.call(r.context,r),r.node})),r.node}}gobbleSpaces(){let t=this.code;for(;t===e.SPACE_CODE||t===e.TAB_CODE||t===e.LF_CODE||t===e.CR_CODE;)t=this.expr.charCodeAt(++this.index);this.runHook("gobble-spaces")}parse(){this.runHook("before-all");const t=this.gobbleExpressions(),r=1===t.length?t[0]:{type:e.COMPOUND,body:t};return this.runHook("after-all",r)}gobbleExpressions(t){let r,i,s=[];for(;this.index<this.expr.length;)if(r=this.code,r===e.SEMCOL_CODE||r===e.COMMA_CODE)this.index++;else if(i=this.gobbleExpression())s.push(i);else if(this.index<this.expr.length){if(r===t)break;this.throwError('Unexpected "'+this.char+'"')}return s}gobbleExpression(){const e=this.searchHook("gobble-expression")||this.gobbleBinaryExpression();return this.gobbleSpaces(),this.runHook("after-expression",e)}gobbleBinaryOp(){this.gobbleSpaces();let t=this.expr.substr(this.index,e.max_binop_len),r=t.length;for(;r>0;){if(e.binary_ops.hasOwnProperty(t)&&(!e.isIdentifierStart(this.code)||this.index+t.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+t.length))))return this.index+=r,t;t=t.substr(0,--r)}return!1}gobbleBinaryExpression(){let t,r,i,s,n,o,a,h,l;if(o=this.gobbleToken(),!o)return o;if(r=this.gobbleBinaryOp(),!r)return o;for(n={value:r,prec:e.binaryPrecedence(r),right_a:e.right_associative.has(r)},a=this.gobbleToken(),a||this.throwError("Expected expression after "+r),s=[o,n,a];r=this.gobbleBinaryOp();){if(i=e.binaryPrecedence(r),0===i){this.index-=r.length;break}n={value:r,prec:i,right_a:e.right_associative.has(r)},l=r;const h=e=>n.right_a&&e.right_a?i>e.prec:i<=e.prec;for(;s.length>2&&h(s[s.length-2]);)a=s.pop(),r=s.pop().value,o=s.pop(),t={type:e.BINARY_EXP,operator:r,left:o,right:a},s.push(t);t=this.gobbleToken(),t||this.throwError("Expected expression after "+l),s.push(n,t)}for(h=s.length-1,t=s[h];h>1;)t={type:e.BINARY_EXP,operator:s[h-1].value,left:s[h-2],right:t},h-=2;return t}gobbleToken(){let t,r,i,s;if(this.gobbleSpaces(),s=this.searchHook("gobble-token"),s)return this.runHook("after-token",s);if(t=this.code,e.isDecimalDigit(t)||t===e.PERIOD_CODE)return this.gobbleNumericLiteral();if(t===e.SQUOTE_CODE||t===e.DQUOTE_CODE)s=this.gobbleStringLiteral();else if(t===e.OBRACK_CODE)s=this.gobbleArray();else{for(r=this.expr.substr(this.index,e.max_unop_len),i=r.length;i>0;){if(e.unary_ops.hasOwnProperty(r)&&(!e.isIdentifierStart(this.code)||this.index+r.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+r.length)))){this.index+=i;const t=this.gobbleToken();return t||this.throwError("missing unaryOp argument"),this.runHook("after-token",{type:e.UNARY_EXP,operator:r,argument:t,prefix:!0})}r=r.substr(0,--i)}e.isIdentifierStart(t)?(s=this.gobbleIdentifier(),e.literals.hasOwnProperty(s.name)?s={type:e.LITERAL,value:e.literals[s.name],raw:s.name}:s.name===e.this_str&&(s={type:e.THIS_EXP})):t===e.OPAREN_CODE&&(s=this.gobbleGroup())}return s?(s=this.gobbleTokenProperty(s),this.runHook("after-token",s)):this.runHook("after-token",!1)}gobbleTokenProperty(t){this.gobbleSpaces();let r=this.code;for(;r===e.PERIOD_CODE||r===e.OBRACK_CODE||r===e.OPAREN_CODE||r===e.QUMARK_CODE;){let i;if(r===e.QUMARK_CODE){if(this.expr.charCodeAt(this.index+1)!==e.PERIOD_CODE)break;i=!0,this.index+=2,this.gobbleSpaces(),r=this.code}this.index++,r===e.OBRACK_CODE?(t={type:e.MEMBER_EXP,computed:!0,object:t,property:this.gobbleExpression()},this.gobbleSpaces(),r=this.code,r!==e.CBRACK_CODE&&this.throwError("Unclosed ["),this.index++):r===e.OPAREN_CODE?t={type:e.CALL_EXP,arguments:this.gobbleArguments(e.CPAREN_CODE),callee:t}:(r===e.PERIOD_CODE||i)&&(i&&this.index--,this.gobbleSpaces(),t={type:e.MEMBER_EXP,computed:!1,object:t,property:this.gobbleIdentifier()}),i&&(t.optional=!0),this.gobbleSpaces(),r=this.code}return t}gobbleNumericLiteral(){let t,r,i="";for(;e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(this.code===e.PERIOD_CODE)for(i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(t=this.char,"e"===t||"E"===t){for(i+=this.expr.charAt(this.index++),t=this.char,"+"!==t&&"-"!==t||(i+=this.expr.charAt(this.index++));e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.expr.charCodeAt(this.index-1))||this.throwError("Expected exponent ("+i+this.char+")")}return r=this.code,e.isIdentifierStart(r)?this.throwError("Variable names cannot start with a number ("+i+this.char+")"):(r===e.PERIOD_CODE||1===i.length&&i.charCodeAt(0)===e.PERIOD_CODE)&&this.throwError("Unexpected period"),{type:e.LITERAL,value:parseFloat(i),raw:i}}gobbleStringLiteral(){let t="";const r=this.index,i=this.expr.charAt(this.index++);let s=!1;for(;this.index<this.expr.length;){let e=this.expr.charAt(this.index++);if(e===i){s=!0;break}if("\\"===e)switch(e=this.expr.charAt(this.index++),e){case"n":t+="\n";break;case"r":t+="\r";break;case"t":t+="\t";break;case"b":t+="\b";break;case"f":t+="\f";break;case"v":t+="\v";break;default:t+=e}else t+=e}return s||this.throwError('Unclosed quote after "'+t+'"'),{type:e.LITERAL,value:t,raw:this.expr.substring(r,this.index)}}gobbleIdentifier(){let t=this.code,r=this.index;for(e.isIdentifierStart(t)?this.index++:this.throwError("Unexpected "+this.char);this.index<this.expr.length&&(t=this.code,e.isIdentifierPart(t));)this.index++;return{type:e.IDENTIFIER,name:this.expr.slice(r,this.index)}}gobbleArguments(t){const r=[];let i=!1,s=0;for(;this.index<this.expr.length;){this.gobbleSpaces();let n=this.code;if(n===t){i=!0,this.index++,t===e.CPAREN_CODE&&s&&s>=r.length&&this.throwError("Unexpected token "+String.fromCharCode(t));break}if(n===e.COMMA_CODE){if(this.index++,s++,s!==r.length)if(t===e.CPAREN_CODE)this.throwError("Unexpected token ,");else if(t===e.CBRACK_CODE)for(let e=r.length;e<s;e++)r.push(null)}else if(r.length!==s&&0!==s)this.throwError("Expected comma");else{const t=this.gobbleExpression();t&&t.type!==e.COMPOUND||this.throwError("Expected comma"),r.push(t)}}return i||this.throwError("Expected "+String.fromCharCode(t)),r}gobbleGroup(){this.index++;let t=this.gobbleExpressions(e.CPAREN_CODE);if(this.code===e.CPAREN_CODE)return this.index++,1===t.length?t[0]:!!t.length&&{type:e.SEQUENCE_EXP,expressions:t};this.throwError("Unclosed (")}gobbleArray(){return this.index++,{type:e.ARRAY_EXP,elements:this.gobbleArguments(e.CBRACK_CODE)}}}const t=new class{add(e,t,r){if("string"!=typeof arguments[0])for(let e in arguments[0])this.add(e,arguments[0][e],arguments[1]);else(Array.isArray(e)?e:[e]).forEach((function(e){this[e]=this[e]||[],t&&this[e][r?"unshift":"push"](t)}),this)}run(e,t){this[e]=this[e]||[],this[e].forEach((function(e){e.call(t&&t.context?t.context:t,t)}))}};Object.assign(e,{hooks:t,plugins:new class{constructor(e){this.jsep=e,this.registered={}}register(...e){e.forEach((e=>{if("object"!=typeof e||!e.name||!e.init)throw new Error("Invalid JSEP plugin format");this.registered[e.name]||(e.init(this.jsep),this.registered[e.name]=e)}))}}(e),COMPOUND:"Compound",SEQUENCE_EXP:"SequenceExpression",IDENTIFIER:"Identifier",MEMBER_EXP:"MemberExpression",LITERAL:"Literal",THIS_EXP:"ThisExpression",CALL_EXP:"CallExpression",UNARY_EXP:"UnaryExpression",BINARY_EXP:"BinaryExpression",ARRAY_EXP:"ArrayExpression",TAB_CODE:9,LF_CODE:10,CR_CODE:13,SPACE_CODE:32,PERIOD_CODE:46,COMMA_CODE:44,SQUOTE_CODE:39,DQUOTE_CODE:34,OPAREN_CODE:40,CPAREN_CODE:41,OBRACK_CODE:91,CBRACK_CODE:93,QUMARK_CODE:63,SEMCOL_CODE:59,COLON_CODE:58,unary_ops:{"-":1,"!":1,"~":1,"+":1},binary_ops:{"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},right_associative:new Set,additional_identifier_chars:new Set(["$","_"]),literals:{true:!0,false:!1,null:null},this_str:"this"}),e.max_unop_len=e.getMaxKeyLen(e.unary_ops),e.max_binop_len=e.getMaxKeyLen(e.binary_ops);const r=t=>new e(t).parse();Object.getOwnPropertyNames(e).forEach((t=>{void 0===r[t]&&"prototype"!==t&&(r[t]=e[t])})),r.Jsep=e;const i="ConditionalExpression";var s={name:"ternary",init(e){function t(e){for(;e.left&&":"===e.left.operator;)e=e.left;return e}e.hooks.add("after-expression",(function(r){if(r.node&&this.code===e.QUMARK_CODE){this.index++;const s=r.node,n=this.gobbleExpression();if(n||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const e=this.gobbleExpression();e||this.throwError("Expected expression"),r.node={type:i,test:s,consequent:n,alternate:e}}else if(":"===s.operator){n.right||this.throwError("Expected :");const e=t(n);s.right={type:i,test:s.right,consequent:e.left,alternate:e===n?e.right:{operator:":",left:e.right,right:n.right}},r.node=s}else if(":"===n.operator)!function(e,t){e.type=i,e.test=t,e.consequent=e.left,e.alternate=e.right,delete e.operator,delete e.left,delete e.right}(t(n),s),r.node=n;else if(n.alternate){let e=n.alternate;for(;e.alternate;)e=e.alternate;r.node={type:i,test:s,consequent:n,alternate:e.right},delete e.operator,delete e.right,Object.assign(e,e.left)}else this.throwError("Expected :");if(r.node.test&&r.node.test.operator&&e.binary_ops[r.node.test.operator]<.95){const e=r.node;r.node=e.test,r.node.right={type:i,test:e.test.right,consequent:e.consequent,alternate:e.alternate}}}}))}};return r.plugins.register(s),r}(); | ||
//# sourceMappingURL=jsep.iife.min.js.map |
@@ -129,7 +129,14 @@ /** | ||
* @param {number} precedence The precedence of the binary op (can be a float). Higher number = higher precedence | ||
* @param {boolean} [isRightAssociative=false] whether operator is right-associative | ||
* @returns {Jsep} | ||
*/ | ||
static addBinaryOp(op_name, precedence) { | ||
static addBinaryOp(op_name, precedence, isRightAssociative) { | ||
Jsep.max_binop_len = Math.max(op_name.length, Jsep.max_binop_len); | ||
Jsep.binary_ops[op_name] = precedence; | ||
if (isRightAssociative) { | ||
Jsep.right_associative.add(op_name); | ||
} | ||
else { | ||
Jsep.right_associative.delete(op_name); | ||
} | ||
return Jsep; | ||
@@ -204,2 +211,3 @@ } | ||
} | ||
Jsep.right_associative.delete(op_name); | ||
@@ -497,3 +505,3 @@ return Jsep; | ||
// precedence structure | ||
biop_info = { value: biop, prec: Jsep.binaryPrecedence(biop)}; | ||
biop_info = { value: biop, prec: Jsep.binaryPrecedence(biop), right_a: Jsep.right_associative.has(biop) }; | ||
@@ -517,3 +525,3 @@ right = this.gobbleToken(); | ||
biop_info = { value: biop, prec }; | ||
biop_info = { value: biop, prec, right_a: Jsep.right_associative.has(biop) }; | ||
@@ -523,3 +531,6 @@ cur_biop = biop; | ||
// Reduce: make a binary expression from the three topmost entries. | ||
while ((stack.length > 2) && (prec <= stack[stack.length - 2].prec)) { | ||
const comparePrev = prev => biop_info.right_a && prev.right_a | ||
? prec > prev.prec | ||
: prec <= prev.prec; | ||
while ((stack.length > 2) && comparePrev(stack[stack.length - 2])) { | ||
right = stack.pop(); | ||
@@ -1027,2 +1038,5 @@ biop = stack.pop().value; | ||
// sets specific binary_ops as right-associative | ||
right_associative: new Set(), | ||
// Additional valid identifier chars, apart from a-z, A-Z and 0-9 (except on the starting char) | ||
@@ -1029,0 +1043,0 @@ additional_identifier_chars: new Set(['$', '_']), |
@@ -1,2 +0,2 @@ | ||
class e{static get version(){return"1.0.2"}static toString(){return"JavaScript Expression Parser (JSEP) v"+e.version}static addUnaryOp(t){return e.max_unop_len=Math.max(t.length,e.max_unop_len),e.unary_ops[t]=1,e}static addBinaryOp(t,r){return e.max_binop_len=Math.max(t.length,e.max_binop_len),e.binary_ops[t]=r,e}static addIdentifierChar(t){return e.additional_identifier_chars.add(t),e}static addLiteral(t,r){return e.literals[t]=r,e}static removeUnaryOp(t){return delete e.unary_ops[t],t.length===e.max_unop_len&&(e.max_unop_len=e.getMaxKeyLen(e.unary_ops)),e}static removeAllUnaryOps(){return e.unary_ops={},e.max_unop_len=0,e}static removeIdentifierChar(t){return e.additional_identifier_chars.delete(t),e}static removeBinaryOp(t){return delete e.binary_ops[t],t.length===e.max_binop_len&&(e.max_binop_len=e.getMaxKeyLen(e.binary_ops)),e}static removeAllBinaryOps(){return e.binary_ops={},e.max_binop_len=0,e}static removeLiteral(t){return delete e.literals[t],e}static removeAllLiterals(){return e.literals={},e}get char(){return this.expr.charAt(this.index)}get code(){return this.expr.charCodeAt(this.index)}constructor(e){this.expr=e,this.index=0}static parse(t){return new e(t).parse()}static getMaxKeyLen(e){return Math.max(0,...Object.keys(e).map((e=>e.length)))}static isDecimalDigit(e){return e>=48&&e<=57}static binaryPrecedence(t){return e.binary_ops[t]||0}static isIdentifierStart(t){return t>=65&&t<=90||t>=97&&t<=122||t>=128&&!e.binary_ops[String.fromCharCode(t)]||e.additional_identifier_chars.has(String.fromCharCode(t))}static isIdentifierPart(t){return e.isIdentifierStart(t)||e.isDecimalDigit(t)}throwError(e){const t=new Error(e+" at character "+this.index);throw t.index=this.index,t.description=e,t}runHook(t,r){if(e.hooks[t]){const i={context:this,node:r};return e.hooks.run(t,i),i.node}return r}searchHook(t){if(e.hooks[t]){const r={context:this};return e.hooks[t].find((function(e){return e.call(r.context,r),r.node})),r.node}}gobbleSpaces(){let t=this.code;for(;t===e.SPACE_CODE||t===e.TAB_CODE||t===e.LF_CODE||t===e.CR_CODE;)t=this.expr.charCodeAt(++this.index);this.runHook("gobble-spaces")}parse(){this.runHook("before-all");const t=this.gobbleExpressions(),r=1===t.length?t[0]:{type:e.COMPOUND,body:t};return this.runHook("after-all",r)}gobbleExpressions(t){let r,i,s=[];for(;this.index<this.expr.length;)if(r=this.code,r===e.SEMCOL_CODE||r===e.COMMA_CODE)this.index++;else if(i=this.gobbleExpression())s.push(i);else if(this.index<this.expr.length){if(r===t)break;this.throwError('Unexpected "'+this.char+'"')}return s}gobbleExpression(){const e=this.searchHook("gobble-expression")||this.gobbleBinaryExpression();return this.gobbleSpaces(),this.runHook("after-expression",e)}gobbleBinaryOp(){this.gobbleSpaces();let t=this.expr.substr(this.index,e.max_binop_len),r=t.length;for(;r>0;){if(e.binary_ops.hasOwnProperty(t)&&(!e.isIdentifierStart(this.code)||this.index+t.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+t.length))))return this.index+=r,t;t=t.substr(0,--r)}return!1}gobbleBinaryExpression(){let t,r,i,s,n,o,h,a,l;if(o=this.gobbleToken(),!o)return o;if(r=this.gobbleBinaryOp(),!r)return o;for(n={value:r,prec:e.binaryPrecedence(r)},h=this.gobbleToken(),h||this.throwError("Expected expression after "+r),s=[o,n,h];r=this.gobbleBinaryOp();){if(i=e.binaryPrecedence(r),0===i){this.index-=r.length;break}for(n={value:r,prec:i},l=r;s.length>2&&i<=s[s.length-2].prec;)h=s.pop(),r=s.pop().value,o=s.pop(),t={type:e.BINARY_EXP,operator:r,left:o,right:h},s.push(t);t=this.gobbleToken(),t||this.throwError("Expected expression after "+l),s.push(n,t)}for(a=s.length-1,t=s[a];a>1;)t={type:e.BINARY_EXP,operator:s[a-1].value,left:s[a-2],right:t},a-=2;return t}gobbleToken(){let t,r,i,s;if(this.gobbleSpaces(),s=this.searchHook("gobble-token"),s)return this.runHook("after-token",s);if(t=this.code,e.isDecimalDigit(t)||t===e.PERIOD_CODE)return this.gobbleNumericLiteral();if(t===e.SQUOTE_CODE||t===e.DQUOTE_CODE)s=this.gobbleStringLiteral();else if(t===e.OBRACK_CODE)s=this.gobbleArray();else{for(r=this.expr.substr(this.index,e.max_unop_len),i=r.length;i>0;){if(e.unary_ops.hasOwnProperty(r)&&(!e.isIdentifierStart(this.code)||this.index+r.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+r.length)))){this.index+=i;const t=this.gobbleToken();return t||this.throwError("missing unaryOp argument"),this.runHook("after-token",{type:e.UNARY_EXP,operator:r,argument:t,prefix:!0})}r=r.substr(0,--i)}e.isIdentifierStart(t)?(s=this.gobbleIdentifier(),e.literals.hasOwnProperty(s.name)?s={type:e.LITERAL,value:e.literals[s.name],raw:s.name}:s.name===e.this_str&&(s={type:e.THIS_EXP})):t===e.OPAREN_CODE&&(s=this.gobbleGroup())}return s?(s=this.gobbleTokenProperty(s),this.runHook("after-token",s)):this.runHook("after-token",!1)}gobbleTokenProperty(t){this.gobbleSpaces();let r=this.code;for(;r===e.PERIOD_CODE||r===e.OBRACK_CODE||r===e.OPAREN_CODE||r===e.QUMARK_CODE;){let i;if(r===e.QUMARK_CODE){if(this.expr.charCodeAt(this.index+1)!==e.PERIOD_CODE)break;i=!0,this.index+=2,this.gobbleSpaces(),r=this.code}this.index++,r===e.OBRACK_CODE?(t={type:e.MEMBER_EXP,computed:!0,object:t,property:this.gobbleExpression()},this.gobbleSpaces(),r=this.code,r!==e.CBRACK_CODE&&this.throwError("Unclosed ["),this.index++):r===e.OPAREN_CODE?t={type:e.CALL_EXP,arguments:this.gobbleArguments(e.CPAREN_CODE),callee:t}:(r===e.PERIOD_CODE||i)&&(i&&this.index--,this.gobbleSpaces(),t={type:e.MEMBER_EXP,computed:!1,object:t,property:this.gobbleIdentifier()}),i&&(t.optional=!0),this.gobbleSpaces(),r=this.code}return t}gobbleNumericLiteral(){let t,r,i="";for(;e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(this.code===e.PERIOD_CODE)for(i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(t=this.char,"e"===t||"E"===t){for(i+=this.expr.charAt(this.index++),t=this.char,"+"!==t&&"-"!==t||(i+=this.expr.charAt(this.index++));e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.expr.charCodeAt(this.index-1))||this.throwError("Expected exponent ("+i+this.char+")")}return r=this.code,e.isIdentifierStart(r)?this.throwError("Variable names cannot start with a number ("+i+this.char+")"):(r===e.PERIOD_CODE||1===i.length&&i.charCodeAt(0)===e.PERIOD_CODE)&&this.throwError("Unexpected period"),{type:e.LITERAL,value:parseFloat(i),raw:i}}gobbleStringLiteral(){let t="";const r=this.index,i=this.expr.charAt(this.index++);let s=!1;for(;this.index<this.expr.length;){let e=this.expr.charAt(this.index++);if(e===i){s=!0;break}if("\\"===e)switch(e=this.expr.charAt(this.index++),e){case"n":t+="\n";break;case"r":t+="\r";break;case"t":t+="\t";break;case"b":t+="\b";break;case"f":t+="\f";break;case"v":t+="\v";break;default:t+=e}else t+=e}return s||this.throwError('Unclosed quote after "'+t+'"'),{type:e.LITERAL,value:t,raw:this.expr.substring(r,this.index)}}gobbleIdentifier(){let t=this.code,r=this.index;for(e.isIdentifierStart(t)?this.index++:this.throwError("Unexpected "+this.char);this.index<this.expr.length&&(t=this.code,e.isIdentifierPart(t));)this.index++;return{type:e.IDENTIFIER,name:this.expr.slice(r,this.index)}}gobbleArguments(t){const r=[];let i=!1,s=0;for(;this.index<this.expr.length;){this.gobbleSpaces();let n=this.code;if(n===t){i=!0,this.index++,t===e.CPAREN_CODE&&s&&s>=r.length&&this.throwError("Unexpected token "+String.fromCharCode(t));break}if(n===e.COMMA_CODE){if(this.index++,s++,s!==r.length)if(t===e.CPAREN_CODE)this.throwError("Unexpected token ,");else if(t===e.CBRACK_CODE)for(let e=r.length;e<s;e++)r.push(null)}else if(r.length!==s&&0!==s)this.throwError("Expected comma");else{const t=this.gobbleExpression();t&&t.type!==e.COMPOUND||this.throwError("Expected comma"),r.push(t)}}return i||this.throwError("Expected "+String.fromCharCode(t)),r}gobbleGroup(){this.index++;let t=this.gobbleExpressions(e.CPAREN_CODE);if(this.code===e.CPAREN_CODE)return this.index++,1===t.length?t[0]:!!t.length&&{type:e.SEQUENCE_EXP,expressions:t};this.throwError("Unclosed (")}gobbleArray(){return this.index++,{type:e.ARRAY_EXP,elements:this.gobbleArguments(e.CBRACK_CODE)}}}const t=new class{add(e,t,r){if("string"!=typeof arguments[0])for(let e in arguments[0])this.add(e,arguments[0][e],arguments[1]);else(Array.isArray(e)?e:[e]).forEach((function(e){this[e]=this[e]||[],t&&this[e][r?"unshift":"push"](t)}),this)}run(e,t){this[e]=this[e]||[],this[e].forEach((function(e){e.call(t&&t.context?t.context:t,t)}))}};Object.assign(e,{hooks:t,plugins:new class{constructor(e){this.jsep=e,this.registered={}}register(...e){e.forEach((e=>{if("object"!=typeof e||!e.name||!e.init)throw new Error("Invalid JSEP plugin format");this.registered[e.name]||(e.init(this.jsep),this.registered[e.name]=e)}))}}(e),COMPOUND:"Compound",SEQUENCE_EXP:"SequenceExpression",IDENTIFIER:"Identifier",MEMBER_EXP:"MemberExpression",LITERAL:"Literal",THIS_EXP:"ThisExpression",CALL_EXP:"CallExpression",UNARY_EXP:"UnaryExpression",BINARY_EXP:"BinaryExpression",ARRAY_EXP:"ArrayExpression",TAB_CODE:9,LF_CODE:10,CR_CODE:13,SPACE_CODE:32,PERIOD_CODE:46,COMMA_CODE:44,SQUOTE_CODE:39,DQUOTE_CODE:34,OPAREN_CODE:40,CPAREN_CODE:41,OBRACK_CODE:91,CBRACK_CODE:93,QUMARK_CODE:63,SEMCOL_CODE:59,COLON_CODE:58,unary_ops:{"-":1,"!":1,"~":1,"+":1},binary_ops:{"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},additional_identifier_chars:new Set(["$","_"]),literals:{true:!0,false:!1,null:null},this_str:"this"}),e.max_unop_len=e.getMaxKeyLen(e.unary_ops),e.max_binop_len=e.getMaxKeyLen(e.binary_ops);const r=t=>new e(t).parse();Object.getOwnPropertyNames(e).forEach((t=>{void 0===r[t]&&"prototype"!==t&&(r[t]=e[t])})),r.Jsep=e;const i="ConditionalExpression";var s={name:"ternary",init(e){function t(e){for(;e.left&&":"===e.left.operator;)e=e.left;return e}e.hooks.add("after-expression",(function(r){if(r.node&&this.code===e.QUMARK_CODE){this.index++;const s=r.node,n=this.gobbleExpression();if(n||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const e=this.gobbleExpression();e||this.throwError("Expected expression"),r.node={type:"ConditionalExpression",test:s,consequent:n,alternate:e}}else if(":"===s.operator){n.right||this.throwError("Expected :");const e=t(n);s.right={type:"ConditionalExpression",test:s.right,consequent:e.left,alternate:e===n?e.right:{operator:":",left:e.right,right:n.right}},r.node=s}else if(":"===n.operator)!function(e,t){e.type=i,e.test=t,e.consequent=e.left,e.alternate=e.right,delete e.operator,delete e.left,delete e.right}(t(n),s),r.node=n;else if(n.alternate){let e=n.alternate;for(;e.alternate;)e=e.alternate;r.node={type:i,test:s,consequent:n,alternate:e.right},delete e.operator,delete e.right,Object.assign(e,e.left)}else this.throwError("Expected :");if(r.node.test&&r.node.test.operator&&e.binary_ops[r.node.test.operator]<.95){const e=r.node;r.node=e.test,r.node.right={type:i,test:e.test.right,consequent:e.consequent,alternate:e.alternate}}}}))}};r.plugins.register(s);export{e as Jsep,r as default}; | ||
class e{static get version(){return"1.0.2"}static toString(){return"JavaScript Expression Parser (JSEP) v"+e.version}static addUnaryOp(t){return e.max_unop_len=Math.max(t.length,e.max_unop_len),e.unary_ops[t]=1,e}static addBinaryOp(t,r,i){return e.max_binop_len=Math.max(t.length,e.max_binop_len),e.binary_ops[t]=r,i?e.right_associative.add(t):e.right_associative.delete(t),e}static addIdentifierChar(t){return e.additional_identifier_chars.add(t),e}static addLiteral(t,r){return e.literals[t]=r,e}static removeUnaryOp(t){return delete e.unary_ops[t],t.length===e.max_unop_len&&(e.max_unop_len=e.getMaxKeyLen(e.unary_ops)),e}static removeAllUnaryOps(){return e.unary_ops={},e.max_unop_len=0,e}static removeIdentifierChar(t){return e.additional_identifier_chars.delete(t),e}static removeBinaryOp(t){return delete e.binary_ops[t],t.length===e.max_binop_len&&(e.max_binop_len=e.getMaxKeyLen(e.binary_ops)),e.right_associative.delete(t),e}static removeAllBinaryOps(){return e.binary_ops={},e.max_binop_len=0,e}static removeLiteral(t){return delete e.literals[t],e}static removeAllLiterals(){return e.literals={},e}get char(){return this.expr.charAt(this.index)}get code(){return this.expr.charCodeAt(this.index)}constructor(e){this.expr=e,this.index=0}static parse(t){return new e(t).parse()}static getMaxKeyLen(e){return Math.max(0,...Object.keys(e).map((e=>e.length)))}static isDecimalDigit(e){return e>=48&&e<=57}static binaryPrecedence(t){return e.binary_ops[t]||0}static isIdentifierStart(t){return t>=65&&t<=90||t>=97&&t<=122||t>=128&&!e.binary_ops[String.fromCharCode(t)]||e.additional_identifier_chars.has(String.fromCharCode(t))}static isIdentifierPart(t){return e.isIdentifierStart(t)||e.isDecimalDigit(t)}throwError(e){const t=new Error(e+" at character "+this.index);throw t.index=this.index,t.description=e,t}runHook(t,r){if(e.hooks[t]){const i={context:this,node:r};return e.hooks.run(t,i),i.node}return r}searchHook(t){if(e.hooks[t]){const r={context:this};return e.hooks[t].find((function(e){return e.call(r.context,r),r.node})),r.node}}gobbleSpaces(){let t=this.code;for(;t===e.SPACE_CODE||t===e.TAB_CODE||t===e.LF_CODE||t===e.CR_CODE;)t=this.expr.charCodeAt(++this.index);this.runHook("gobble-spaces")}parse(){this.runHook("before-all");const t=this.gobbleExpressions(),r=1===t.length?t[0]:{type:e.COMPOUND,body:t};return this.runHook("after-all",r)}gobbleExpressions(t){let r,i,s=[];for(;this.index<this.expr.length;)if(r=this.code,r===e.SEMCOL_CODE||r===e.COMMA_CODE)this.index++;else if(i=this.gobbleExpression())s.push(i);else if(this.index<this.expr.length){if(r===t)break;this.throwError('Unexpected "'+this.char+'"')}return s}gobbleExpression(){const e=this.searchHook("gobble-expression")||this.gobbleBinaryExpression();return this.gobbleSpaces(),this.runHook("after-expression",e)}gobbleBinaryOp(){this.gobbleSpaces();let t=this.expr.substr(this.index,e.max_binop_len),r=t.length;for(;r>0;){if(e.binary_ops.hasOwnProperty(t)&&(!e.isIdentifierStart(this.code)||this.index+t.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+t.length))))return this.index+=r,t;t=t.substr(0,--r)}return!1}gobbleBinaryExpression(){let t,r,i,s,n,o,a,h,l;if(o=this.gobbleToken(),!o)return o;if(r=this.gobbleBinaryOp(),!r)return o;for(n={value:r,prec:e.binaryPrecedence(r),right_a:e.right_associative.has(r)},a=this.gobbleToken(),a||this.throwError("Expected expression after "+r),s=[o,n,a];r=this.gobbleBinaryOp();){if(i=e.binaryPrecedence(r),0===i){this.index-=r.length;break}n={value:r,prec:i,right_a:e.right_associative.has(r)},l=r;const h=e=>n.right_a&&e.right_a?i>e.prec:i<=e.prec;for(;s.length>2&&h(s[s.length-2]);)a=s.pop(),r=s.pop().value,o=s.pop(),t={type:e.BINARY_EXP,operator:r,left:o,right:a},s.push(t);t=this.gobbleToken(),t||this.throwError("Expected expression after "+l),s.push(n,t)}for(h=s.length-1,t=s[h];h>1;)t={type:e.BINARY_EXP,operator:s[h-1].value,left:s[h-2],right:t},h-=2;return t}gobbleToken(){let t,r,i,s;if(this.gobbleSpaces(),s=this.searchHook("gobble-token"),s)return this.runHook("after-token",s);if(t=this.code,e.isDecimalDigit(t)||t===e.PERIOD_CODE)return this.gobbleNumericLiteral();if(t===e.SQUOTE_CODE||t===e.DQUOTE_CODE)s=this.gobbleStringLiteral();else if(t===e.OBRACK_CODE)s=this.gobbleArray();else{for(r=this.expr.substr(this.index,e.max_unop_len),i=r.length;i>0;){if(e.unary_ops.hasOwnProperty(r)&&(!e.isIdentifierStart(this.code)||this.index+r.length<this.expr.length&&!e.isIdentifierPart(this.expr.charCodeAt(this.index+r.length)))){this.index+=i;const t=this.gobbleToken();return t||this.throwError("missing unaryOp argument"),this.runHook("after-token",{type:e.UNARY_EXP,operator:r,argument:t,prefix:!0})}r=r.substr(0,--i)}e.isIdentifierStart(t)?(s=this.gobbleIdentifier(),e.literals.hasOwnProperty(s.name)?s={type:e.LITERAL,value:e.literals[s.name],raw:s.name}:s.name===e.this_str&&(s={type:e.THIS_EXP})):t===e.OPAREN_CODE&&(s=this.gobbleGroup())}return s?(s=this.gobbleTokenProperty(s),this.runHook("after-token",s)):this.runHook("after-token",!1)}gobbleTokenProperty(t){this.gobbleSpaces();let r=this.code;for(;r===e.PERIOD_CODE||r===e.OBRACK_CODE||r===e.OPAREN_CODE||r===e.QUMARK_CODE;){let i;if(r===e.QUMARK_CODE){if(this.expr.charCodeAt(this.index+1)!==e.PERIOD_CODE)break;i=!0,this.index+=2,this.gobbleSpaces(),r=this.code}this.index++,r===e.OBRACK_CODE?(t={type:e.MEMBER_EXP,computed:!0,object:t,property:this.gobbleExpression()},this.gobbleSpaces(),r=this.code,r!==e.CBRACK_CODE&&this.throwError("Unclosed ["),this.index++):r===e.OPAREN_CODE?t={type:e.CALL_EXP,arguments:this.gobbleArguments(e.CPAREN_CODE),callee:t}:(r===e.PERIOD_CODE||i)&&(i&&this.index--,this.gobbleSpaces(),t={type:e.MEMBER_EXP,computed:!1,object:t,property:this.gobbleIdentifier()}),i&&(t.optional=!0),this.gobbleSpaces(),r=this.code}return t}gobbleNumericLiteral(){let t,r,i="";for(;e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(this.code===e.PERIOD_CODE)for(i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);if(t=this.char,"e"===t||"E"===t){for(i+=this.expr.charAt(this.index++),t=this.char,"+"!==t&&"-"!==t||(i+=this.expr.charAt(this.index++));e.isDecimalDigit(this.code);)i+=this.expr.charAt(this.index++);e.isDecimalDigit(this.expr.charCodeAt(this.index-1))||this.throwError("Expected exponent ("+i+this.char+")")}return r=this.code,e.isIdentifierStart(r)?this.throwError("Variable names cannot start with a number ("+i+this.char+")"):(r===e.PERIOD_CODE||1===i.length&&i.charCodeAt(0)===e.PERIOD_CODE)&&this.throwError("Unexpected period"),{type:e.LITERAL,value:parseFloat(i),raw:i}}gobbleStringLiteral(){let t="";const r=this.index,i=this.expr.charAt(this.index++);let s=!1;for(;this.index<this.expr.length;){let e=this.expr.charAt(this.index++);if(e===i){s=!0;break}if("\\"===e)switch(e=this.expr.charAt(this.index++),e){case"n":t+="\n";break;case"r":t+="\r";break;case"t":t+="\t";break;case"b":t+="\b";break;case"f":t+="\f";break;case"v":t+="\v";break;default:t+=e}else t+=e}return s||this.throwError('Unclosed quote after "'+t+'"'),{type:e.LITERAL,value:t,raw:this.expr.substring(r,this.index)}}gobbleIdentifier(){let t=this.code,r=this.index;for(e.isIdentifierStart(t)?this.index++:this.throwError("Unexpected "+this.char);this.index<this.expr.length&&(t=this.code,e.isIdentifierPart(t));)this.index++;return{type:e.IDENTIFIER,name:this.expr.slice(r,this.index)}}gobbleArguments(t){const r=[];let i=!1,s=0;for(;this.index<this.expr.length;){this.gobbleSpaces();let n=this.code;if(n===t){i=!0,this.index++,t===e.CPAREN_CODE&&s&&s>=r.length&&this.throwError("Unexpected token "+String.fromCharCode(t));break}if(n===e.COMMA_CODE){if(this.index++,s++,s!==r.length)if(t===e.CPAREN_CODE)this.throwError("Unexpected token ,");else if(t===e.CBRACK_CODE)for(let e=r.length;e<s;e++)r.push(null)}else if(r.length!==s&&0!==s)this.throwError("Expected comma");else{const t=this.gobbleExpression();t&&t.type!==e.COMPOUND||this.throwError("Expected comma"),r.push(t)}}return i||this.throwError("Expected "+String.fromCharCode(t)),r}gobbleGroup(){this.index++;let t=this.gobbleExpressions(e.CPAREN_CODE);if(this.code===e.CPAREN_CODE)return this.index++,1===t.length?t[0]:!!t.length&&{type:e.SEQUENCE_EXP,expressions:t};this.throwError("Unclosed (")}gobbleArray(){return this.index++,{type:e.ARRAY_EXP,elements:this.gobbleArguments(e.CBRACK_CODE)}}}const t=new class{add(e,t,r){if("string"!=typeof arguments[0])for(let e in arguments[0])this.add(e,arguments[0][e],arguments[1]);else(Array.isArray(e)?e:[e]).forEach((function(e){this[e]=this[e]||[],t&&this[e][r?"unshift":"push"](t)}),this)}run(e,t){this[e]=this[e]||[],this[e].forEach((function(e){e.call(t&&t.context?t.context:t,t)}))}};Object.assign(e,{hooks:t,plugins:new class{constructor(e){this.jsep=e,this.registered={}}register(...e){e.forEach((e=>{if("object"!=typeof e||!e.name||!e.init)throw new Error("Invalid JSEP plugin format");this.registered[e.name]||(e.init(this.jsep),this.registered[e.name]=e)}))}}(e),COMPOUND:"Compound",SEQUENCE_EXP:"SequenceExpression",IDENTIFIER:"Identifier",MEMBER_EXP:"MemberExpression",LITERAL:"Literal",THIS_EXP:"ThisExpression",CALL_EXP:"CallExpression",UNARY_EXP:"UnaryExpression",BINARY_EXP:"BinaryExpression",ARRAY_EXP:"ArrayExpression",TAB_CODE:9,LF_CODE:10,CR_CODE:13,SPACE_CODE:32,PERIOD_CODE:46,COMMA_CODE:44,SQUOTE_CODE:39,DQUOTE_CODE:34,OPAREN_CODE:40,CPAREN_CODE:41,OBRACK_CODE:91,CBRACK_CODE:93,QUMARK_CODE:63,SEMCOL_CODE:59,COLON_CODE:58,unary_ops:{"-":1,"!":1,"~":1,"+":1},binary_ops:{"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},right_associative:new Set,additional_identifier_chars:new Set(["$","_"]),literals:{true:!0,false:!1,null:null},this_str:"this"}),e.max_unop_len=e.getMaxKeyLen(e.unary_ops),e.max_binop_len=e.getMaxKeyLen(e.binary_ops);const r=t=>new e(t).parse();Object.getOwnPropertyNames(e).forEach((t=>{void 0===r[t]&&"prototype"!==t&&(r[t]=e[t])})),r.Jsep=e;const i="ConditionalExpression";var s={name:"ternary",init(e){function t(e){for(;e.left&&":"===e.left.operator;)e=e.left;return e}e.hooks.add("after-expression",(function(r){if(r.node&&this.code===e.QUMARK_CODE){this.index++;const s=r.node,n=this.gobbleExpression();if(n||this.throwError("Expected expression"),this.gobbleSpaces(),this.code===e.COLON_CODE){this.index++;const e=this.gobbleExpression();e||this.throwError("Expected expression"),r.node={type:"ConditionalExpression",test:s,consequent:n,alternate:e}}else if(":"===s.operator){n.right||this.throwError("Expected :");const e=t(n);s.right={type:"ConditionalExpression",test:s.right,consequent:e.left,alternate:e===n?e.right:{operator:":",left:e.right,right:n.right}},r.node=s}else if(":"===n.operator)!function(e,t){e.type=i,e.test=t,e.consequent=e.left,e.alternate=e.right,delete e.operator,delete e.left,delete e.right}(t(n),s),r.node=n;else if(n.alternate){let e=n.alternate;for(;e.alternate;)e=e.alternate;r.node={type:i,test:s,consequent:n,alternate:e.right},delete e.operator,delete e.right,Object.assign(e,e.left)}else this.throwError("Expected :");if(r.node.test&&r.node.test.operator&&e.binary_ops[r.node.test.operator]<.95){const e=r.node;r.node=e.test,r.node.right={type:i,test:e.test.right,consequent:e.consequent,alternate:e.alternate}}}}))}};r.plugins.register(s);export{e as Jsep,r as default}; | ||
//# sourceMappingURL=jsep.min.js.map |
{ | ||
"name": "jsep", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "a tiny JavaScript expression parser", | ||
@@ -58,2 +58,3 @@ "author": "Stephen Oney <swloney@gmail.com> (http://from.so/)", | ||
"src/", | ||
"packages/ternary/src/", | ||
"types", | ||
@@ -60,0 +61,0 @@ "typings/", |
@@ -64,4 +64,8 @@ ## jsep: A Tiny JavaScript Expression Parser | ||
// Add a custom ^ binary operator with precedence 10 | ||
// (Note that higher number = higher precedence) | ||
jsep.addBinaryOp("^", 10); | ||
// Add exponentiation operator (right-to-left) | ||
jsep.addBinaryOp('**', 11, true); | ||
// Add a custom @ unary operator | ||
@@ -68,0 +72,0 @@ jsep.addUnaryOp('@'); |
@@ -139,2 +139,3 @@ declare module 'jsep' { | ||
let binary_ops: { [op: string]: number }; | ||
let right_associative: Set<string>; | ||
let additional_identifier_chars: Set<string>; | ||
@@ -144,3 +145,3 @@ let literals: { [literal: string]: any }; | ||
function addBinaryOp(operatorName: string, precedence: number): void; | ||
function addBinaryOp(operatorName: string, precedence: number, rightToLeft?: boolean): void; | ||
@@ -147,0 +148,0 @@ function addUnaryOp(operatorName: string): void; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
415385
3429
195