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

being stricter about body-less scoped loops.

This commit is contained in:
Jeremy Ashkenas 2010-12-21 20:28:48 -05:00
parent 3c86c57765
commit f567dafe62
7 changed files with 185 additions and 172 deletions

View file

@ -34,10 +34,13 @@
],
Expression: [o('Value'), o('Invocation'), o('Code'), o('Operation'), o('Assign'), o('If'), o('Try'), o('While'), o('For'), o('Switch'), o('Class')],
Block: [
o('INDENT OUTDENT', function() {
return new Expressions;
}), o('FullBlock')
],
FullBlock: [
o('INDENT Body OUTDENT', function() {
return $2;
}), o('INDENT OUTDENT', function() {
return new Expressions;
})
],
Identifier: [
@ -373,7 +376,7 @@
return new For($1, $2);
}), o('ForBody Block', function() {
return new For($2, $1);
}), o('ForBody FuncGlyph Block', function() {
}), o('ForBody FuncGlyph FullBlock', function() {
$1.scoped = true;
return new For($3, $1);
})

View file

@ -1821,7 +1821,7 @@
forPart = "" + ivar + " = 0, " + lvar + " = " + svar + ".length; " + ivar + " < " + lvar + "; " + stepPart;
}
}
if (this.scoped && body.expressions.length) {
if (this.scoped) {
body = Closure.wrap(body, true, !this.returns);
}
if (!this.pattern) {

File diff suppressed because one or more lines are too long

View file

@ -181,18 +181,21 @@
}
tokens.splice(i, 0, ['CALL_START', '(', token[2]]);
this.detectEnd(i + 1, function(token, i) {
var post, _ref;
var post, _ref, _ref2;
tag = token[0];
if (seenFor && (tag === '->' || tag === '=>') && ((_ref = this.tokens[i + 1]) != null ? _ref[0] : void 0) === 'INDENT') {
return true;
}
if (!seenSingle && token.fromThen) {
return true;
}
tag = token[0];
if (tag === 'IF' || tag === 'ELSE' || tag === '->' || tag === '=>') {
seenSingle = true;
}
if ((tag === '.' || tag === '?.' || tag === '::') && this.tag(i - 1) === 'OUTDENT') {
return true;
}
return !token.generated && this.tag(i - 1) !== ',' && __indexOf.call(IMPLICIT_END, tag) >= 0 && (tag !== 'INDENT' || (this.tag(i - 2) !== 'CLASS' && (_ref = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{')));
return !token.generated && this.tag(i - 1) !== ',' && __indexOf.call(IMPLICIT_END, tag) >= 0 && (tag !== 'INDENT' || (this.tag(i - 2) !== 'CLASS' && (_ref2 = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref2) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{')));
}, action);
if (prev[0] === '?') {
prev[0] = 'FUNC_EXIST';

View file

@ -104,8 +104,12 @@ grammar =
# will convert some postfix forms into blocks for us, by adjusting the
# token stream.
Block: [
o 'INDENT Body OUTDENT', -> $2
o 'INDENT OUTDENT', -> new Expressions
o 'FullBlock'
]
FullBlock: [
o 'INDENT Body OUTDENT', -> $2
]
# A literal identifier, a variable name or property.
@ -420,7 +424,7 @@ grammar =
o 'Statement ForBody', -> new For $1, $2
o 'Expression ForBody', -> new For $1, $2
o 'ForBody Block', -> new For $2, $1
o 'ForBody FuncGlyph Block', -> $1.scoped = yes; new For $3, $1
o 'ForBody FuncGlyph FullBlock', -> $1.scoped = yes; new For $3, $1
]
ForBody: [

View file

@ -1472,7 +1472,7 @@ exports.For = class For extends Base
lvar = scope.freeVariable 'len'
stepPart = if @step then "#{ivar} += #{ @step.compile(o, LEVEL_OP) }" else "#{ivar}++"
forPart = "#{ivar} = 0, #{lvar} = #{svar}.length; #{ivar} < #{lvar}; #{stepPart}"
if @scoped and body.expressions.length
if @scoped
body = Closure.wrap body, true, not @returns
defPart += @pluckDirectCall o, body, name, index unless @pattern
if @returns and not hasPureLast

View file

@ -151,8 +151,9 @@ class exports.Rewriter
not (seenFor and tag in ['->', '=>'] and next and next[0] is 'INDENT')
tokens.splice i, 0, ['CALL_START', '(', token[2]]
@detectEnd i + 1, (token, i) ->
return yes if not seenSingle and token.fromThen
[tag] = token
return yes if seenFor and tag in ['->', '=>'] and @tokens[i + 1]?[0] is 'INDENT'
return yes if not seenSingle and token.fromThen
seenSingle = yes if tag in ['IF', 'ELSE', '->', '=>']
return yes if tag in ['.', '?.', '::'] and @tag(i - 1) is 'OUTDENT'
not token.generated and @tag(i - 1) isnt ',' and tag in IMPLICIT_END and