ast_node_visitor.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. var filename = '__main__.py';
  3. var python_source = 'a, b = 0\nfor x in y:\n t = 0';
  4. parse = Sk.parse(filename, python_source);
  5. ast = Sk.astFromParse(parse.cst, filename, parse.flags);
  6. */
  7. var iter_fields = function(node) {
  8. /** Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
  9. that is present on *node*. **/
  10. var fieldList = [];
  11. for (var i = 0; i < node._fields.length; i += 2) {
  12. var field = node._fields[i];
  13. if (field in node) {
  14. fieldList.push([field, node[field]]);
  15. }
  16. }
  17. return fieldList;
  18. }
  19. var iter_child_nodes = function(node) {
  20. var fieldList = iter_fields(node);
  21. var resultList = [];
  22. for (var i = 0; i < fieldList.length; i += 1) {
  23. var field = fieldList[i][0], value = fieldList[i][1];
  24. if (value === null) {
  25. continue;
  26. }
  27. if ("_astname" in value) {
  28. resultList.push(value);
  29. } else if (value.constructor === Array) {
  30. for (var j = 0; j < value.length; j += 1) {
  31. var subvalue = value[j];
  32. if ("_astname" in subvalue) {
  33. resultList.push(subvalue);
  34. }
  35. }
  36. }
  37. }
  38. return resultList;
  39. }
  40. function NodeVisitor() {};
  41. NodeVisitor.prototype.visit = function(node) {
  42. /** Visit a node. **/
  43. var method_name = 'visit_' + node._astname;
  44. if (method_name in this) {
  45. return this[method_name](node);
  46. } else {
  47. return this.generic_visit(node);
  48. }
  49. }
  50. NodeVisitor.prototype.walk = function(node) {
  51. var resultList = [node];
  52. var childList = iter_child_nodes(node);
  53. for (var i = 0; i < childList.length; i += 1) {
  54. var child = childList[i];
  55. resultList.concat(this.walk(child));
  56. }
  57. return resultList;
  58. }
  59. NodeVisitor.prototype.visitList = function(nodes) {
  60. for (var j = 0; j < nodes.length; j += 1) {
  61. var node = nodes[j];
  62. if ("_astname" in node) {
  63. this.visit(node);
  64. }
  65. }
  66. }
  67. NodeVisitor.prototype.generic_visit = function(node) {
  68. /** Called if no explicit visitor function exists for a node. **/
  69. var fieldList = iter_fields(node);
  70. for (var i = 0; i < fieldList.length; i += 1) {
  71. var field = fieldList[i][0], value = fieldList[i][1];
  72. if (value === null) {
  73. continue;
  74. }
  75. if (Array === value.constructor) {
  76. for (var j = 0; j < value.length; j += 1) {
  77. var subvalue = value[j];
  78. if (subvalue instanceof Object && "_astname" in subvalue) {
  79. this.visit(subvalue);
  80. }
  81. }
  82. } else if (value instanceof Object && "_astname" in value) {
  83. this.visit(value);
  84. }
  85. }
  86. }
  87. NodeVisitor.prototype.recursive_walk = function(node) {
  88. var todo = [node];
  89. var result = [];
  90. while (todo.length > 0) {
  91. node = todo.shift();
  92. todo = todo.concat(iter_child_nodes(node))
  93. result.push(node);
  94. }
  95. return result;
  96. }
  97. /*
  98. function CodeAnalyzer() {
  99. NodeVisitor.apply(this, Array.prototype.slice.call(arguments));
  100. this.id = 0;
  101. };
  102. CodeAnalyzer.prototype = new NodeVisitor();
  103. CodeAnalyzer.prototype.visit = function(node) {
  104. node._id = this.id;
  105. this.id += 1;
  106. NodeVisitor.prototype.visit.call(this, node);
  107. //console.log(node);
  108. }
  109. */
  110. /*
  111. CodeAnalyzer.prototype.visit_Num = function(node) {
  112. node._id = this.id;
  113. this.id += 1;
  114. console.log(node.n.v);
  115. // NodeVisitor.prototype.visit_Num.call(this, node);
  116. };*/
  117. //console.log((new NodeVisitor()).visit(ast));