Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Commit

Permalink
Added Unix-like piping. Allows chaining of function calls where every…
Browse files Browse the repository at this point in the history
… succeeding call receives as first argument the result of all preceding expressions.
  • Loading branch information
StanAngeloff committed Mar 27, 2010
1 parent 8f3ea1d commit 7ee10e0
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 167 deletions.
12 changes: 9 additions & 3 deletions lib/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
// CoffeeScript is the **Expression** -- you'll notice that there is no
// "statement" nonterminal. Expressions serve as the building blocks
// of many other rules, making them somewhat circular.
Expression: [o("Value"), o("Call"), o("Curry"), o("Code"), o("Operation"), o("Assign"), o("If"), o("Try"), o("Throw"), o("Return"), o("While"), o("For"), o("Switch"), o("Extends"), o("Class"), o("Splat"), o("Existence"), o("Comment"), o("Extension")],
Expression: [o("Value"), o("Pipe"), o("Call"), o("Curry"), o("Code"), o("Operation"), o("Assign"), o("If"), o("Try"), o("Throw"), o("Return"), o("While"), o("For"), o("Switch"), o("Extends"), o("Class"), o("Splat"), o("Existence"), o("Comment"), o("Extension")],
// A an indented block of expressions. Note that the [Rewriter](rewriter.html)
// will convert some postfix forms into blocks for us, by adjusting the
// token stream.
Expand Down Expand Up @@ -272,6 +272,14 @@
return $2;
})
],
// Unix-like pipe for chained function calls. Reverts to `OpNode` for bitwise
// operations.
Pipe: [o("Expression | Expression", function() {
return new PipeNode($1, $3);
}), o("Expression | || Expression", function() {
return new PipeNode($1, $4, true);
})
],
// The three flavors of function call: normal, object instantiation with `new`,
// and calling `super()`
Call: [o("Invocation"), o("NEW Invocation", function() {
Expand Down Expand Up @@ -565,8 +573,6 @@
return new OpNode('>>>', $1, $3);
}), o("Expression & Expression", function() {
return new OpNode('&', $1, $3);
}), o("Expression | Expression", function() {
return new OpNode('|', $1, $3);
}), o("Expression ^ Expression", function() {
return new OpNode('^', $1, $3);
}), o("Expression <= Expression", function() {
Expand Down
22 changes: 21 additions & 1 deletion lib/nodes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(function(){
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, CurryNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, ValueNode, WhileNode, compact, del, flatten, helpers, literal, merge, statement;
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, CurryNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PipeNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, ValueNode, WhileNode, compact, del, flatten, helpers, literal, merge, statement;
var __extends = function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
Expand Down Expand Up @@ -1711,6 +1711,26 @@
};
return IfNode;
}).call(this);
//### PipeNode
// Unix-like piping. Allows chaining of function calls where every succeeding
// call receives as first argument the result of all preceding expressions.
exports.PipeNode = (function() {
PipeNode = function PipeNode(left, right, fallback) {
this.children = [(this.left = left), (this.right = right)];
this.fallback = fallback;
return this;
};
__extends(PipeNode, BaseNode);
PipeNode.prototype.type = 'Pipe';
PipeNode.prototype.compile_node = function compile_node(o) {
if (this.right instanceof CallNode && !(this.right.is_new || this.right.is_super)) {
this.right.args.unshift(this.left);
return this.right.compile(o);
}
return new OpNode((this.fallback && this.left instanceof PipeNode ? '||' : '|'), this.left, this.right).compile(o);
};
return PipeNode;
}).call(this);
// Faux-Nodes
// ----------
//### PushNode
Expand Down
Loading

0 comments on commit 7ee10e0

Please sign in to comment.