1 | import lex from 'coffee-lex';
|
2 | import { patchCoffeeScript } from './ext/coffee-script';
|
3 | import mapProgram from './mappers/mapProgram';
|
4 | import parseCS1AsCS2 from './parseCS1AsCS2';
|
5 | import fixLocations from './util/fixLocations';
|
6 | import ParseContext from './util/ParseContext';
|
7 | export function parse(source) {
|
8 | patchCoffeeScript();
|
9 | var context = ParseContext.fromSource(source, lex, parseCS1AsCS2);
|
10 | fixLocations(context, context.ast);
|
11 | var program = mapProgram(context);
|
12 | traverse(program, function (node, parent) {
|
13 | node.parentNode = parent;
|
14 | });
|
15 | return program;
|
16 | }
|
17 | export function traverse(node, callback) {
|
18 | function traverseRec(currentNode, currentParent) {
|
19 | var shouldDescend = callback(currentNode, currentParent);
|
20 | if (shouldDescend !== false) {
|
21 | for (var _i = 0, _a = currentNode.getChildren(); _i < _a.length; _i++) {
|
22 | var child = _a[_i];
|
23 | traverseRec(child, currentNode);
|
24 | }
|
25 | }
|
26 | }
|
27 | traverseRec(node, null);
|
28 | }
|