Comparing version 0.3.5 to 0.4.0
@@ -1,2 +0,2 @@ | ||
// JavaScript Expression Parser (JSEP) 0.3.5 | ||
// JavaScript Expression Parser (JSEP) 0.4.0 | ||
// JSEP may be freely distributed under the MIT License | ||
@@ -63,2 +63,4 @@ // https://ericsmekens.github.io/jsep/ | ||
}, | ||
// Additional valid identifier chars, apart from a-z, A-Z and 0-9 (except on the starting char) | ||
additional_identifier_chars = {'$': t, '_': t}, | ||
// Get return the longest key length of any object | ||
@@ -106,13 +108,13 @@ getMaxKeyLen = function(obj) { | ||
isIdentifierStart = function(ch) { | ||
return (ch === 36) || (ch === 95) || // `$` and `_` | ||
(ch >= 65 && ch <= 90) || // A...Z | ||
return (ch >= 65 && ch <= 90) || // A...Z | ||
(ch >= 97 && ch <= 122) || // a...z | ||
(ch >= 128 && !binary_ops[String.fromCharCode(ch)]); // any non-ASCII that is not an operator | ||
(ch >= 128 && !binary_ops[String.fromCharCode(ch)]) || // any non-ASCII that is not an operator | ||
(additional_identifier_chars.hasOwnProperty(String.fromCharCode(ch))); // additional characters | ||
}, | ||
isIdentifierPart = function(ch) { | ||
return (ch === 36) || (ch === 95) || // `$` and `_` | ||
(ch >= 65 && ch <= 90) || // A...Z | ||
return (ch >= 65 && ch <= 90) || // A...Z | ||
(ch >= 97 && ch <= 122) || // a...z | ||
(ch >= 48 && ch <= 57) || // 0...9 | ||
(ch >= 128 && !binary_ops[String.fromCharCode(ch)]); // any non-ASCII that is not an operator | ||
(ch >= 128 && !binary_ops[String.fromCharCode(ch)])|| // any non-ASCII that is not an operator | ||
(additional_identifier_chars.hasOwnProperty(String.fromCharCode(ch))); // additional characters | ||
}, | ||
@@ -147,2 +149,3 @@ | ||
gobbleSpaces(); | ||
if(exprICode(index) === QUMARK_CODE) { | ||
@@ -262,3 +265,3 @@ // Ternary expression: test ? consequent : alternate | ||
gobbleToken = function() { | ||
var ch, to_check, tc_len; | ||
var ch, to_check, tc_len, node; | ||
@@ -271,7 +274,9 @@ gobbleSpaces(); | ||
return gobbleNumericLiteral(); | ||
} else if(ch === SQUOTE_CODE || ch === DQUOTE_CODE) { | ||
} | ||
if(ch === SQUOTE_CODE || ch === DQUOTE_CODE) { | ||
// Single or double quotes | ||
return gobbleStringLiteral(); | ||
node = gobbleStringLiteral(); | ||
} else if (ch === OBRACK_CODE) { | ||
return gobbleArray(); | ||
node = gobbleArray(); | ||
} else { | ||
@@ -299,9 +304,58 @@ to_check = expr.substr(index, max_unop_len); | ||
if (isIdentifierStart(ch) || ch === OPAREN_CODE) { // open parenthesis | ||
// `foo`, `bar.baz` | ||
return gobbleVariable(); | ||
if (isIdentifierStart(ch)) { | ||
node = gobbleIdentifier(); | ||
} else if (ch === OPAREN_CODE) { // open parenthesis | ||
node = gobbleGroup(); | ||
} | ||
} | ||
return false; | ||
if (!node) { | ||
return false; | ||
} | ||
gobbleSpaces(); | ||
ch = exprICode(index); | ||
// Gobble properties of of identifiers/strings/arrays/groups. | ||
// e.g. `foo`, `bar.baz`, `foo['bar'].baz` | ||
// It also gobbles function calls: | ||
// e.g. `Math.acos(obj.angle)` | ||
while(ch === PERIOD_CODE || ch === OBRACK_CODE || ch === OPAREN_CODE) { | ||
index++; | ||
if(ch === PERIOD_CODE) { | ||
gobbleSpaces(); | ||
node = { | ||
type: MEMBER_EXP, | ||
computed: false, | ||
object: node, | ||
property: gobbleIdentifier() | ||
}; | ||
} else if(ch === OBRACK_CODE) { | ||
node = { | ||
type: MEMBER_EXP, | ||
computed: true, | ||
object: node, | ||
property: gobbleExpression() | ||
}; | ||
gobbleSpaces(); | ||
ch = exprICode(index); | ||
if(ch !== CBRACK_CODE) { | ||
throwError('Unclosed [', index); | ||
} | ||
index++; | ||
} else if(ch === OPAREN_CODE) { | ||
// A function call is being made; gobble all the arguments | ||
node = { | ||
type: CALL_EXP, | ||
'arguments': gobbleArguments(CPAREN_CODE), | ||
callee: node | ||
}; | ||
} | ||
gobbleSpaces(); | ||
ch = exprICode(index); | ||
} | ||
return node; | ||
}, | ||
@@ -478,54 +532,2 @@ // Parse simple numeric literals: `12`, `3.4`, `.5`. Do this by using a string to | ||
// Gobble a non-literal variable name. This variable name may include properties | ||
// e.g. `foo`, `bar.baz`, `foo['bar'].baz` | ||
// It also gobbles function calls: | ||
// e.g. `Math.acos(obj.angle)` | ||
gobbleVariable = function() { | ||
var ch_i, node; | ||
ch_i = exprICode(index); | ||
if(ch_i === OPAREN_CODE) { | ||
node = gobbleGroup(); | ||
} else { | ||
node = gobbleIdentifier(); | ||
} | ||
gobbleSpaces(); | ||
ch_i = exprICode(index); | ||
while(ch_i === PERIOD_CODE || ch_i === OBRACK_CODE || ch_i === OPAREN_CODE) { | ||
index++; | ||
if(ch_i === PERIOD_CODE) { | ||
gobbleSpaces(); | ||
node = { | ||
type: MEMBER_EXP, | ||
computed: false, | ||
object: node, | ||
property: gobbleIdentifier() | ||
}; | ||
} else if(ch_i === OBRACK_CODE) { | ||
node = { | ||
type: MEMBER_EXP, | ||
computed: true, | ||
object: node, | ||
property: gobbleExpression() | ||
}; | ||
gobbleSpaces(); | ||
ch_i = exprICode(index); | ||
if(ch_i !== CBRACK_CODE) { | ||
throwError('Unclosed [', index); | ||
} | ||
index++; | ||
} else if(ch_i === OPAREN_CODE) { | ||
// A function call is being made; gobble all the arguments | ||
node = { | ||
type: CALL_EXP, | ||
'arguments': gobbleArguments(CPAREN_CODE), | ||
callee: node | ||
}; | ||
} | ||
gobbleSpaces(); | ||
ch_i = exprICode(index); | ||
} | ||
return node; | ||
}, | ||
// Responsible for parsing a group of things within parentheses `()` | ||
@@ -592,3 +594,3 @@ // This function assumes that it needs to gobble the opening parenthesis | ||
// To be filled in by the template | ||
jsep.version = '0.3.5'; | ||
jsep.version = '0.4.0'; | ||
jsep.toString = function() { return 'JavaScript Expression Parser (JSEP) v' + jsep.version; }; | ||
@@ -619,2 +621,11 @@ | ||
/** | ||
* @method jsep.addIdentifierChar | ||
* @param {string} char The additional character to treat as a valid part of an identifier | ||
* @return jsep | ||
*/ | ||
jsep.addIdentifierChar = function(char) { | ||
additional_identifier_chars[char] = t; return this; | ||
}; | ||
/** | ||
* @method jsep.addLiteral | ||
@@ -655,2 +666,13 @@ * @param {string} literal_name The name of the literal to add | ||
/** | ||
* @method jsep.removeIdentifierChar | ||
* @param {string} char The additional character to stop treating as a valid part of an identifier | ||
* @return jsep | ||
*/ | ||
jsep.removeIdentifierChar = function(char) { | ||
delete additional_identifier_chars[char]; | ||
return this; | ||
}; | ||
/** | ||
* @method jsep.removeBinaryOp | ||
@@ -657,0 +679,0 @@ * @param {string} op_name The name of the binary op to remove |
@@ -1,3 +0,3 @@ | ||
/* jsep v0.3.5 (https://ericsmekens.github.io/jsep/) */ | ||
!function(e){"use strict";function C(e,r){var t=new Error(e+" at character "+r);throw t.index=r,t.description=e,t}function r(e){var r,t=0;for(var n in e)(r=n.length)>t&&e.hasOwnProperty(n)&&(t=r);return t}function U(e){return B[e]||0}function w(e,r,t){return{type:"||"===e||"&&"===e?"LogicalExpression":"BinaryExpression",operator:e,left:r,right:t}}function k(e){return 48<=e&&e<=57}function O(e){return 36===e||95===e||65<=e&&e<=90||97<=e&&e<=122||128<=e&&!B[String.fromCharCode(e)]}function S(e){return 36===e||95===e||65<=e&&e<=90||97<=e&&e<=122||48<=e&&e<=57||128<=e&&!B[String.fromCharCode(e)]}function t(n){for(var e,r,p=0,t=n.charAt,o=n.charCodeAt,i=function(e){return t.call(n,e)},a=function(e){return o.call(n,e)},s=n.length,f=function(){for(var e=a(p);32===e||9===e||10===e||13===e;)e=a(++p)},c=function(){var e,r,t=u();return f(),63!==a(p)?t:(p++,(e=c())||C("Expected expression",p),f(),58===a(p)?(p++,(r=c())||C("Expected expression",p),{type:"ConditionalExpression",test:t,consequent:e,alternate:r}):void C("Expected :",p))},l=function(){f();for(var e=n.substr(p,q),r=e.length;0<r;){if(B.hasOwnProperty(e)&&(!O(a(p))||p+e.length<n.length&&!S(a(p+e.length))))return p+=r,e;e=e.substr(0,--r)}return!1},u=function(){var e,r,t,n,o,i,u,a=h(),s=l();if(!s)return a;for(n={value:s,prec:U(s)},(o=h())||C("Expected expression after "+s,p),t=[a,n,o];(s=l())&&0!==(r=U(s));){for(n={value:s,prec:r},u=s;2<t.length&&r<=t[t.length-2].prec;)o=t.pop(),s=t.pop().value,a=t.pop(),e=w(s,a,o),t.push(e);(e=h())||C("Expected expression after "+u,p),t.push(n,e)}for(e=t[i=t.length-1];1<i;)e=w(t[i-1].value,t[i-2],e),i-=2;return e},h=function(){var e,r,t;if(f(),e=a(p),k(e)||46===e)return d();if(39===e||34===e)return v();if(91===e)return b();for(t=(r=n.substr(p,M)).length;0<t;){if(L.hasOwnProperty(r)&&(!O(a(p))||p+r.length<n.length&&!S(a(p+r.length))))return p+=t,{type:"UnaryExpression",operator:r,argument:h(),prefix:!0};r=r.substr(0,--t)}return!(!O(e)&&40!==e)&&g()},d=function(){for(var e,r,t="";k(a(p));)t+=i(p++);if(46===a(p))for(t+=i(p++);k(a(p));)t+=i(p++);if("e"===(e=i(p))||"E"===e){for(t+=i(p++),"+"!==(e=i(p))&&"-"!==e||(t+=i(p++));k(a(p));)t+=i(p++);k(a(p-1))||C("Expected exponent ("+t+i(p)+")",p)}return r=a(p),O(r)?C("Variable names cannot start with a number ("+t+i(p)+")",p):46===r&&C("Unexpected period",p),{type:P,value:parseFloat(t),raw:t}},v=function(){for(var e,r="",t=i(p++),n=!1;p<s;){if((e=i(p++))===t){n=!0;break}if("\\"===e)switch(e=i(p++)){case"n":r+="\n";break;case"r":r+="\r";break;case"t":r+="\t";break;case"b":r+="\b";break;case"f":r+="\f";break;case"v":r+="\v";break;default:r+=e}else r+=e}return n||C('Unclosed quote after "'+r+'"',p),{type:P,value:r,raw:t+r+t}},x=function(){var e,r=a(p),t=p;for(O(r)?p++:C("Unexpected "+i(p),p);p<s&&(r=a(p),S(r));)p++;return e=n.slice(t,p),J.hasOwnProperty(e)?{type:P,value:J[e],raw:e}:"this"===e?{type:"ThisExpression"}:{type:"Identifier",name:e}},y=function(e){for(var r,t,n=[],o=!1,i=0;p<s;){if(f(),(r=a(p))===e){o=!0,p++,41===e&&i&&i>=n.length&&C("Unexpected token "+String.fromCharCode(e),p);break}if(44===r){if(p++,++i!==n.length)if(41===e)C("Unexpected token ,",p);else if(93===e)for(var u=n.length;u<i;u++)n.push(null)}else(t=c())&&t.type!==j||C("Expected comma",p),n.push(t)}return o||C("Expected "+String.fromCharCode(e),p),n},g=function(){var e=a(p),r=(40===e?m:x)();for(f(),e=a(p);46===e||91===e||40===e;)p++,46===e?(f(),r={type:A,computed:!1,object:r,property:x()}):91===e?(r={type:A,computed:!0,object:r,property:c()},f(),93!==(e=a(p))&&C("Unclosed [",p),p++):40===e&&(r={type:"CallExpression",arguments:y(41),callee:r}),f(),e=a(p);return r},m=function(){p++;var e=c();if(f(),41===a(p))return p++,e;C("Unclosed (",p)},b=function(){return p++,{type:"ArrayExpression",elements:y(93)}},E=[];p<s;)59===(e=a(p))||44===e?p++:(r=c())?E.push(r):p<s&&C('Unexpected "'+i(p)+'"',p);return 1===E.length?E[0]:{type:j,body:E}}var n,j="Compound",A="MemberExpression",P="Literal",L={"-":!0,"!":!0,"~":!0,"+":!0},B={"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},M=r(L),q=r(B),J={true:!0,false:!1,null:null};t.version="0.3.5",t.toString=function(){return"JavaScript Expression Parser (JSEP) v"+t.version},t.addUnaryOp=function(e){return M=Math.max(e.length,M),L[e]=!0,this},t.addBinaryOp=function(e,r){return q=Math.max(e.length,q),B[e]=r,this},t.addLiteral=function(e,r){return J[e]=r,this},t.removeUnaryOp=function(e){return delete L[e],e.length===M&&(M=r(L)),this},t.removeAllUnaryOps=function(){return L={},M=0,this},t.removeBinaryOp=function(e){return delete B[e],e.length===q&&(q=r(B)),this},t.removeAllBinaryOps=function(){return B={},q=0,this},t.removeLiteral=function(e){return delete J[e],this},t.removeAllLiterals=function(){return J={},this},"undefined"==typeof exports?(n=e.jsep,(e.jsep=t).noConflict=function(){return e.jsep===t&&(e.jsep=n),t}):"undefined"!=typeof module&&module.exports?exports=module.exports=t:exports.parse=t}(this); | ||
/* jsep v0.4.0 (https://ericsmekens.github.io/jsep/) */ | ||
!function(e){"use strict";function E(e,r){var t=new Error(e+" at character "+r);throw t.index=r,t.description=e,t}function r(e){var r,t=0;for(var n in e)(r=n.length)>t&&e.hasOwnProperty(n)&&(t=r);return t}function C(e){return L[e]||0}function w(e,r,t){return{type:"||"===e||"&&"===e?"LogicalExpression":"BinaryExpression",operator:e,left:r,right:t}}function O(e){return 48<=e&&e<=57}function U(e){return 65<=e&&e<=90||97<=e&&e<=122||128<=e&&!L[String.fromCharCode(e)]||i.hasOwnProperty(String.fromCharCode(e))}function k(e){return 65<=e&&e<=90||97<=e&&e<=122||48<=e&&e<=57||128<=e&&!L[String.fromCharCode(e)]||i.hasOwnProperty(String.fromCharCode(e))}function t(o){for(var e,r,p=0,t=o.charAt,n=o.charCodeAt,i=function(e){return t.call(o,e)},u=function(e){return n.call(o,e)},s=o.length,f=function(){for(var e=u(p);32===e||9===e||10===e||13===e;)e=u(++p)},c=function(){var e,r,t=a();return f(),63!==u(p)?t:(p++,(e=c())||E("Expected expression",p),f(),58===u(p)?(p++,(r=c())||E("Expected expression",p),{type:"ConditionalExpression",test:t,consequent:e,alternate:r}):void E("Expected :",p))},l=function(){f();for(var e=o.substr(p,I),r=e.length;0<r;){if(L.hasOwnProperty(e)&&(!U(u(p))||p+e.length<o.length&&!k(u(p+e.length))))return p+=r,e;e=e.substr(0,--r)}return!1},a=function(){var e,r,t,n,o,i,a,u=h(),s=l();if(!s)return u;for(n={value:s,prec:C(s)},(o=h())||E("Expected expression after "+s,p),t=[u,n,o];(s=l())&&0!==(r=C(s));){for(n={value:s,prec:r},a=s;2<t.length&&r<=t[t.length-2].prec;)o=t.pop(),s=t.pop().value,u=t.pop(),e=w(s,u,o),t.push(e);(e=h())||E("Expected expression after "+a,p),t.push(n,e)}for(e=t[i=t.length-1];1<i;)e=w(t[i-1].value,t[i-2],e),i-=2;return e},h=function(){var e,r,t,n;if(f(),e=u(p),O(e)||46===e)return d();if(39===e||34===e)n=v();else if(91===e)n=m();else{for(t=(r=o.substr(p,B)).length;0<t;){if(A.hasOwnProperty(r)&&(!U(u(p))||p+r.length<o.length&&!k(u(p+r.length))))return p+=t,{type:"UnaryExpression",operator:r,argument:h(),prefix:!0};r=r.substr(0,--t)}U(e)?n=x():40===e&&(n=g())}if(!n)return!1;for(f(),e=u(p);46===e||91===e||40===e;)p++,46===e?(f(),n={type:P,computed:!1,object:n,property:x()}):91===e?(n={type:P,computed:!0,object:n,property:c()},f(),93!==(e=u(p))&&E("Unclosed [",p),p++):40===e&&(n={type:"CallExpression",arguments:y(41),callee:n}),f(),e=u(p);return n},d=function(){for(var e,r,t="";O(u(p));)t+=i(p++);if(46===u(p))for(t+=i(p++);O(u(p));)t+=i(p++);if("e"===(e=i(p))||"E"===e){for(t+=i(p++),"+"!==(e=i(p))&&"-"!==e||(t+=i(p++));O(u(p));)t+=i(p++);O(u(p-1))||E("Expected exponent ("+t+i(p)+")",p)}return r=u(p),U(r)?E("Variable names cannot start with a number ("+t+i(p)+")",p):46===r&&E("Unexpected period",p),{type:j,value:parseFloat(t),raw:t}},v=function(){for(var e,r="",t=i(p++),n=!1;p<s;){if((e=i(p++))===t){n=!0;break}if("\\"===e)switch(e=i(p++)){case"n":r+="\n";break;case"r":r+="\r";break;case"t":r+="\t";break;case"b":r+="\b";break;case"f":r+="\f";break;case"v":r+="\v";break;default:r+=e}else r+=e}return n||E('Unclosed quote after "'+r+'"',p),{type:j,value:r,raw:t+r+t}},x=function(){var e,r=u(p),t=p;for(U(r)?p++:E("Unexpected "+i(p),p);p<s&&(r=u(p),k(r));)p++;return e=o.slice(t,p),M.hasOwnProperty(e)?{type:j,value:M[e],raw:e}:"this"===e?{type:"ThisExpression"}:{type:"Identifier",name:e}},y=function(e){for(var r,t,n=[],o=!1,i=0;p<s;){if(f(),(r=u(p))===e){o=!0,p++,41===e&&i&&i>=n.length&&E("Unexpected token "+String.fromCharCode(e),p);break}if(44===r){if(p++,++i!==n.length)if(41===e)E("Unexpected token ,",p);else if(93===e)for(var a=n.length;a<i;a++)n.push(null)}else(t=c())&&t.type!==S||E("Expected comma",p),n.push(t)}return o||E("Expected "+String.fromCharCode(e),p),n},g=function(){p++;var e=c();if(f(),41===u(p))return p++,e;E("Unclosed (",p)},m=function(){return p++,{type:"ArrayExpression",elements:y(93)}},b=[];p<s;)59===(e=u(p))||44===e?p++:(r=c())?b.push(r):p<s&&E('Unexpected "'+i(p)+'"',p);return 1===b.length?b[0]:{type:S,body:b}}var n,S="Compound",P="MemberExpression",j="Literal",o=!0,A={"-":o,"!":o,"~":o,"+":o},L={"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":10,"/":10,"%":10},i={$:o,_:o},B=r(A),I=r(L),M={true:!0,false:!1,null:null};t.version="0.4.0",t.toString=function(){return"JavaScript Expression Parser (JSEP) v"+t.version},t.addUnaryOp=function(e){return B=Math.max(e.length,B),A[e]=o,this},t.addBinaryOp=function(e,r){return I=Math.max(e.length,I),L[e]=r,this},t.addIdentifierChar=function(e){return i[e]=o,this},t.addLiteral=function(e,r){return M[e]=r,this},t.removeUnaryOp=function(e){return delete A[e],e.length===B&&(B=r(A)),this},t.removeAllUnaryOps=function(){return A={},B=0,this},t.removeIdentifierChar=function(e){return delete i[e],this},t.removeBinaryOp=function(e){return delete L[e],e.length===I&&(I=r(L)),this},t.removeAllBinaryOps=function(){return L={},I=0,this},t.removeLiteral=function(e){return delete M[e],this},t.removeAllLiterals=function(){return M={},this},"undefined"==typeof exports?(n=e.jsep,(e.jsep=t).noConflict=function(){return e.jsep===t&&(e.jsep=n),t}):"undefined"!=typeof module&&module.exports?exports=module.exports=t:exports.parse=t}(this); | ||
//# sourceMappingURL=jsep.min.js.map |
@@ -0,1 +1,9 @@ | ||
## 0.4.0 - 2021-03-21 | ||
### Added | ||
- You can add or remove additional valid identifier chars. | ||
- Support for gobble properties from array/strings. e.g. (`[1].length`) | ||
### Updated | ||
- Updated several dependancies for audit fixes. | ||
## 0.3.5 - 2018-08-23 | ||
@@ -2,0 +10,0 @@ ### Updated |
{ | ||
"name": "jsep", | ||
"version": "0.3.5", | ||
"version": "0.4.0", | ||
"description": "a tiny JavaScript expression parser", | ||
@@ -22,3 +22,3 @@ "author": "Stephen Oney <swloney@gmail.com> (http://from.so/)", | ||
"grunt": "~1.3.0", | ||
"grunt-contrib-jshint": "~2.1.0", | ||
"grunt-contrib-jshint": "~3.0.0", | ||
"grunt-contrib-qunit": "~4.0.0", | ||
@@ -28,3 +28,3 @@ "grunt-contrib-uglify": "~5.0.0", | ||
"grunt-contrib-watch": "~1.1.0", | ||
"grunt-contrib-compress": "~1.6.0", | ||
"grunt-contrib-compress": "~2.0.0", | ||
"grunt-contrib-clean": "~2.0.0", | ||
@@ -34,3 +34,3 @@ "grunt-docco": "~0.5.0" | ||
"engines": { | ||
"node": ">= 6.0.0" | ||
"node": ">= 10.16.0" | ||
}, | ||
@@ -37,0 +37,0 @@ "directories": { |
@@ -47,2 +47,12 @@ ## jsep: A Tiny JavaScript Expression Parser | ||
#### Custom Identifiers | ||
You can add or remove additional valid identifier chars. ('_' and '$' are already treated like this.) | ||
```javascript | ||
// Add a custom @ identifier | ||
jsep.addIdentifierChar("@"); | ||
// Removes a custom @ identifier | ||
jsep.removeIdentifierChar('@'); | ||
``` | ||
### License | ||
@@ -49,0 +59,0 @@ jsep is under the MIT license. See LICENSE file. |
154
src/jsep.js
@@ -63,2 +63,4 @@ // JavaScript Expression Parser (JSEP) <%= version %> | ||
}, | ||
// Additional valid identifier chars, apart from a-z, A-Z and 0-9 (except on the starting char) | ||
additional_identifier_chars = {'$': t, '_': t}, | ||
// Get return the longest key length of any object | ||
@@ -106,13 +108,13 @@ getMaxKeyLen = function(obj) { | ||
isIdentifierStart = function(ch) { | ||
return (ch === 36) || (ch === 95) || // `$` and `_` | ||
(ch >= 65 && ch <= 90) || // A...Z | ||
return (ch >= 65 && ch <= 90) || // A...Z | ||
(ch >= 97 && ch <= 122) || // a...z | ||
(ch >= 128 && !binary_ops[String.fromCharCode(ch)]); // any non-ASCII that is not an operator | ||
(ch >= 128 && !binary_ops[String.fromCharCode(ch)]) || // any non-ASCII that is not an operator | ||
(additional_identifier_chars.hasOwnProperty(String.fromCharCode(ch))); // additional characters | ||
}, | ||
isIdentifierPart = function(ch) { | ||
return (ch === 36) || (ch === 95) || // `$` and `_` | ||
(ch >= 65 && ch <= 90) || // A...Z | ||
return (ch >= 65 && ch <= 90) || // A...Z | ||
(ch >= 97 && ch <= 122) || // a...z | ||
(ch >= 48 && ch <= 57) || // 0...9 | ||
(ch >= 128 && !binary_ops[String.fromCharCode(ch)]); // any non-ASCII that is not an operator | ||
(ch >= 128 && !binary_ops[String.fromCharCode(ch)])|| // any non-ASCII that is not an operator | ||
(additional_identifier_chars.hasOwnProperty(String.fromCharCode(ch))); // additional characters | ||
}, | ||
@@ -147,2 +149,3 @@ | ||
gobbleSpaces(); | ||
if(exprICode(index) === QUMARK_CODE) { | ||
@@ -262,3 +265,3 @@ // Ternary expression: test ? consequent : alternate | ||
gobbleToken = function() { | ||
var ch, to_check, tc_len; | ||
var ch, to_check, tc_len, node; | ||
@@ -271,7 +274,9 @@ gobbleSpaces(); | ||
return gobbleNumericLiteral(); | ||
} else if(ch === SQUOTE_CODE || ch === DQUOTE_CODE) { | ||
} | ||
if(ch === SQUOTE_CODE || ch === DQUOTE_CODE) { | ||
// Single or double quotes | ||
return gobbleStringLiteral(); | ||
node = gobbleStringLiteral(); | ||
} else if (ch === OBRACK_CODE) { | ||
return gobbleArray(); | ||
node = gobbleArray(); | ||
} else { | ||
@@ -299,9 +304,58 @@ to_check = expr.substr(index, max_unop_len); | ||
if (isIdentifierStart(ch) || ch === OPAREN_CODE) { // open parenthesis | ||
// `foo`, `bar.baz` | ||
return gobbleVariable(); | ||
if (isIdentifierStart(ch)) { | ||
node = gobbleIdentifier(); | ||
} else if (ch === OPAREN_CODE) { // open parenthesis | ||
node = gobbleGroup(); | ||
} | ||
} | ||
return false; | ||
if (!node) { | ||
return false; | ||
} | ||
gobbleSpaces(); | ||
ch = exprICode(index); | ||
// Gobble properties of of identifiers/strings/arrays/groups. | ||
// e.g. `foo`, `bar.baz`, `foo['bar'].baz` | ||
// It also gobbles function calls: | ||
// e.g. `Math.acos(obj.angle)` | ||
while(ch === PERIOD_CODE || ch === OBRACK_CODE || ch === OPAREN_CODE) { | ||
index++; | ||
if(ch === PERIOD_CODE) { | ||
gobbleSpaces(); | ||
node = { | ||
type: MEMBER_EXP, | ||
computed: false, | ||
object: node, | ||
property: gobbleIdentifier() | ||
}; | ||
} else if(ch === OBRACK_CODE) { | ||
node = { | ||
type: MEMBER_EXP, | ||
computed: true, | ||
object: node, | ||
property: gobbleExpression() | ||
}; | ||
gobbleSpaces(); | ||
ch = exprICode(index); | ||
if(ch !== CBRACK_CODE) { | ||
throwError('Unclosed [', index); | ||
} | ||
index++; | ||
} else if(ch === OPAREN_CODE) { | ||
// A function call is being made; gobble all the arguments | ||
node = { | ||
type: CALL_EXP, | ||
'arguments': gobbleArguments(CPAREN_CODE), | ||
callee: node | ||
}; | ||
} | ||
gobbleSpaces(); | ||
ch = exprICode(index); | ||
} | ||
return node; | ||
}, | ||
@@ -478,54 +532,2 @@ // Parse simple numeric literals: `12`, `3.4`, `.5`. Do this by using a string to | ||
// Gobble a non-literal variable name. This variable name may include properties | ||
// e.g. `foo`, `bar.baz`, `foo['bar'].baz` | ||
// It also gobbles function calls: | ||
// e.g. `Math.acos(obj.angle)` | ||
gobbleVariable = function() { | ||
var ch_i, node; | ||
ch_i = exprICode(index); | ||
if(ch_i === OPAREN_CODE) { | ||
node = gobbleGroup(); | ||
} else { | ||
node = gobbleIdentifier(); | ||
} | ||
gobbleSpaces(); | ||
ch_i = exprICode(index); | ||
while(ch_i === PERIOD_CODE || ch_i === OBRACK_CODE || ch_i === OPAREN_CODE) { | ||
index++; | ||
if(ch_i === PERIOD_CODE) { | ||
gobbleSpaces(); | ||
node = { | ||
type: MEMBER_EXP, | ||
computed: false, | ||
object: node, | ||
property: gobbleIdentifier() | ||
}; | ||
} else if(ch_i === OBRACK_CODE) { | ||
node = { | ||
type: MEMBER_EXP, | ||
computed: true, | ||
object: node, | ||
property: gobbleExpression() | ||
}; | ||
gobbleSpaces(); | ||
ch_i = exprICode(index); | ||
if(ch_i !== CBRACK_CODE) { | ||
throwError('Unclosed [', index); | ||
} | ||
index++; | ||
} else if(ch_i === OPAREN_CODE) { | ||
// A function call is being made; gobble all the arguments | ||
node = { | ||
type: CALL_EXP, | ||
'arguments': gobbleArguments(CPAREN_CODE), | ||
callee: node | ||
}; | ||
} | ||
gobbleSpaces(); | ||
ch_i = exprICode(index); | ||
} | ||
return node; | ||
}, | ||
// Responsible for parsing a group of things within parentheses `()` | ||
@@ -618,2 +620,11 @@ // This function assumes that it needs to gobble the opening parenthesis | ||
/** | ||
* @method jsep.addIdentifierChar | ||
* @param {string} char The additional character to treat as a valid part of an identifier | ||
* @return jsep | ||
*/ | ||
jsep.addIdentifierChar = function(char) { | ||
additional_identifier_chars[char] = t; return this; | ||
}; | ||
/** | ||
* @method jsep.addLiteral | ||
@@ -654,2 +665,13 @@ * @param {string} literal_name The name of the literal to add | ||
/** | ||
* @method jsep.removeIdentifierChar | ||
* @param {string} char The additional character to stop treating as a valid part of an identifier | ||
* @return jsep | ||
*/ | ||
jsep.removeIdentifierChar = function(char) { | ||
delete additional_identifier_chars[char]; | ||
return this; | ||
}; | ||
/** | ||
* @method jsep.removeBinaryOp | ||
@@ -656,0 +678,0 @@ * @param {string} op_name The name of the binary op to remove |
@@ -84,2 +84,6 @@ declare module 'jsep' { | ||
function addIdentifierChar(identifierName: string): void; | ||
function removeIdentifierChar(identifierName: string): void; | ||
const version: string; | ||
@@ -86,0 +90,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
78701
1925
62
0