| | |
| | | |
| | | ***********************************************************************/ |
| | | |
| | | import * as ast from "./ast.js"; |
| | | import { make_node } from "./utils/index.js"; |
| | | import { |
| | | AST_Accessor, |
| | |
| | | AST_With, |
| | | AST_Yield, |
| | | } from "./ast.js"; |
| | | import { is_basic_identifier_string } from "./parse.js"; |
| | | |
| | | (function() { |
| | | |
| | |
| | | return body; |
| | | }; |
| | | |
| | | const assert_clause_from_moz = (assertions) => { |
| | | if (assertions && assertions.length > 0) { |
| | | return new AST_Object({ |
| | | start: my_start_token(assertions), |
| | | end: my_end_token(assertions), |
| | | properties: assertions.map((assertion_kv) => |
| | | new AST_ObjectKeyVal({ |
| | | start: my_start_token(assertion_kv), |
| | | end: my_end_token(assertion_kv), |
| | | key: assertion_kv.key.name || assertion_kv.key.value, |
| | | value: from_moz(assertion_kv.value) |
| | | }) |
| | | ) |
| | | }); |
| | | } |
| | | return null; |
| | | }; |
| | | |
| | | var MOZ_TO_ME = { |
| | | Program: function(M) { |
| | | return new AST_Toplevel({ |
| | |
| | | body: normalize_directives(M.body.map(from_moz)) |
| | | }); |
| | | }, |
| | | |
| | | ArrayPattern: function(M) { |
| | | return new AST_Destructuring({ |
| | | start: my_start_token(M), |
| | |
| | | is_array: true |
| | | }); |
| | | }, |
| | | |
| | | ObjectPattern: function(M) { |
| | | return new AST_Destructuring({ |
| | | start: my_start_token(M), |
| | |
| | | is_array: false |
| | | }); |
| | | }, |
| | | |
| | | AssignmentPattern: function(M) { |
| | | return new AST_DefaultAssign({ |
| | | start: my_start_token(M), |
| | |
| | | right: from_moz(M.right) |
| | | }); |
| | | }, |
| | | |
| | | SpreadElement: function(M) { |
| | | return new AST_Expansion({ |
| | | start: my_start_token(M), |
| | |
| | | expression: from_moz(M.argument) |
| | | }); |
| | | }, |
| | | |
| | | RestElement: function(M) { |
| | | return new AST_Expansion({ |
| | | start: my_start_token(M), |
| | |
| | | expression: from_moz(M.argument) |
| | | }); |
| | | }, |
| | | |
| | | TemplateElement: function(M) { |
| | | return new AST_TemplateSegment({ |
| | | start: my_start_token(M), |
| | |
| | | raw: M.value.raw |
| | | }); |
| | | }, |
| | | |
| | | TemplateLiteral: function(M) { |
| | | var segments = []; |
| | | for (var i = 0; i < M.quasis.length; i++) { |
| | |
| | | segments: segments |
| | | }); |
| | | }, |
| | | |
| | | TaggedTemplateExpression: function(M) { |
| | | return new AST_PrefixedTemplateString({ |
| | | start: my_start_token(M), |
| | |
| | | prefix: from_moz(M.tag) |
| | | }); |
| | | }, |
| | | |
| | | FunctionDeclaration: function(M) { |
| | | return new AST_Defun({ |
| | | start: my_start_token(M), |
| | |
| | | body: normalize_directives(from_moz(M.body).body) |
| | | }); |
| | | }, |
| | | |
| | | FunctionExpression: function(M) { |
| | | return new AST_Function({ |
| | | start: my_start_token(M), |
| | |
| | | body: normalize_directives(from_moz(M.body).body) |
| | | }); |
| | | }, |
| | | |
| | | ArrowFunctionExpression: function(M) { |
| | | const body = M.body.type === "BlockStatement" |
| | | ? from_moz(M.body).body |
| | |
| | | async: M.async, |
| | | }); |
| | | }, |
| | | |
| | | ExpressionStatement: function(M) { |
| | | return new AST_SimpleStatement({ |
| | | start: my_start_token(M), |
| | |
| | | body: from_moz(M.expression) |
| | | }); |
| | | }, |
| | | |
| | | TryStatement: function(M) { |
| | | var handlers = M.handlers || [M.handler]; |
| | | if (handlers.length > 1 || M.guardedHandlers && M.guardedHandlers.length) { |
| | |
| | | bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null |
| | | }); |
| | | }, |
| | | |
| | | Property: function(M) { |
| | | var key = M.key; |
| | | var args = { |
| | |
| | | return new AST_ConciseMethod(args); |
| | | } |
| | | }, |
| | | |
| | | MethodDefinition: function(M) { |
| | | var args = { |
| | | start : my_start_token(M), |
| | |
| | | args.async = M.value.async; |
| | | return new AST_ConciseMethod(args); |
| | | }, |
| | | |
| | | FieldDefinition: function(M) { |
| | | let key; |
| | | if (M.computed) { |
| | |
| | | static : M.static, |
| | | }); |
| | | }, |
| | | |
| | | PropertyDefinition: function(M) { |
| | | let key; |
| | | if (M.computed) { |
| | |
| | | static : M.static, |
| | | }); |
| | | }, |
| | | |
| | | ArrayExpression: function(M) { |
| | | return new AST_Array({ |
| | | start : my_start_token(M), |
| | |
| | | }) |
| | | }); |
| | | }, |
| | | |
| | | ObjectExpression: function(M) { |
| | | return new AST_Object({ |
| | | start : my_start_token(M), |
| | |
| | | }) |
| | | }); |
| | | }, |
| | | |
| | | SequenceExpression: function(M) { |
| | | return new AST_Sequence({ |
| | | start : my_start_token(M), |
| | |
| | | expressions: M.expressions.map(from_moz) |
| | | }); |
| | | }, |
| | | |
| | | MemberExpression: function(M) { |
| | | return new (M.computed ? AST_Sub : AST_Dot)({ |
| | | start : my_start_token(M), |
| | |
| | | optional : M.optional || false |
| | | }); |
| | | }, |
| | | |
| | | ChainExpression: function(M) { |
| | | return new AST_Chain({ |
| | | start : my_start_token(M), |
| | |
| | | expression : from_moz(M.expression) |
| | | }); |
| | | }, |
| | | |
| | | SwitchCase: function(M) { |
| | | return new (M.test ? AST_Case : AST_Default)({ |
| | | start : my_start_token(M), |
| | |
| | | body : M.consequent.map(from_moz) |
| | | }); |
| | | }, |
| | | |
| | | VariableDeclaration: function(M) { |
| | | return new (M.kind === "const" ? AST_Const : |
| | | M.kind === "let" ? AST_Let : AST_Var)({ |
| | |
| | | end : my_end_token(M), |
| | | imported_name: imported_name, |
| | | imported_names : imported_names, |
| | | module_name : from_moz(M.source) |
| | | module_name : from_moz(M.source), |
| | | assert_clause: assert_clause_from_moz(M.assertions) |
| | | }); |
| | | }, |
| | | |
| | | ExportAllDeclaration: function(M) { |
| | | return new AST_Export({ |
| | | start: my_start_token(M), |
| | |
| | | foreign_name: new AST_SymbolExportForeign({ name: "*" }) |
| | | }) |
| | | ], |
| | | module_name: from_moz(M.source) |
| | | module_name: from_moz(M.source), |
| | | assert_clause: assert_clause_from_moz(M.assertions) |
| | | }); |
| | | }, |
| | | |
| | | ExportNamedDeclaration: function(M) { |
| | | return new AST_Export({ |
| | | start: my_start_token(M), |
| | |
| | | name: from_moz(specifier.local) |
| | | }); |
| | | }) : null, |
| | | module_name: from_moz(M.source) |
| | | module_name: from_moz(M.source), |
| | | assert_clause: assert_clause_from_moz(M.assertions) |
| | | }); |
| | | }, |
| | | |
| | | ExportDefaultDeclaration: function(M) { |
| | | return new AST_Export({ |
| | | start: my_start_token(M), |
| | |
| | | is_default: true |
| | | }); |
| | | }, |
| | | |
| | | Literal: function(M) { |
| | | var val = M.value, args = { |
| | | start : my_start_token(M), |
| | |
| | | return new (val ? AST_True : AST_False)(args); |
| | | } |
| | | }, |
| | | |
| | | MetaProperty: function(M) { |
| | | if (M.meta.name === "new" && M.property.name === "target") { |
| | | return new AST_NewTarget({ |
| | |
| | | }); |
| | | } |
| | | }, |
| | | |
| | | Identifier: function(M) { |
| | | var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2]; |
| | | return new ( p.type == "LabeledStatement" ? AST_Label |
| | |
| | | name : M.name |
| | | }); |
| | | }, |
| | | |
| | | BigIntLiteral(M) { |
| | | return new AST_BigInt({ |
| | | start : my_start_token(M), |
| | | end : my_end_token(M), |
| | | value : M.value |
| | | }); |
| | | }, |
| | | |
| | | EmptyStatement: function(M) { |
| | | return new AST_EmptyStatement({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M) |
| | | }); |
| | | }, |
| | | |
| | | BlockStatement: function(M) { |
| | | return new AST_BlockStatement({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | body: M.body.map(from_moz) |
| | | }); |
| | | }, |
| | | |
| | | IfStatement: function(M) { |
| | | return new AST_If({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | condition: from_moz(M.test), |
| | | body: from_moz(M.consequent), |
| | | alternative: from_moz(M.alternate) |
| | | }); |
| | | }, |
| | | |
| | | LabeledStatement: function(M) { |
| | | return new AST_LabeledStatement({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | label: from_moz(M.label), |
| | | body: from_moz(M.body) |
| | | }); |
| | | }, |
| | | |
| | | BreakStatement: function(M) { |
| | | return new AST_Break({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | label: from_moz(M.label) |
| | | }); |
| | | }, |
| | | |
| | | ContinueStatement: function(M) { |
| | | return new AST_Continue({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | label: from_moz(M.label) |
| | | }); |
| | | }, |
| | | |
| | | WithStatement: function(M) { |
| | | return new AST_With({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | expression: from_moz(M.object), |
| | | body: from_moz(M.body) |
| | | }); |
| | | }, |
| | | |
| | | SwitchStatement: function(M) { |
| | | return new AST_Switch({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | expression: from_moz(M.discriminant), |
| | | body: M.cases.map(from_moz) |
| | | }); |
| | | }, |
| | | |
| | | ReturnStatement: function(M) { |
| | | return new AST_Return({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | value: from_moz(M.argument) |
| | | }); |
| | | }, |
| | | |
| | | ThrowStatement: function(M) { |
| | | return new AST_Throw({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | value: from_moz(M.argument) |
| | | }); |
| | | }, |
| | | |
| | | WhileStatement: function(M) { |
| | | return new AST_While({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | condition: from_moz(M.test), |
| | | body: from_moz(M.body) |
| | | }); |
| | | }, |
| | | |
| | | DoWhileStatement: function(M) { |
| | | return new AST_Do({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | condition: from_moz(M.test), |
| | | body: from_moz(M.body) |
| | | }); |
| | | }, |
| | | |
| | | ForStatement: function(M) { |
| | | return new AST_For({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | init: from_moz(M.init), |
| | | condition: from_moz(M.test), |
| | | step: from_moz(M.update), |
| | | body: from_moz(M.body) |
| | | }); |
| | | }, |
| | | |
| | | ForInStatement: function(M) { |
| | | return new AST_ForIn({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | init: from_moz(M.left), |
| | | object: from_moz(M.right), |
| | | body: from_moz(M.body) |
| | | }); |
| | | }, |
| | | |
| | | ForOfStatement: function(M) { |
| | | return new AST_ForOf({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | init: from_moz(M.left), |
| | | object: from_moz(M.right), |
| | | body: from_moz(M.body), |
| | | await: M.await |
| | | }); |
| | | }, |
| | | |
| | | AwaitExpression: function(M) { |
| | | return new AST_Await({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | expression: from_moz(M.argument) |
| | | }); |
| | | }, |
| | | |
| | | YieldExpression: function(M) { |
| | | return new AST_Yield({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | expression: from_moz(M.argument), |
| | | is_star: M.delegate |
| | | }); |
| | | }, |
| | | |
| | | DebuggerStatement: function(M) { |
| | | return new AST_Debugger({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M) |
| | | }); |
| | | }, |
| | | |
| | | VariableDeclarator: function(M) { |
| | | return new AST_VarDef({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | name: from_moz(M.id), |
| | | value: from_moz(M.init) |
| | | }); |
| | | }, |
| | | |
| | | CatchClause: function(M) { |
| | | return new AST_Catch({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | argname: from_moz(M.param), |
| | | body: from_moz(M.body).body |
| | | }); |
| | | }, |
| | | |
| | | ThisExpression: function(M) { |
| | | return new AST_This({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M) |
| | | }); |
| | | }, |
| | | |
| | | Super: function(M) { |
| | | return new AST_Super({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M) |
| | | }); |
| | | }, |
| | | |
| | | BinaryExpression: function(M) { |
| | | return new AST_Binary({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | operator: M.operator, |
| | | left: from_moz(M.left), |
| | | right: from_moz(M.right) |
| | | }); |
| | | }, |
| | | |
| | | LogicalExpression: function(M) { |
| | | return new AST_Binary({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | operator: M.operator, |
| | | left: from_moz(M.left), |
| | | right: from_moz(M.right) |
| | | }); |
| | | }, |
| | | |
| | | AssignmentExpression: function(M) { |
| | | return new AST_Assign({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | operator: M.operator, |
| | | left: from_moz(M.left), |
| | | right: from_moz(M.right) |
| | | }); |
| | | }, |
| | | |
| | | ConditionalExpression: function(M) { |
| | | return new AST_Conditional({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | condition: from_moz(M.test), |
| | | consequent: from_moz(M.consequent), |
| | | alternative: from_moz(M.alternate) |
| | | }); |
| | | }, |
| | | |
| | | NewExpression: function(M) { |
| | | return new AST_New({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | expression: from_moz(M.callee), |
| | | args: M.arguments.map(from_moz) |
| | | }); |
| | | }, |
| | | |
| | | CallExpression: function(M) { |
| | | return new AST_Call({ |
| | | start: my_start_token(M), |
| | | end: my_end_token(M), |
| | | expression: from_moz(M.callee), |
| | | optional: M.optional, |
| | | args: M.arguments.map(from_moz) |
| | | }); |
| | | } |
| | | }; |
| | |
| | | }); |
| | | }; |
| | | |
| | | map("EmptyStatement", AST_EmptyStatement); |
| | | map("BlockStatement", AST_BlockStatement, "body@body"); |
| | | map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative"); |
| | | map("LabeledStatement", AST_LabeledStatement, "label>label, body>body"); |
| | | map("BreakStatement", AST_Break, "label>label"); |
| | | map("ContinueStatement", AST_Continue, "label>label"); |
| | | map("WithStatement", AST_With, "object>expression, body>body"); |
| | | map("SwitchStatement", AST_Switch, "discriminant>expression, cases@body"); |
| | | map("ReturnStatement", AST_Return, "argument>value"); |
| | | map("ThrowStatement", AST_Throw, "argument>value"); |
| | | map("WhileStatement", AST_While, "test>condition, body>body"); |
| | | map("DoWhileStatement", AST_Do, "test>condition, body>body"); |
| | | map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body"); |
| | | map("ForInStatement", AST_ForIn, "left>init, right>object, body>body"); |
| | | map("ForOfStatement", AST_ForOf, "left>init, right>object, body>body, await=await"); |
| | | map("AwaitExpression", AST_Await, "argument>expression"); |
| | | map("YieldExpression", AST_Yield, "argument>expression, delegate=is_star"); |
| | | map("DebuggerStatement", AST_Debugger); |
| | | map("VariableDeclarator", AST_VarDef, "id>name, init>value"); |
| | | map("CatchClause", AST_Catch, "param>argname, body%body"); |
| | | def_to_moz(AST_EmptyStatement, function To_Moz_EmptyStatement() { |
| | | return { |
| | | type: "EmptyStatement" |
| | | }; |
| | | }); |
| | | def_to_moz(AST_BlockStatement, function To_Moz_BlockStatement(M) { |
| | | return { |
| | | type: "BlockStatement", |
| | | body: M.body.map(to_moz) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_If, function To_Moz_IfStatement(M) { |
| | | return { |
| | | type: "IfStatement", |
| | | test: to_moz(M.condition), |
| | | consequent: to_moz(M.body), |
| | | alternate: to_moz(M.alternative) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_LabeledStatement, function To_Moz_LabeledStatement(M) { |
| | | return { |
| | | type: "LabeledStatement", |
| | | label: to_moz(M.label), |
| | | body: to_moz(M.body) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Break, function To_Moz_BreakStatement(M) { |
| | | return { |
| | | type: "BreakStatement", |
| | | label: to_moz(M.label) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Continue, function To_Moz_ContinueStatement(M) { |
| | | return { |
| | | type: "ContinueStatement", |
| | | label: to_moz(M.label) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_With, function To_Moz_WithStatement(M) { |
| | | return { |
| | | type: "WithStatement", |
| | | object: to_moz(M.expression), |
| | | body: to_moz(M.body) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Switch, function To_Moz_SwitchStatement(M) { |
| | | return { |
| | | type: "SwitchStatement", |
| | | discriminant: to_moz(M.expression), |
| | | cases: M.body.map(to_moz) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Return, function To_Moz_ReturnStatement(M) { |
| | | return { |
| | | type: "ReturnStatement", |
| | | argument: to_moz(M.value) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Throw, function To_Moz_ThrowStatement(M) { |
| | | return { |
| | | type: "ThrowStatement", |
| | | argument: to_moz(M.value) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_While, function To_Moz_WhileStatement(M) { |
| | | return { |
| | | type: "WhileStatement", |
| | | test: to_moz(M.condition), |
| | | body: to_moz(M.body) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Do, function To_Moz_DoWhileStatement(M) { |
| | | return { |
| | | type: "DoWhileStatement", |
| | | test: to_moz(M.condition), |
| | | body: to_moz(M.body) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_For, function To_Moz_ForStatement(M) { |
| | | return { |
| | | type: "ForStatement", |
| | | init: to_moz(M.init), |
| | | test: to_moz(M.condition), |
| | | update: to_moz(M.step), |
| | | body: to_moz(M.body) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_ForIn, function To_Moz_ForInStatement(M) { |
| | | return { |
| | | type: "ForInStatement", |
| | | left: to_moz(M.init), |
| | | right: to_moz(M.object), |
| | | body: to_moz(M.body) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_ForOf, function To_Moz_ForOfStatement(M) { |
| | | return { |
| | | type: "ForOfStatement", |
| | | left: to_moz(M.init), |
| | | right: to_moz(M.object), |
| | | body: to_moz(M.body), |
| | | await: M.await |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Await, function To_Moz_AwaitExpression(M) { |
| | | return { |
| | | type: "AwaitExpression", |
| | | argument: to_moz(M.expression) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Yield, function To_Moz_YieldExpression(M) { |
| | | return { |
| | | type: "YieldExpression", |
| | | argument: to_moz(M.expression), |
| | | delegate: M.is_star |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Debugger, function To_Moz_DebuggerStatement() { |
| | | return { |
| | | type: "DebuggerStatement" |
| | | }; |
| | | }); |
| | | def_to_moz(AST_VarDef, function To_Moz_VariableDeclarator(M) { |
| | | return { |
| | | type: "VariableDeclarator", |
| | | id: to_moz(M.name), |
| | | init: to_moz(M.value) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Catch, function To_Moz_CatchClause(M) { |
| | | return { |
| | | type: "CatchClause", |
| | | param: to_moz(M.argname), |
| | | body: to_moz_block(M) |
| | | }; |
| | | }); |
| | | |
| | | map("ThisExpression", AST_This); |
| | | map("Super", AST_Super); |
| | | map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right"); |
| | | map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right"); |
| | | map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right"); |
| | | map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative"); |
| | | map("NewExpression", AST_New, "callee>expression, arguments@args"); |
| | | map("CallExpression", AST_Call, "callee>expression, optional=optional, arguments@args"); |
| | | def_to_moz(AST_This, function To_Moz_ThisExpression() { |
| | | return { |
| | | type: "ThisExpression" |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Super, function To_Moz_Super() { |
| | | return { |
| | | type: "Super" |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) { |
| | | return { |
| | | type: "BinaryExpression", |
| | | operator: M.operator, |
| | | left: to_moz(M.left), |
| | | right: to_moz(M.right) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Binary, function To_Moz_LogicalExpression(M) { |
| | | return { |
| | | type: "LogicalExpression", |
| | | operator: M.operator, |
| | | left: to_moz(M.left), |
| | | right: to_moz(M.right) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Assign, function To_Moz_AssignmentExpression(M) { |
| | | return { |
| | | type: "AssignmentExpression", |
| | | operator: M.operator, |
| | | left: to_moz(M.left), |
| | | right: to_moz(M.right) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Conditional, function To_Moz_ConditionalExpression(M) { |
| | | return { |
| | | type: "ConditionalExpression", |
| | | test: to_moz(M.condition), |
| | | consequent: to_moz(M.consequent), |
| | | alternate: to_moz(M.alternative) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_New, function To_Moz_NewExpression(M) { |
| | | return { |
| | | type: "NewExpression", |
| | | callee: to_moz(M.expression), |
| | | arguments: M.args.map(to_moz) |
| | | }; |
| | | }); |
| | | def_to_moz(AST_Call, function To_Moz_CallExpression(M) { |
| | | return { |
| | | type: "CallExpression", |
| | | callee: to_moz(M.expression), |
| | | optional: M.optional, |
| | | arguments: M.args.map(to_moz) |
| | | }; |
| | | }); |
| | | |
| | | def_to_moz(AST_Toplevel, function To_Moz_Program(M) { |
| | | return to_moz_scope("Program", M); |
| | |
| | | }; |
| | | }); |
| | | |
| | | const assert_clause_to_moz = assert_clause => { |
| | | const assertions = []; |
| | | if (assert_clause) { |
| | | for (const { key, value } of assert_clause.properties) { |
| | | const key_moz = is_basic_identifier_string(key) |
| | | ? { type: "Identifier", name: key } |
| | | : { type: "Literal", value: key, raw: JSON.stringify(key) }; |
| | | assertions.push({ |
| | | type: "ImportAttribute", |
| | | key: key_moz, |
| | | value: to_moz(value) |
| | | }); |
| | | } |
| | | } |
| | | return assertions; |
| | | }; |
| | | |
| | | def_to_moz(AST_Export, function To_Moz_ExportDeclaration(M) { |
| | | if (M.exported_names) { |
| | | if (M.exported_names[0].name.name === "*") { |
| | | return { |
| | | type: "ExportAllDeclaration", |
| | | source: to_moz(M.module_name) |
| | | source: to_moz(M.module_name), |
| | | assertions: assert_clause_to_moz(M.assert_clause) |
| | | }; |
| | | } |
| | | return { |
| | |
| | | }; |
| | | }), |
| | | declaration: to_moz(M.exported_definition), |
| | | source: to_moz(M.module_name) |
| | | source: to_moz(M.module_name), |
| | | assertions: assert_clause_to_moz(M.assert_clause) |
| | | }; |
| | | } |
| | | return { |
| | |
| | | return { |
| | | type: "ImportDeclaration", |
| | | specifiers: specifiers, |
| | | source: to_moz(M.module_name) |
| | | source: to_moz(M.module_name), |
| | | assertions: assert_clause_to_moz(M.assert_clause) |
| | | }; |
| | | }); |
| | | |
| | |
| | | [], |
| | | loc && loc.source, |
| | | ); |
| | | } |
| | | |
| | | function map(moztype, mytype, propmap) { |
| | | var moz_to_me = "function From_Moz_" + moztype + "(M){\n"; |
| | | moz_to_me += "return new U2." + mytype.name + "({\n" + |
| | | "start: my_start_token(M),\n" + |
| | | "end: my_end_token(M)"; |
| | | |
| | | var me_to_moz = "function To_Moz_" + moztype + "(M){\n"; |
| | | me_to_moz += "return {\n" + |
| | | "type: " + JSON.stringify(moztype); |
| | | |
| | | if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop) { |
| | | var m = /([a-z0-9$_]+)([=@>%])([a-z0-9$_]+)/i.exec(prop); |
| | | if (!m) throw new Error("Can't understand property map: " + prop); |
| | | var moz = m[1], how = m[2], my = m[3]; |
| | | moz_to_me += ",\n" + my + ": "; |
| | | me_to_moz += ",\n" + moz + ": "; |
| | | switch (how) { |
| | | case "@": |
| | | moz_to_me += "M." + moz + ".map(from_moz)"; |
| | | me_to_moz += "M." + my + ".map(to_moz)"; |
| | | break; |
| | | case ">": |
| | | moz_to_me += "from_moz(M." + moz + ")"; |
| | | me_to_moz += "to_moz(M." + my + ")"; |
| | | break; |
| | | case "=": |
| | | moz_to_me += "M." + moz; |
| | | me_to_moz += "M." + my; |
| | | break; |
| | | case "%": |
| | | moz_to_me += "from_moz(M." + moz + ").body"; |
| | | me_to_moz += "to_moz_block(M)"; |
| | | break; |
| | | default: |
| | | throw new Error("Can't understand operator in propmap: " + prop); |
| | | } |
| | | }); |
| | | |
| | | moz_to_me += "\n})\n}"; |
| | | me_to_moz += "\n}\n}"; |
| | | |
| | | moz_to_me = new Function("U2", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")( |
| | | ast, my_start_token, my_end_token, from_moz |
| | | ); |
| | | me_to_moz = new Function("to_moz", "to_moz_block", "to_moz_scope", "return(" + me_to_moz + ")")( |
| | | to_moz, to_moz_block, to_moz_scope |
| | | ); |
| | | MOZ_TO_ME[moztype] = moz_to_me; |
| | | def_to_moz(mytype, me_to_moz); |
| | | } |
| | | |
| | | var FROM_MOZ_STACK = null; |