Starting ES6 classes
This commit is contained in:
106
lib/parse.js
106
lib/parse.js
@@ -44,9 +44,9 @@
|
||||
|
||||
"use strict";
|
||||
|
||||
var KEYWORDS = 'break case catch const continue debugger default delete do else finally for function if in instanceof new return switch throw try typeof var let void while with';
|
||||
var KEYWORDS = 'break case catch class const continue debugger default delete do else extends finally for function if in instanceof new return switch throw try typeof var let void while with';
|
||||
var KEYWORDS_ATOM = 'false null true';
|
||||
var RESERVED_WORDS = 'abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield'
|
||||
var RESERVED_WORDS = 'abstract boolean byte char double enum export final float goto implements import int interface long native package private protected public short static super synchronized this throws transient volatile yield'
|
||||
+ " " + KEYWORDS_ATOM + " " + KEYWORDS;
|
||||
var KEYWORDS_BEFORE_EXPRESSION = 'return new delete throw else case';
|
||||
|
||||
@@ -855,6 +855,9 @@ function parse($TEXT, options) {
|
||||
case "for":
|
||||
return for_();
|
||||
|
||||
case "class":
|
||||
return class_();
|
||||
|
||||
case "function":
|
||||
return function_(AST_Defun);
|
||||
|
||||
@@ -1374,6 +1377,13 @@ function parse($TEXT, options) {
|
||||
func.end = prev();
|
||||
return subscripts(func, allow_calls);
|
||||
}
|
||||
if (is("keyword", "class")) {
|
||||
next();
|
||||
var cls = class_();
|
||||
cls.start = start;
|
||||
cls.end = prev();
|
||||
return subscripts(cls, allow_calls);
|
||||
}
|
||||
if (ATOMIC_START_TOKEN[S.token.type]) {
|
||||
return subscripts(as_atom_node(), allow_calls);
|
||||
}
|
||||
@@ -1446,32 +1456,9 @@ function parse($TEXT, options) {
|
||||
var type = start.type;
|
||||
var name = as_property_name();
|
||||
if (type != "string" && type != "num" && !is("punc", ":")) {
|
||||
if (is("punc", "(")) {
|
||||
a.push(new AST_ConciseMethod({
|
||||
start : start,
|
||||
name : new AST_SymbolMethod({ name: name }),
|
||||
argnames : params_or_seq_().as_params(croak),
|
||||
body : _function_body(true),
|
||||
end : prev()
|
||||
}))
|
||||
continue;
|
||||
}
|
||||
if (name == "get") {
|
||||
a.push(new AST_ObjectGetter({
|
||||
start : start,
|
||||
key : as_atom_node(),
|
||||
value : function_(AST_Accessor),
|
||||
end : prev()
|
||||
}));
|
||||
continue;
|
||||
}
|
||||
if (name == "set") {
|
||||
a.push(new AST_ObjectSetter({
|
||||
start : start,
|
||||
key : as_atom_node(),
|
||||
value : function_(AST_Accessor),
|
||||
end : prev()
|
||||
}));
|
||||
var concise = concise_method_or_getset(name, start);
|
||||
if (concise) {
|
||||
a.push(concise);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -1510,6 +1497,69 @@ function parse($TEXT, options) {
|
||||
return new AST_Object({ properties: a })
|
||||
});
|
||||
|
||||
function class_() {
|
||||
var start, method, class_name, name, extends_, a = [];
|
||||
|
||||
if (S.token.type == "name" && S.token.value != "extends") {
|
||||
class_name = as_symbol(AST_SymbolClassName)
|
||||
}
|
||||
|
||||
if (S.token.value == "extends") {
|
||||
next();
|
||||
extends_ = expression(true);
|
||||
}
|
||||
|
||||
expect("{");
|
||||
|
||||
if (is("punc", ";")) { next(); } // Leading semicolons are okay in class bodies.
|
||||
while (!is("punc", "}")) {
|
||||
start = S.token;
|
||||
name = as_property_name();
|
||||
method = concise_method_or_getset(name, start);
|
||||
if (!method) { croak(); }
|
||||
a.push(method);
|
||||
if (is("punc", ";")) { next(); }
|
||||
}
|
||||
|
||||
next();
|
||||
|
||||
return new AST_Class({
|
||||
start: start,
|
||||
name: class_name,
|
||||
extends: extends_,
|
||||
properties: a,
|
||||
end: prev(),
|
||||
});
|
||||
}
|
||||
|
||||
function concise_method_or_getset(name, start) {
|
||||
if (is("punc", "(")) {
|
||||
return new AST_ConciseMethod({
|
||||
start : start,
|
||||
name : new AST_SymbolMethod({ name: name }),
|
||||
argnames : params_or_seq_().as_params(croak),
|
||||
body : _function_body(true),
|
||||
end : prev()
|
||||
});
|
||||
}
|
||||
if (name == "get") {
|
||||
return new AST_ObjectGetter({
|
||||
start : start,
|
||||
key : as_atom_node(),
|
||||
value : function_(AST_Accessor),
|
||||
end : prev()
|
||||
});
|
||||
}
|
||||
if (name == "set") {
|
||||
return new AST_ObjectSetter({
|
||||
start : start,
|
||||
key : as_atom_node(),
|
||||
value : function_(AST_Accessor),
|
||||
end : prev()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function as_property_name() {
|
||||
var tmp = S.token;
|
||||
next();
|
||||
|
||||
Reference in New Issue
Block a user