Issue #572. Flexible JavaScript block comments, compatible with JSDoc, YUI-compressor, and Google Closure compiler preservation syntax.

This commit is contained in:
Jeremy Ashkenas
2010-08-23 20:27:34 -04:00
parent 9fd92bf884
commit f90fac0e55
4 changed files with 10 additions and 15 deletions

View File

@@ -163,17 +163,16 @@
return true; return true;
}; };
Lexer.prototype.commentToken = function() { Lexer.prototype.commentToken = function() {
var comment, match; var match;
if (!(match = this.chunk.match(COMMENT))) { if (!(match = this.chunk.match(COMMENT))) {
return false; return false;
} }
this.line += count(match[1], "\n"); this.line += count(match[1], "\n");
this.i += match[1].length; this.i += match[1].length;
if (match[2]) { if (match[2]) {
comment = this.sanitizeHeredoc(match[2], { this.token('HERECOMMENT', this.sanitizeHeredoc(match[2], {
herecomment: true herecomment: true
}); }));
this.token('HERECOMMENT', comment.split(MULTILINER));
this.token('TERMINATOR', '\n'); this.token('TERMINATOR', '\n');
} }
return true; return true;
@@ -604,7 +603,7 @@
HEREDOC = /^("{6}|'{6}|"{3}\n?([\s\S]*?)\n?([ \t]*)"{3}|'{3}\n?([\s\S]*?)\n?([ \t]*)'{3})/; HEREDOC = /^("{6}|'{6}|"{3}\n?([\s\S]*?)\n?([ \t]*)"{3}|'{3}\n?([\s\S]*?)\n?([ \t]*)'{3})/;
OPERATOR = /^(-[\-=>]?|\+[+=]?|[*&|\/%=<>^:!?]+)([ \t]*)/; OPERATOR = /^(-[\-=>]?|\+[+=]?|[*&|\/%=<>^:!?]+)([ \t]*)/;
WHITESPACE = /^([ \t]+)/; WHITESPACE = /^([ \t]+)/;
COMMENT = /^(\s*\#{3}(?!#)[ \t]*\n+([\s\S]*?)[ \t]*\n+[ \t]*\#{3}|(\s*#(?!##[^#])[^\n]*)+)/; COMMENT = /^(\s*\#{3}(?!#)([\s\S]*?)\#{3}[ \t]*\n|(\s*#(?!##[^#])[^\n]*)+)/;
CODE = /^((-|=)>)/; CODE = /^((-|=)>)/;
MULTI_DENT = /^((\n([ \t]*))+)(\.)?/; MULTI_DENT = /^((\n([ \t]*))+)(\.)?/;
LAST_DENTS = /\n([ \t]*)/g; LAST_DENTS = /\n([ \t]*)/g;

View File

@@ -425,7 +425,7 @@
})(); })();
exports.CommentNode = (function() { exports.CommentNode = (function() {
CommentNode = function(_b) { CommentNode = function(_b) {
this.lines = _b; this.comment = _b;
CommentNode.__superClass__.constructor.call(this); CommentNode.__superClass__.constructor.call(this);
return this; return this;
}; };
@@ -438,9 +438,7 @@
return this; return this;
}; };
CommentNode.prototype.compileNode = function(o) { CommentNode.prototype.compileNode = function(o) {
var sep; return this.tab + '/*' + this.comment.replace(/\r?\n/g, '\n' + this.tab) + '*/';
sep = this.tab + '// ';
return sep + this.lines.join('\n' + sep);
}; };
return CommentNode; return CommentNode;
})(); })();

View File

@@ -145,8 +145,7 @@ exports.Lexer = class Lexer
@line += count match[1], "\n" @line += count match[1], "\n"
@i += match[1].length @i += match[1].length
if match[2] if match[2]
comment = @sanitizeHeredoc match[2], herecomment: true @token 'HERECOMMENT', @sanitizeHeredoc match[2], herecomment: true
@token 'HERECOMMENT', comment.split MULTILINER
@token 'TERMINATOR', '\n' @token 'TERMINATOR', '\n'
true true
@@ -521,7 +520,7 @@ NUMBER = /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-
HEREDOC = /^("{6}|'{6}|"{3}\n?([\s\S]*?)\n?([ \t]*)"{3}|'{3}\n?([\s\S]*?)\n?([ \t]*)'{3})/ HEREDOC = /^("{6}|'{6}|"{3}\n?([\s\S]*?)\n?([ \t]*)"{3}|'{3}\n?([\s\S]*?)\n?([ \t]*)'{3})/
OPERATOR = /^(-[\-=>]?|\+[+=]?|[*&|\/%=<>^:!?]+)([ \t]*)/ OPERATOR = /^(-[\-=>]?|\+[+=]?|[*&|\/%=<>^:!?]+)([ \t]*)/
WHITESPACE = /^([ \t]+)/ WHITESPACE = /^([ \t]+)/
COMMENT = /^(\s*\#{3}(?!#)[ \t]*\n+([\s\S]*?)[ \t]*\n+[ \t]*\#{3}|(\s*#(?!##[^#])[^\n]*)+)/ COMMENT = /^(\s*\#{3}(?!#)([\s\S]*?)\#{3}[ \t]*\n|(\s*#(?!##[^#])[^\n]*)+)/
CODE = /^((-|=)>)/ CODE = /^((-|=)>)/
MULTI_DENT = /^((\n([ \t]*))+)(\.)?/ MULTI_DENT = /^((\n([ \t]*))+)(\.)?/
LAST_DENTS = /\n([ \t]*)/g LAST_DENTS = /\n([ \t]*)/g

View File

@@ -392,15 +392,14 @@ exports.CommentNode = class CommentNode extends BaseNode
class: 'CommentNode' class: 'CommentNode'
isStatement: -> yes isStatement: -> yes
constructor: (@lines) -> constructor: (@comment) ->
super() super()
makeReturn: -> makeReturn: ->
this this
compileNode: (o) -> compileNode: (o) ->
sep = @tab + '// ' @tab + '/*' + @comment.replace(/\r?\n/g, '\n' + @tab) + '*/'
sep + @lines.join '\n' + sep
#### CallNode #### CallNode