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

refactor($parse): remove the support of JSON parsing mode #7552

Merged
merged 1 commit into from
May 22, 2014
Merged
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
62 changes: 15 additions & 47 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ Lexer.prototype = {

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 +137,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 +162,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 +245,8 @@ Lexer.prototype = {
this.tokens.push({
index: start,
text: number,
json: true,
literal: true,
constant: true,
fn: function() { return number; }
});
},
Expand Down Expand Up @@ -309,7 +298,8 @@ 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];
token.literal = true;
token.constant = true;
} else {
var getter = getterFn(ident, this.options, this.text);
token.fn = extend(function(self, locals) {
Expand All @@ -326,13 +316,11 @@ Lexer.prototype = {
if (methodName) {
this.tokens.push({
index:lastDot,
text: '.',
json: false
text: '.'
});
this.tokens.push({
index: lastDot + 1,
text: methodName,
json: false
text: methodName
});
}
},
Expand Down Expand Up @@ -370,7 +358,8 @@ Lexer.prototype = {
index: start,
text: rawString,
string: string,
json: true,
literal: true,
constant: true,
fn: function() { return string; }
});
return;
Expand Down Expand Up @@ -402,28 +391,12 @@ 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,10 +423,8 @@ Parser.prototype = {
if (!primary) {
this.throwError('not a primary expression', token);
}
if (token.json) {
primary.constant = true;
primary.literal = true;
}
primary.literal = !!token.literal;
primary.constant = !!token.constant;
}

var next, context;
Expand Down Expand Up @@ -501,9 +472,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 @@ -1257,7 +1225,7 @@ function $ParseProvider() {

var lexer = new Lexer($parseOptions);
var parser = new Parser(lexer, $filter, $parseOptions);
parsedExpression = parser.parse(exp, false);
parsedExpression = parser.parse(exp);

if (exp !== 'hasOwnProperty') {
// Only cache the value if it's not going to mess up the cache object
Expand Down