This commit is contained in:
Mihai Bazon
2012-05-27 17:25:31 +03:00
parent 22bb5e8306
commit 861e26a666
6 changed files with 913 additions and 846 deletions

View File

@@ -18,42 +18,75 @@ function DEFNODE(type, props, methods, base) {
if (type) {
ctor.prototype.TYPE = ctor.TYPE = type;
}
if (methods) for (var i in methods) if (HOP(methods, i)) {
if (methods) for (i in methods) if (HOP(methods, i)) {
ctor.prototype[i] = methods[i];
}
return ctor;
};
var AST_Token = DEFNODE("Token", "type value line col pos endpos nlb", {
var AST_Token = DEFNODE("Token", "type value line col pos endpos nlb comments_before", {
}, null);
var AST_Node = DEFNODE("Node", "start end", {
renew: function(args) {
var ctor = this.CTOR, props = ctor.props;
for (var i in props) if (!HOP(args, i)) args[i] = this[i];
return new ctor(args);
},
walk: function(w) {
w._visit(this);
}
}, null);
var AST_Directive = DEFNODE("Directive", "value", {
print: function(output) {
output.string(this.value);
}
});
var AST_Debugger = DEFNODE("Debugger", null, {
print: function(output) {
output.print("debugger");
}
});
var AST_Parenthesized = DEFNODE("Parenthesized", "expression", {
documentation: "Represents an expression which is always parenthesized. Used for the \
conditions in IF/WHILE."
$documentation: "Represents an expression which is always parenthesized. Used for the \
conditions in IF/WHILE/DO and expression in SWITCH/WITH.",
walk: function(w) {
w._visit(this, function(){
this.expression.walk(w);
});
}
});
var AST_Bracketed = DEFNODE("Bracketed", "body", {
documentation: "Represents a block of statements that are always included in brackets. \
Used for bodies of FUNCTION/TRY/CATCH/THROW/SWITCH."
$documentation: "Represents a block of statements that are always included in brackets. \
Used for bodies of FUNCTION/TRY/CATCH/THROW/SWITCH.",
walk: function(w) {
w._visit(this, function(){
this.body.forEach(function(stat){
stat.walk(w);
});
});
}
});
/* -----[ loops ]----- */
var AST_LabeledStatement = DEFNODE("LabeledStatement", "label body", {
walk: function(w) {
w._visit(this, function(){
if (this.label) this.label.walk(w);
if (this.body) {
if (this.body instanceof Array)
AST_Bracketed.prototype.walk.call(this, w);
else
this.body.walk(w);
}
});
}
});
var AST_Statement = DEFNODE("Statement", null, {
@@ -61,39 +94,64 @@ var AST_Statement = DEFNODE("Statement", null, {
}, AST_LabeledStatement);
var AST_Do = DEFNODE("Do", "condition", {
walk: function(w) {
w._visit(this, function(){
this.condition.walk(w);
AST_LabeledStatement.prototype.walk.call(this, w);
});
}
}, AST_LabeledStatement);
var AST_While = DEFNODE("While", "condition", {
walk: function(w) {
w._visit(this, function(){
this.condition.walk(w);
AST_LabeledStatement.prototype.walk.call(this, w);
});
}
}, AST_LabeledStatement);
var AST_For = DEFNODE("For", "init condition step", {
walk: function(w) {
w._visit(this, function(){
if (this.init) this.init.walk(w);
if (this.condition) this.condition.walk(w);
if (this.step) this.step.walk(w);
AST_LabeledStatement.prototype.walk.call(this, w);
});
}
}, AST_LabeledStatement);
var AST_ForIn = DEFNODE("ForIn", "init name object", {
walk: function(w) {
w._visit(this, function(){
if (this.init) this.init.walk(w);
this.object.walk(w);
AST_LabeledStatement.prototype.walk.call(this, w);
});
}
}, AST_LabeledStatement);
var AST_With = DEFNODE("With", "expression body", {
walk: function(w) {
w._visit(this, function(){
this.expression.walk(w);
AST_LabeledStatement.prototype.walk.call(this, w);
});
var AST_LoopControl = DEFNODE("LoopControl", "label", {
}
});
var AST_Break = DEFNODE("Break", null, {
}, AST_LoopControl);
var AST_Continue = DEFNODE("Continue", null, {
}, AST_LoopControl);
/* -----[ functions ]----- */
var AST_Scope = DEFNODE("Scope", "identifiers body", {
walk: function(w) {
w._visit(this, function(){
if (this.identifiers) this.identifiers.forEach(function(el){
el.walk(w);
});
AST_LabeledStatement.prototype.walk.call(this, w);
});
}
});
var AST_Toplevel = DEFNODE("Toplevel", null, {
@@ -101,37 +159,84 @@ var AST_Toplevel = DEFNODE("Toplevel", null, {
}, AST_Scope);
var AST_Lambda = DEFNODE("Lambda", "name argnames", {
walk: function(w) {
w._visit(this, function(){
if (this.name) this.name.walk(w);
this.argnames.forEach(function(el){
el.walk(w);
});
AST_Scope.prototype.walk.call(this, w);
});
}
}, AST_Scope);
var AST_Function = DEFNODE("Function", null, {
}, AST_Lambda);
var AST_Defun = DEFNODE("Defun", null, {
}, AST_Function);
/* -----[ JUMPS ]----- */
var AST_Jump = DEFNODE("Jump", "value");
var AST_Jump = DEFNODE("Jump", null, {
});
var AST_Exit = DEFNODE("Exit", "value", {
walk: function(w) {
w._visit(this, function(){
if (this.value) this.value.walk(w);
});
}
}, AST_Jump);
var AST_Return = DEFNODE("Return", null, {
}, AST_Jump);
}, AST_Exit);
var AST_Throw = DEFNODE("Throw", null, {
}, AST_Exit);
var AST_LoopControl = DEFNODE("LoopControl", "label", {
walk: function(w) {
w._visit(this, function(){
if (this.label) this.label.walk(w);
});
}
}, AST_Jump);
var AST_Break = DEFNODE("Break", null, {
}, AST_LoopControl);
var AST_Continue = DEFNODE("Continue", null, {
}, AST_LoopControl);
/* -----[ IF ]----- */
var AST_If = DEFNODE("If", "condition consequent alternative", {
walk: function(w) {
w._visit(this, function(){
this.condition.walk(w);
this.consequent.walk(w);
if (this.alternative) this.alternative.walk(w);
});
}
});
/* -----[ SWITCH ]----- */
var AST_Switch = DEFNODE("Switch", "expression", {
walk: function(w) {
w._visit(this, function(){
this.expression.walk(w);
AST_LabeledStatement.prototype.walk.call(this, w);
});
}
}, AST_LabeledStatement);
var AST_SwitchBlock = DEFNODE("SwitchBlock", null, {
@@ -143,21 +248,41 @@ var AST_SwitchBranch = DEFNODE("SwitchBranch", "body", {
});
var AST_Default = DEFNODE("Default", null, {
walk: function(w) {
w._visit(this, function(){
AST_Statement.prototype.walk.call(this, w);
});
}
}, AST_SwitchBranch);
var AST_Case = DEFNODE("Case", "expression", {
walk: function(w) {
w._visit(this, function(){
this.expression.walk(w);
AST_Statement.prototype.walk.call(this, w);
});
}
}, AST_SwitchBranch);
/* -----[ EXCEPTIONS ]----- */
var AST_Try = DEFNODE("Try", "btry bcatch bfinally", {
walk: function(w) {
w._visit(this, function(){
this.btry.walk(w);
if (this.bcatch) this.bcatch.walk(w);
if (this.bfinally) this.bfinally.walk(w);
});
}
});
var AST_Catch = DEFNODE("Catch", "argname body", {
walk: function(w) {
w._visit(this, function(){
this.argname.walk(w);
this.body.walk(w);
});
}
});
var AST_Finally = DEFNODE("Finally", null, {
@@ -167,7 +292,13 @@ var AST_Finally = DEFNODE("Finally", null, {
/* -----[ VAR/CONST ]----- */
var AST_Definitions = DEFNODE("Definitions", "definitions", {
walk: function(w) {
w._visit(this, function(){
this.definitions.forEach(function(el){
el.walk(w);
});
});
}
});
var AST_Var = DEFNODE("Var", null, {
@@ -179,13 +310,25 @@ var AST_Const = DEFNODE("Const", null, {
}, AST_Definitions);
var AST_VarDef = DEFNODE("VarDef", "name value", {
walk: function(w) {
w._visit(this, function(){
this.name.walk(w);
if (this.value) this.value.walk(w);
});
}
});
/* -----[ OTHER ]----- */
var AST_Call = DEFNODE("Call", "expression args", {
walk: function(w) {
w._visit(this, function(){
this.expression.walk(w);
this.args.forEach(function(el){
el.walk(w);
});
});
}
});
var AST_New = DEFNODE("New", null, {
@@ -193,7 +336,12 @@ var AST_New = DEFNODE("New", null, {
}, AST_Call);
var AST_Seq = DEFNODE("Seq", "first second", {
walk: function(w) {
w._visit(this, function(){
this.first.walk(w);
this.second.walk(w);
});
}
});
var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
@@ -201,15 +349,28 @@ var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
});
var AST_Dot = DEFNODE("Dot", null, {
walk: function(w) {
w._visit(this, function(){
this.expression.walk(w);
});
}
}, AST_PropAccess);
var AST_Sub = DEFNODE("Sub", null, {
walk: function(w) {
w._visit(this, function(){
this.expression.walk(w);
this.property.walk(w);
});
}
}, AST_PropAccess);
var AST_Unary = DEFNODE("Unary", "operator expression", {
walk: function(w) {
w._visit(this, function(){
this.expression.walk(w);
});
}
});
var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, {
@@ -221,77 +382,129 @@ var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, {
}, AST_Unary);
var AST_Binary = DEFNODE("Binary", "left operator right", {
walk: function(w) {
w._visit(this, function(){
this.left.walk(w);
this.right.walk(w);
});
}
});
var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative", {
walk: function(w) {
w._visit(this, function(){
this.condition.walk(w);
this.consequent.walk(w);
this.alternative.walk(w);
});
}
});
var AST_Assign = DEFNODE("Assign", "left operator right", {
var AST_Assign = DEFNODE("Assign", null, {
});
}, AST_Binary);
/* -----[ LITERALS ]----- */
var AST_RegExp = DEFNODE("Regexp", "pattern mods", {
});
var AST_Array = DEFNODE("Array", "elements", {
walk: function(w) {
w._visit(this, function(){
this.elements.forEach(function(el){
el.walk(w);
});
});
}
});
var AST_Object = DEFNODE("Object", "properties", {
walk: function(w) {
w._visit(this, function(){
this.properties.forEach(function(prop){
prop.walk(w);
});
});
}
});
var AST_ObjectProperty = DEFNODE("ObjectProperty");
var AST_ObjectKeyVal = DEFNODE("ObjectKeyval", "key value", {
walk: function(w) {
w._visit(this, function(){
this.value.walk(w);
});
}
}, AST_ObjectProperty);
var AST_ObjectSetter = DEFNODE("ObjectSetter", "name func", {
walk: function(w) {
w._visit(this, function(){
this.func.walk(w);
});
}
}, AST_ObjectProperty);
var AST_ObjectGetter = DEFNODE("ObjectGetter", "name func", {
walk: function(w) {
w._visit(this, function(){
this.func.walk(w);
});
}
}, AST_ObjectProperty);
var AST_Symbol = DEFNODE("Symbol", "name", {
});
var AST_This = DEFNODE("This", null, {
}, AST_Symbol);
var AST_SymbolRef = DEFNODE("SymbolRef", "scope symbol", {
}, AST_Symbol);
var AST_Label = DEFNODE("Label", null, {
}, AST_SymbolRef);
var AST_Constant = DEFNODE("Constant", null, {
getValue: function() {
return this.value;
}
});
var AST_String = DEFNODE("String", "value", {
});
}, AST_Constant);
var AST_Number = DEFNODE("Number", "value", {
});
}, AST_Constant);
var AST_Boolean = DEFNODE("Boolean", "value", {
});
var AST_RegExp = DEFNODE("Regexp", "pattern mods", {
getValue: function() {
return this._regexp || (
this._regexp = new RegExp(this.pattern, this.mods)
);
}
}, AST_Constant);
var AST_Atom = DEFNODE("Atom", null, {
});
}, AST_Constant);
var AST_Null = DEFNODE("Null", null, {
getValue: function() { return null }
}, AST_Atom);
var AST_Undefined = DEFNODE("Undefined", null, {
getValue: function() { return (function(){}()) }
}, AST_Atom);
var AST_False = DEFNODE("False", null, {
getValue: function() { return false }
}, AST_Atom);
var AST_True = DEFNODE("True", null, {
getValue: function() { return true }
}, AST_Atom);

View File

@@ -1,13 +1,18 @@
#! /usr/bin/env node
(function(){
var fs = require("fs");
var vm = require("vm");
var sys = require("util");
function load_global(file) {
var code = fs.readFileSync(file, "utf8");
return global.eval(code);
return vm.runInThisContext(code, file);
};
load_global("./utils.js");
load_global("./output.js");
load_global("./ast.js");
load_global("./parse.js");
@@ -18,3 +23,13 @@ console.time("parse");
var ast = parse(fs.readFileSync(filename, "utf8"));
console.timeEnd("parse");
console.time("walk");
ast.walk({
_visit: function(node, descend) {
//console.log(node);
if (descend) descend.call(node);
}
});
console.timeEnd("walk");
})();

134
lib/output.js Normal file
View File

@@ -0,0 +1,134 @@
function OutputStream(options) {
options = defaults(options, {
indent_start : 0,
indent_level : 4,
quote_keys : false,
space_colon : false,
beautify : true,
ascii_only : false,
inline_script : false,
width : 80
});
var indentation = 0;
var current_col = 0;
var OUTPUT = "";
function to_ascii(str) {
return str.replace(/[\u0080-\uffff]/g, function(ch) {
var code = ch.charCodeAt(0).toString(16);
while (code.length < 4) code = "0" + code;
return "\\u" + code;
});
};
function make_string(str) {
var dq = 0, sq = 0;
str = str.replace(/[\\\b\f\n\r\t\x22\x27\u2028\u2029\0]/g, function(s){
switch (s) {
case "\\": return "\\\\";
case "\b": return "\\b";
case "\f": return "\\f";
case "\n": return "\\n";
case "\r": return "\\r";
case "\u2028": return "\\u2028";
case "\u2029": return "\\u2029";
case '"': ++dq; return '"';
case "'": ++sq; return "'";
case "\0": return "\\0";
}
return s;
});
if (options.ascii_only) str = to_ascii(str);
if (dq > sq) return "'" + str.replace(/\x27/g, "\\'") + "'";
else return '"' + str.replace(/\x22/g, '\\"') + '"';
};
function print(str) {
var nl = str.lastIndexOf("\n");
if (nl >= 0) {
current_col = nl;
} else {
current_col += str.length;
}
OUTPUT += str;
};
function encode_string(str) {
var ret = make_string(str);
if (options.inline_script)
ret = ret.replace(/<\x2fscript([>\/\t\n\f\r ])/gi, "<\\/script$1");
return ret;
};
function make_name(name) {
name = name.toString();
if (options.ascii_only)
name = to_ascii(name);
return name;
};
function make_indent(line) {
if (line == null)
line = "";
if (beautify)
line = repeat_string(" ", options.indent_start + indentation) + line;
return line;
};
function with_indent(col, cont) {
var save_indentation = indentation;
indentation = col;
var ret = cont();
indentation = save_indentation;
return ret;
};
function indent() {
if (options.beautify) print(make_indent());
};
function newline() {
if (options.beautify) {
print("\n");
print(make_indent());
}
};
function next_indent() {
return indentation + options.indent_level;
};
function with_block(cont) {
var ret;
print("{");
with_indent(next_indent(), function(){
newline();
ret = cont();
newline();
});
indent();
print("}");
return ret;
};
function with_parens(cont) {
print("(");
var ret = with_indent(current_col, cont);
print(")");
return ret;
};
return {
get : function() { return OUTPUT },
indent : indent,
newline : newline,
print : print,
string : function(str) { print(encode_string(str)) },
with_indent : with_indent,
with_block : with_block,
with_parens : with_parens,
options : function() { return options }
};
};

View File

@@ -577,13 +577,13 @@ var UNARY_POSTFIX = array_to_hash([ "--", "++" ]);
var ASSIGNMENT = (function(a, ret, i){
while (i < a.length) {
ret[a[i]] = a[i].substr(0, a[i].length - 1);
ret[a[i]] = a[i];
i++;
}
return ret;
})(
["+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&="],
{ "=": true },
[ "=", "+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=" ],
{},
0
);
@@ -695,10 +695,11 @@ function parse($TEXT, exigent_mode) {
};
function parenthesised() {
expect("(");
var ex = expression();
expect(")");
return ex;
return new AST_Parenthesized({
start : expect("("),
expression : expression(),
end : expect(")")
});
};
function embed_tokens(parser) {
@@ -828,8 +829,7 @@ function parse($TEXT, exigent_mode) {
});
function labeled_statement() {
var label = S.token.value;
next();
var label = as_symbol(true);
expect(":");
S.labels.push(label);
var start = S.token, stat = statement();
@@ -845,19 +845,21 @@ function parse($TEXT, exigent_mode) {
};
function break_cont(type) {
var name = null;
var name = null, label = null;
if (!can_insert_semicolon()) {
name = is("name") ? S.token.value : null;
}
if (name != null) {
next();
if (!member(name, S.labels))
label = find_if(function(l){ return l.name == name }, S.labels);
if (!label)
croak("Label " + name + " without matching loop or statement");
label = new AST_Label({ name: name, symbol: label });
}
else if (S.in_loop == 0)
croak(type.TYPE + " not inside a loop or switch");
semicolon();
return new type({ label: name });
return new type({ label: label });
};
function for_() {
@@ -892,19 +894,19 @@ function parse($TEXT, exigent_mode) {
};
function for_in(init) {
var lhs = init instanceof AST_Var ? init.definitions[0].name : init;
var lhs = init instanceof AST_Var ? init.definitions[0].name : null;
var obj = expression();
expect(")");
return new AST_ForIn({
init : init,
lhs : lhs,
name : lhs,
object : obj,
body : in_loop(statement)
});
};
var function_ = function(in_statement) {
var name = is("name") ? as_symbol() : null;
var name = is("name") ? as_symbol(true) : null;
if (in_statement && !name)
unexpected();
expect("(");
@@ -914,7 +916,7 @@ function parse($TEXT, exigent_mode) {
argnames: (function(first, a){
while (!is("punc", ")")) {
if (first) first = false; else expect(",");
a.push(as_symbol());
a.push(as_symbol(true));
}
next();
return a;
@@ -922,11 +924,14 @@ function parse($TEXT, exigent_mode) {
body: embed_tokens(function(){
++S.in_function;
var loop = S.in_loop;
var labels = S.labels;
S.in_directives = true;
S.in_loop = 0;
S.labels = [];
var a = block_();
--S.in_function;
S.in_loop = loop;
S.labels = labels;
return new AST_Bracketed({ body: a });
})()
});
@@ -958,47 +963,70 @@ function parse($TEXT, exigent_mode) {
var switch_block_ = embed_tokens(curry(in_loop, function(){
expect("{");
var a = [], cur = null;
var a = [], cur = null, branch = null;
while (!is("punc", "}")) {
if (is("eof")) unexpected();
if (is("keyword", "case")) {
next();
if (branch) branch.end = prev();
cur = [];
a.push(new AST_Case({ expression: expression(), body: cur }));
branch = new AST_Case({
start : prog1(S.token, next),
expression : expression(),
body : cur
});
a.push(branch);
expect(":");
}
else if (is("keyword", "default")) {
next();
expect(":");
if (branch) branch.end = prev();
cur = [];
a.push(new AST_Default({ body: cur }));
branch = new AST_Default({
start : prog1(S.token, next, curry(expect, ":")),
body : cur
})
a.push(branch);
}
else {
if (!cur) unexpected();
cur.push(statement());
}
}
if (branch) branch.end = prev();
next();
return new AST_SwitchBlock({ body: a });
}));
function try_() {
var body = new AST_Bracketed({
body: block_()
start : S.token,
body : block_(),
end : prev()
}), bcatch = null, bfinally = null;
if (is("keyword", "catch")) {
var start = S.token;
next();
expect("(");
var name = as_symbol();
var name = as_symbol(true);
expect(")");
bcatch = new AST_Catch({
start : start,
argname : name,
body : new AST_Bracketed({ body: block_() })
body : new AST_Bracketed({
start : S.token,
body : block_(),
end : prev()
}),
end : prev()
});
}
if (is("keyword", "finally")) {
var start = S.token;
next();
bfinally = new AST_Finally({ body: block_() });
bfinally = new AST_Finally({
start : start,
body : block_(),
end : prev()
});
}
if (!bcatch && !bfinally)
croak("Missing catch/finally blocks");
@@ -1014,7 +1042,7 @@ function parse($TEXT, exigent_mode) {
for (;;) {
a.push(new AST_VarDef({
start : S.token,
name : as_symbol(),
name : as_symbol(true),
value : is("operator", "=") ? (next(), expression(false, no_in)) : null,
end : prev()
}));
@@ -1025,19 +1053,25 @@ function parse($TEXT, exigent_mode) {
return a;
};
var var_ = embed_tokens(function(no_in) {
var var_ = function(no_in) {
return new AST_Var({
definitions: vardefs(no_in)
});
start : prev(),
definitions : vardefs(no_in),
end : prev()
});
};
var const_ = embed_tokens(function() {
var const_ = function() {
return new AST_Const({
definitions: vardefs()
});
start : prev(),
definitions : vardefs(),
end : prev()
});
};
var new_ = embed_tokens(function() {
var new_ = function() {
var start = S.token;
expect_token("operator", "new");
var newexp = expr_atom(false), args;
if (is("punc", "(")) {
next();
@@ -1046,10 +1080,12 @@ function parse($TEXT, exigent_mode) {
args = [];
}
return subscripts(new AST_New({
start : start,
expression : newexp,
args : args
args : args,
end : prev()
}), true);
});
};
function as_atom_node() {
var tok = S.token, ret;
@@ -1085,25 +1121,26 @@ function parse($TEXT, exigent_mode) {
var expr_atom = function(allow_calls) {
if (is("operator", "new")) {
next();
return new_();
}
var start = S.token;
if (is("punc")) {
switch (S.token.value) {
switch (start.value) {
case "(":
next();
return subscripts(prog1(expression, curry(expect, ")")), allow_calls);
var ex = expression();
ex.start = start;
ex.end = S.token;
expect(")");
return subscripts(ex, allow_calls);
case "[":
next();
return subscripts(array_(), allow_calls);
case "{":
next();
return subscripts(object_(), allow_calls);
}
unexpected();
}
if (is("keyword", "function")) {
var start = S.token;
next();
var func = function_(false);
func.start = start;
@@ -1131,13 +1168,15 @@ function parse($TEXT, exigent_mode) {
return a;
};
function array_() {
var array_ = embed_tokens(function() {
expect("[");
return new AST_Array({
elements: expr_list("]", !exigent_mode, true)
});
};
});
var object_ = embed_tokens(function() {
expect("{");
var first = true, a = [];
while (!is("punc", "}")) {
if (first) first = false; else expect(",");
@@ -1183,9 +1222,14 @@ function parse($TEXT, exigent_mode) {
switch (S.token.type) {
case "num":
case "string":
return as_symbol(true);
case "name":
case "operator":
case "keyword":
case "atom":
return prog1(S.token.value, next);
default:
unexpected();
}
return as_name();
};
function as_name() {
@@ -1194,15 +1238,16 @@ function parse($TEXT, exigent_mode) {
case "operator":
case "keyword":
case "atom":
return as_symbol(true);
return prog1(S.token.value, next);
default:
unexpected();
}
};
function as_symbol(noerror) {
if (!noerror && !is("name")) croak("Name expected");
var sym = new AST_Symbol({
function as_symbol(def) {
if (!is("name")) croak("Name expected");
var name = S.token.value;
var sym = new (name == "this" ? AST_This : def ? AST_Symbol : AST_SymbolRef)({
name : String(S.token.value),
start : S.token,
end : S.token
@@ -1211,44 +1256,59 @@ function parse($TEXT, exigent_mode) {
return sym;
};
var subscripts = embed_tokens(function(expr, allow_calls) {
var subscripts = function(expr, allow_calls) {
var start = expr.start;
if (is("punc", ".")) {
next();
return subscripts(new AST_Dot({
start : start,
expression : expr,
property : as_name()
property : as_name(),
end : prev()
}), allow_calls);
}
if (is("punc", "[")) {
next();
var prop = expression();
expect("]");
return subscripts(new AST_Sub({
start : start,
expression : expr,
property : prog1(expression, curry(expect, "]"))
property : prop,
end : prev()
}), allow_calls);
}
if (allow_calls && is("punc", "(")) {
next();
return subscripts(new AST_Call({
start : start,
expression : expr,
args : expr_list(")")
args : expr_list(")"),
end : prev()
}), true);
}
return expr;
});
};
var maybe_unary = embed_tokens(function(allow_calls) {
var maybe_unary = function(allow_calls) {
var start = S.token;
if (is("operator") && HOP(UNARY_PREFIX, S.token.value)) {
return make_unary(AST_UnaryPrefix,
var ex = make_unary(AST_UnaryPrefix,
prog1(S.token.value, next),
maybe_unary(allow_calls));
ex.start = start;
ex.end = prev();
return ex;
}
var val = expr_atom(allow_calls);
while (is("operator") && HOP(UNARY_POSTFIX, S.token.value) && !S.token.nlb) {
val = make_unary(AST_UnaryPostfix, S.token.value, val);
val.start = start;
val.end = S.token;
next();
}
return val;
});
};
function make_unary(ctor, op, expr) {
if ((op == "++" || op == "--") && !is_assignable(expr))
@@ -1256,7 +1316,7 @@ function parse($TEXT, exigent_mode) {
return new ctor({ operator: op, expression: expr });
};
var expr_op = embed_tokens(function(left, min_prec, no_in) {
var expr_op = function(left, min_prec, no_in) {
var op = is("operator") ? S.token.value : null;
if (op == "in" && no_in) op = null;
var prec = op != null ? PRECEDENCE[op] : null;
@@ -1264,32 +1324,37 @@ function parse($TEXT, exigent_mode) {
next();
var right = expr_op(maybe_unary(true), prec, no_in);
return expr_op(new AST_Binary({
start : left.start,
left : left,
operator : op,
right : right
right : right,
end : right.end
}), min_prec, no_in);
}
return left;
});
};
function expr_ops(no_in) {
return expr_op(maybe_unary(true), 0, no_in);
};
var maybe_conditional = embed_tokens(function(no_in) {
var maybe_conditional = function(no_in) {
var start = S.token;
var expr = expr_ops(no_in);
if (is("operator", "?")) {
next();
var yes = expression(false);
expect(":");
return new AST_Conditional({
start : start,
condition : expr,
consequent : yes,
alternative: expression(false, no_in)
alternative : expression(false, no_in),
end : peek()
});
}
return expr;
});
};
function is_assignable(expr) {
if (!exigent_mode) return true;
@@ -1304,35 +1369,41 @@ function parse($TEXT, exigent_mode) {
}
};
var maybe_assign = embed_tokens(function(no_in) {
var maybe_assign = function(no_in) {
var start = S.token;
var left = maybe_conditional(no_in), val = S.token.value;
if (is("operator") && HOP(ASSIGNMENT, val)) {
if (is_assignable(left)) {
next();
return new AST_Assign({
start : start,
left : left,
operator : ASSIGNMENT[val],
right : maybe_assign(no_in)
right : maybe_assign(no_in),
end : peek()
});
}
croak("Invalid assignment");
}
return left;
});
};
var expression = embed_tokens(function(commas, no_in) {
var expression = function(commas, no_in) {
if (arguments.length == 0)
commas = true;
var start = S.token;
var expr = maybe_assign(no_in);
if (commas && is("punc", ",")) {
next();
return new AST_Seq({
start : start,
first : expr,
second : expression(true, no_in)
second : expression(true, no_in),
end : peek()
});
}
return expr;
});
};
function in_loop(cont) {
++S.in_loop;
@@ -1342,11 +1413,13 @@ function parse($TEXT, exigent_mode) {
};
return new AST_Toplevel({
start: S.token,
body: (function(a){
while (!is("eof"))
a.push(statement());
return a;
})([])
})([]),
end: prev()
});
};

File diff suppressed because it is too large Load Diff

View File

@@ -33,6 +33,32 @@ function member(name, array) {
return false;
};
function find_if(func, array) {
for (var i = 0, n = array.length; i < n; ++i) {
if (func(array[i]))
return array[i];
}
};
function HOP(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
};
function repeat_string(str, i) {
if (i <= 0) return "";
if (i == 1) return str;
var d = repeat_string(str, i >> 1);
d += d;
if (i & 1) d += str;
return d;
};
function defaults(args, defs) {
var ret = {};
if (args === true)
args = {};
for (var i in defs) if (HOP(defs, i)) {
ret[i] = (args && HOP(args, i)) ? args[i] : defs[i];
}
return ret;
};