WIP
This commit is contained in:
343
lib/ast.js
343
lib/ast.js
@@ -18,42 +18,75 @@ function DEFNODE(type, props, methods, base) {
|
|||||||
if (type) {
|
if (type) {
|
||||||
ctor.prototype.TYPE = ctor.TYPE = 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];
|
ctor.prototype[i] = methods[i];
|
||||||
}
|
}
|
||||||
return ctor;
|
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);
|
}, null);
|
||||||
|
|
||||||
var AST_Node = DEFNODE("Node", "start end", {
|
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);
|
}, null);
|
||||||
|
|
||||||
var AST_Directive = DEFNODE("Directive", "value", {
|
var AST_Directive = DEFNODE("Directive", "value", {
|
||||||
|
print: function(output) {
|
||||||
|
output.string(this.value);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var AST_Debugger = DEFNODE("Debugger", null, {
|
var AST_Debugger = DEFNODE("Debugger", null, {
|
||||||
|
print: function(output) {
|
||||||
|
output.print("debugger");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var AST_Parenthesized = DEFNODE("Parenthesized", "expression", {
|
var AST_Parenthesized = DEFNODE("Parenthesized", "expression", {
|
||||||
documentation: "Represents an expression which is always parenthesized. Used for the \
|
$documentation: "Represents an expression which is always parenthesized. Used for the \
|
||||||
conditions in IF/WHILE."
|
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", {
|
var AST_Bracketed = DEFNODE("Bracketed", "body", {
|
||||||
documentation: "Represents a block of statements that are always included in brackets. \
|
$documentation: "Represents a block of statements that are always included in brackets. \
|
||||||
Used for bodies of FUNCTION/TRY/CATCH/THROW/SWITCH."
|
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 ]----- */
|
/* -----[ loops ]----- */
|
||||||
|
|
||||||
var AST_LabeledStatement = DEFNODE("LabeledStatement", "label body", {
|
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, {
|
var AST_Statement = DEFNODE("Statement", null, {
|
||||||
@@ -61,39 +94,64 @@ var AST_Statement = DEFNODE("Statement", null, {
|
|||||||
}, AST_LabeledStatement);
|
}, AST_LabeledStatement);
|
||||||
|
|
||||||
var AST_Do = DEFNODE("Do", "condition", {
|
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);
|
}, AST_LabeledStatement);
|
||||||
|
|
||||||
var AST_While = DEFNODE("While", "condition", {
|
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);
|
}, AST_LabeledStatement);
|
||||||
|
|
||||||
var AST_For = DEFNODE("For", "init condition step", {
|
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);
|
}, AST_LabeledStatement);
|
||||||
|
|
||||||
var AST_ForIn = DEFNODE("ForIn", "init name object", {
|
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);
|
}, AST_LabeledStatement);
|
||||||
|
|
||||||
var AST_With = DEFNODE("With", "expression body", {
|
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 ]----- */
|
/* -----[ functions ]----- */
|
||||||
|
|
||||||
var AST_Scope = DEFNODE("Scope", "identifiers body", {
|
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, {
|
var AST_Toplevel = DEFNODE("Toplevel", null, {
|
||||||
@@ -101,37 +159,84 @@ var AST_Toplevel = DEFNODE("Toplevel", null, {
|
|||||||
}, AST_Scope);
|
}, AST_Scope);
|
||||||
|
|
||||||
var AST_Lambda = DEFNODE("Lambda", "name argnames", {
|
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);
|
}, AST_Scope);
|
||||||
|
|
||||||
var AST_Function = DEFNODE("Function", null, {
|
var AST_Function = DEFNODE("Function", null, {
|
||||||
|
|
||||||
}, AST_Lambda);
|
}, AST_Lambda);
|
||||||
|
|
||||||
var AST_Defun = DEFNODE("Defun", null, {
|
var AST_Defun = DEFNODE("Defun", null, {
|
||||||
|
|
||||||
}, AST_Function);
|
}, AST_Function);
|
||||||
|
|
||||||
/* -----[ JUMPS ]----- */
|
/* -----[ 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, {
|
var AST_Return = DEFNODE("Return", null, {
|
||||||
|
|
||||||
}, AST_Jump);
|
}, AST_Exit);
|
||||||
|
|
||||||
var AST_Throw = DEFNODE("Throw", null, {
|
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);
|
}, AST_Jump);
|
||||||
|
|
||||||
|
var AST_Break = DEFNODE("Break", null, {
|
||||||
|
|
||||||
|
}, AST_LoopControl);
|
||||||
|
|
||||||
|
var AST_Continue = DEFNODE("Continue", null, {
|
||||||
|
|
||||||
|
}, AST_LoopControl);
|
||||||
|
|
||||||
/* -----[ IF ]----- */
|
/* -----[ IF ]----- */
|
||||||
|
|
||||||
var AST_If = DEFNODE("If", "condition consequent alternative", {
|
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 ]----- */
|
/* -----[ SWITCH ]----- */
|
||||||
|
|
||||||
var AST_Switch = DEFNODE("Switch", "expression", {
|
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);
|
}, AST_LabeledStatement);
|
||||||
|
|
||||||
var AST_SwitchBlock = DEFNODE("SwitchBlock", null, {
|
var AST_SwitchBlock = DEFNODE("SwitchBlock", null, {
|
||||||
@@ -143,21 +248,41 @@ var AST_SwitchBranch = DEFNODE("SwitchBranch", "body", {
|
|||||||
});
|
});
|
||||||
|
|
||||||
var AST_Default = DEFNODE("Default", null, {
|
var AST_Default = DEFNODE("Default", null, {
|
||||||
|
walk: function(w) {
|
||||||
|
w._visit(this, function(){
|
||||||
|
AST_Statement.prototype.walk.call(this, w);
|
||||||
|
});
|
||||||
|
}
|
||||||
}, AST_SwitchBranch);
|
}, AST_SwitchBranch);
|
||||||
|
|
||||||
var AST_Case = DEFNODE("Case", "expression", {
|
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);
|
}, AST_SwitchBranch);
|
||||||
|
|
||||||
/* -----[ EXCEPTIONS ]----- */
|
/* -----[ EXCEPTIONS ]----- */
|
||||||
|
|
||||||
var AST_Try = DEFNODE("Try", "btry bcatch bfinally", {
|
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", {
|
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, {
|
var AST_Finally = DEFNODE("Finally", null, {
|
||||||
@@ -167,7 +292,13 @@ var AST_Finally = DEFNODE("Finally", null, {
|
|||||||
/* -----[ VAR/CONST ]----- */
|
/* -----[ VAR/CONST ]----- */
|
||||||
|
|
||||||
var AST_Definitions = DEFNODE("Definitions", "definitions", {
|
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, {
|
var AST_Var = DEFNODE("Var", null, {
|
||||||
@@ -179,13 +310,25 @@ var AST_Const = DEFNODE("Const", null, {
|
|||||||
}, AST_Definitions);
|
}, AST_Definitions);
|
||||||
|
|
||||||
var AST_VarDef = DEFNODE("VarDef", "name value", {
|
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 ]----- */
|
/* -----[ OTHER ]----- */
|
||||||
|
|
||||||
var AST_Call = DEFNODE("Call", "expression args", {
|
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, {
|
var AST_New = DEFNODE("New", null, {
|
||||||
@@ -193,7 +336,12 @@ var AST_New = DEFNODE("New", null, {
|
|||||||
}, AST_Call);
|
}, AST_Call);
|
||||||
|
|
||||||
var AST_Seq = DEFNODE("Seq", "first second", {
|
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", {
|
var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
|
||||||
@@ -201,15 +349,28 @@ var AST_PropAccess = DEFNODE("PropAccess", "expression property", {
|
|||||||
});
|
});
|
||||||
|
|
||||||
var AST_Dot = DEFNODE("Dot", null, {
|
var AST_Dot = DEFNODE("Dot", null, {
|
||||||
|
walk: function(w) {
|
||||||
|
w._visit(this, function(){
|
||||||
|
this.expression.walk(w);
|
||||||
|
});
|
||||||
|
}
|
||||||
}, AST_PropAccess);
|
}, AST_PropAccess);
|
||||||
|
|
||||||
var AST_Sub = DEFNODE("Sub", null, {
|
var AST_Sub = DEFNODE("Sub", null, {
|
||||||
|
walk: function(w) {
|
||||||
|
w._visit(this, function(){
|
||||||
|
this.expression.walk(w);
|
||||||
|
this.property.walk(w);
|
||||||
|
});
|
||||||
|
}
|
||||||
}, AST_PropAccess);
|
}, AST_PropAccess);
|
||||||
|
|
||||||
var AST_Unary = DEFNODE("Unary", "operator expression", {
|
var AST_Unary = DEFNODE("Unary", "operator expression", {
|
||||||
|
walk: function(w) {
|
||||||
|
w._visit(this, function(){
|
||||||
|
this.expression.walk(w);
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, {
|
var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, {
|
||||||
@@ -221,77 +382,129 @@ var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, {
|
|||||||
}, AST_Unary);
|
}, AST_Unary);
|
||||||
|
|
||||||
var AST_Binary = DEFNODE("Binary", "left operator right", {
|
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", {
|
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 ]----- */
|
/* -----[ LITERALS ]----- */
|
||||||
|
|
||||||
var AST_RegExp = DEFNODE("Regexp", "pattern mods", {
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
var AST_Array = DEFNODE("Array", "elements", {
|
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", {
|
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_ObjectProperty = DEFNODE("ObjectProperty");
|
||||||
|
|
||||||
var AST_ObjectKeyVal = DEFNODE("ObjectKeyval", "key value", {
|
var AST_ObjectKeyVal = DEFNODE("ObjectKeyval", "key value", {
|
||||||
|
walk: function(w) {
|
||||||
|
w._visit(this, function(){
|
||||||
|
this.value.walk(w);
|
||||||
|
});
|
||||||
|
}
|
||||||
}, AST_ObjectProperty);
|
}, AST_ObjectProperty);
|
||||||
|
|
||||||
var AST_ObjectSetter = DEFNODE("ObjectSetter", "name func", {
|
var AST_ObjectSetter = DEFNODE("ObjectSetter", "name func", {
|
||||||
|
walk: function(w) {
|
||||||
|
w._visit(this, function(){
|
||||||
|
this.func.walk(w);
|
||||||
|
});
|
||||||
|
}
|
||||||
}, AST_ObjectProperty);
|
}, AST_ObjectProperty);
|
||||||
|
|
||||||
var AST_ObjectGetter = DEFNODE("ObjectGetter", "name func", {
|
var AST_ObjectGetter = DEFNODE("ObjectGetter", "name func", {
|
||||||
|
walk: function(w) {
|
||||||
|
w._visit(this, function(){
|
||||||
|
this.func.walk(w);
|
||||||
|
});
|
||||||
|
}
|
||||||
}, AST_ObjectProperty);
|
}, AST_ObjectProperty);
|
||||||
|
|
||||||
var AST_Symbol = DEFNODE("Symbol", "name", {
|
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", {
|
var AST_String = DEFNODE("String", "value", {
|
||||||
|
|
||||||
});
|
}, AST_Constant);
|
||||||
|
|
||||||
var AST_Number = DEFNODE("Number", "value", {
|
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, {
|
var AST_Atom = DEFNODE("Atom", null, {
|
||||||
|
|
||||||
});
|
}, AST_Constant);
|
||||||
|
|
||||||
var AST_Null = DEFNODE("Null", null, {
|
var AST_Null = DEFNODE("Null", null, {
|
||||||
|
getValue: function() { return null }
|
||||||
}, AST_Atom);
|
}, AST_Atom);
|
||||||
|
|
||||||
var AST_Undefined = DEFNODE("Undefined", null, {
|
var AST_Undefined = DEFNODE("Undefined", null, {
|
||||||
|
getValue: function() { return (function(){}()) }
|
||||||
}, AST_Atom);
|
}, AST_Atom);
|
||||||
|
|
||||||
var AST_False = DEFNODE("False", null, {
|
var AST_False = DEFNODE("False", null, {
|
||||||
|
getValue: function() { return false }
|
||||||
}, AST_Atom);
|
}, AST_Atom);
|
||||||
|
|
||||||
var AST_True = DEFNODE("True", null, {
|
var AST_True = DEFNODE("True", null, {
|
||||||
|
getValue: function() { return true }
|
||||||
}, AST_Atom);
|
}, AST_Atom);
|
||||||
|
|||||||
41
lib/node.js
41
lib/node.js
@@ -1,20 +1,35 @@
|
|||||||
#! /usr/bin/env node
|
#! /usr/bin/env node
|
||||||
|
|
||||||
var fs = require("fs");
|
(function(){
|
||||||
|
|
||||||
function load_global(file) {
|
var fs = require("fs");
|
||||||
var code = fs.readFileSync(file, "utf8");
|
var vm = require("vm");
|
||||||
return global.eval(code);
|
var sys = require("util");
|
||||||
};
|
|
||||||
|
|
||||||
load_global("./utils.js");
|
function load_global(file) {
|
||||||
load_global("./ast.js");
|
var code = fs.readFileSync(file, "utf8");
|
||||||
load_global("./parse.js");
|
return vm.runInThisContext(code, file);
|
||||||
|
};
|
||||||
|
|
||||||
///
|
load_global("./utils.js");
|
||||||
|
load_global("./output.js");
|
||||||
|
load_global("./ast.js");
|
||||||
|
load_global("./parse.js");
|
||||||
|
|
||||||
var filename = process.argv[2];
|
///
|
||||||
console.time("parse");
|
|
||||||
var ast = parse(fs.readFileSync(filename, "utf8"));
|
|
||||||
console.timeEnd("parse");
|
|
||||||
|
|
||||||
|
var filename = process.argv[2];
|
||||||
|
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
134
lib/output.js
Normal 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 }
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
223
lib/parse.js
223
lib/parse.js
@@ -577,13 +577,13 @@ var UNARY_POSTFIX = array_to_hash([ "--", "++" ]);
|
|||||||
|
|
||||||
var ASSIGNMENT = (function(a, ret, i){
|
var ASSIGNMENT = (function(a, ret, i){
|
||||||
while (i < a.length) {
|
while (i < a.length) {
|
||||||
ret[a[i]] = a[i].substr(0, a[i].length - 1);
|
ret[a[i]] = a[i];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
})(
|
})(
|
||||||
["+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&="],
|
[ "=", "+=", "-=", "/=", "*=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=" ],
|
||||||
{ "=": true },
|
{},
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -695,10 +695,11 @@ function parse($TEXT, exigent_mode) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function parenthesised() {
|
function parenthesised() {
|
||||||
expect("(");
|
return new AST_Parenthesized({
|
||||||
var ex = expression();
|
start : expect("("),
|
||||||
expect(")");
|
expression : expression(),
|
||||||
return ex;
|
end : expect(")")
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function embed_tokens(parser) {
|
function embed_tokens(parser) {
|
||||||
@@ -828,8 +829,7 @@ function parse($TEXT, exigent_mode) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function labeled_statement() {
|
function labeled_statement() {
|
||||||
var label = S.token.value;
|
var label = as_symbol(true);
|
||||||
next();
|
|
||||||
expect(":");
|
expect(":");
|
||||||
S.labels.push(label);
|
S.labels.push(label);
|
||||||
var start = S.token, stat = statement();
|
var start = S.token, stat = statement();
|
||||||
@@ -845,19 +845,21 @@ function parse($TEXT, exigent_mode) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function break_cont(type) {
|
function break_cont(type) {
|
||||||
var name = null;
|
var name = null, label = null;
|
||||||
if (!can_insert_semicolon()) {
|
if (!can_insert_semicolon()) {
|
||||||
name = is("name") ? S.token.value : null;
|
name = is("name") ? S.token.value : null;
|
||||||
}
|
}
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
next();
|
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");
|
croak("Label " + name + " without matching loop or statement");
|
||||||
|
label = new AST_Label({ name: name, symbol: label });
|
||||||
}
|
}
|
||||||
else if (S.in_loop == 0)
|
else if (S.in_loop == 0)
|
||||||
croak(type.TYPE + " not inside a loop or switch");
|
croak(type.TYPE + " not inside a loop or switch");
|
||||||
semicolon();
|
semicolon();
|
||||||
return new type({ label: name });
|
return new type({ label: label });
|
||||||
};
|
};
|
||||||
|
|
||||||
function for_() {
|
function for_() {
|
||||||
@@ -892,19 +894,19 @@ function parse($TEXT, exigent_mode) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function for_in(init) {
|
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();
|
var obj = expression();
|
||||||
expect(")");
|
expect(")");
|
||||||
return new AST_ForIn({
|
return new AST_ForIn({
|
||||||
init : init,
|
init : init,
|
||||||
lhs : lhs,
|
name : lhs,
|
||||||
object : obj,
|
object : obj,
|
||||||
body : in_loop(statement)
|
body : in_loop(statement)
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
var function_ = function(in_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)
|
if (in_statement && !name)
|
||||||
unexpected();
|
unexpected();
|
||||||
expect("(");
|
expect("(");
|
||||||
@@ -914,7 +916,7 @@ function parse($TEXT, exigent_mode) {
|
|||||||
argnames: (function(first, a){
|
argnames: (function(first, a){
|
||||||
while (!is("punc", ")")) {
|
while (!is("punc", ")")) {
|
||||||
if (first) first = false; else expect(",");
|
if (first) first = false; else expect(",");
|
||||||
a.push(as_symbol());
|
a.push(as_symbol(true));
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
return a;
|
return a;
|
||||||
@@ -922,11 +924,14 @@ function parse($TEXT, exigent_mode) {
|
|||||||
body: embed_tokens(function(){
|
body: embed_tokens(function(){
|
||||||
++S.in_function;
|
++S.in_function;
|
||||||
var loop = S.in_loop;
|
var loop = S.in_loop;
|
||||||
|
var labels = S.labels;
|
||||||
S.in_directives = true;
|
S.in_directives = true;
|
||||||
S.in_loop = 0;
|
S.in_loop = 0;
|
||||||
|
S.labels = [];
|
||||||
var a = block_();
|
var a = block_();
|
||||||
--S.in_function;
|
--S.in_function;
|
||||||
S.in_loop = loop;
|
S.in_loop = loop;
|
||||||
|
S.labels = labels;
|
||||||
return new AST_Bracketed({ body: a });
|
return new AST_Bracketed({ body: a });
|
||||||
})()
|
})()
|
||||||
});
|
});
|
||||||
@@ -958,47 +963,70 @@ function parse($TEXT, exigent_mode) {
|
|||||||
|
|
||||||
var switch_block_ = embed_tokens(curry(in_loop, function(){
|
var switch_block_ = embed_tokens(curry(in_loop, function(){
|
||||||
expect("{");
|
expect("{");
|
||||||
var a = [], cur = null;
|
var a = [], cur = null, branch = null;
|
||||||
while (!is("punc", "}")) {
|
while (!is("punc", "}")) {
|
||||||
if (is("eof")) unexpected();
|
if (is("eof")) unexpected();
|
||||||
if (is("keyword", "case")) {
|
if (is("keyword", "case")) {
|
||||||
next();
|
if (branch) branch.end = prev();
|
||||||
cur = [];
|
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(":");
|
expect(":");
|
||||||
}
|
}
|
||||||
else if (is("keyword", "default")) {
|
else if (is("keyword", "default")) {
|
||||||
next();
|
if (branch) branch.end = prev();
|
||||||
expect(":");
|
|
||||||
cur = [];
|
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 {
|
else {
|
||||||
if (!cur) unexpected();
|
if (!cur) unexpected();
|
||||||
cur.push(statement());
|
cur.push(statement());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (branch) branch.end = prev();
|
||||||
next();
|
next();
|
||||||
return new AST_SwitchBlock({ body: a });
|
return new AST_SwitchBlock({ body: a });
|
||||||
}));
|
}));
|
||||||
|
|
||||||
function try_() {
|
function try_() {
|
||||||
var body = new AST_Bracketed({
|
var body = new AST_Bracketed({
|
||||||
body: block_()
|
start : S.token,
|
||||||
|
body : block_(),
|
||||||
|
end : prev()
|
||||||
}), bcatch = null, bfinally = null;
|
}), bcatch = null, bfinally = null;
|
||||||
if (is("keyword", "catch")) {
|
if (is("keyword", "catch")) {
|
||||||
|
var start = S.token;
|
||||||
next();
|
next();
|
||||||
expect("(");
|
expect("(");
|
||||||
var name = as_symbol();
|
var name = as_symbol(true);
|
||||||
expect(")");
|
expect(")");
|
||||||
bcatch = new AST_Catch({
|
bcatch = new AST_Catch({
|
||||||
|
start : start,
|
||||||
argname : name,
|
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")) {
|
if (is("keyword", "finally")) {
|
||||||
|
var start = S.token;
|
||||||
next();
|
next();
|
||||||
bfinally = new AST_Finally({ body: block_() });
|
bfinally = new AST_Finally({
|
||||||
|
start : start,
|
||||||
|
body : block_(),
|
||||||
|
end : prev()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (!bcatch && !bfinally)
|
if (!bcatch && !bfinally)
|
||||||
croak("Missing catch/finally blocks");
|
croak("Missing catch/finally blocks");
|
||||||
@@ -1014,7 +1042,7 @@ function parse($TEXT, exigent_mode) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
a.push(new AST_VarDef({
|
a.push(new AST_VarDef({
|
||||||
start : S.token,
|
start : S.token,
|
||||||
name : as_symbol(),
|
name : as_symbol(true),
|
||||||
value : is("operator", "=") ? (next(), expression(false, no_in)) : null,
|
value : is("operator", "=") ? (next(), expression(false, no_in)) : null,
|
||||||
end : prev()
|
end : prev()
|
||||||
}));
|
}));
|
||||||
@@ -1025,19 +1053,25 @@ function parse($TEXT, exigent_mode) {
|
|||||||
return a;
|
return a;
|
||||||
};
|
};
|
||||||
|
|
||||||
var var_ = embed_tokens(function(no_in) {
|
var var_ = function(no_in) {
|
||||||
return new AST_Var({
|
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({
|
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;
|
var newexp = expr_atom(false), args;
|
||||||
if (is("punc", "(")) {
|
if (is("punc", "(")) {
|
||||||
next();
|
next();
|
||||||
@@ -1046,10 +1080,12 @@ function parse($TEXT, exigent_mode) {
|
|||||||
args = [];
|
args = [];
|
||||||
}
|
}
|
||||||
return subscripts(new AST_New({
|
return subscripts(new AST_New({
|
||||||
|
start : start,
|
||||||
expression : newexp,
|
expression : newexp,
|
||||||
args : args
|
args : args,
|
||||||
|
end : prev()
|
||||||
}), true);
|
}), true);
|
||||||
});
|
};
|
||||||
|
|
||||||
function as_atom_node() {
|
function as_atom_node() {
|
||||||
var tok = S.token, ret;
|
var tok = S.token, ret;
|
||||||
@@ -1085,25 +1121,26 @@ function parse($TEXT, exigent_mode) {
|
|||||||
|
|
||||||
var expr_atom = function(allow_calls) {
|
var expr_atom = function(allow_calls) {
|
||||||
if (is("operator", "new")) {
|
if (is("operator", "new")) {
|
||||||
next();
|
|
||||||
return new_();
|
return new_();
|
||||||
}
|
}
|
||||||
|
var start = S.token;
|
||||||
if (is("punc")) {
|
if (is("punc")) {
|
||||||
switch (S.token.value) {
|
switch (start.value) {
|
||||||
case "(":
|
case "(":
|
||||||
next();
|
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 "[":
|
case "[":
|
||||||
next();
|
|
||||||
return subscripts(array_(), allow_calls);
|
return subscripts(array_(), allow_calls);
|
||||||
case "{":
|
case "{":
|
||||||
next();
|
|
||||||
return subscripts(object_(), allow_calls);
|
return subscripts(object_(), allow_calls);
|
||||||
}
|
}
|
||||||
unexpected();
|
unexpected();
|
||||||
}
|
}
|
||||||
if (is("keyword", "function")) {
|
if (is("keyword", "function")) {
|
||||||
var start = S.token;
|
|
||||||
next();
|
next();
|
||||||
var func = function_(false);
|
var func = function_(false);
|
||||||
func.start = start;
|
func.start = start;
|
||||||
@@ -1131,13 +1168,15 @@ function parse($TEXT, exigent_mode) {
|
|||||||
return a;
|
return a;
|
||||||
};
|
};
|
||||||
|
|
||||||
function array_() {
|
var array_ = embed_tokens(function() {
|
||||||
|
expect("[");
|
||||||
return new AST_Array({
|
return new AST_Array({
|
||||||
elements: expr_list("]", !exigent_mode, true)
|
elements: expr_list("]", !exigent_mode, true)
|
||||||
});
|
});
|
||||||
};
|
});
|
||||||
|
|
||||||
var object_ = embed_tokens(function() {
|
var object_ = embed_tokens(function() {
|
||||||
|
expect("{");
|
||||||
var first = true, a = [];
|
var first = true, a = [];
|
||||||
while (!is("punc", "}")) {
|
while (!is("punc", "}")) {
|
||||||
if (first) first = false; else expect(",");
|
if (first) first = false; else expect(",");
|
||||||
@@ -1183,9 +1222,14 @@ function parse($TEXT, exigent_mode) {
|
|||||||
switch (S.token.type) {
|
switch (S.token.type) {
|
||||||
case "num":
|
case "num":
|
||||||
case "string":
|
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() {
|
function as_name() {
|
||||||
@@ -1194,15 +1238,16 @@ function parse($TEXT, exigent_mode) {
|
|||||||
case "operator":
|
case "operator":
|
||||||
case "keyword":
|
case "keyword":
|
||||||
case "atom":
|
case "atom":
|
||||||
return as_symbol(true);
|
return prog1(S.token.value, next);
|
||||||
default:
|
default:
|
||||||
unexpected();
|
unexpected();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function as_symbol(noerror) {
|
function as_symbol(def) {
|
||||||
if (!noerror && !is("name")) croak("Name expected");
|
if (!is("name")) croak("Name expected");
|
||||||
var sym = new AST_Symbol({
|
var name = S.token.value;
|
||||||
|
var sym = new (name == "this" ? AST_This : def ? AST_Symbol : AST_SymbolRef)({
|
||||||
name : String(S.token.value),
|
name : String(S.token.value),
|
||||||
start : S.token,
|
start : S.token,
|
||||||
end : S.token
|
end : S.token
|
||||||
@@ -1211,44 +1256,59 @@ function parse($TEXT, exigent_mode) {
|
|||||||
return sym;
|
return sym;
|
||||||
};
|
};
|
||||||
|
|
||||||
var subscripts = embed_tokens(function(expr, allow_calls) {
|
var subscripts = function(expr, allow_calls) {
|
||||||
|
var start = expr.start;
|
||||||
if (is("punc", ".")) {
|
if (is("punc", ".")) {
|
||||||
next();
|
next();
|
||||||
return subscripts(new AST_Dot({
|
return subscripts(new AST_Dot({
|
||||||
|
start : start,
|
||||||
expression : expr,
|
expression : expr,
|
||||||
property : as_name()
|
property : as_name(),
|
||||||
|
end : prev()
|
||||||
}), allow_calls);
|
}), allow_calls);
|
||||||
}
|
}
|
||||||
if (is("punc", "[")) {
|
if (is("punc", "[")) {
|
||||||
next();
|
next();
|
||||||
|
var prop = expression();
|
||||||
|
expect("]");
|
||||||
return subscripts(new AST_Sub({
|
return subscripts(new AST_Sub({
|
||||||
|
start : start,
|
||||||
expression : expr,
|
expression : expr,
|
||||||
property : prog1(expression, curry(expect, "]"))
|
property : prop,
|
||||||
|
end : prev()
|
||||||
}), allow_calls);
|
}), allow_calls);
|
||||||
}
|
}
|
||||||
if (allow_calls && is("punc", "(")) {
|
if (allow_calls && is("punc", "(")) {
|
||||||
next();
|
next();
|
||||||
return subscripts(new AST_Call({
|
return subscripts(new AST_Call({
|
||||||
|
start : start,
|
||||||
expression : expr,
|
expression : expr,
|
||||||
args : expr_list(")")
|
args : expr_list(")"),
|
||||||
|
end : prev()
|
||||||
}), true);
|
}), true);
|
||||||
}
|
}
|
||||||
return expr;
|
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)) {
|
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),
|
prog1(S.token.value, next),
|
||||||
maybe_unary(allow_calls));
|
maybe_unary(allow_calls));
|
||||||
|
ex.start = start;
|
||||||
|
ex.end = prev();
|
||||||
|
return ex;
|
||||||
}
|
}
|
||||||
var val = expr_atom(allow_calls);
|
var val = expr_atom(allow_calls);
|
||||||
while (is("operator") && HOP(UNARY_POSTFIX, S.token.value) && !S.token.nlb) {
|
while (is("operator") && HOP(UNARY_POSTFIX, S.token.value) && !S.token.nlb) {
|
||||||
val = make_unary(AST_UnaryPostfix, S.token.value, val);
|
val = make_unary(AST_UnaryPostfix, S.token.value, val);
|
||||||
|
val.start = start;
|
||||||
|
val.end = S.token;
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
return val;
|
return val;
|
||||||
});
|
};
|
||||||
|
|
||||||
function make_unary(ctor, op, expr) {
|
function make_unary(ctor, op, expr) {
|
||||||
if ((op == "++" || op == "--") && !is_assignable(expr))
|
if ((op == "++" || op == "--") && !is_assignable(expr))
|
||||||
@@ -1256,7 +1316,7 @@ function parse($TEXT, exigent_mode) {
|
|||||||
return new ctor({ operator: op, expression: expr });
|
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;
|
var op = is("operator") ? S.token.value : null;
|
||||||
if (op == "in" && no_in) op = null;
|
if (op == "in" && no_in) op = null;
|
||||||
var prec = op != null ? PRECEDENCE[op] : null;
|
var prec = op != null ? PRECEDENCE[op] : null;
|
||||||
@@ -1264,32 +1324,37 @@ function parse($TEXT, exigent_mode) {
|
|||||||
next();
|
next();
|
||||||
var right = expr_op(maybe_unary(true), prec, no_in);
|
var right = expr_op(maybe_unary(true), prec, no_in);
|
||||||
return expr_op(new AST_Binary({
|
return expr_op(new AST_Binary({
|
||||||
|
start : left.start,
|
||||||
left : left,
|
left : left,
|
||||||
operator : op,
|
operator : op,
|
||||||
right : right
|
right : right,
|
||||||
|
end : right.end
|
||||||
}), min_prec, no_in);
|
}), min_prec, no_in);
|
||||||
}
|
}
|
||||||
return left;
|
return left;
|
||||||
});
|
};
|
||||||
|
|
||||||
function expr_ops(no_in) {
|
function expr_ops(no_in) {
|
||||||
return expr_op(maybe_unary(true), 0, 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);
|
var expr = expr_ops(no_in);
|
||||||
if (is("operator", "?")) {
|
if (is("operator", "?")) {
|
||||||
next();
|
next();
|
||||||
var yes = expression(false);
|
var yes = expression(false);
|
||||||
expect(":");
|
expect(":");
|
||||||
return new AST_Conditional({
|
return new AST_Conditional({
|
||||||
condition: expr,
|
start : start,
|
||||||
consequent: yes,
|
condition : expr,
|
||||||
alternative: expression(false, no_in)
|
consequent : yes,
|
||||||
|
alternative : expression(false, no_in),
|
||||||
|
end : peek()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return expr;
|
return expr;
|
||||||
});
|
};
|
||||||
|
|
||||||
function is_assignable(expr) {
|
function is_assignable(expr) {
|
||||||
if (!exigent_mode) return true;
|
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;
|
var left = maybe_conditional(no_in), val = S.token.value;
|
||||||
if (is("operator") && HOP(ASSIGNMENT, val)) {
|
if (is("operator") && HOP(ASSIGNMENT, val)) {
|
||||||
if (is_assignable(left)) {
|
if (is_assignable(left)) {
|
||||||
next();
|
next();
|
||||||
return new AST_Assign({
|
return new AST_Assign({
|
||||||
|
start : start,
|
||||||
left : left,
|
left : left,
|
||||||
operator : ASSIGNMENT[val],
|
operator : ASSIGNMENT[val],
|
||||||
right : maybe_assign(no_in)
|
right : maybe_assign(no_in),
|
||||||
|
end : peek()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
croak("Invalid assignment");
|
croak("Invalid assignment");
|
||||||
}
|
}
|
||||||
return left;
|
return left;
|
||||||
});
|
};
|
||||||
|
|
||||||
var expression = embed_tokens(function(commas, no_in) {
|
var expression = function(commas, no_in) {
|
||||||
if (arguments.length == 0)
|
if (arguments.length == 0)
|
||||||
commas = true;
|
commas = true;
|
||||||
|
var start = S.token;
|
||||||
var expr = maybe_assign(no_in);
|
var expr = maybe_assign(no_in);
|
||||||
if (commas && is("punc", ",")) {
|
if (commas && is("punc", ",")) {
|
||||||
next();
|
next();
|
||||||
return new AST_Seq({
|
return new AST_Seq({
|
||||||
|
start : start,
|
||||||
first : expr,
|
first : expr,
|
||||||
second : expression(true, no_in)
|
second : expression(true, no_in),
|
||||||
|
end : peek()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return expr;
|
return expr;
|
||||||
});
|
};
|
||||||
|
|
||||||
function in_loop(cont) {
|
function in_loop(cont) {
|
||||||
++S.in_loop;
|
++S.in_loop;
|
||||||
@@ -1342,11 +1413,13 @@ function parse($TEXT, exigent_mode) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return new AST_Toplevel({
|
return new AST_Toplevel({
|
||||||
|
start: S.token,
|
||||||
body: (function(a){
|
body: (function(a){
|
||||||
while (!is("eof"))
|
while (!is("eof"))
|
||||||
a.push(statement());
|
a.push(statement());
|
||||||
return a;
|
return a;
|
||||||
})([])
|
})([]),
|
||||||
|
end: prev()
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
992
lib/test.js
992
lib/test.js
File diff suppressed because it is too large
Load Diff
26
lib/utils.js
26
lib/utils.js
@@ -33,6 +33,32 @@ function member(name, array) {
|
|||||||
return false;
|
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) {
|
function HOP(obj, prop) {
|
||||||
return Object.prototype.hasOwnProperty.call(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;
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user