1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

nodes.coffee is continuing to roll along -- maybe a tenth implemented

This commit is contained in:
Jeremy Ashkenas 2010-02-08 19:49:39 -05:00
parent 135620b14a
commit aabfba9599
7 changed files with 730 additions and 699 deletions

View file

@ -84,6 +84,8 @@
return new AssignNode(new ValueNode(yytext), $3, 'object');
}), o("STRING ASSIGN Expression", function() {
return new AssignNode(new ValueNode(new LiteralNode(yytext)), $3, 'object');
}), o("NUMBER ASSIGN Expression", function() {
return new AssignNode(new ValueNode(new LiteralNode(yytext)), $3, 'object');
}), o("Comment")
],
// A return statement.
@ -210,208 +212,264 @@
return new ValueNode($1);
}), o("Range", function() {
return new ValueNode($1);
}),
// o "Value Accessor", -> $1.push($2)
o("Invocation Accessor", function() {
}), o("Value Accessor", function() {
return $1.push($2);
}), o("Invocation Accessor", function() {
return new ValueNode($1, [$2]);
})
],
// Accessing into an object or array, through dot or index notation.
Accessor: [o("PROPERTY_ACCESS IDENTIFIER", function() {
return new AccessorNode(yytext);
}), o("PROTOTYPE_ACCESS IDENTIFIER", function() {
return new AccessorNode(yytext, 'prototype');
}), o("SOAK_ACCESS IDENTIFIER", function() {
return new AccessorNode(yytext, 'soak');
}), o("Index"), o("Slice", function() {
return new SliceNode($1);
})
],
// Indexing into an object or array.
Index: [o("INDEX_START Expression INDEX_END", function() {
return new IndexNode($2);
})
],
// An object literal.
Object: [o("{ AssignList }", function() {
return new ObjectNode($2);
})
],
// Assignment within an object literal (comma or newline separated).
AssignList: [o("", function() {
return [];
}), o("AssignObj", function() {
return [$1];
}), o("AssignList , AssignObj", function() {
return $1.push($3);
}), o("AssignList TERMINATOR AssignObj", function() {
return $1.push($3);
}), o("AssignList , TERMINATOR AssignObj", function() {
return $1.push($4);
}), o("INDENT AssignList OUTDENT", function() {
return $2;
})
],
// All flavors of function call (instantiation, super, and regular).
Call: [o("Invocation", function() {
return $1;
}), o("NEW Invocation", function() {
return $2.new_instance();
}), o("Super", function() {
return $1;
})
],
// Extending an object's prototype.
Extends: [o("Value EXTENDS Value", function() {
return new ExtendsNode($1, $3);
})
],
// A generic function invocation.
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 invocation.
Arguments: [o("CALL_START ArgList CALL_END", function() {
return $2;
})
],
// Calling super.
Super: [o("SUPER CALL_START ArgList CALL_END", function() {
return new CallNode('super', $3);
})
],
// The range literal.
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() {
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 ]", function() {
return new ArrayNode($2);
})
],
// A list of arguments to a method call, or as the contents of an array.
ArgList: [o("", function() {
return [];
}), o("Expression", function() {
return val;
}), o("INDENT Expression", function() {
return [$2];
}), o("ArgList , Expression", function() {
return $1.push($3);
}), o("ArgList TERMINATOR Expression", function() {
return $1.push($3);
}), o("ArgList , TERMINATOR Expression", function() {
return $1.push($4);
}), o("ArgList , INDENT Expression", function() {
return $1.push($4);
}), o("ArgList OUTDENT", function() {
return $1;
})
],
// Just simple, comma-separated, required arguments (no fancy syntax).
SimpleArgs: [o("Expression", function() {
return $1;
}), o("SimpleArgs , Expression", function() {
return ([$1].push($3)).reduce(function(a, b) {
return a.concat(b);
});
})
],
// Try/catch/finally exception handling blocks.
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);
}), o("TRY Block Catch FINALLY Block", function() {
return new TryNode($2, $3[0], $3[1], $5);
})
],
// A catch clause.
Catch: [o("CATCH IDENTIFIER Block", function() {
return [$2, $3];
})
],
// Throw an exception.
Throw: [o("THROW Expression", function() {
return new ThrowNode($2);
})
],
// Parenthetical expressions.
Parenthetical: [o("( Expression )", function() {
return new ParentheticalNode($2);
})
],
// The while loop. (there is no do..while).
While: [o("WHILE Expression Block", function() {
return new WhileNode($2, $3);
}), o("WHILE Expression", function() {
return new WhileNode($2, null);
}), o("Expression WHILE Expression", function() {
return new WhileNode($3, Expressions.wrap($1));
})
],
// Array comprehensions, including guard and current index.
// Looks a little confusing, check nodes.rb for the arguments to ForNode.
For: [o("Expression FOR ForVariables ForSource", function() {
return new ForNode($1, $4, $3[0], $3[1]);
}), o("FOR ForVariables ForSource Block", function() {
return new ForNode($4, $3, $2[0], $2[1]);
})
],
// An array comprehension has variables for the current element and index.
ForVariables: [o("IDENTIFIER", function() {
return [$1];
}), o("IDENTIFIER , IDENTIFIER", function() {
return [$1, $3];
})
],
// The source of the array comprehension can optionally be filtered.
ForSource: [o("IN Expression", function() {
return {
source: $2
};
}), o("OF Expression", function() {
return {
source: $2,
object: true
};
}), o("ForSource WHEN Expression", function() {
$1.filter = $3;
return $1;
}), o("ForSource BY Expression", function() {
$1.step = $3;
return $1;
})
],
// Switch/When blocks.
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);
})
],
// The inner list of whens.
Whens: [o("When", function() {
return $1;
}), o("Whens When", function() {
return $1.push($2);
})
],
// An individual when.
When: [o("LEADING_WHEN SimpleArgs Block", function() {
return new IfNode($2, $3, null, {
statement: true
});
}), o("LEADING_WHEN SimpleArgs Block TERMINATOR", function() {
return new IfNode($2, $3, null, {
statement: true
});
}), o("Comment TERMINATOR When", function() {
return $3.add_comment($1);
})
],
// The most basic form of "if".
IfBlock: [o("IF Expression Block", function() {
return new IfNode($2, $3);
})
],
// An elsif portion of an if-else block.
ElsIf: [o("ELSE IfBlock", function() {
return $2.force_statement();
})
],
// Multiple elsifs can be chained together.
ElsIfs: [o("ElsIf", function() {
return $1;
}), o("ElsIfs ElsIf", function() {
return $1.add_else($2);
})
],
// Terminating else bodies are strictly optional.
ElseBody: [o("", function() {
return null;
}), o("ELSE Block", function() {
return $2;
})
],
// All the alternatives for ending an if-else block.
IfEnd: [o("ElseBody", function() {
return $1;
}), o("ElsIfs ElseBody", function() {
return $1.add_else($2);
})
],
// The full complement of if blocks, including postfix one-liner ifs and unlesses.
If: [o("IfBlock IfEnd", function() {
return $1.add_else($2);
}), o("Expression IF Expression", function() {
return new IfNode($3, Expressions.wrap($1), null, {
statement: true
});
}), o("Expression UNLESS Expression", function() {
return new IfNode($3, Expressions.wrap($1), null, {
statement: true,
invert: true
});
})
]
// # Accessing into an object or array, through dot or index notation.
// Accessor: [
// o "PROPERTY_ACCESS IDENTIFIER", -> new AccessorNode($2)
// o "PROTOTYPE_ACCESS IDENTIFIER", -> new AccessorNode($2, 'prototype')
// o "SOAK_ACCESS IDENTIFIER", -> new AccessorNode($2, 'soak')
// o "Index"
// o "Slice", -> new SliceNode($1)
// ]
//
// # Indexing into an object or array.
// Index: [
// o "INDEX_START Expression INDEX_END", -> new IndexNode($2)
// ]
//
// # An object literal.
// Object: [
// o "{ AssignList }", -> new ObjectNode($2)
// ]
//
// # Assignment within an object literal (comma or newline separated).
// AssignList: [
// o "", -> []
// o "AssignObj", -> [$1]
// o "AssignList , AssignObj", -> $1.push $3
// o "AssignList TERMINATOR AssignObj", -> $1.push $3
// o "AssignList , TERMINATOR AssignObj", -> $1.push $4
// o "INDENT AssignList OUTDENT", -> $2
// ]
//
// # All flavors of function call (instantiation, super, and regular).
// Call: [
// o "Invocation", -> $1
// o "NEW Invocation", -> $2.new_instance()
// o "Super", -> $1
// ]
//
// # Extending an object's prototype.
// Extends: [
// o "Value EXTENDS Value", -> new ExtendsNode($1, $3)
// ]
//
// # A generic function invocation.
// Invocation: [
// o "Value Arguments", -> new CallNode($1, $2)
// o "Invocation Arguments", -> new CallNode($1, $2)
// ]
//
// # The list of arguments to a function invocation.
// Arguments: [
// o "CALL_START ArgList CALL_END", -> $2
// ]
//
// # Calling super.
// Super: [
// o "SUPER CALL_START ArgList CALL_END", -> new CallNode('super', $3)
// ]
//
// # The range literal.
// Range: [
// o "[ Expression . . Expression ]", -> new RangeNode($2, $5)
// o "[ Expression . . . Expression ]", -> new RangeNode($2, $6, true)
// ]
//
// # The slice literal.
// Slice: [
// o "INDEX_START Expression . . Expression INDEX_END", -> new RangeNode($2, $5)
// o "INDEX_START Expression . . . Expression INDEX_END", -> new RangeNode($2, $6, true)
// ]
//
// # The array literal.
// Array: [
// o "[ ArgList ]", -> new ArrayNode($2)
// ]
//
// # A list of arguments to a method call, or as the contents of an array.
// ArgList: [
// o "", -> []
// o "Expression", -> val
// o "INDENT Expression", -> [$2]
// o "ArgList , Expression", -> $1.push $3
// o "ArgList TERMINATOR Expression", -> $1.push $3
// o "ArgList , TERMINATOR Expression", -> $1.push $4
// o "ArgList , INDENT Expression", -> $1.push $4
// o "ArgList OUTDENT", -> $1
// ]
//
// # Just simple, comma-separated, required arguments (no fancy syntax).
// SimpleArgs: [
// o "Expression", -> $1
// o "SimpleArgs , Expression", ->
// ([$1].push($3)).reduce (a, b) -> a.concat(b)
// ]
//
// # Try/catch/finally exception handling blocks.
// Try: [
// o "TRY Block Catch", -> new TryNode($2, $3[0], $3[1])
// o "TRY Block FINALLY Block", -> new TryNode($2, nil, nil, $4)
// o "TRY Block Catch FINALLY Block", -> new TryNode($2, $3[0], $3[1], $5)
// ]
//
// # A catch clause.
// Catch: [
// o "CATCH IDENTIFIER Block", -> [$2, $3]
// ]
//
// # Throw an exception.
// Throw: [
// o "THROW Expression", -> new ThrowNode($2)
// ]
//
// # Parenthetical expressions.
// Parenthetical: [
// o "( Expression )", -> new ParentheticalNode($2)
// ]
//
// # The while loop. (there is no do..while).
// While: [
// o "WHILE Expression Block", -> new WhileNode($2, $3)
// o "WHILE Expression", -> new WhileNode($2, nil)
// o "Expression WHILE Expression", -> new WhileNode($3, Expressions.wrap($1))
// ]
//
// # Array comprehensions, including guard and current index.
// # Looks a little confusing, check nodes.rb for the arguments to ForNode.
// For: [
// o "Expression FOR ForVariables ForSource", -> new ForNode($1, $4, $3[0], $3[1])
// o "FOR ForVariables ForSource Block", -> new ForNode($4, $3, $2[0], $2[1])
// ]
//
// # An array comprehension has variables for the current element and index.
// ForVariables: [
// o "IDENTIFIER", -> [$1]
// o "IDENTIFIER , IDENTIFIER", -> [$1, $3]
// ]
//
// # The source of the array comprehension can optionally be filtered.
// ForSource: [
// o "IN Expression", -> {source: $2}
// o "OF Expression", -> {source: $2, object: true}
// o "ForSource WHEN Expression", -> $1.filter: $3; $1
// o "ForSource BY Expression", -> $1.step: $3; $1
// ]
//
// # Switch/When blocks.
// Switch: [
// o "SWITCH Expression INDENT Whens OUTDENT", -> $4.rewrite_condition($2)
// o "SWITCH Expression INDENT Whens ELSE Block OUTDENT", -> $4.rewrite_condition($2).add_else($6)
// ]
//
// # The inner list of whens.
// Whens: [
// o "When", -> $1
// o "Whens When", -> $1.push $2
// ]
//
// # An individual when.
// When: [
// o "LEADING_WHEN SimpleArgs Block", -> new IfNode($2, $3, nil, {statement: true})
// o "LEADING_WHEN SimpleArgs Block TERMINATOR", -> new IfNode($2, $3, nil, {statement: true})
// o "Comment TERMINATOR When", -> $3.add_comment($1)
// ]
//
// # The most basic form of "if".
// IfBlock: [
// o "IF Expression Block", -> new IfNode($2, $3)
// ]
//
// # An elsif portion of an if-else block.
// ElsIf: [
// o "ELSE IfBlock", -> $2.force_statement()
// ]
//
// # Multiple elsifs can be chained together.
// ElsIfs: [
// o "ElsIf", -> $1
// o "ElsIfs ElsIf", -> $1.add_else($2)
// ]
//
// # Terminating else bodies are strictly optional.
// ElseBody: [
// o "", -> null
// o "ELSE Block", -> $2
// ]
//
// # All the alternatives for ending an if-else block.
// IfEnd: [
// o "ElseBody", -> $1
// o "ElsIfs ElseBody", -> $1.add_else($2)
// ]
//
// # The full complement of if blocks, including postfix one-liner ifs and unlesses.
// If: [
// o "IfBlock IfEnd", -> $1.add_else($2)
// o "Expression IF Expression", -> new IfNode($3, Expressions.wrap($1), nil, {statement: true})
// o "Expression UNLESS Expression", -> new IfNode($3, Expressions.wrap($1), nil, {statement: true, invert: true})
// ]
};
// Helpers ==============================================================
// Make the Jison parser.