Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

refactor(parse): remove the support for JSON parsing #7543

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 31 additions & 65 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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('"\'')) {
Expand All @@ -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++;
Expand All @@ -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 {
Expand Down Expand Up @@ -257,7 +248,7 @@ Lexer.prototype = {
this.tokens.push({
index: start,
text: number,
json: true,
constant: true,
fn: function() { return number; }
});
},
Expand Down Expand Up @@ -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) {
Expand All @@ -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
});
}
},
Expand Down Expand Up @@ -370,7 +361,7 @@ Lexer.prototype = {
index: start,
text: rawString,
string: string,
json: true,
constant: true,
fn: function() { return string; }
});
return;
Expand Down Expand Up @@ -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]);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
},

Expand Down