Skip to content

Commit

Permalink
add parallel{} syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
guileen committed May 9, 2011
1 parent 46d2d54 commit 21d584b
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions lib/index.js
Expand Up @@ -6,10 +6,11 @@
* close block in a single line without expression. e.g. `foo.bar();}` is wrong
* close ONE block in ONE line, e.g. ` });});` is wrong , `}}` is wrong.
*/
var ex_async = /^(\s*)((var\s)?([^=]+)=\s*)?(.*\(\s*)([^\)]*)\b_\b\s*\);?(.*)/,
ex_fn = /^(.*[\(\s=:&|]?function(?:\s[^\(]*|)\(\s*)([^\)]*)\b_\b(\s*\)\s*\{.*)/,
ex_nor_fn = /^.*[\(\s=:&|]?function(?:\s[^\(]*|)\(\s*[^\)]*\)\s*\{.*/,
ex_return = /^(.*[\s\{&|])?return\s+([^;]*)(.*)/;
var ex_async = /^(\s*)((var\s)?([^=]+)=\s*)?(.*\(\s*)([^\)]*)\b_\b\s*\);?(.*)/,
ex_fn = /^(.*[\(\s=:&|]?function(?:\s[^\(]*|)\(\s*)([^\)]*)\b_\b(\s*\)\s*\{.*)/,
ex_nor_fn = /^.*[\(\s=:&|]?function(?:\s[^\(]*|)\(\s*[^\)]*\)\s*\{.*/,
ex_parallel = /^\s*parallel\s*\{\s*$/,
ex_return = /^(.*[\s\{&|])?return\s+([^;]*)(.*)/;

function getDeltaBlocks(line){
var close=false,levels = 0;
Expand Down Expand Up @@ -49,6 +50,7 @@ function compile(codes){
foo = m[5],
args = m[6],
tail = m[7],
prefix = '',
cb = 'function(__err' + (assignment && ', ' || ''),
errHandler = '){if(__err)return __cb(__err);';

Expand All @@ -65,22 +67,46 @@ function compile(codes){
}else{
cb += errHandler;
}
results.push( indent + foo + args + cb + tail);

if(block.in_parallel){
block.count_parallel++;
if(block.after_async){
results[i-1] += '__update();';
prefix = '});';
block.level--;
}
block.after_async = true;
}

results.push( indent + prefix + foo + args + cb + tail);
}
return !!m;
}

function parse_default(){
if(deltaBlocks[0] || i == len-1){
var close = ''
while(blocks[last_block_level].level-- > 0) close += '});';
var close = '',
last_block = blocks[last_block_level];

while(last_block.level-- > 0) close += '});';
line = line.replace(/^\s*/, '$&' + close);

if(last_block.in_parallel){
if(last_block.after_async){
results[i-1] += '__update();';
}
results[last_block.start_line] += ('var __count=' + last_block.count_parallel +';');
line += ')(function(__err){if(__err)return __cb(__err);';
block.level ++;
}

// only 1 block will be fully closed. so don't put 2 close } in 1 line.
blocks[last_block_level].level = 0;
last_block.level = 0;
// leave level
if(deltaBlocks[1] < 0) blocks[last_block_level] = null;
line = line.replace(/^\s*/, '$&' + close);
}
if(block.level > 0 && /\/\/\s*}/.test(line)){
line = line.replace(/^\s*/,'$&'+'});');
line = line.replace(/^\s*/,'$&});');
block.level--;
}
results.push(line);
Expand Down Expand Up @@ -112,6 +138,17 @@ function compile(codes){
return false;
}

function parse_parallel(){
if(ex_parallel.test(line)){
block.in_parallel = true;
block.start_line = i;
block.count_parallel = 0;
results.push(line.replace(/\S.*$/,'(function(__cb){function __update(){if(--__count==0)__cb()}'));
return true;
}
return false;
}

for(; i< len; i++){
line = lines[i];
deltaBlocks = getDeltaBlocks(line);
Expand All @@ -123,8 +160,7 @@ function compile(codes){
in_async: (blocks[last_block_level] && blocks[last_block_level].in_async)
});

parse_fn() || parse_async() || parse_return() || parse_default();

parse_parallel() || parse_fn() || parse_async() || parse_return() || parse_default();
}
results[0] = 'var __cb=global.__cb || function(e){console.log(e)};' + results[0];
return results.join('\n');
Expand Down

0 comments on commit 21d584b

Please sign in to comment.