Comparing version 0.0.7 to 0.1.1
@@ -7,3 +7,3 @@ | ||
return this; | ||
} | ||
}; | ||
@@ -13,4 +13,10 @@ var def = function def(name, ini) { | ||
var obj = {}; | ||
obj.type = name; | ||
ini.call(obj,a,b,c,d,e,f,g,h); | ||
obj.type = name; | ||
if (obj.loc) { | ||
obj.range = obj.loc.range || [0,0]; | ||
delete obj.loc; | ||
obj.loc = arguments[ini.length-(name=='Literal' ? 2:1)]; | ||
delete obj.loc.range; | ||
} | ||
return obj; | ||
@@ -36,7 +42,2 @@ }; | ||
this.loc = loc; | ||
this.body.forEach(function (el) { | ||
if (el.type == "VariableDeclaration" && el.kind == "let") { | ||
el.kind = "var"; | ||
} | ||
}); | ||
}); | ||
@@ -64,4 +65,5 @@ | ||
// Literal expression node | ||
def('Literal', function (val, loc) { | ||
def('Literal', function (val, loc, raw) { | ||
this.value = val; | ||
if (raw) this.raw = raw; | ||
this.loc = loc; | ||
@@ -75,4 +77,4 @@ }); | ||
def('VariableDeclaration', function (kind, declarations, loc) { | ||
this.declarations = declarations; | ||
this.kind = kind; | ||
this.declarations = declarations; | ||
this.loc = loc; | ||
@@ -97,2 +99,9 @@ }); | ||
def('Property', function (key, value, kind, loc) { | ||
this.key = key; | ||
this.value = value; | ||
this.kind = kind; | ||
this.loc = loc; | ||
}); | ||
// Function declaration node | ||
@@ -103,4 +112,2 @@ var funIni = function (ident, params, body, isGen, isExp, loc) { | ||
this.body = body; | ||
this.generator = isGen; | ||
this.expression = isExp; | ||
this.loc = loc; | ||
@@ -126,5 +133,5 @@ if (!this.expression) { | ||
def('TryStatement', function (block, handler, finalizer, loc) { | ||
def('TryStatement', function (block, handlers, finalizer, loc) { | ||
this.block = block; | ||
this.handler = handler; | ||
this.handlers = handlers || []; | ||
this.finalizer = finalizer; | ||
@@ -164,4 +171,3 @@ this.loc = loc; | ||
this.discriminant = discriminant; | ||
this.cases = cases; | ||
this.lexical = !!lexical; | ||
if (cases.length) this.cases = cases; | ||
this.loc = loc; | ||
@@ -186,4 +192,4 @@ }); | ||
this.test = test; | ||
this.consequent = consequent; | ||
this.alternate = alternate; | ||
this.consequent = consequent; | ||
this.loc = loc; | ||
@@ -222,3 +228,3 @@ }); | ||
this.argument = argument; | ||
this.prefix = prefix; | ||
//this.prefix = prefix; | ||
this.loc = loc; | ||
@@ -296,4 +302,4 @@ }); | ||
this.test = test; | ||
this.consequent = consequent; | ||
this.alternate = alternate; | ||
this.consequent = consequent; | ||
this.loc = loc; | ||
@@ -313,3 +319,3 @@ }); | ||
return def; | ||
} | ||
}; | ||
var parser = require("./parser").parser, | ||
nodes = require("./nodes"), | ||
mozNodes = require("./moznodes"), | ||
stringify = require("./stringify").stringify; | ||
@@ -12,12 +13,17 @@ | ||
JSParser.prototype = { | ||
parse: function (source) { | ||
return this.parser.parse(source); | ||
parse: function (source, options) { | ||
return this.parser.parse(source, options); | ||
} | ||
}; | ||
var builder = {}; | ||
var defaultBuilder = {}; | ||
var mozBuilder = {}; | ||
// Define AST nodes | ||
nodes.defineNodes(builder); | ||
nodes.defineNodes(defaultBuilder); | ||
// Define Mozilla style AST nodes | ||
nodes.defineNodes(mozBuilder); | ||
mozNodes.defineNodes(mozBuilder); | ||
function Parser (options) { | ||
@@ -27,3 +33,3 @@ this.yy.source = options.source||null; | ||
this.yy.noloc = options.loc === false; | ||
this.yy.builder = options.builder||null; | ||
this.yy.builder = options.builder||defaultBuilder; | ||
} | ||
@@ -34,8 +40,9 @@ | ||
// allow yy.NodeType calls in parser | ||
for (var con in builder) { | ||
if (builder.hasOwnProperty(con)) { | ||
for (var con in defaultBuilder) { | ||
if (defaultBuilder.hasOwnProperty(con)) { | ||
parser.yy[con] = function (name){ | ||
var context = this; | ||
return function (a,b,c,d,e,f,g,h) { | ||
return builder[name](a,b,c,d,e,f,g,h); | ||
} | ||
return context.builder[name](a,b,c,d,e,f,g,h); | ||
}; | ||
}(con); | ||
@@ -50,4 +57,4 @@ } | ||
return this.builder[buildName](a,b,c,d,e,f,g,h); | ||
} else if (builder[buildName]) { | ||
return builder[buildName](a,b,c,d,e,f,g,h); | ||
} else if (mozBuilder[buildName]) { | ||
return mozBuilder[buildName](a,b,c,d,e,f,g,h); | ||
} else { | ||
@@ -61,2 +68,3 @@ throw 'no such node type: '+type; | ||
start.last_column = end.last_column; | ||
start.range = [start.range[0], end.range[1]]; | ||
return start; | ||
@@ -69,8 +77,12 @@ }; | ||
return { source: this.source | ||
, start: { line: this.startLine+loc.first_line - 1 | ||
, column: loc.first_column } | ||
, end: { line: this.startLine+loc.last_line - 1 | ||
, column: loc.last_column } | ||
}; | ||
var newLoc = { start: { line: this.startLine+loc.first_line - 1, | ||
column: loc.first_column }, | ||
end: { line: this.startLine+loc.last_line - 1, | ||
column: loc.last_column }, | ||
range: loc.range | ||
}; | ||
if (this.source || this.builder !== defaultBuilder) | ||
newLoc.source = this.source; | ||
return newLoc; | ||
}; | ||
@@ -81,3 +93,3 @@ | ||
// don't print error for missing semicolon | ||
if (!(hash.expected.indexOf("';'") >= 0 && (hash.token === 'CLOSEBRACE' || parser.yy.lineBreak || parser.yy.lastLineBreak || hash.token === 1))) { | ||
if (!((!hash.expected || hash.expected.indexOf("';'") >= 0) && (hash.token === 'CLOSEBRACE' || parser.yy.lineBreak || parser.yy.lastLineBreak || hash.token === 1 || parser.yy.DOW))) { | ||
throw new SyntaxError(err); | ||
@@ -87,2 +99,4 @@ } | ||
parser.lexer.options.ranges = true; | ||
// used to check if last match was a line break (for ; insertion) | ||
@@ -93,5 +107,54 @@ var realLex = parser.lexer.lex; | ||
parser.yy.lineBreak = false; | ||
return realLex.call(parser.lexer); | ||
return realLex.call(this); | ||
}; | ||
var realNext = parser.lexer.next; | ||
parser.lexer.next = function () { | ||
var ret = realNext.call(this); | ||
if (ret === 'COMMENT' || ret === 'COMMENT_BLOCK') { | ||
if (this.yy.options.comment) { | ||
this.yy.comments.push({range: this.yylloc.range, type: types[ret], value: this.yytext}); | ||
} | ||
return; | ||
} | ||
if (ret && ret !== 1 && ret !== 199) { | ||
if (this.yy.options.tokens) { | ||
var tokens = this.yy.tokens; | ||
var last = tokens[tokens.length-1]; | ||
if (tokens.length && (last.value == '/' || last.value == '/=')) { | ||
tokens[tokens.length-1] = tokenObject(this, ret); | ||
var t = tokens[tokens.length-1]; | ||
t.range[0] = last.range[0]; | ||
t.value = last.value + t.value; | ||
} else { | ||
this.yy.tokens.push(tokenObject(this, ret)); | ||
} | ||
} | ||
} | ||
return ret; | ||
}; | ||
var types = { | ||
"NULLTOKEN": "Null", | ||
"THISTOKEN": "Keyword", | ||
"VAR": "Keyword", | ||
"IDENT": "Identifier", | ||
"NUMBER": "Numeric", | ||
"=": "Punctuator", | ||
"[": "Punctuator", | ||
"]": "Punctuator", | ||
"REGEXP_BODY": "RegularExpression", | ||
"COMMENT": "Line", | ||
"COMMENT_BLOCK": "Block" | ||
}; | ||
function tokenObject (lexer, token) { | ||
var symbols = lexer.yy.parser.terminals_; | ||
return { | ||
"type": types[symbols[token] || token], | ||
"value": lexer.match, | ||
"range": lexer.yylloc.range | ||
}; | ||
} | ||
parser.yy.escapeString = function (s) { | ||
@@ -102,6 +165,10 @@ return s.replace(/\\\n/,'').replace(/\\([^xubfnvrt0\\])/g, '$1'); | ||
var oldParse = parser.parse; | ||
parser.parse = function (source) { | ||
parser.yy.lineBreak = false; | ||
parser.yy.inRegex = false; | ||
parser.yy.ASI = false; | ||
parser.parse = function (source, options) { | ||
this.yy.lineBreak = false; | ||
this.yy.inRegex = false; | ||
this.yy.ASI = false; | ||
this.yy.tokens = []; | ||
this.yy.raw = []; | ||
this.yy.comments = []; | ||
this.yy.options = options || {}; | ||
return oldParse.call(this,source); | ||
@@ -112,3 +179,3 @@ }; | ||
parse: function (src, options) { | ||
return new JSParser(options).parse(src); | ||
return new JSParser(options).parse(src, options); | ||
}, | ||
@@ -120,3 +187,4 @@ stringify: stringify | ||
exports.stringify = stringify; | ||
exports.builder = builder; | ||
exports.builder = defaultBuilder; | ||
exports.mozBuilder = mozBuilder; | ||
@@ -581,3 +581,3 @@ // by Jason Orendorff | ||
var s = indent + "try" + substmt(n.block, indent, true); | ||
var h = n.handler; | ||
var h = n.handlers; | ||
var handlers = h === null ? [] : "length" in h ? h : [h]; | ||
@@ -584,0 +584,0 @@ for (var i = 0; i < handlers.length; i++) { |
@@ -5,3 +5,3 @@ { | ||
"description": "JavaScript parser adhering to Mozilla's parser API", | ||
"version": "0.0.7", | ||
"version": "0.1.1", | ||
"keywords": [ | ||
@@ -25,3 +25,3 @@ "parser", | ||
"devDependencies": { | ||
"jison": "*", | ||
"jison": "0.3.9", | ||
"uglify-js": "*" | ||
@@ -28,0 +28,0 @@ }, |
@@ -26,2 +26,3 @@ // Bundles the built code base as a standalone javascript file | ||
"stringify.js": "dist/stringify.js", | ||
"moznodes.js": "dist/moznodes.js", | ||
"nodes.js": "dist/nodes.js" | ||
@@ -32,3 +33,3 @@ }; | ||
var out = "var Reflect = (function() {\n" + script + ";\nreturn require('reflect').Reflect;\n})();"; | ||
require("sys").puts(out); | ||
console.log(out); | ||
} | ||
@@ -35,0 +36,0 @@ |
var Reflect = require('../dist/reflect').Reflect; | ||
var mozBuilder = require('../dist/reflect').mozBuilder; | ||
@@ -279,3 +280,3 @@ var print = console.log; | ||
try { | ||
a = Reflect.stringify(Reflect.parse(b, {loc: false})); | ||
a = Reflect.stringify(Reflect.parse(b, {loc: false, builder: mozBuilder})); | ||
if (typeof a !== "string") { | ||
@@ -282,0 +283,0 @@ throw new TypeError("Reflect.stringify returned " + |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1057077
32
22952
5
3