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

refactor($parse): add lgalfaso's refactors and suggestions #7571

Merged
merged 3 commits into from
Jun 4, 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
64 changes: 23 additions & 41 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,8 @@ Lexer.prototype = {

lex: function (text) {
this.text = text;

this.index = 0;
this.ch = undefined;
this.lastCh = ':'; // can start regexp

this.tokens = [];

while (this.index < this.text.length) {
Expand All @@ -143,7 +140,6 @@ Lexer.prototype = {
this.index++;
} else if (this.isWhitespace(this.ch)) {
this.index++;
continue;
} else {
var ch2 = this.ch + this.peek();
var ch3 = ch2 + this.peek(2);
Expand All @@ -167,7 +163,6 @@ Lexer.prototype = {
this.throwError('Unexpected next character ', this.index, this.index + 1);
}
}
this.lastCh = this.ch;
}
return this.tokens;
},
Expand All @@ -176,10 +171,6 @@ Lexer.prototype = {
return chars.indexOf(this.ch) !== -1;
},

was: function(chars) {
return chars.indexOf(this.lastCh) !== -1;
},

peek: function(i) {
var num = i || 1;
return (this.index + num < this.text.length) ? this.text.charAt(this.index + num) : false;
Expand Down Expand Up @@ -243,7 +234,6 @@ Lexer.prototype = {
this.tokens.push({
index: start,
text: number,
literal: true,
constant: true,
fn: function() { return number; }
});
Expand Down Expand Up @@ -296,15 +286,14 @@ 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.literal = true;
token.constant = true;
} else {
var getter = getterFn(ident, this.options, this.text);
token.fn = extend(function(self, locals) {
return (getter(self, locals));
}, {
assign: function(self, value) {
return setter(self, ident, value, parser.text, parser.options);
return setter(self, ident, value, parser.text);
}
});
}
Expand All @@ -313,7 +302,7 @@ Lexer.prototype = {

if (methodName) {
this.tokens.push({
index:lastDot,
index: lastDot,
text: '.'
});
this.tokens.push({
Expand Down Expand Up @@ -356,7 +345,6 @@ Lexer.prototype = {
index: start,
text: rawString,
string: string,
literal: true,
constant: true,
fn: function() { return string; }
});
Expand Down Expand Up @@ -391,7 +379,6 @@ Parser.prototype = {

parse: function (text) {
this.text = text;

this.tokens = this.lexer.lex(text);

var value = this.statements();
Expand Down Expand Up @@ -421,8 +408,10 @@ Parser.prototype = {
if (!primary) {
this.throwError('not a primary expression', token);
}
primary.literal = !!token.literal;
primary.constant = !!token.constant;
if (token.constant) {
primary.constant = true;
primary.literal = true;
}
}

var next, context;
Expand Down Expand Up @@ -546,21 +535,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 Expand Up @@ -680,7 +665,7 @@ Parser.prototype = {
return getter(self || object(scope, locals));
}, {
assign: function(scope, value, locals) {
return setter(object(scope, locals), field, value, parser.text, parser.options);
return setter(object(scope, locals), field, value, parser.text);
}
});
},
Expand All @@ -694,7 +679,7 @@ Parser.prototype = {
return extend(function(self, locals) {
var o = obj(self, locals),
i = indexFn(self, locals),
v, p;
v;

if (!o) return undefined;
v = ensureSafeObject(o[i], parser.text);
Expand Down Expand Up @@ -812,9 +797,7 @@ Parser.prototype = {
// Parser helper functions
//////////////////////////////////////////////////

function setter(obj, path, setValue, fullExp, options) {
//needed?
options = options || {};
function setter(obj, path, setValue, fullExp) {

var element = path.split('.'), key;
for (var i = 0; element.length > 1; i++) {
Expand All @@ -838,7 +821,7 @@ var getterFnCache = {};
* - http://jsperf.com/angularjs-parse-getter/4
* - http://jsperf.com/path-evaluation-simplified/7
*/
function cspSafeGetterFn(key0, key1, key2, key3, key4, fullExp, options) {
function cspSafeGetterFn(key0, key1, key2, key3, key4, fullExp) {
ensureSafeMemberName(key0, fullExp);
ensureSafeMemberName(key1, fullExp);
ensureSafeMemberName(key2, fullExp);
Expand Down Expand Up @@ -911,14 +894,13 @@ function getterFn(path, options, fullExp) {
fn = simpleGetterFn2(pathKeys[0], pathKeys[1], fullExp);
} else if (options.csp) {
if (pathKeysLength < 6) {
fn = cspSafeGetterFn(pathKeys[0], pathKeys[1], pathKeys[2], pathKeys[3], pathKeys[4], fullExp,
options);
fn = cspSafeGetterFn(pathKeys[0], pathKeys[1], pathKeys[2], pathKeys[3], pathKeys[4], fullExp);
} else {
fn = function(scope, locals) {
var i = 0, val;
do {
val = cspSafeGetterFn(pathKeys[i++], pathKeys[i++], pathKeys[i++], pathKeys[i++],
pathKeys[i++], fullExp, options)(scope, locals);
pathKeys[i++], fullExp)(scope, locals);

locals = undefined; // clear after first iteration
scope = val;
Expand Down Expand Up @@ -1014,7 +996,7 @@ function $ParseProvider() {
};


this.$get = ['$filter', '$sniffer', '$log', function($filter, $sniffer, $log) {
this.$get = ['$filter', '$sniffer', function($filter, $sniffer) {
$parseOptions.csp = $sniffer.csp;

return function(exp) {
Expand Down