OLD | NEW |
1 // Output an JsonML codec for the AST | 1 // Output an JsonML codec for the AST |
2 | 2 |
3 // Explanation of a node: | 3 // Explanation of a node: |
4 // { | 4 // { |
5 // type: The type of the node | 5 // type: The type of the node |
6 // location: "line:col-line:col" | 6 // location: "line:col-line:col" |
7 // } | 7 // } |
8 /** | 8 /** |
9 * Node formats: | 9 * Node formats: |
10 * Program | 10 * Program |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 function walkAST(ast, visitor) { | 100 function walkAST(ast, visitor) { |
101 function astVisitor(node) { | 101 function astVisitor(node) { |
102 let info = structure[node.type]; | 102 let info = structure[node.type]; |
103 if (!info) | 103 if (!info) |
104 throw "Need to define " + node.type; | 104 throw "Need to define " + node.type; |
105 let cback = "visit" + node.type; | 105 let cback = "visit" + node.type; |
106 let deep = false; | 106 let deep = false; |
107 if (cback in visitor) | 107 if (cback in visitor) |
108 deep = visitor[cback](node); | 108 deep = visitor[cback](node); |
109 if (!deep) { | 109 if (!deep) { |
110 for each (let part in info) { | 110 for (let part of info) { |
111 let piece = node[part]; | 111 let piece = node[part]; |
112 if (piece instanceof Array) { | 112 if (piece instanceof Array) { |
113 [astVisitor(x) for each (x in piece)]; | 113 piece.map(astVisitor); |
114 } else if (piece) { | 114 } else if (piece) { |
115 astVisitor(piece); | 115 astVisitor(piece); |
116 } | 116 } |
117 } | 117 } |
118 } | 118 } |
119 cback = "post" + cback; | 119 cback = "post" + cback; |
120 if (cback in visitor) | 120 if (cback in visitor) |
121 visitor[cback](node); | 121 visitor[cback](node); |
122 } | 122 } |
123 astVisitor(ast); | 123 astVisitor(ast); |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 let ast = shellNode(pn, "MemberExpression"); | 336 let ast = shellNode(pn, "MemberExpression"); |
337 ast.precedence = 1; | 337 ast.precedence = 1; |
338 ast.container = parseToAst(pn.kids[0]); | 338 ast.container = parseToAst(pn.kids[0]); |
339 ast.member = parseToAst(pn.kids[1]); | 339 ast.member = parseToAst(pn.kids[1]); |
340 return ast; | 340 return ast; |
341 } | 341 } |
342 | 342 |
343 function convertTOK_RB(pn) { | 343 function convertTOK_RB(pn) { |
344 let ast = shellNode(pn, "ArrayLiteral"); | 344 let ast = shellNode(pn, "ArrayLiteral"); |
345 ast.precedence = 0; | 345 ast.precedence = 0; |
346 ast.members = [parseToAst(x) for each (x in pn.kids)]; | 346 ast.members = pn.kids.map(parseToAst); |
347 return ast; | 347 return ast; |
348 } | 348 } |
349 | 349 |
350 /* Returns a list */ | 350 /* Returns a list */ |
351 function convertTOK_LC(pn) { | 351 function convertTOK_LC(pn) { |
352 let ast = shellNode(pn, "BlockStatement"); | 352 let ast = shellNode(pn, "BlockStatement"); |
353 ast.statements = [parseToAst(x) for each (x in pn.kids)]; | 353 ast.statements = pn.kinds.map(parseToAst); |
354 if (ast.statements.length == 0) { | 354 if (ast.statements.length == 0) { |
355 return shellNode(pn, "EmptyStatement"); | 355 return shellNode(pn, "EmptyStatement"); |
356 } | 356 } |
357 return ast; | 357 return ast; |
358 } | 358 } |
359 | 359 |
360 function convertTOK_RC(pn) { | 360 function convertTOK_RC(pn) { |
361 let ast = shellNode(pn, "ObjectLiteral"); | 361 let ast = shellNode(pn, "ObjectLiteral"); |
362 ast.setters = [parseToAst(x) for each (x in pn.kids)]; | 362 ast.setters = pn.kids.map(parseToAst); |
363 return ast; | 363 return ast; |
364 } | 364 } |
365 | 365 |
366 function convertTOK_LP(pn) { | 366 function convertTOK_LP(pn) { |
367 if (pn.op != JSOP_CALL && pn.op != JSOP_APPLY) { | 367 if (pn.op != JSOP_CALL && pn.op != JSOP_APPLY) { |
368 let ast = shellNode(pn, "LetStatement"); | 368 let ast = shellNode(pn, "LetStatement"); |
369 ast.variables = [parseToAst(x) for each (x in pn.kids)]; | 369 ast.variables = pn.kids.map(parseToAst); |
370 return ast; | 370 return ast; |
371 } | 371 } |
372 let ast = shellNode(pn, "CallExpression"); | 372 let ast = shellNode(pn, "CallExpression"); |
373 ast.precedence = 2; | 373 ast.precedence = 2; |
374 ast.func = parseToAst(pn.kids[0]); | 374 ast.func = parseToAst(pn.kids[0]); |
375 ast.arguments = []; | 375 ast.arguments = []; |
376 for (let i = 1; i < pn.kids.length; i++) | 376 for (let i = 1; i < pn.kids.length; i++) |
377 ast.arguments[i - 1] = parseToAst(pn.kids[i]); | 377 ast.arguments[i - 1] = parseToAst(pn.kids[i]); |
378 return ast; | 378 return ast; |
379 } | 379 } |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 } | 536 } |
537 | 537 |
538 function convertTOK_IN(pn) { return binaryNode(pn, "in", 8); } | 538 function convertTOK_IN(pn) { return binaryNode(pn, "in", 8); } |
539 | 539 |
540 function convertTOK_VAR(pn) { | 540 function convertTOK_VAR(pn) { |
541 let ast = shellNode(pn, "VarStatement"); | 541 let ast = shellNode(pn, "VarStatement"); |
542 if (pn.op == JSOP_DEFCONST) | 542 if (pn.op == JSOP_DEFCONST) |
543 ast.vartype = "const"; | 543 ast.vartype = "const"; |
544 else | 544 else |
545 ast.vartype = "var"; | 545 ast.vartype = "var"; |
546 ast.variables = [parseToAst(x) for each (x in pn.kids)]; | 546 ast.variables = pn.kids.map(parseToAst); |
547 for each (let x in ast.variables) { | 547 for (let x of ast.variables) { |
548 if (x.type == "LetStatement") | 548 if (x.type == "LetStatement") |
549 return x; | 549 return x; |
550 if (x.type == "IdentifierExpression") | 550 if (x.type == "IdentifierExpression") |
551 x.type = "VarDeclaration"; | 551 x.type = "VarDeclaration"; |
552 } | 552 } |
553 return ast; | 553 return ast; |
554 } | 554 } |
555 | 555 |
556 function convertTOK_WITH(pn) { | 556 function convertTOK_WITH(pn) { |
557 let ast = shellNode(pn, "WithStatement"); | 557 let ast = shellNode(pn, "WithStatement"); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 ast.init = pn.kids[0] ? parseToAst(pn.kids[0]) : | 672 ast.init = pn.kids[0] ? parseToAst(pn.kids[0]) : |
673 shellNode(pn, "EmptyStatement"); | 673 shellNode(pn, "EmptyStatement"); |
674 ast.condition = pn.kids[1] ? parseToAst(pn.kids[1]) : | 674 ast.condition = pn.kids[1] ? parseToAst(pn.kids[1]) : |
675 shellNode(pn, "EmptyStatement"); | 675 shellNode(pn, "EmptyStatement"); |
676 ast.increment = pn.kids[2] ? parseToAst(pn.kids[2]) : | 676 ast.increment = pn.kids[2] ? parseToAst(pn.kids[2]) : |
677 shellNode(pn, "EmptyStatement"); | 677 shellNode(pn, "EmptyStatement"); |
678 return ast; | 678 return ast; |
679 } | 679 } |
680 | 680 |
681 function convertTOK_RESERVED(pn) { | 681 function convertTOK_RESERVED(pn) { |
682 return [parseToAst(x) for each (x in pn.kids)]; | 682 return pn.kids.map(parseToAst); |
683 } | 683 } |
OLD | NEW |