Skip to content
Browse files

Render comments.

  • Loading branch information...
1 parent 86c3a69 commit 8b91f2387d39efe4b6bba9387e46292b27f0eb10 @creationix creationix committed Jan 28, 2010
Showing with 32 additions and 8 deletions.
  1. +24 −4 lib/jack/generator.js
  2. +8 −4 lib/jack/grammar.js
View
28 lib/jack/generator.js
@@ -1,7 +1,7 @@
(function () {
var root = (typeof exports !== "undefined" && exports) || this,
Jack = root.Jack || (root.Jack = {}),
- uses, scope, semi, need_return,
+ uses, scope, semi, need_return, comment, last_comment,
Generators;
Generators = {
@@ -11,10 +11,18 @@
var contents = node.value.map(function (item, index) {
var inside;
semi = true;
+ last_comment = comment;
+ comment = false;
+
if (need_return && index === node.value.length - 1) {
- return "return " + render(item) + (semi ? ";" : "");
+ inside = "return " + render(item) + (semi ? ";" : "");
+ } else {
+ inside = render(item) + (semi ? ";" : "");
+ }
+ if (comment && !last_comment) {
+ inside = "\n" + inside;
}
- return render(item) + (semi ? ";" : "");
+ return inside;
}).join("\n");
var vars = Object.keys(scope);
scope = scope.__proto__;
@@ -47,6 +55,12 @@
return render(node.id) + " = " + render(node.value);
},
+ COMMENT: function (node) {
+ semi = false;
+ comment = true;
+ return "//" + node.value;
+ },
+
BOOLEAN: function (node) {
return node.value ? "true" : "false";
},
@@ -87,7 +101,13 @@
payload = block_indent(render(payload));
need_return = false;
} else {
- payload = " return " + render(payload) + "; ";
+
+ payload = "return " + render(payload) + ";";
+ if (payload.length > 20) {
+ payload = block_indent(payload);
+ } else {
+ payload = " " + payload + " ";
+ }
}
return "function (" + args + ") {" + payload + "}";
},
View
12 lib/jack/grammar.js
@@ -10,17 +10,21 @@
],
Block: [
["BlockPart", "$$ = {name: 'Block', value: $1}"],
- ["NEWLINE", "$$ = {name: 'Block', value: []}"]
+ ["NEWLINE", "$$ = {name: 'Block', value: []}"],
+ ["", "$$ = {name: 'Block', value: []}"]
],
BlockPart: [
["Statement", "$$ = $1"],
["BlockPart Statement", "$$ = $1.concat($2)"]
],
Statement: [
["NEWLINE", "$$ = []"],
- ["COMMENT NEWLINE", "$$ = []"],
+ ["Comment NEWLINE", "$$ = [$1]"],
["Expression NEWLINE", "$$ = [$1]"]
],
+ Comment: [
+ ["COMMENT", "$$ = yytext"]
+ ],
Expression: [
["STRING", '$$ = yytext'],
["BOOLEAN", '$$ = yytext'],
@@ -80,8 +84,8 @@
["Id = Expression", "$$ = [$1, $3]"]
],
Function: [
- ["fun WS Args ARROW Expression", "$$ = {name: 'Function', value: [$3, $5]}"],
- ["fun WS Args ARROW NEWLINE Block end", "$$ = {name: 'Function', value: [$3, $6]}"]
+ ["fun Ws Args ARROW Expression", "$$ = {name: 'Function', value: [$3, $5]}"],
+ ["fun Ws Args ARROW NEWLINE Block end", "$$ = {name: 'Function', value: [$3, $6]}"]
],
Args: [
["", "$$ = []"],

0 comments on commit 8b91f23

Please sign in to comment.
Something went wrong with that request. Please try again.