123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- var tmp = {};
- (function (root) {
- 'use strict';
-
-
-
-
- var COMPOUND = 'Compound',
- IDENTIFIER = 'Identifier',
- MEMBER_EXP = 'MemberExpression',
- LITERAL = 'Literal',
- THIS_EXP = 'ThisExpression',
- CALL_EXP = 'CallExpression',
- UNARY_EXP = 'UnaryExpression',
- BINARY_EXP = 'BinaryExpression',
- LOGICAL_EXP = 'LogicalExpression',
- CONDITIONAL_EXP = 'ConditionalExpression',
- ARRAY_EXP = 'ArrayExpression',
- 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,
- throwError = function(message, index) {
- var error = new Error(message + ' at character ' + index);
- error.index = index;
- error.description = message;
- throw error;
- },
-
-
-
- t = true,
-
-
- unary_ops = {'-': t, '!': t, '~': t, '+': t},
-
-
-
- binary_ops = {
- '||': 1, '&&': 2, '|': 3, '^': 4, '&': 5,
- '==': 6, '!=': 6, '===': 6, '!==': 6,
- '<': 7, '>': 7, '<=': 7, '>=': 7,
- '<<':8, '>>': 8, '>>>': 8,
- '+': 9, '-': 9,
- '*': 10, '/': 10, '%': 10
- },
-
- getMaxKeyLen = function(obj) {
- var max_len = 0, len;
- for(var key in obj) {
- if((len = key.length) > max_len && obj.hasOwnProperty(key)) {
- max_len = len;
- }
- }
- return max_len;
- },
- max_unop_len = getMaxKeyLen(unary_ops),
- max_binop_len = getMaxKeyLen(binary_ops),
-
-
-
- literals = {
- 'true': true,
- 'false': false,
- 'null': null
- },
-
- this_str = 'this',
-
- binaryPrecedence = function(op_val) {
- return binary_ops[op_val] || 0;
- },
-
-
- createBinaryExpression = function (operator, left, right) {
- var type = (operator === '||' || operator === '&&') ? LOGICAL_EXP : BINARY_EXP;
- return {
- type: type,
- operator: operator,
- left: left,
- right: right
- };
- },
-
- isDecimalDigit = function(ch) {
- return (ch >= 48 && ch <= 57);
- },
- isIdentifierStart = function(ch) {
- return (ch === 36) || (ch === 95) ||
- (ch >= 65 && ch <= 90) ||
- (ch >= 97 && ch <= 122) ||
- (ch >= 128 && !binary_ops[String.fromCharCode(ch)]);
- },
- isIdentifierPart = function(ch) {
- return (ch === 36) || (ch === 95) ||
- (ch >= 65 && ch <= 90) ||
- (ch >= 97 && ch <= 122) ||
- (ch >= 48 && ch <= 57) ||
- (ch >= 128 && !binary_ops[String.fromCharCode(ch)]);
- },
-
-
-
- jsep = function(expr) {
-
-
- var index = 0,
- charAtFunc = expr.charAt,
- charCodeAtFunc = expr.charCodeAt,
- exprI = function(i) { return charAtFunc.call(expr, i); },
- exprICode = function(i) { return charCodeAtFunc.call(expr, i); },
- length = expr.length,
-
- gobbleSpaces = function() {
- var ch = exprICode(index);
-
- while(ch === 32 || ch === 9) {
- ch = exprICode(++index);
- }
- },
-
- gobbleExpression = function() {
- var test = gobbleBinaryExpression(),
- consequent, alternate;
- gobbleSpaces();
- if(exprICode(index) === QUMARK_CODE) {
-
- index++;
- consequent = gobbleExpression();
- if(!consequent) {
- throwError('Expected expression', index);
- }
- gobbleSpaces();
- if(exprICode(index) === COLON_CODE) {
- index++;
- alternate = gobbleExpression();
- if(!alternate) {
- throwError('Expected expression', index);
- }
- return {
- type: CONDITIONAL_EXP,
- test: test,
- consequent: consequent,
- alternate: alternate
- };
- } else {
- throwError('Expected :', index);
- }
- } else {
- return test;
- }
- },
-
-
-
-
- gobbleBinaryOp = function() {
- gobbleSpaces();
- var biop, to_check = expr.substr(index, max_binop_len), tc_len = to_check.length;
- while(tc_len > 0) {
- if(binary_ops.hasOwnProperty(to_check)) {
- index += tc_len;
- return to_check;
- }
- to_check = to_check.substr(0, --tc_len);
- }
- return false;
- },
-
-
- gobbleBinaryExpression = function() {
- var ch_i, node, biop, prec, stack, biop_info, left, right, i;
-
-
- left = gobbleToken();
- biop = gobbleBinaryOp();
-
- if(!biop) {
- return left;
- }
-
-
- biop_info = { value: biop, prec: binaryPrecedence(biop)};
- right = gobbleToken();
- if(!right) {
- throwError("Expected expression after " + biop, index);
- }
- stack = [left, biop_info, right];
-
- while((biop = gobbleBinaryOp())) {
- prec = binaryPrecedence(biop);
- if(prec === 0) {
- break;
- }
- biop_info = { value: biop, prec: prec };
-
- while ((stack.length > 2) && (prec <= stack[stack.length - 2].prec)) {
- right = stack.pop();
- biop = stack.pop().value;
- left = stack.pop();
- node = createBinaryExpression(biop, left, right);
- stack.push(node);
- }
- node = gobbleToken();
- if(!node) {
- throwError("Expected expression after " + biop, index);
- }
- stack.push(biop_info, node);
- }
- i = stack.length - 1;
- node = stack[i];
- while(i > 1) {
- node = createBinaryExpression(stack[i - 1].value, stack[i - 2], node);
- i -= 2;
- }
- return node;
- },
-
-
- gobbleToken = function() {
- var ch, to_check, tc_len;
- gobbleSpaces();
- ch = exprICode(index);
- if(isDecimalDigit(ch) || ch === PERIOD_CODE) {
-
- return gobbleNumericLiteral();
- } else if(ch === SQUOTE_CODE || ch === DQUOTE_CODE) {
-
- return gobbleStringLiteral();
- } else if(isIdentifierStart(ch) || ch === OPAREN_CODE) {
-
- return gobbleVariable();
- } else if (ch === OBRACK_CODE) {
- return gobbleArray();
- } else {
- to_check = expr.substr(index, max_unop_len);
- tc_len = to_check.length;
- while(tc_len > 0) {
- if(unary_ops.hasOwnProperty(to_check)) {
- index += tc_len;
- return {
- type: UNARY_EXP,
- operator: to_check,
- argument: gobbleToken(),
- prefix: true
- };
- }
- to_check = to_check.substr(0, --tc_len);
- }
- return false;
- }
- },
-
-
- gobbleNumericLiteral = function() {
- var number = '', ch, chCode;
- while(isDecimalDigit(exprICode(index))) {
- number += exprI(index++);
- }
- if(exprICode(index) === PERIOD_CODE) {
- number += exprI(index++);
- while(isDecimalDigit(exprICode(index))) {
- number += exprI(index++);
- }
- }
- ch = exprI(index);
- if(ch === 'e' || ch === 'E') {
- number += exprI(index++);
- ch = exprI(index);
- if(ch === '+' || ch === '-') {
- number += exprI(index++);
- }
- while(isDecimalDigit(exprICode(index))) {
- number += exprI(index++);
- }
- if(!isDecimalDigit(exprICode(index-1)) ) {
- throwError('Expected exponent (' + number + exprI(index) + ')', index);
- }
- }
- chCode = exprICode(index);
-
- if(isIdentifierStart(chCode)) {
- throwError('Variable names cannot start with a number (' +
- number + exprI(index) + ')', index);
- } else if(chCode === PERIOD_CODE) {
- throwError('Unexpected period', index);
- }
- return {
- type: LITERAL,
- value: parseFloat(number),
- raw: number
- };
- },
-
-
- gobbleStringLiteral = function() {
- var str = '', quote = exprI(index++), closed = false, ch;
- while(index < length) {
- ch = exprI(index++);
- if(ch === quote) {
- closed = true;
- break;
- } else if(ch === '\\') {
-
- ch = exprI(index++);
- switch(ch) {
- case 'n': str += '\n'; break;
- case 'r': str += '\r'; break;
- case 't': str += '\t'; break;
- case 'b': str += '\b'; break;
- case 'f': str += '\f'; break;
- case 'v': str += '\x0B'; break;
- default : str += '\\' + ch;
- }
- } else {
- str += ch;
- }
- }
- if(!closed) {
- throwError('Unclosed quote after "'+str+'"', index);
- }
- return {
- type: LITERAL,
- value: str,
- raw: quote + str + quote
- };
- },
-
-
-
-
- gobbleIdentifier = function() {
- var ch = exprICode(index), start = index, identifier;
- if(isIdentifierStart(ch)) {
- index++;
- } else {
- throwError('Unexpected ' + exprI(index), index);
- }
- while(index < length) {
- ch = exprICode(index);
- if(isIdentifierPart(ch)) {
- index++;
- } else {
- break;
- }
- }
- identifier = expr.slice(start, index);
- if(literals.hasOwnProperty(identifier)) {
- return {
- type: LITERAL,
- value: literals[identifier],
- raw: identifier
- };
- } else if(identifier === this_str) {
- return { type: THIS_EXP };
- } else {
- return {
- type: IDENTIFIER,
- name: identifier
- };
- }
- },
-
-
-
-
-
- gobbleArguments = function(termination) {
- var ch_i, args = [], node, closed = false;
- while(index < length) {
- gobbleSpaces();
- ch_i = exprICode(index);
- if(ch_i === termination) {
- closed = true;
- index++;
- break;
- } else if (ch_i === COMMA_CODE) {
- index++;
- } else {
- node = gobbleExpression();
- if(!node || node.type === COMPOUND) {
- throwError('Expected comma', index);
- }
- args.push(node);
- }
- }
- if (!closed) {
- throwError('Expected ' + String.fromCharCode(termination), index);
- }
- return args;
- },
-
-
-
-
- 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) {
-
- node = {
- type: CALL_EXP,
- 'arguments': gobbleArguments(CPAREN_CODE),
- callee: node
- };
- }
- gobbleSpaces();
- ch_i = exprICode(index);
- }
- return node;
- },
-
-
-
-
-
- gobbleGroup = function() {
- index++;
- var node = gobbleExpression();
- gobbleSpaces();
- if(exprICode(index) === CPAREN_CODE) {
- index++;
- return node;
- } else {
- throwError('Unclosed (', index);
- }
- },
-
-
-
- gobbleArray = function() {
- index++;
- return {
- type: ARRAY_EXP,
- elements: gobbleArguments(CBRACK_CODE)
- };
- },
- nodes = [], ch_i, node;
- while(index < length) {
- ch_i = exprICode(index);
-
-
- if(ch_i === SEMCOL_CODE || ch_i === COMMA_CODE) {
- index++;
- } else {
-
- if((node = gobbleExpression())) {
- nodes.push(node);
-
-
- } else if(index < length) {
- throwError('Unexpected "' + exprI(index) + '"', index);
- }
- }
- }
-
- if(nodes.length === 1) {
- return nodes[0];
- } else {
- return {
- type: COMPOUND,
- body: nodes
- };
- }
- };
-
- jsep.version = '0.3.1';
- jsep.toString = function() { return 'JavaScript Expression Parser (JSEP) v' + jsep.version; };
-
- jsep.addUnaryOp = function(op_name) {
- max_unop_len = Math.max(op_name.length, max_unop_len);
- unary_ops[op_name] = t; return this;
- };
-
- jsep.addBinaryOp = function(op_name, precedence) {
- max_binop_len = Math.max(op_name.length, max_binop_len);
- binary_ops[op_name] = precedence;
- return this;
- };
-
- jsep.addLiteral = function(literal_name, literal_value) {
- literals[literal_name] = literal_value;
- return this;
- };
-
- jsep.removeUnaryOp = function(op_name) {
- delete unary_ops[op_name];
- if(op_name.length === max_unop_len) {
- max_unop_len = getMaxKeyLen(unary_ops);
- }
- return this;
- };
-
- jsep.removeAllUnaryOps = function() {
- unary_ops = {};
- max_unop_len = 0;
- return this;
- };
-
- jsep.removeBinaryOp = function(op_name) {
- delete binary_ops[op_name];
- if(op_name.length === max_binop_len) {
- max_binop_len = getMaxKeyLen(binary_ops);
- }
- return this;
- };
-
- jsep.removeAllBinaryOps = function() {
- binary_ops = {};
- max_binop_len = 0;
- return this;
- };
-
- jsep.removeLiteral = function(literal_name) {
- delete literals[literal_name];
- return this;
- };
-
- jsep.removeAllLiterals = function() {
- literals = {};
- return this;
- };
- root.jsep = jsep;
- }(tmp));
- export default tmp.jsep;
|