diff --git a/acorn.js b/acorn.js index 9aca7c29bf..2a107deaf6 100644 --- a/acorn.js +++ b/acorn.js @@ -262,7 +262,7 @@ function makePredicate(words) { words = words.split(" "); - var f = "(function(str){", cats = []; + var f = "", cats = []; out: for (var i = 0; i < words.length; ++i) { for (var j = 0; j < cats.length; ++j) if (cats[j][0].length == words[i].length) { @@ -296,7 +296,7 @@ } else { compareTo(words); } - return (1, eval)(f + "})"); + return new Function("str", f); } // The ECMAScript 3 reserved word list. diff --git a/index.html b/index.html index 31df065527..0afca7fb38 100644 --- a/index.html +++ b/index.html @@ -11,20 +11,21 @@ https://github.com/marijnh/acorn.git
Please use the github bug tracker to report issues.
(function(exports) {
- "strict mode";
+ "use strict";
exports.version = "0.0.1";The main exported interface (under window.acorn when in the
browser) is a parse function that takes a code string and
returns an abstract syntax tree as specified by Mozilla parser
API, with the caveat that the SpiderMonkey-specific syntax
-(let, yield, inline XML, etc) is not recognized.
var options, input, inputLen;
+(let, yield, inline XML, etc) is not recognized. var options, input, inputLen, sourceFile;
exports.parse = function(inpt, opts) {
input = String(inpt); inputLen = input.length;
options = opts || {};
for (var opt in defaultOptions) if (!options.hasOwnProperty(opt))
options[opt] = defaultOptions[opt];
- return parseTopLevel();
+ sourceFile = options.sourceFile || null;
+ return parseTopLevel(options.program);
};A second optional argument can be given to further configure the parser process. These options are recognized:
var defaultOptions = exports.defaultOptions = {ecmaVersion indicates the ECMAScript version to parse. Must
be either 3 or 5. This
@@ -152,7 +153,7 @@ predicate from a space-separated string of words.
It starts by sorting the words by length.
function makePredicate(words) {
words = words.split(" ");
- var f = "(function(str){", cats = [];
+ var f = "", cats = [];
out: for (var i = 0; i < words.length; ++i) {
for (var j = 0; j < cats.length; ++j)
if (cats[j][0].length == words[i].length) {
@@ -178,7 +179,7 @@ switch first dispatches on the lengths, to save on comparisons.
f += "}";Otherwise, simply generate a flat switch statement.
} else {
compareTo(words);
}
- return (1, eval)(f + "})");
+ return new Function("str", f);
}The ECMAScript 3 reserved word list.
var isReservedWord3 = makePredicate("abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile");ECMAScript 5 reserved words.
var isReservedWord5 = makePredicate("class enum extends super const export import");The additional reserved words in strict mode.
var isStrictReservedWord = makePredicate("implements interface let package private protected public static yield");The forbidden variable names in strict mode.
var isStrictBadIdWord = makePredicate("eval arguments");And the keywords.
var isKeyword = makePredicate("break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in");Big ugly regular expressions that match characters in the whitespace, identifier, and identifier-start categories. These are only applied when a character is found to actually have a @@ -551,7 +552,7 @@ tests ("use strict"; 010; -- should fail).
Start a node whose start offset/comments information should be based on the start of another node. For example, a binary @@ -563,7 +564,7 @@ already been parsed.
Finish an AST node, adding type, end, and commentsAfter
@@ -619,7 +620,9 @@ to.
Parse a program. Initializes the parser, reads any number of -statements, and wraps them in a Program node.
function parseTopLevel() {
+statements, and wraps them in a Program node. Optionally takes a
+program argument. If present, the statements will be appended
+to its body instead of creating a new node. function parseTopLevel(program) {
initTokenState();
lastStart = lastEnd = tokPos;
if (options.locations) lastEndLoc = curLineLoc();
@@ -627,8 +630,8 @@ statements, and wraps them in a Program node.