1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

coco 98271e6: made 'extends' chainable

This commit is contained in:
Jeremy Ashkenas 2010-11-09 00:26:31 -05:00
parent d7f1193f22
commit cb81f86434
6 changed files with 193 additions and 202 deletions

View file

@ -38,7 +38,7 @@
return new Literal($1); return new Literal($1);
}), o('Comment') }), o('Comment')
], ],
Expression: [o('Value'), o('Invocation'), o('Code'), o('Operation'), o('Assign'), o('If'), o('Try'), o('While'), o('For'), o('Switch'), o('Extends'), o('Class')], Expression: [o('Value'), o('Invocation'), o('Code'), o('Operation'), o('Assign'), o('If'), o('Try'), o('While'), o('For'), o('Switch'), o('Class')],
Block: [ Block: [
o('INDENT Body OUTDENT', function() { o('INDENT Body OUTDENT', function() {
return $2; return $2;
@ -245,11 +245,6 @@
return $2; return $2;
}) })
], ],
Extends: [
o('SimpleAssignable EXTENDS Value', function() {
return new Extends($1, $3);
})
],
Invocation: [ Invocation: [
o('Value OptFuncExist Arguments', function() { o('Value OptFuncExist Arguments', function() {
return new Call($1, $3, $2); return new Call($1, $3, $2);
@ -590,10 +585,12 @@
}), o('SimpleAssignable COMPOUND_ASSIGN\ }), o('SimpleAssignable COMPOUND_ASSIGN\
INDENT Expression OUTDENT', function() { INDENT Expression OUTDENT', function() {
return new Assign($1, $4, $2); return new Assign($1, $4, $2);
}), o('SimpleAssignable EXTENDS Expression', function() {
return new Extends($1, $3);
}) })
] ]
}; };
operators = [['left', '.', '?.', '::'], ['left', 'CALL_START', 'CALL_END'], ['nonassoc', '++', '--'], ['left', '?'], ['right', 'UNARY'], ['left', 'MATH'], ['left', '+', '-'], ['left', 'SHIFT'], ['left', 'RELATION'], ['left', 'COMPARE'], ['left', 'LOGIC'], ['nonassoc', 'INDENT', 'OUTDENT'], ['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN'], ['right', 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'FROM', 'TO', 'BY', 'THROW', 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS', 'EXTENDS'], ['right', 'POST_IF', 'POST_UNLESS']]; operators = [['left', '.', '?.', '::'], ['left', 'CALL_START', 'CALL_END'], ['nonassoc', '++', '--'], ['left', '?'], ['right', 'UNARY'], ['left', 'MATH'], ['left', '+', '-'], ['left', 'SHIFT'], ['left', 'RELATION'], ['left', 'COMPARE'], ['left', 'LOGIC'], ['nonassoc', 'INDENT', 'OUTDENT'], ['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN', 'EXTENDS'], ['right', 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'FROM', 'TO', 'BY', 'THROW', 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'SUPER', 'CLASS'], ['right', 'POST_IF', 'POST_UNLESS']];
tokens = []; tokens = [];
for (name in grammar) { for (name in grammar) {
alternatives = grammar[name]; alternatives = grammar[name];

View file

@ -6,6 +6,7 @@
child.prototype = new ctor; child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child); if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype; child.__super__ = parent.prototype;
return child;
}, __slice = Array.prototype.slice; }, __slice = Array.prototype.slice;
Scope = require('./scope').Scope; Scope = require('./scope').Scope;
_ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, extend = _ref.extend, merge = _ref.merge, del = _ref.del, starts = _ref.starts, ends = _ref.ends, last = _ref.last; _ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, extend = _ref.extend, merge = _ref.merge, del = _ref.del, starts = _ref.starts, ends = _ref.ends, last = _ref.last;
@ -2024,7 +2025,7 @@
} }
}; };
UTILITIES = { UTILITIES = {
"extends": 'function(child, parent) {\n function ctor() { this.constructor = child; }\n ctor.prototype = parent.prototype;\n child.prototype = new ctor;\n if (typeof parent.extended === "function") parent.extended(child);\n child.__super__ = parent.prototype;\n}', "extends": 'function(child, parent) {\n function ctor() { this.constructor = child; }\n ctor.prototype = parent.prototype;\n child.prototype = new ctor;\n if (typeof parent.extended === "function") parent.extended(child);\n child.__super__ = parent.prototype;\n return child;\n}',
bind: 'function(func, context) {\n return function() { return func.apply(context, arguments); };\n}', bind: 'function(func, context) {\n return function() { return func.apply(context, arguments); };\n}',
indexOf: 'Array.prototype.indexOf || function(item) {\n for (var i = 0, l = this.length; i < l; i++) {\n if (this[i] === item) return i;\n }\n return -1;\n}', indexOf: 'Array.prototype.indexOf || function(item) {\n for (var i = 0, l = this.length; i < l; i++) {\n if (this[i] === item) return i;\n }\n return -1;\n}',
hasProp: 'Object.prototype.hasOwnProperty', hasProp: 'Object.prototype.hasOwnProperty',

File diff suppressed because one or more lines are too long

View file

@ -100,7 +100,6 @@ grammar =
o 'While' o 'While'
o 'For' o 'For'
o 'Switch' o 'Switch'
o 'Extends'
o 'Class' o 'Class'
] ]
@ -304,12 +303,6 @@ grammar =
o '{ ClassBody }', -> $2 o '{ ClassBody }', -> $2
] ]
# Extending an object by setting its prototype chain to reference a parent
# object.
Extends: [
o 'SimpleAssignable EXTENDS Value', -> new Extends $1, $3
]
# Ordinary function invocation, or a chained series of calls. # Ordinary function invocation, or a chained series of calls.
Invocation: [ Invocation: [
o 'Value OptFuncExist Arguments', -> new Call $1, $3, $2 o 'Value OptFuncExist Arguments', -> new Call $1, $3, $2
@ -545,6 +538,7 @@ grammar =
Expression', -> new Assign $1, $3, $2 Expression', -> new Assign $1, $3, $2
o 'SimpleAssignable COMPOUND_ASSIGN o 'SimpleAssignable COMPOUND_ASSIGN
INDENT Expression OUTDENT', -> new Assign $1, $4, $2 INDENT Expression OUTDENT', -> new Assign $1, $4, $2
o 'SimpleAssignable EXTENDS Expression', -> new Extends $1, $3
] ]
@ -572,10 +566,10 @@ operators = [
['left', 'COMPARE'] ['left', 'COMPARE']
['left', 'LOGIC'] ['left', 'LOGIC']
['nonassoc', 'INDENT', 'OUTDENT'] ['nonassoc', 'INDENT', 'OUTDENT']
['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN'] ['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN', 'EXTENDS']
['right', 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'FROM', 'TO', 'BY', ['right', 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'FROM', 'TO', 'BY',
'THROW', 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP', 'THROW', 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP',
'SUPER', 'CLASS', 'EXTENDS'] 'SUPER', 'CLASS']
['right', 'POST_IF', 'POST_UNLESS'] ['right', 'POST_IF', 'POST_UNLESS']
] ]

View file

@ -1560,6 +1560,7 @@ UTILITIES =
child.prototype = new ctor; child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child); if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype; child.__super__ = parent.prototype;
return child;
} }
''' '''

View file

@ -78,19 +78,19 @@ Base::['func-func'] = (string) ->
"dynamic-#{string}" "dynamic-#{string}"
FirstChild = -> FirstChild = ->
FirstChild extends Base
FirstChild::func = (string) ->
super('one/') + string
SecondChild = -> SecondChild = ->
SecondChild extends FirstChild
SecondChild::func = (string) ->
super('two/') + string
ThirdChild = -> ThirdChild = ->
@array = [1, 2, 3] @array = [1, 2, 3]
this this
ThirdChild extends SecondChild
ThirdChild extends SecondChild extends FirstChild extends Base
FirstChild::func = (string) ->
super('one/') + string
SecondChild::func = (string) ->
super('two/') + string
ThirdChild::func = (string) -> ThirdChild::func = (string) ->
super('three/') + string super('three/') + string