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

fixing broken multi-line optional-paren blocks

This commit is contained in:
Jeremy Ashkenas 2010-03-10 09:28:00 -05:00
parent 21b5d2cac5
commit 1f9bb6a1c4
6 changed files with 27 additions and 11 deletions

View file

@ -95,6 +95,7 @@ alert reverse '.eeffoC yrT'</textarea></div>
<a href="documentation/docs/rewriter.html">The Rewriter &mdash; src/rewriter</a>
<a href="documentation/docs/nodes.html">The Syntax Tree &mdash; src/nodes</a>
<a href="documentation/docs/scope.html">Lexical Scope &mdash; src/scope</a>
<a href="documentation/docs/helpers.html">Helpers &amp; Utility Functions &mdash; src/helpers</a>
<a href="documentation/docs/coffee-script.html">The CoffeeScript Module &mdash; src/coffee-script</a>
<a href="documentation/docs/cake.html">Cake &amp; Cakefiles &mdash; src/cake</a>
<a href="documentation/docs/command.html">"coffee" Command-Line Utility &mdash; src/command</a>

View file

@ -1,6 +1,9 @@
(function(){
var __hasProp = Object.prototype.hasOwnProperty;
// Set up exported variables for both Node.js and the browser.
// This file contains the common helper functions that we'd like to share among
// the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten
// arrays, count characters, that sort of thing.
// Set up exported variables for both **Node.js** and the browser.
if (!((typeof process !== "undefined" && process !== null))) {
this.exports = this;
}
@ -36,8 +39,8 @@
return num;
};
// Merge objects, returning a fresh copy with attributes from both sides.
// Used every time `compile` is called, to allow properties in the options hash
// to propagate down the tree without polluting other branches.
// Used every time `BaseNode#compile` is called, to allow properties in the
// options hash to propagate down the tree without polluting other branches.
exports.merge = function merge(options, overrides) {
var _a, _b, fresh, key, val;
fresh = {};
@ -56,7 +59,7 @@
return fresh;
};
// Return a completely flattened version of an array. Handy for getting a
// list of `children`.
// list of `children` from the nodes.
exports.flatten = function flatten(array) {
var _a, _b, _c, item, memo;
memo = [];
@ -78,7 +81,7 @@
// Matches a balanced group such as a single or double-quoted string. Pass in
// a series of delimiters, all of which must be nested correctly within the
// contents of the string. This method allows us to have strings within
// interpolations within strings etc...
// interpolations within strings, ad infinitum.
exports.balanced_string = function balanced_string(str, delimited, options) {
var _a, _b, _c, _d, close, i, levels, open, pair, slash;
options = options || {};

View file

@ -149,7 +149,7 @@
calls = 0;
return this.scan_tokens((function(__this) {
var __func = function(prev, token, post, i) {
var _a, _b, _c, _d, idx, last, size, stack_pointer, tag, tmp;
var _a, _b, _c, _d, idx, last, open, size, stack_pointer, tag, tmp;
tag = token[0];
if (tag === 'CALL_START') {
calls += 1;
@ -161,7 +161,8 @@
last = stack.pop();
stack[stack.length - 1] += last;
}
if (tag === 'CALL_END' && calls < 0) {
open = stack[stack.length - 1] > 0;
if (tag === 'CALL_END' && calls < 0 && open) {
stack[stack.length - 1] -= 1;
this.tokens.splice(i, 0, ['CALL_END', ')', token[2]]);
return 2;
@ -170,7 +171,7 @@
if (tag === 'INDENT' && prev && include(IMPLICIT_BLOCK, prev[0])) {
return 1;
}
if (stack[stack.length - 1] > 0 || tag === 'INDENT') {
if (open || tag === 'INDENT') {
idx = tag === 'OUTDENT' ? i + 1 : i;
stack_pointer = tag === 'INDENT' ? 2 : 1;
_c = 0; _d = stack[stack.length - stack_pointer];

View file

@ -116,13 +116,14 @@ exports.Rewriter: class Rewriter
when 'OUTDENT'
last: stack.pop()
stack[stack.length - 1] += last
if tag is 'CALL_END' and calls < 0
open: stack[stack.length - 1] > 0
if tag is 'CALL_END' and calls < 0 and open
stack[stack.length - 1] -= 1
@tokens.splice(i, 0, ['CALL_END', ')', token[2]])
return 2
if !post? or include IMPLICIT_END, tag
return 1 if tag is 'INDENT' and prev and include IMPLICIT_BLOCK, prev[0]
if stack[stack.length - 1] > 0 or tag is 'INDENT'
if open or tag is 'INDENT'
idx: if tag is 'OUTDENT' then i + 1 else i
stack_pointer: if tag is 'INDENT' then 2 else 1
for tmp in [0...stack[stack.length - stack_pointer]]

View file

@ -21,7 +21,7 @@ class SplitNode extends BaseNode
# Extend CoffeeScript with our lexing function that matches --wordgoeshere--
# and creates a SplitNode.
CoffeeScript.extend ->
return false unless variable: @match /^--(\w+)--/, 1
return false unless variable: @match(/^--(\w+)--/, 1)
@i += variable.length + 4
@token 'EXTENSION', new SplitNode(variable)
true

View file

@ -80,6 +80,16 @@ fn: (arg) -> arg
ok fn(fn {prop: 101}).prop is 101
# Multi-blocks with optional parens.
result: fn( ->
fn ->
"Wrapped"
)
ok result()() is 'Wrapped'
# And even with strange things like this:
funcs: [((x) -> x), ((x) -> x * x)]