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

combination of satyr's three comment patches ... comments are now statements, not expressions...

This commit is contained in:
Jeremy Ashkenas 2010-11-08 23:39:21 -05:00
parent 936abc381b
commit 042af51751
8 changed files with 48 additions and 105 deletions

View file

@ -36,9 +36,9 @@
return new Literal($1);
}), o('DEBUGGER', function() {
return new Literal($1);
})
}), o('Comment')
],
Expression: [o('Value'), o('Invocation'), o('Code'), o('Operation'), o('Assign'), o('If'), o('Try'), o('While'), o('For'), o('Switch'), o('Extends'), o('Class'), o('Comment')],
Expression: [o('Value'), o('Invocation'), o('Code'), o('Operation'), o('Assign'), o('If'), o('Try'), o('While'), o('For'), o('Switch'), o('Extends'), o('Class')],
Block: [
o('INDENT Body OUTDENT', function() {
return $2;

View file

@ -502,8 +502,13 @@
Comment.prototype.isPureStatement = YES;
Comment.prototype.isStatement = YES;
Comment.prototype.makeReturn = THIS;
Comment.prototype.compileNode = function(o) {
return this.tab + '/*' + multident(this.comment, this.tab) + '*/';
Comment.prototype.compileNode = function(o, level) {
var code;
code = '/*' + multident(this.comment, this.tab) + '*/';
if ((level != null ? level : o.level) === LEVEL_TOP) {
code = o.indent + code;
}
return code;
};
return Comment;
})();
@ -738,7 +743,7 @@
} else if (!(prop instanceof Assign) && !(prop instanceof Comment)) {
prop = new Assign(prop, prop, 'object');
}
_result.push(indent + prop.compile(o) + join);
_result.push(indent + prop.compile(o, LEVEL_TOP) + join);
}
return _result;
}).call(this);
@ -759,7 +764,7 @@
for (i = 0, _len = props.length; i < _len; i++) {
prop = props[i];
if (prop instanceof Comment) {
code += prop.compile(o) + ' ';
code += prop.compile(o, LEVEL_LIST) + ' ';
continue;
}
if (prop instanceof Assign) {
@ -807,23 +812,24 @@
__extends(Arr, Base);
Arr.prototype.children = ['objects'];
Arr.prototype.compileNode = function(o) {
var code, i, obj, objects, _len, _ref;
var code, obj, _i, _len, _ref, _result;
o.indent += TAB;
if (code = Splat.compileSplattedArray(o, this.objects)) {
return code;
}
objects = [];
_ref = this.objects;
for (i = 0, _len = _ref.length; i < _len; i++) {
obj = _ref[i];
code = obj.compile(o, LEVEL_LIST);
objects.push((obj instanceof Comment ? "\n" + code + "\n" + o.indent : i === this.objects.length - 1 ? code : code + ', '));
}
objects = objects.join('');
if (0 < objects.indexOf('\n')) {
return "[\n" + o.indent + objects + "\n" + this.tab + "]";
code = ((function() {
_ref = this.objects;
_result = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
obj = _ref[_i];
_result.push(obj.compile(o, LEVEL_LIST));
}
return _result;
}).call(this)).join(', ');
if (code.indexOf('\n') >= 0) {
return "[\n" + o.indent + code + "\n" + this.tab + "]";
} else {
return "[" + objects + "]";
return "[" + code + "]";
}
};
Arr.prototype.assigns = function(name) {

File diff suppressed because one or more lines are too long

View file

@ -14,7 +14,6 @@
})();
exports.Rewriter.prototype.rewrite = function(_arg) {
this.tokens = _arg;
this.adjustComments();
this.removeLeadingNewlines();
this.removeMidExpressionNewlines();
this.closeOpenCalls();
@ -56,34 +55,6 @@
}
return i - 1;
};
exports.Rewriter.prototype.adjustComments = function() {
return this.scanTokens(function(token, i, tokens) {
var after, before, post, prev, _ref;
if (token[0] !== 'HERECOMMENT') {
return 1;
}
before = tokens[i - 2], prev = tokens[i - 1], post = tokens[i + 1], after = tokens[i + 2];
if ((after != null ? after[0] : void 0) === 'INDENT') {
tokens.splice(i + 2, 1);
if ((before != null ? before[0] : void 0) === 'OUTDENT' && (post != null ? post[0] : void 0) === 'TERMINATOR') {
tokens.splice(i - 2, 1);
} else {
tokens.splice(i, 0, after);
}
} else if (prev && ((_ref = prev[0]) !== 'TERMINATOR' && _ref !== 'INDENT' && _ref !== 'OUTDENT')) {
if ((post != null ? post[0] : void 0) === 'TERMINATOR' && (after != null ? after[0] : void 0) === 'OUTDENT') {
tokens.splice.apply(tokens, [i + 2, 0].concat(__slice.call(tokens.splice(i, 2))));
if (tokens[i + 2][0] !== 'TERMINATOR') {
tokens.splice(i + 2, 0, ['TERMINATOR', '\n', prev[2]]);
}
} else {
tokens.splice(i, 0, ['TERMINATOR', '\n', prev[2]]);
}
return 2;
}
return 1;
});
};
exports.Rewriter.prototype.removeLeadingNewlines = function() {
var i, tag, _len;
for (i = 0, _len = this.tokens.length; i < _len; i++) {

View file

@ -82,6 +82,7 @@ grammar =
o 'BREAK', -> new Literal $1
o 'CONTINUE', -> new Literal $1
o 'DEBUGGER', -> new Literal $1
o 'Comment'
]
# All the different types of expressions in our language. The basic unit of
@ -101,7 +102,6 @@ grammar =
o 'Switch'
o 'Extends'
o 'Class'
o 'Comment'
]
# An indented block of expressions. Note that the [Rewriter](rewriter.html)

View file

@ -400,15 +400,16 @@ exports.Value = class Value extends Base
# at the same position.
exports.Comment = class Comment extends Base
isPureStatement: YES
isStatement: YES
constructor: (@comment) ->
makeReturn: THIS
isPureStatement: YES
isStatement: YES
makeReturn: THIS
compileNode: (o) ->
@tab + '/*' + multident(@comment, @tab) + '*/'
compileNode: (o, level) ->
code = '/*' + multident(@comment, @tab) + '*/'
code = o.indent + code if (level ? o.level) is LEVEL_TOP
code
#### Call
@ -588,7 +589,7 @@ exports.Obj = class Obj extends Base
prop = new Assign prop.properties[0].name, prop, 'object'
else if prop not instanceof Assign and prop not instanceof Comment
prop = new Assign prop, prop, 'object'
indent + prop.compile(o) + join
indent + prop.compile(o, LEVEL_TOP) + join
props = props.join ''
obj = "{#{ props and '\n' + props + '\n' + @tab }}"
return @compileDynamic o, obj, rest if rest
@ -598,7 +599,7 @@ exports.Obj = class Obj extends Base
code = "#{ oref = o.scope.freeVariable 'obj' } = #{code}, "
for prop, i in props
if prop instanceof Comment
code += prop.compile(o) + ' '
code += prop.compile(o, LEVEL_LIST) + ' '
continue
if prop instanceof Assign
acc = prop.variable.base
@ -633,21 +634,11 @@ exports.Arr = class Arr extends Base
compileNode: (o) ->
o.indent += TAB
return code if code = Splat.compileSplattedArray o, @objects
objects = []
for obj, i in @objects
code = obj.compile o, LEVEL_LIST
objects.push (if obj instanceof Comment
"\n#{code}\n#{o.indent}"
else if i is @objects.length - 1
code
else
code + ', '
)
objects = objects.join ''
if 0 < objects.indexOf '\n'
"[\n#{o.indent}#{objects}\n#{@tab}]"
code = (obj.compile o, LEVEL_LIST for obj in @objects).join ', '
if code.indexOf('\n') >= 0
"[\n#{o.indent}#{code}\n#{@tab}]"
else
"[#{objects}]"
"[#{code}]"
assigns: (name) ->
for obj in @objects when obj.assigns name then return yes

View file

@ -18,7 +18,6 @@ class exports.Rewriter
# like this. The order of these passes matters -- indentation must be
# corrected before implicit parentheses can be wrapped around blocks of code.
rewrite: (@tokens) ->
@adjustComments()
@removeLeadingNewlines()
@removeMidExpressionNewlines()
@closeOpenCalls()
@ -55,28 +54,6 @@ class exports.Rewriter
i += 1
i - 1
# Massage newlines and indentations so that comments don't have to be
# correctly indented, or appear on a line of their own.
adjustComments: ->
@scanTokens (token, i, tokens) ->
return 1 unless token[0] is 'HERECOMMENT'
{(i-2): before, (i-1): prev, (i+1): post, (i+2): after} = tokens
if after?[0] is 'INDENT'
tokens.splice i + 2, 1
if before?[0] is 'OUTDENT' and post?[0] is 'TERMINATOR'
tokens.splice i - 2, 1
else
tokens.splice i, 0, after
else if prev and prev[0] not in ['TERMINATOR', 'INDENT', 'OUTDENT']
if post?[0] is 'TERMINATOR' and after?[0] is 'OUTDENT'
tokens.splice i + 2, 0, tokens.splice(i, 2)...
if tokens[i + 2][0] isnt 'TERMINATOR'
tokens.splice i + 2, 0, ['TERMINATOR', '\n', prev[2]]
else
tokens.splice i, 0, ['TERMINATOR', '\n', prev[2]]
return 2
1
# Leading newlines would introduce an ambiguity in the grammar, so we
# dispatch them here.
removeLeadingNewlines: ->

View file

@ -98,14 +98,6 @@ obj = {
c: 'd'
}
arr = [
1, 2, 3,
###
four
###
5, 6, 7
]
# Spaced comments in if / elses.
result = if false
1