Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

grammar: optimized some `.concat`s

  • Loading branch information...
commit 85d7168f99efa4be29a60abd78d3e9889b553825 1 parent 2c7b590
Satoshi Murakami authored
Showing with 36 additions and 21 deletions.
  1. +15 −7 lib/grammar.js
  2. +12 −6 lib/parser.js
  3. +9 −8 src/grammar.co
22 lib/grammar.js
View
@@ -5,7 +5,7 @@ o = function(patterns, action, options){
patterns = patterns.trim().split(/\s+/);
action && (action = action === ditto
? last
- : (action + "").replace(/^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/, '$$$$ = $1;').replace(/\b(?!Er)[A-Z][\w.]*/g, 'yy.$&').replace(/\.L\(/g, '$&yylineno, '));
+ : (action + "").replace(/^function\s*\(\)\s*\{\s*([\s\S]*\S)\s*\}/, '$1').replace(/\breturn\b/, '$$$$ =').replace(/\b(?!Er)[A-Z][\w.]*/g, 'yy.$&').replace(/\.L\(/g, '$&yylineno, '));
return [patterns, last = action || '', options];
};
bnf = {
@@ -94,10 +94,14 @@ bnf = {
}), o('Arg', function(){
return [$1];
}), o('ArgList , Arg', function(){
- return $1.concat($3);
+ $1.push($3);
+ return $1;
}), o('ArgList OptComma NEWLINE Arg', function(){
+ $1.push($4);
+ return $1;
+ }), o('ArgList OptComma INDENT ArgList OptComma DEDENT', function(){
return $1.concat($4);
- }), o('ArgList OptComma INDENT ArgList OptComma DEDENT', ditto)
+ })
],
Arg: [
o('Expression'), o('... Expression', function(){
@@ -193,7 +197,8 @@ bnf = {
o('Expression', function(){
return [$1];
}), o('Exprs , Expression', function(){
- return $1.concat($3);
+ $1.push($3);
+ return $1;
})
],
KeyValue: [
@@ -232,9 +237,11 @@ bnf = {
}), o('Property', function(){
return [$1];
}), o('Properties , Property', function(){
- return $1.concat($3);
+ $1.push($3);
+ return $1;
}), o('Properties OptComma NEWLINE Property', function(){
- return $1.concat($4);
+ $1.push($4);
+ return $1;
}), o('INDENT Properties OptComma DEDENT', function(){
return $2;
})
@@ -321,7 +328,8 @@ bnf = {
o('CASE Exprs Block', function(){
return [new Case($2, $3)];
}), o('Cases CASE Exprs Block', function(){
- return $1.concat(new Case($3, $4));
+ $1.push(new Case($3, $4));
+ return $1;
})
],
OptExtends: [
18 lib/parser.js
View
@@ -79,9 +79,11 @@ case 38:this.$ = [];
break;
case 39:this.$ = [$$[$0]];
break;
-case 40:this.$ = $$[$0-2].concat($$[$0]);
+case 40:$$[$0-2].push($$[$0]);
+ this.$ = $$[$0-2];
break;
-case 41:this.$ = $$[$0-3].concat($$[$0]);
+case 41:$$[$0-3].push($$[$0]);
+ this.$ = $$[$0-3];
break;
case 42:this.$ = $$[$0-5].concat($$[$0-2]);
break;
@@ -171,7 +173,8 @@ case 94:this.$ = yy.Decl($$[$0-4], $$[$0-2], yylineno + 1);
break;
case 95:this.$ = [$$[$0]];
break;
-case 96:this.$ = $$[$0-2].concat($$[$0]);
+case 96:$$[$0-2].push($$[$0]);
+ this.$ = $$[$0-2];
break;
case 98:this.$ = yy.Prop(yy.L(yylineno, yy.Key($$[$0], $$[$0] !== 'arguments' && $$[$0] !== 'eval')), yy.L(yylineno, yy.Literal($$[$0])));
break;
@@ -201,9 +204,11 @@ case 111:this.$ = [];
break;
case 112:this.$ = [$$[$0]];
break;
-case 113:this.$ = $$[$0-2].concat($$[$0]);
+case 113:$$[$0-2].push($$[$0]);
+ this.$ = $$[$0-2];
break;
-case 114:this.$ = $$[$0-3].concat($$[$0]);
+case 114:$$[$0-3].push($$[$0]);
+ this.$ = $$[$0-3];
break;
case 115:this.$ = $$[$0-2];
break;
@@ -274,7 +279,8 @@ case 132:this.$ = yy.If($$[$0-3], $$[$0-2]).addElse($$[$0]);
break;
case 133:this.$ = [new yy.Case($$[$0-1], $$[$0])];
break;
-case 134:this.$ = $$[$0-3].concat(new yy.Case($$[$0-1], $$[$0]));
+case 134:$$[$0-3].push(new yy.Case($$[$0-1], $$[$0]));
+ this.$ = $$[$0-3];
break;
case 135:this.$ = $$[$0];
break;
17 src/grammar.co
View
@@ -32,7 +32,8 @@ o = (patterns, action, options) ->
patterns.=trim!split /\s+/
action &&= if action is ditto then last else
"#action"
- .replace /^function\s*\(\)\s*\{\s*return\s*([\s\S]*);\s*\}/ '$$$$ = $1;'
+ .replace /^function\s*\(\)\s*\{\s*([\s\S]*\S)\s*\}/ '$1'
+ .replace /\breturn\b/ '$$$$ ='
.replace /\b(?!Er)[A-Z][\w.]*/g \yy.$&
.replace /\.L\(/g '$&yylineno, '
[patterns, last := action or '', options]
@@ -125,9 +126,9 @@ bnf =
ArgList:
o '' -> []
o \Arg -> [$1]
- o 'ArgList , Arg' -> $1.concat $3
- o 'ArgList OptComma NEWLINE Arg' -> $1.concat $4
- o 'ArgList OptComma INDENT ArgList OptComma DEDENT' ditto
+ o 'ArgList , Arg' -> $1.push $3; $1
+ o 'ArgList OptComma NEWLINE Arg' -> $1.push $4; $1
+ o 'ArgList OptComma INDENT ArgList OptComma DEDENT' -> $1.concat $4
Arg:
o \Expression
o '... Expression' -> Splat $2
@@ -232,7 +233,7 @@ bnf =
Exprs:
o \Expression -> [$1]
- o 'Exprs , Expression' -> $1.concat $3
+ o 'Exprs , Expression' -> $1.push $3; $1
# The various forms of property.
KeyValue:
@@ -260,8 +261,8 @@ bnf =
Properties:
o '' -> []
o \Property -> [$1]
- o 'Properties , Property' -> $1.concat $3
- o 'Properties OptComma NEWLINE Property' -> $1.concat $4
+ o 'Properties , Property' -> $1.push $3; $1
+ o 'Properties OptComma NEWLINE Property' -> $1.push $4; $1
o 'INDENT Properties OptComma DEDENT' -> $2
Parenthetical:
@@ -307,7 +308,7 @@ bnf =
Cases:
o 'CASE Exprs Block' -> [new Case $2, $3]
- o 'Cases CASE Exprs Block' -> $1.concat new Case $3, $4
+ o 'Cases CASE Exprs Block' -> $1.push new Case $3, $4; $1
OptExtends:
o 'EXTENDS Expression' -> $2
Please sign in to comment.
Something went wrong with that request. Please try again.