diff options
Diffstat (limited to 'lib/parse.js')
-rw-r--r-- | lib/parse.js | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/lib/parse.js b/lib/parse.js index 6a5aad26..f04b7c6a 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -266,7 +266,7 @@ function is_token(token, type, val) { var EX_EOF = {}; -function tokenizer($TEXT) { +function tokenizer($TEXT, filename) { var S = { text : $TEXT.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, ''), @@ -324,7 +324,8 @@ function tokenizer($TEXT) { col : S.tokcol, pos : S.tokpos, endpos : S.pos, - nlb : S.newline_before + nlb : S.newline_before, + file : filename }; if (!is_comment) { ret.comments_before = S.comments_before; @@ -669,10 +670,16 @@ var ATOMIC_START_TOKEN = array_to_hash([ "atom", "num", "string", "regexp", "nam /* -----[ Parser ]----- */ -function parse($TEXT, exigent_mode) { +function parse($TEXT, options) { + + options = defaults(options, { + strict : false, + filename : null, + toplevel : null + }); var S = { - input : typeof $TEXT == "string" ? tokenizer($TEXT, true) : $TEXT, + input : typeof $TEXT == "string" ? tokenizer($TEXT, options.filename) : $TEXT, token : null, prev : null, peeked : null, @@ -736,7 +743,7 @@ function parse($TEXT, exigent_mode) { function expect(punc) { return expect_token("punc", punc); }; function can_insert_semicolon() { - return !exigent_mode && ( + return !options.strict && ( S.token.nlb || is("eof") || is("punc", "}") ); }; @@ -1226,7 +1233,7 @@ function parse($TEXT, exigent_mode) { var array_ = embed_tokens(function() { expect("["); return new AST_Array({ - elements: expr_list("]", !exigent_mode, true) + elements: expr_list("]", !options.strict, true) }); }); @@ -1235,7 +1242,7 @@ function parse($TEXT, exigent_mode) { var first = true, a = []; while (!is("punc", "}")) { if (first) first = false; else expect(","); - if (!exigent_mode && is("punc", "}")) + if (!options.strict && is("punc", "}")) // allow trailing comma break; var start = S.token; @@ -1415,7 +1422,7 @@ function parse($TEXT, exigent_mode) { }; function is_assignable(expr) { - if (!exigent_mode) return true; + if (!options.strict) return true; switch (expr[0]+"") { case "dot": case "sub": @@ -1470,15 +1477,21 @@ function parse($TEXT, exigent_mode) { return ret; }; - return new AST_Toplevel({ - start: S.token, - body: (function(a){ - while (!is("eof")) - a.push(statement()); - return a; - })([]), - end: prev() - }); + return (function(){ + var start = S.token; + var body = []; + while (!is("eof")) + body.push(statement()); + var end = prev(); + var toplevel = options.toplevel; + if (toplevel) { + toplevel.body = toplevel.body.concat(body); + toplevel.end = end; + } else { + toplevel = new AST_Toplevel({ start: start, body: body, end: end }); + } + return toplevel; + })(); }; |