adding existence soaks for indexed-lookup property accesses: obj?['property']

This commit is contained in:
Jeremy Ashkenas
2010-02-24 00:06:01 -05:00
parent 4eeb8c4bd2
commit 10d335ccb1
10 changed files with 111 additions and 83 deletions

View File

@@ -285,6 +285,8 @@
// Indexing into an object or array.
Index: [o("INDEX_START Expression INDEX_END", function() {
return new IndexNode($2);
}), o("SOAKED_INDEX_START Expression SOAKED_INDEX_END", function() {
return new IndexNode($2, 'soak');
})
],
// An object literal.

View File

@@ -285,24 +285,30 @@
// Multi-character operators are also literal tokens, so that Racc can assign
// the proper order of operations.
lex.prototype.literal_token = function literal_token() {
var match, tag, value;
var match, not_spaced, tag, value;
match = this.chunk.match(OPERATOR);
value = match && match[1];
if (value && value.match(CODE)) {
this.tag_parameters();
}
value = value || this.chunk.substr(0, 1);
not_spaced = !this.prev() || !this.prev().spaced;
tag = value;
if (value.match(ASSIGNMENT)) {
tag = 'ASSIGN';
if (JS_FORBIDDEN.indexOf(this.value()) >= 0) {
throw new Error('SyntaxError: Reserved word "' + this.value() + '" on line ' + this.line + ' can\'t be assigned');
}
}
if (value === ';') {
} else if (value === ';') {
tag = 'TERMINATOR';
}
if (CALLABLE.indexOf(this.tag()) >= 0 && (!this.prev() || !this.prev().spaced)) {
} else if (value === '[' && this.tag() === '?' && not_spaced) {
tag = 'SOAKED_INDEX_START';
this.soaked_index = true;
this.tokens.pop();
} else if (value === ']' && this.soaked_index) {
tag = 'SOAKED_INDEX_END';
this.soaked_index = false;
} else if (CALLABLE.indexOf(this.tag()) >= 0 && not_spaced) {
if (value === '(') {
tag = 'CALL_START';
}

View File

@@ -379,7 +379,7 @@
for (_b = 0; _b < _a.length; _b++) {
prop = _a[_b];
this.source = baseline;
if (prop instanceof AccessorNode && prop.soak) {
if (prop.soak_node) {
soaked = true;
if (this.base instanceof CallNode && prop === props[0]) {
temp = o.scope.free_variable();
@@ -511,7 +511,7 @@
constructor: function constructor(name, tag) {
this.children = [(this.name = name)];
this.prototype = tag === 'prototype';
this.soak = tag === 'soak';
this.soak_node = tag === 'soak';
return this;
},
compile_node: function compile_node(o) {
@@ -521,8 +521,9 @@
// An indexed accessor into a part of an array or object.
IndexNode = (exports.IndexNode = inherit(Node, {
type: 'Index',
constructor: function constructor(index) {
constructor: function constructor(index, tag) {
this.children = [(this.index = index)];
this.soak_node = tag === 'soak';
return this;
},
compile_node: function compile_node(o) {

File diff suppressed because one or more lines are too long

View File

@@ -9,7 +9,7 @@
// indentation, and single-line flavors of expressions.
exports.Rewriter = (re = function re() { });
// Tokens that must be balanced.
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['PARAM_START', 'PARAM_END'], ['CALL_START', 'CALL_END'], ['INDEX_START', 'INDEX_END']];
BALANCED_PAIRS = [['(', ')'], ['[', ']'], ['{', '}'], ['INDENT', 'OUTDENT'], ['PARAM_START', 'PARAM_END'], ['CALL_START', 'CALL_END'], ['INDEX_START', 'INDEX_END'], ['SOAKED_INDEX_START', 'SOAKED_INDEX_END']];
// Tokens that signal the start of a balanced pair.
EXPRESSION_START = (function() {
_a = []; _b = BALANCED_PAIRS;