From dc0385b2f539c7f2e7ccee9fe9731c921054bfd1 Mon Sep 17 00:00:00 2001 From: Lucas Galfaso Date: Wed, 21 May 2014 21:37:59 -0300 Subject: [PATCH] refactor(parse): remove the support for JSON parsing Remove the support to exclusively parse JSON --- src/ng/parse.js | 96 ++++++++++++++++--------------------------------- 1 file changed, 31 insertions(+), 65 deletions(-) diff --git a/src/ng/parse.js b/src/ng/parse.js index 50da14fa0770..4a4ed0e68678 100644 --- a/src/ng/parse.js +++ b/src/ng/parse.js @@ -64,12 +64,15 @@ function ensureSafeObject(obj, fullExpression) { return obj; } -var OPERATORS = { - /* jshint bitwise : false */ +var CONSTANTS = { 'null':function(){return null;}, 'true':function(){return true;}, 'false':function(){return false;}, - undefined:noop, + undefined:noop +}; + +var OPERATORS = { + /* jshint bitwise : false */ '+':function(self, locals, a,b){ a=a(self, locals); b=b(self, locals); if (isDefined(a)) { @@ -121,17 +124,14 @@ Lexer.prototype = { constructor: Lexer, lex: function (text) { - this.text = text; + var token; + this.text = text; this.index = 0; this.ch = undefined; this.lastCh = ':'; // can start regexp - this.tokens = []; - var token; - var json = []; - while (this.index < this.text.length) { this.ch = this.text.charAt(this.index); if (this.is('"\'')) { @@ -140,19 +140,11 @@ Lexer.prototype = { this.readNumber(); } else if (this.isIdent(this.ch)) { this.readIdent(); - // identifiers can only be if the preceding char was a { or , - if (this.was('{,') && json[0] === '{' && - (token = this.tokens[this.tokens.length - 1])) { - token.json = token.text.indexOf('.') === -1; - } } else if (this.is('(){}[].,;:?')) { this.tokens.push({ index: this.index, - text: this.ch, - json: (this.was(':[,') && this.is('{[')) || this.is('}]:,') + text: this.ch }); - if (this.is('{[')) json.unshift(this.ch); - if (this.is('}]')) json.shift(); this.index++; } else if (this.isWhitespace(this.ch)) { this.index++; @@ -173,8 +165,7 @@ Lexer.prototype = { this.tokens.push({ index: this.index, text: this.ch, - fn: fn, - json: (this.was('[,:') && this.is('+-')) + fn: fn }); this.index += 1; } else { @@ -257,7 +248,7 @@ Lexer.prototype = { this.tokens.push({ index: start, text: number, - json: true, + constant: true, fn: function() { return number; } }); }, @@ -309,7 +300,9 @@ Lexer.prototype = { // OPERATORS is our own object so we don't need to use special hasOwnPropertyFn if (OPERATORS.hasOwnProperty(ident)) { token.fn = OPERATORS[ident]; - token.json = OPERATORS[ident]; + } else if (CONSTANTS.hasOwnProperty(ident)) { + token.fn = CONSTANTS[ident]; + token.constant = true; } else { var getter = getterFn(ident, this.options, this.text); token.fn = extend(function(self, locals) { @@ -325,14 +318,12 @@ Lexer.prototype = { if (methodName) { this.tokens.push({ - index:lastDot, - text: '.', - json: false + index: lastDot, + text: '.' }); this.tokens.push({ index: lastDot + 1, - text: methodName, - json: false + text: methodName }); } }, @@ -370,7 +361,7 @@ Lexer.prototype = { index: start, text: rawString, string: string, - json: true, + constant: true, fn: function() { return string; } }); return; @@ -402,28 +393,10 @@ Parser.ZERO = extend(function () { Parser.prototype = { constructor: Parser, - parse: function (text, json) { + parse: function (text) { this.text = text; - - //TODO(i): strip all the obsolte json stuff from this file - this.json = json; - this.tokens = this.lexer.lex(text); - - if (json) { - // The extra level of aliasing is here, just in case the lexer misses something, so that - // we prevent any accidental execution in JSON. - this.assignment = this.logicalOR; - - this.functionCall = - this.fieldAccess = - this.objectIndex = - this.filterChain = function() { - this.throwError('is not valid json', {text: text, index: 0}); - }; - } - - var value = json ? this.primary() : this.statements(); + var value = this.statements(); if (this.tokens.length !== 0) { this.throwError('is an unexpected token', this.tokens[0]); @@ -450,7 +423,7 @@ Parser.prototype = { if (!primary) { this.throwError('not a primary expression', token); } - if (token.json) { + if (token.constant) { primary.constant = true; primary.literal = true; } @@ -501,9 +474,6 @@ Parser.prototype = { expect: function(e1, e2, e3, e4){ var token = this.peek(e1, e2, e3, e4); if (token) { - if (this.json && !token.json) { - this.throwError('is not valid json', token); - } this.tokens.shift(); return token; } @@ -580,21 +550,17 @@ Parser.prototype = { var token = this.expect(); var fn = this.$filter(token.text); var argsFn = []; - while (true) { - if ((token = this.expect(':'))) { - argsFn.push(this.expression()); - } else { - var fnInvoke = function(self, locals, input) { - var args = [input]; - for (var i = 0; i < argsFn.length; i++) { - args.push(argsFn[i](self, locals)); - } - return fn.apply(self, args); - }; - return function() { - return fnInvoke; - }; + while(this.expect(':')) { + argsFn.push(this.expression()); + } + return valueFn(fnInvoke); + + function fnInvoke(self, locals, input) { + var args = [input]; + for (var i = 0; i < argsFn.length; i++) { + args.push(argsFn[i](self, locals)); } + return fn.apply(self, args); } },