adding pattern matching for comprehensions.

This commit is contained in:
Jeremy Ashkenas 2010-04-26 23:46:35 -04:00
parent d62baf5a5d
commit b5606a247d
3 changed files with 133 additions and 70 deletions

View File

@ -49,14 +49,16 @@
grammar = {
// The **Root** is the top-level node in the syntax tree. Since we parse bottom-up,
// all parsing must end here.
Root: [o("", function() {
Root: [
o("", function() {
return new Expressions();
}), o("TERMINATOR", function() {
return new Expressions();
}), o("Body"), o("Block TERMINATOR")
],
// Any list of statements and expressions, seperated by line breaks or semicolons.
Body: [o("Line", function() {
Body: [
o("Line", function() {
return Expressions.wrap([$1]);
}), o("Body TERMINATOR Line", function() {
return $1.push($3);
@ -65,7 +67,8 @@
// Expressions and statements, which make up a line in a body.
Line: [o("Expression"), o("Statement")],
// Pure statements which cannot be expressions.
Statement: [o("Return"), o("Throw"), o("BREAK", function() {
Statement: [
o("Return"), o("Throw"), o("BREAK", function() {
return new LiteralNode($1);
}), o("CONTINUE", function() {
return new LiteralNode($1);
@ -79,7 +82,8 @@
// 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.
Block: [o("INDENT Body OUTDENT", function() {
Block: [
o("INDENT Body OUTDENT", function() {
return $2;
}), o("INDENT OUTDENT", function() {
return new Expressions();
@ -88,13 +92,15 @@
})
],
// A literal identifier, a variable name or property.
Identifier: [o("IDENTIFIER", function() {
Identifier: [
o("IDENTIFIER", function() {
return new LiteralNode($1);
})
],
// Alphanumerics are separated from the other **Literal** matchers because
// they can also serve as keys in object literals.
AlphaNumeric: [o("NUMBER", function() {
AlphaNumeric: [
o("NUMBER", function() {
return new LiteralNode($1);
}), o("STRING", function() {
return new LiteralNode($1);
@ -102,7 +108,8 @@
],
// All of our immediate values. These can (in general), be passed straight
// through and printed to JavaScript.
Literal: [o("AlphaNumeric"), o("JS", function() {
Literal: [
o("AlphaNumeric"), o("JS", function() {
return new LiteralNode($1);
}), o("REGEX", function() {
return new LiteralNode($1);
@ -121,13 +128,15 @@
})
],
// Assignment of a variable, property, or index to a value.
Assign: [o("Assignable ASSIGN Expression", function() {
Assign: [
o("Assignable ASSIGN Expression", function() {
return new AssignNode($1, $3);
})
],
// Assignment when it happens within an object literal. The difference from
// the ordinary **Assign** is that these allow numbers and strings as keys.
AssignObj: [o("Identifier", function() {
AssignObj: [
o("Identifier", function() {
return new ValueNode($1);
}), o("AlphaNumeric"), o("Identifier ASSIGN Expression", function() {
return new AssignNode(new ValueNode($1), $3, 'object');
@ -136,7 +145,8 @@
}), o("Comment")
],
// A return statement from a function body.
Return: [o("RETURN Expression", function() {
Return: [
o("RETURN Expression", function() {
return new ReturnNode($2);
}), o("RETURN", function() {
return new ReturnNode(new ValueNode(new LiteralNode('null')));
@ -145,19 +155,22 @@
// A comment. Because CoffeeScript passes comments through to JavaScript, we
// have to parse comments like any other construct, and identify all of the
// positions in which they can occur in the grammar.
Comment: [o("COMMENT", function() {
Comment: [
o("COMMENT", function() {
return new CommentNode($1);
})
],
// [The existential operator](http://jashkenas.github.com/coffee-script/#existence).
Existence: [o("Expression ?", function() {
Existence: [
o("Expression ?", function() {
return new ExistenceNode($1);
})
],
// The **Code** node is the function literal. It's defined by an indented block
// of **Expressions** preceded by a function arrow, with an optional parameter
// list.
Code: [o("PARAM_START ParamList PARAM_END FuncGlyph Block", function() {
Code: [
o("PARAM_START ParamList PARAM_END FuncGlyph Block", function() {
return new CodeNode($2, $5, $4);
}), o("FuncGlyph Block", function() {
return new CodeNode([], $2, $1);
@ -165,7 +178,8 @@
],
// CoffeeScript has two different symbols for functions. `->` is for ordinary
// functions, and `=>` is for functions bound to the current value of *this*.
FuncGlyph: [o("->", function() {
FuncGlyph: [
o("->", function() {
return 'func';
}), o("=>", function() {
return 'boundfunc';
@ -174,7 +188,8 @@
// An optional, trailing comma.
OptComma: [o(''), o(',')],
// The list of parameters that a function accepts can be of any length.
ParamList: [o("", function() {
ParamList: [
o("", function() {
return [];
}), o("Param", function() {
return [$1];
@ -184,19 +199,22 @@
],
// A single parameter in a function definition can be ordinary, or a splat
// that hoovers up the remaining arguments.
Param: [o("PARAM", function() {
Param: [
o("PARAM", function() {
return new LiteralNode($1);
}), o("Param . . .", function() {
return new SplatNode($1);
})
],
// A splat that occurs outside of a parameter list.
Splat: [o("Expression . . .", function() {
Splat: [
o("Expression . . .", function() {
return new SplatNode($1);
})
],
// Variables and properties that can be assigned to.
SimpleAssignable: [o("Identifier", function() {
SimpleAssignable: [
o("Identifier", function() {
return new ValueNode($1);
}), o("Value Accessor", function() {
return $1.push($2);
@ -205,7 +223,8 @@
}), o("ThisProperty")
],
// Everything that can be assigned to.
Assignable: [o("SimpleAssignable"), o("Array", function() {
Assignable: [
o("SimpleAssignable"), o("Array", function() {
return new ValueNode($1);
}), o("Object", function() {
return new ValueNode($1);
@ -213,7 +232,8 @@
],
// The types of things that can be treated as values -- assigned to, invoked
// as functions, indexed into, named as a class, etc.
Value: [o("Assignable"), o("Literal", function() {
Value: [
o("Assignable"), o("Literal", function() {
return new ValueNode($1);
}), o("Parenthetical", function() {
return new ValueNode($1);
@ -225,7 +245,8 @@
],
// The general group of accessors into an object, by property, by prototype
// or by array index or slice.
Accessor: [o("PROPERTY_ACCESS Identifier", function() {
Accessor: [
o("PROPERTY_ACCESS Identifier", function() {
return new AccessorNode($2);
}), o("PROTOTYPE_ACCESS Identifier", function() {
return new AccessorNode($2, 'prototype');
@ -238,20 +259,23 @@
})
],
// Indexing into an object or array using bracket notation.
Index: [o("INDEX_START Expression INDEX_END", function() {
Index: [
o("INDEX_START Expression INDEX_END", function() {
return new IndexNode($2);
}), o("SOAKED_INDEX_START Expression SOAKED_INDEX_END", function() {
return new IndexNode($2, 'soak');
})
],
// In CoffeeScript, an object literal is simply a list of assignments.
Object: [o("{ AssignList OptComma }", function() {
Object: [
o("{ AssignList OptComma }", function() {
return new ObjectNode($2);
})
],
// Assignment of properties within an object literal can be separated by
// comma, as in JavaScript, or simply by newline.
AssignList: [o("", function() {
AssignList: [
o("", function() {
return [];
}), o("AssignObj", function() {
return [$1];
@ -267,7 +291,8 @@
],
// Class definitions have optional bodies of prototype property assignments,
// and optional references to the superclass.
Class: [o("CLASS SimpleAssignable", function() {
Class: [
o("CLASS SimpleAssignable", function() {
return new ClassNode($2);
}), o("CLASS SimpleAssignable EXTENDS Value", function() {
return new ClassNode($2, $4);
@ -278,14 +303,16 @@
})
],
// Assignments that can happen directly inside a class declaration.
ClassAssign: [o("AssignObj", function() {
ClassAssign: [
o("AssignObj", function() {
return $1;
}), o("ThisProperty ASSIGN Expression", function() {
return new AssignNode(new ValueNode($1), $3, 'this');
})
],
// A list of assignments to a class.
ClassBody: [o("", function() {
ClassBody: [
o("", function() {
return [];
}), o("ClassAssign", function() {
return [$1];
@ -295,73 +322,85 @@
],
// The three flavors of function call: normal, object instantiation with `new`,
// and calling `super()`
Call: [o("Invocation"), o("NEW Invocation", function() {
Call: [
o("Invocation"), o("NEW Invocation", function() {
return $2.new_instance();
}), o("Super")
],
// Binds a function call to a context and/or arguments.
Curry: [o("Value <- Arguments", function() {
Curry: [
o("Value <- Arguments", function() {
return new CurryNode($1, $3);
})
],
// Extending an object by setting its prototype chain to reference a parent
// object.
Extends: [o("SimpleAssignable EXTENDS Value", function() {
Extends: [
o("SimpleAssignable EXTENDS Value", function() {
return new ExtendsNode($1, $3);
})
],
// Ordinary function invocation, or a chained series of calls.
Invocation: [o("Value Arguments", function() {
Invocation: [
o("Value Arguments", function() {
return new CallNode($1, $2);
}), o("Invocation Arguments", function() {
return new CallNode($1, $2);
})
],
// The list of arguments to a function call.
Arguments: [o("CALL_START ArgList OptComma CALL_END", function() {
Arguments: [
o("CALL_START ArgList OptComma CALL_END", function() {
return $2;
})
],
// Calling super.
Super: [o("SUPER CALL_START ArgList OptComma CALL_END", function() {
Super: [
o("SUPER CALL_START ArgList OptComma CALL_END", function() {
return new CallNode('super', $3);
})
],
// A reference to the *this* current object.
This: [o("THIS", function() {
This: [
o("THIS", function() {
return new ValueNode(new LiteralNode('this'));
}), o("@", function() {
return new ValueNode(new LiteralNode('this'));
})
],
// A reference to a property on *this*.
ThisProperty: [o("@ Identifier", function() {
ThisProperty: [
o("@ Identifier", function() {
return new ValueNode(new LiteralNode('this'), [new AccessorNode($2)]);
})
],
// The CoffeeScript range literal.
Range: [o("[ Expression . . Expression ]", function() {
Range: [
o("[ Expression . . Expression ]", function() {
return new RangeNode($2, $5);
}), o("[ Expression . . . Expression ]", function() {
return new RangeNode($2, $6, true);
})
],
// The slice literal.
Slice: [o("INDEX_START Expression . . Expression INDEX_END", function() {
Slice: [
o("INDEX_START Expression . . Expression INDEX_END", function() {
return new RangeNode($2, $5);
}), o("INDEX_START Expression . . . Expression INDEX_END", function() {
return new RangeNode($2, $6, true);
})
],
// The array literal.
Array: [o("[ ArgList OptComma ]", function() {
Array: [
o("[ ArgList OptComma ]", function() {
return new ArrayNode($2);
})
],
// The **ArgList** is both the list of objects passed into a function call,
// as well as the contents of an array literal
// (i.e. comma-separated expressions). Newlines work as well.
ArgList: [o("", function() {
ArgList: [
o("", function() {
return [];
}), o("Expression", function() {
return [$1];
@ -380,7 +419,8 @@
// Just simple, comma-separated, required arguments (no fancy syntax). We need
// this to be separate from the **ArgList** for use in **Switch** blocks, where
// having the newlines wouldn't make sense.
SimpleArgs: [o("Expression"), o("SimpleArgs , Expression", function() {
SimpleArgs: [
o("Expression"), o("SimpleArgs , Expression", function() {
if ($1 instanceof Array) {
return $1.concat([$3]);
} else {
@ -389,7 +429,8 @@
})
],
// The variants of *try/catch/finally* exception handling blocks.
Try: [o("TRY Block Catch", function() {
Try: [
o("TRY Block Catch", function() {
return new TryNode($2, $3[0], $3[1]);
}), o("TRY Block FINALLY Block", function() {
return new TryNode($2, null, null, $4);
@ -398,12 +439,14 @@
})
],
// A catch clause names its error and runs a block of code.
Catch: [o("CATCH Identifier Block", function() {
Catch: [
o("CATCH Identifier Block", function() {
return [$2, $3];
})
],
// Throw an exception object.
Throw: [o("THROW Expression", function() {
Throw: [
o("THROW Expression", function() {
return new ThrowNode($2);
})
],
@ -411,7 +454,8 @@
// not an **Expression**, so if you need to use an expression in a place
// where only values are accepted, wrapping it in parentheses will always do
// the trick.
Parenthetical: [o("( Line )", function() {
Parenthetical: [
o("( Line )", function() {
return new ParentheticalNode($2);
})
],
@ -419,7 +463,8 @@
// it through unaltered.
Extension: [o("EXTENSION")],
// The condition portion of a while loop.
WhileSource: [o("WHILE Expression", function() {
WhileSource: [
o("WHILE Expression", function() {
return new WhileNode($2);
}), o("WHILE Expression WHEN Expression", function() {
return new WhileNode($2, {
@ -429,7 +474,8 @@
],
// The while loop can either be normal, with a block of expressions to execute,
// or postfix, with a single expression. There is no do..while.
While: [o("WhileSource Block", function() {
While: [
o("WhileSource Block", function() {
return $1.add_body($2);
}), o("Statement WhileSource", function() {
return $2.add_body(Expressions.wrap([$1]));
@ -440,7 +486,8 @@
// Array, object, and range comprehensions, at the most generic level.
// Comprehensions can either be normal, with a block of expressions to execute,
// or postfix, with a single expression.
For: [o("Statement FOR ForVariables ForSource", function() {
For: [
o("Statement FOR ForVariables ForSource", function() {
return new ForNode($1, $4, $3[0], $3[1]);
}), o("Expression FOR ForVariables ForSource", function() {
return new ForNode($1, $4, $3[0], $3[1]);
@ -450,7 +497,8 @@
],
// An array of all accepted values for a variable inside the loop. This
// enables support for pattern matching.
ForValue: [o("Identifier"), o("Array", function() {
ForValue: [
o("Identifier"), o("Array", function() {
return new ValueNode($1);
}), o("Object", function() {
return new ValueNode($1);
@ -459,7 +507,8 @@
// An array or range comprehension has variables for the current element and
// (optional) reference to the current index. Or, *key, value*, in the case
// of object comprehensions.
ForVariables: [o("ForValue", function() {
ForVariables: [
o("ForValue", function() {
return [$1];
}), o("ForValue , ForValue", function() {
return [$1, $3];
@ -468,7 +517,8 @@
// The source of a comprehension is an array or object with an optional filter
// clause. If it's an array comprehension, you can also choose to step through
// in fixed-size increments.
ForSource: [o("IN Expression", function() {
ForSource: [
o("IN Expression", function() {
return {
source: $2
};
@ -509,7 +559,8 @@
],
// The CoffeeScript switch/when/else block replaces the JavaScript
// switch/case/default by compiling into an if-else chain.
Switch: [o("SWITCH Expression INDENT Whens OUTDENT", function() {
Switch: [
o("SWITCH Expression INDENT Whens OUTDENT", function() {
return $4.rewrite_condition($2);
}), o("SWITCH Expression INDENT Whens ELSE Block OUTDENT", function() {
return $4.rewrite_condition($2).add_else($6, true);
@ -517,12 +568,14 @@
],
// The inner list of whens is left recursive. At code-generation time, the
// IfNode will rewrite them into a proper chain.
Whens: [o("When"), o("Whens When", function() {
Whens: [
o("When"), o("Whens When", function() {
return $1.push($2);
})
],
// An individual **When** clause, with action.
When: [o("LEADING_WHEN SimpleArgs Block", function() {
When: [
o("LEADING_WHEN SimpleArgs Block", function() {
return new IfNode($2, $3, null, {
statement: true
});
@ -538,25 +591,29 @@
// The most basic form of *if* is a condition and an action. The following
// if-related rules are broken up along these lines in order to avoid
// ambiguity.
IfStart: [o("IF Expression Block", function() {
IfStart: [
o("IF Expression Block", function() {
return new IfNode($2, $3);
}), o("IfStart ElsIf", function() {
return $1.add_else($2);
})
],
// An **IfStart** can optionally be followed by an else block.
IfBlock: [o("IfStart"), o("IfStart ELSE Block", function() {
IfBlock: [
o("IfStart"), o("IfStart ELSE Block", function() {
return $1.add_else($3);
})
],
// An *else if* continuation of the *if* expression.
ElsIf: [o("ELSE IF Expression Block", function() {
ElsIf: [
o("ELSE IF Expression Block", function() {
return (new IfNode($3, $4)).force_statement();
})
],
// The full complement of *if* expressions, including postfix one-liner
// *if* and *unless*.
If: [o("IfBlock"), o("Statement IF Expression", function() {
If: [
o("IfBlock"), o("Statement IF Expression", function() {
return new IfNode($3, Expressions.wrap([$1]), null, {
statement: true
});
@ -582,7 +639,8 @@
// combine most of these rules into a single generic *Operand OpSymbol Operand*
// -type rule, but in order to make the precedence binding possible, separate
// rules are necessary.
Operation: [o("! Expression", function() {
Operation: [
o("! Expression", function() {
return new OpNode('!', $2);
}), o("!! Expression", function() {
return new OpNode('!!', $2);

View File

@ -688,7 +688,8 @@
var arr, body, name;
name = o.scope.free_variable();
body = Expressions.wrap([literal(name)]);
arr = Expressions.wrap([new ForNode(body, {
arr = Expressions.wrap([
new ForNode(body, {
source: (new ValueNode(this))
}, literal(name))
]);
@ -776,7 +777,7 @@
};
__extends(ArrayNode, BaseNode);
ArrayNode.prototype.compile_node = function compile_node(o) {
var _b, _c, code, ending, i, obj, objects;
var _b, _c, code, i, obj, objects;
o.indent = this.idt(1);
objects = [];
_b = this.objects;
@ -794,8 +795,11 @@
}
}
objects = objects.join('');
ending = objects.indexOf('\n') >= 0 ? ("\n" + this.tab + "]") : ']';
return "[" + objects + ending;
if (objects.indexOf('\n') >= 0) {
return "[\n" + (this.idt(1)) + objects + "\n" + this.tab + "]";
} else {
return "[" + objects + "]";
}
};
return ArrayNode;
})();
@ -1524,9 +1528,10 @@
index_var = null;
source_part = ("" + svar + " = " + (this.source.compile(o)) + ";\n" + this.tab);
if (this.pattern) {
o.indent = this.idt(1);
o.top = true;
var_part = new AssignNode(this.name, literal(("" + svar + "[" + ivar + "]"))).compile(o) + "\n";
var_part = new AssignNode(this.name, literal(("" + svar + "[" + ivar + "]"))).compile(merge(o, {
indent: this.idt(1),
top: true
})) + "\n";
} else if (name) {
var_part = ("" + body_dent + name + " = " + svar + "[" + ivar + "];\n");
}

View File

@ -571,8 +571,10 @@ exports.ArrayNode: class ArrayNode extends BaseNode
else
objects.push "$code, "
objects: objects.join('')
ending: if objects.indexOf('\n') >= 0 then "\n$@tab]" else ']'
"[$objects$ending"
if objects.indexOf('\n') >= 0
"[\n${@idt(1)}$objects\n$@tab]"
else
"[$objects]"
#### ClassNode
@ -1124,9 +1126,7 @@ exports.ForNode: class ForNode extends BaseNode
index_var: null
source_part: "$svar = ${ @source.compile(o) };\n$@tab"
if @pattern
o.indent: @idt 1
o.top: true
var_part: new AssignNode(@name, literal("$svar[$ivar]")).compile(o) + "\n"
var_part: new AssignNode(@name, literal("$svar[$ivar]")).compile(merge o, {indent: @idt(1), top: true}) + "\n"
else
var_part: "$body_dent$name = $svar[$ivar];\n" if name
if not @object