minor clean-up (#4998)

This commit is contained in:
Alex Lam S.L
2021-06-12 09:20:06 +08:00
committed by GitHub
parent ce75477670
commit 70ceda5398

View File

@@ -1134,58 +1134,54 @@
} }
function map(moztype, mytype, propmap) { function map(moztype, mytype, propmap) {
var moz_to_me = "function From_Moz_" + moztype + "(M){\n"; var moz_to_me = [
moz_to_me += "return new U2." + mytype.name + "({\n" + "start: my_start_token(M)",
"start: my_start_token(M),\n" + "end: my_end_token(M)",
"end: my_end_token(M)"; ];
var me_to_moz = [
var me_to_moz = "function To_Moz_" + moztype + "(M){\n"; "type: " + JSON.stringify(moztype),
me_to_moz += "return {\n" + ];
"type: " + JSON.stringify(moztype);
if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop) { if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop) {
var m = /([a-z0-9$_]+)(=|@|>|%)([a-z0-9$_]+)/i.exec(prop); var m = /([a-z0-9$_]+)(=|@|>|%)([a-z0-9$_]+)/i.exec(prop);
if (!m) throw new Error("Can't understand property map: " + prop); if (!m) throw new Error("Can't understand property map: " + prop);
var moz = m[1], how = m[2], my = m[3]; var moz = m[1], how = m[2], my = m[3];
moz_to_me += ",\n" + my + ": ";
me_to_moz += ",\n" + moz + ": ";
switch (how) { switch (how) {
case "@": case "@":
moz_to_me += "M." + moz + ".map(from_moz)"; moz_to_me.push(my + ": M." + moz + ".map(from_moz)");
me_to_moz += "M." + my + ".map(to_moz)"; me_to_moz.push(moz + ": M." + my + ".map(to_moz)");
break; break;
case ">": case ">":
moz_to_me += "from_moz(M." + moz + ")"; moz_to_me.push(my + ": from_moz(M." + moz + ")");
me_to_moz += "to_moz(M." + my + ")"; me_to_moz.push(moz + ": to_moz(M." + my + ")");
break; break;
case "=": case "=":
moz_to_me += "M." + moz; moz_to_me.push(my + ": M." + moz);
me_to_moz += "M." + my; me_to_moz.push(moz + ": M." + my);
break; break;
case "%": case "%":
moz_to_me += "from_moz(M." + moz + ").body"; moz_to_me.push(my + ": from_moz(M." + moz + ").body");
me_to_moz += "to_moz_block(M)"; me_to_moz.push(moz + ": to_moz_block(M)");
break; break;
default: default:
throw new Error("Can't understand operator in propmap: " + prop); throw new Error("Can't understand operator in propmap: " + prop);
} }
}); });
moz_to_me += "\n})\n}"; MOZ_TO_ME[moztype] = new Function("U2", "my_start_token", "my_end_token", "from_moz", [
me_to_moz += "\n}\n}"; "return function From_Moz_" + moztype + "(M) {",
" return new U2.AST_" + mytype.TYPE + "({",
//moz_to_me = parse(moz_to_me).print_to_string({ beautify: true }); moz_to_me.join(",\n"),
//me_to_moz = parse(me_to_moz).print_to_string({ beautify: true }); " });",
//console.log(moz_to_me); "};",
].join("\n"))(exports, my_start_token, my_end_token, from_moz);
moz_to_me = new Function("U2", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")( def_to_moz(mytype, new Function("to_moz", "to_moz_block", "to_moz_scope", [
exports, my_start_token, my_end_token, from_moz "return function To_Moz_" + moztype + "(M) {",
); " return {",
me_to_moz = new Function("to_moz", "to_moz_block", "to_moz_scope", "return(" + me_to_moz + ")")( me_to_moz.join(",\n"),
to_moz, to_moz_block, to_moz_scope " };",
); "};",
MOZ_TO_ME[moztype] = moz_to_me; ].join("\n"))(to_moz, to_moz_block, to_moz_scope));
def_to_moz(mytype, me_to_moz);
} }
var FROM_MOZ_STACK = null; var FROM_MOZ_STACK = null;