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:
parent
d7f1193f22
commit
cb81f86434
6 changed files with 193 additions and 202 deletions
|
@ -38,7 +38,7 @@
|
|||
return new Literal($1);
|
||||
}), 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: [
|
||||
o('INDENT Body OUTDENT', function() {
|
||||
return $2;
|
||||
|
@ -245,11 +245,6 @@
|
|||
return $2;
|
||||
})
|
||||
],
|
||||
Extends: [
|
||||
o('SimpleAssignable EXTENDS Value', function() {
|
||||
return new Extends($1, $3);
|
||||
})
|
||||
],
|
||||
Invocation: [
|
||||
o('Value OptFuncExist Arguments', function() {
|
||||
return new Call($1, $3, $2);
|
||||
|
@ -590,10 +585,12 @@
|
|||
}), o('SimpleAssignable COMPOUND_ASSIGN\
|
||||
INDENT Expression OUTDENT', function() {
|
||||
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 = [];
|
||||
for (name in grammar) {
|
||||
alternatives = grammar[name];
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
child.prototype = new ctor;
|
||||
if (typeof parent.extended === "function") parent.extended(child);
|
||||
child.__super__ = parent.prototype;
|
||||
return child;
|
||||
}, __slice = Array.prototype.slice;
|
||||
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;
|
||||
|
@ -2024,7 +2025,7 @@
|
|||
}
|
||||
};
|
||||
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}',
|
||||
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',
|
||||
|
|
350
lib/parser.js
350
lib/parser.js
File diff suppressed because one or more lines are too long
|
@ -100,7 +100,6 @@ grammar =
|
|||
o 'While'
|
||||
o 'For'
|
||||
o 'Switch'
|
||||
o 'Extends'
|
||||
o 'Class'
|
||||
]
|
||||
|
||||
|
@ -304,12 +303,6 @@ grammar =
|
|||
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.
|
||||
Invocation: [
|
||||
o 'Value OptFuncExist Arguments', -> new Call $1, $3, $2
|
||||
|
@ -545,6 +538,7 @@ grammar =
|
|||
Expression', -> new Assign $1, $3, $2
|
||||
o 'SimpleAssignable COMPOUND_ASSIGN
|
||||
INDENT Expression OUTDENT', -> new Assign $1, $4, $2
|
||||
o 'SimpleAssignable EXTENDS Expression', -> new Extends $1, $3
|
||||
]
|
||||
|
||||
|
||||
|
@ -572,10 +566,10 @@ operators = [
|
|||
['left', 'COMPARE']
|
||||
['left', 'LOGIC']
|
||||
['nonassoc', 'INDENT', 'OUTDENT']
|
||||
['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN']
|
||||
['right', '=', ':', 'COMPOUND_ASSIGN', 'RETURN', 'EXTENDS']
|
||||
['right', 'WHEN', 'LEADING_WHEN', 'FORIN', 'FOROF', 'FROM', 'TO', 'BY',
|
||||
'THROW', 'IF', 'UNLESS', 'ELSE', 'FOR', 'WHILE', 'UNTIL', 'LOOP',
|
||||
'SUPER', 'CLASS', 'EXTENDS']
|
||||
'SUPER', 'CLASS']
|
||||
['right', 'POST_IF', 'POST_UNLESS']
|
||||
]
|
||||
|
||||
|
|
|
@ -1560,6 +1560,7 @@ UTILITIES =
|
|||
child.prototype = new ctor;
|
||||
if (typeof parent.extended === "function") parent.extended(child);
|
||||
child.__super__ = parent.prototype;
|
||||
return child;
|
||||
}
|
||||
'''
|
||||
|
||||
|
|
|
@ -78,19 +78,19 @@ Base::['func-func'] = (string) ->
|
|||
"dynamic-#{string}"
|
||||
|
||||
FirstChild = ->
|
||||
FirstChild extends Base
|
||||
FirstChild::func = (string) ->
|
||||
super('one/') + string
|
||||
|
||||
SecondChild = ->
|
||||
SecondChild extends FirstChild
|
||||
SecondChild::func = (string) ->
|
||||
super('two/') + string
|
||||
|
||||
ThirdChild = ->
|
||||
@array = [1, 2, 3]
|
||||
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) ->
|
||||
super('three/') + string
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue