Permalink
Browse files

Support for licenses statements marked like this:

/*!
  license
*/
These will be new elements in the AST, and will always be generated into the minified code.

I also pass the comments and line numbers out of the parser, by adding extra properties to the AST arrays.
This is useful to those using the parser without the minifier.
  • Loading branch information...
1 parent cd8e0c7 commit 28efb89f067f08995495310da071bae80be0bbd0 Kenneth Kufluk committed Mar 2, 2012
Showing with 47 additions and 3 deletions.
  1. +41 −3 lib/parse-js.js
  2. +6 −0 lib/process.js
View
@@ -258,6 +258,12 @@ function JS_Parse_Error(message, line, col, pos) {
this.col = col + 1;
this.pos = pos + 1;
this.stack = new Error().stack;
+ try {
+ ({})();
+ } catch(ex) {
+ this.stack = ex.stack;
+ console.log(this.toString());
+ };
};
JS_Parse_Error.prototype.toString = function() {
@@ -546,6 +552,21 @@ function tokenizer($TEXT) {
return token("operator", grow(prefix || next()));
};
+ function read_license() {
+ next();
+ return with_eof_error("Unterminated multiline comment", function(){
+ var i = find("*/", true),
+ text = S.text.substring(S.pos, i);
+ for (var j=0; j<text.length+2; j++) {
+ next();
+ }
+ // S.pos = i + 2;
+ // S.line += text.split("\n").length - 1;
+ S.newline_before = text.indexOf("\n") >= 0;
+ return token("license", text);
+ });
+ };
+
function handle_slash() {
next();
var regex_allowed = S.regex_allowed;
@@ -555,8 +576,14 @@ function tokenizer($TEXT) {
S.regex_allowed = regex_allowed;
return next_token();
case "*":
- S.comments_before.push(read_multiline_comment());
+ next();
S.regex_allowed = regex_allowed;
+ if (peek()=='!') {
+ // A license statment starts /*!
+ return read_license();
+ } else {
+ S.comments_before.push(read_multiline_comment());
+ }
return next_token();
}
return S.regex_allowed ? read_regexp("") : read_operator("/");
@@ -682,7 +709,6 @@ function NodeWithToken(str, start, end) {
NodeWithToken.prototype.toString = function() { return this.name; };
function parse($TEXT, exigent_mode, embed_tokens) {
-
var S = {
input : typeof $TEXT == "string" ? tokenizer($TEXT, true) : $TEXT,
token : null,
@@ -755,7 +781,12 @@ function parse($TEXT, exigent_mode, embed_tokens) {
};
function as() {
- return slice(arguments);
+ var arr = slice(arguments);
+ if (S.token.comments_before && S.token.comments_before.length) {
+ arr.comments_before = S.token.comments_before;
+ }
+ if (S.token.line) arr.line = S.token.line;
+ return arr;
};
function parenthesised() {
@@ -792,6 +823,9 @@ function parse($TEXT, exigent_mode, embed_tokens) {
case "atom":
return simple_statement();
+ case "license":
+ return as("license", prog1(S.token.value, next));
+
case "name":
return is_token(peek(), "punc", ":")
? labeled_statement(prog1(S.token.value, next, next))
@@ -886,6 +920,10 @@ function parse($TEXT, exigent_mode, embed_tokens) {
return as("label", label, stat);
};
+ function license() {
+ return as("license", prog1(expression));
+ };
+
function simple_statement() {
return as("stat", prog1(expression, semicolon));
};
View
@@ -202,6 +202,9 @@ function ast_walker() {
},
"atom": function(name) {
return [ this[0], name ];
+ },
+ "license": function(name) {
+ return [ this[0], name ];
}
};
@@ -1547,6 +1550,9 @@ function gen_code(ast, options) {
"string": encode_string,
"num": make_num,
"name": make_name,
+ "license": function(x){
+ return "/*!"+x+'*/';
+ },
"debugger": function(){ return "debugger" },
"toplevel": function(statements) {
return make_block_statements(statements)

0 comments on commit 28efb89

Please sign in to comment.