support generator function declare

This commit is contained in:
jchbh 2015-05-20 09:59:12 -07:00
parent 0552dbd93c
commit b7abd50b05
3 changed files with 13 additions and 1 deletions

View File

@ -359,9 +359,10 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
} }
}, AST_Scope); }, AST_Scope);
var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments", { var AST_Lambda = DEFNODE("Lambda", "name argnames uses_arguments isgenerator", {
$documentation: "Base class for functions", $documentation: "Base class for functions",
$propdoc: { $propdoc: {
isgenerator: "is generatorFn or not",
name: "[AST_SymbolDeclaration?] the name of this function", name: "[AST_SymbolDeclaration?] the name of this function",
argnames: "[AST_SymbolFunarg*] array of function arguments", argnames: "[AST_SymbolFunarg*] array of function arguments",
uses_arguments: "[boolean/S] tells whether this function accesses the arguments array" uses_arguments: "[boolean/S] tells whether this function accesses the arguments array"

View File

@ -220,6 +220,10 @@ function OutputStream(options) {
might_need_space = true; might_need_space = true;
}; };
var star = function(){
print("*");
}
var indent = options.beautify ? function(half) { var indent = options.beautify ? function(half) {
if (options.beautify) { if (options.beautify) {
print(make_indent(half ? 0.5 : 0)); print(make_indent(half ? 0.5 : 0));
@ -332,6 +336,7 @@ function OutputStream(options) {
newline : newline, newline : newline,
print : print, print : print,
space : space, space : space,
star : star,
comma : comma, comma : comma,
colon : colon, colon : colon,
last : function() { return last }, last : function() { return last },
@ -731,6 +736,8 @@ function OutputStream(options) {
var self = this; var self = this;
if (!nokeyword) { if (!nokeyword) {
output.print("function"); output.print("function");
if(this.isgenerator)
output.star();
} }
if (self.name) { if (self.name) {
output.space(); output.space();

View File

@ -953,12 +953,16 @@ function parse($TEXT, options) {
var function_ = function(ctor) { var function_ = function(ctor) {
var in_statement = ctor === AST_Defun; var in_statement = ctor === AST_Defun;
var isgenerator = is("operator", "*");
if (isgenerator)
next();
var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun : AST_SymbolLambda) : null; var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun : AST_SymbolLambda) : null;
if (in_statement && !name) if (in_statement && !name)
unexpected(); unexpected();
expect("("); expect("(");
return new ctor({ return new ctor({
name: name, name: name,
isgenerator: isgenerator,
argnames: (function(first, a){ argnames: (function(first, a){
while (!is("punc", ")")) { while (!is("punc", ")")) {
if (first) first = false; else expect(","); if (first) first = false; else expect(",");