debug_python_ast2.js 675 B

1234567891011121314151617181920212223242526
  1. const { getParser, initGrammars, loadAllGrammars } = require('./dist/extraction/grammars');
  2. (async () => {
  3. await initGrammars();
  4. await loadAllGrammars();
  5. const parser = getParser('python');
  6. const code = `class Child(Parent, Mixin, Base):
  7. pass`;
  8. const tree = parser.parse(code);
  9. function walk(node, depth = 0) {
  10. const indent = ' '.repeat(depth);
  11. const preview = node.text.substring(0, 40).replace(/\n/g, '\\n');
  12. console.log(`${indent}${node.type} "${preview}"`);
  13. for (let i = 0; i < node.namedChildCount; i++) {
  14. const child = node.namedChild(i);
  15. if (child) walk(child, depth + 1);
  16. }
  17. }
  18. walk(tree.rootNode);
  19. })();