ast_node_visitor.js.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: ast_node_visitor.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: ast_node_visitor.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/*
  20. var filename = '__main__.py';
  21. var python_source = 'a, b = 0\nfor x in y:\n t = 0';
  22. parse = Sk.parse(filename, python_source);
  23. ast = Sk.astFromParse(parse.cst, filename, parse.flags);
  24. */
  25. var iter_fields = function(node) {
  26. /** Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
  27. that is present on *node*. **/
  28. var fieldList = [];
  29. for (var i = 0; i &lt; node._fields.length; i += 2) {
  30. var field = node._fields[i];
  31. if (field in node) {
  32. fieldList.push([field, node[field]]);
  33. }
  34. }
  35. return fieldList;
  36. }
  37. var iter_child_nodes = function(node) {
  38. var fieldList = iter_fields(node);
  39. var resultList = [];
  40. for (var i = 0; i &lt; fieldList.length; i += 1) {
  41. var field = fieldList[i][0], value = fieldList[i][1];
  42. if (value === null) {
  43. continue;
  44. }
  45. if ("_astname" in value) {
  46. resultList.push(value);
  47. } else if (value.constructor === Array) {
  48. for (var j = 0; j &lt; value.length; j += 1) {
  49. var subvalue = value[j];
  50. if ("_astname" in subvalue) {
  51. resultList.push(subvalue);
  52. }
  53. }
  54. }
  55. }
  56. return resultList;
  57. }
  58. function NodeVisitor() {};
  59. NodeVisitor.prototype.visit = function(node) {
  60. /** Visit a node. **/
  61. var method_name = 'visit_' + node._astname;
  62. if (method_name in this) {
  63. return this[method_name](node);
  64. } else {
  65. return this.generic_visit(node);
  66. }
  67. }
  68. NodeVisitor.prototype.walk = function(node) {
  69. var resultList = [node];
  70. var childList = iter_child_nodes(node);
  71. for (var i = 0; i &lt; childList.length; i += 1) {
  72. var child = childList[i];
  73. resultList.concat(this.walk(child));
  74. }
  75. return resultList;
  76. }
  77. NodeVisitor.prototype.visitList = function(nodes) {
  78. for (var j = 0; j &lt; nodes.length; j += 1) {
  79. var node = nodes[j];
  80. if ("_astname" in node) {
  81. this.visit(node);
  82. }
  83. }
  84. }
  85. NodeVisitor.prototype.generic_visit = function(node) {
  86. /** Called if no explicit visitor function exists for a node. **/
  87. var fieldList = iter_fields(node);
  88. for (var i = 0; i &lt; fieldList.length; i += 1) {
  89. var field = fieldList[i][0], value = fieldList[i][1];
  90. if (value === null) {
  91. continue;
  92. }
  93. if (Array === value.constructor) {
  94. for (var j = 0; j &lt; value.length; j += 1) {
  95. var subvalue = value[j];
  96. if (subvalue instanceof Object &amp;&amp; "_astname" in subvalue) {
  97. this.visit(subvalue);
  98. }
  99. }
  100. } else if (value instanceof Object &amp;&amp; "_astname" in value) {
  101. this.visit(value);
  102. }
  103. }
  104. }
  105. NodeVisitor.prototype.recursive_walk = function(node) {
  106. var todo = [node];
  107. var result = [];
  108. while (todo.length > 0) {
  109. node = todo.shift();
  110. todo = todo.concat(iter_child_nodes(node))
  111. result.push(node);
  112. }
  113. return result;
  114. }
  115. /*
  116. function CodeAnalyzer() {
  117. NodeVisitor.apply(this, Array.prototype.slice.call(arguments));
  118. this.id = 0;
  119. };
  120. CodeAnalyzer.prototype = new NodeVisitor();
  121. CodeAnalyzer.prototype.visit = function(node) {
  122. node._id = this.id;
  123. this.id += 1;
  124. NodeVisitor.prototype.visit.call(this, node);
  125. //console.log(node);
  126. }
  127. */
  128. /*
  129. CodeAnalyzer.prototype.visit_Num = function(node) {
  130. node._id = this.id;
  131. this.id += 1;
  132. console.log(node.n.v);
  133. // NodeVisitor.prototype.visit_Num.call(this, node);
  134. };*/
  135. //console.log((new NodeVisitor()).visit(ast));
  136. </code></pre>
  137. </article>
  138. </section>
  139. </div>
  140. <nav>
  141. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BlockPy.html">BlockPy</a></li><li><a href="BlockPyCorgis.html">BlockPyCorgis</a></li><li><a href="BlockPyDialog.html">BlockPyDialog</a></li><li><a href="BlockPyEditor.html">BlockPyEditor</a></li><li><a href="BlockPyEngine.html">BlockPyEngine</a></li><li><a href="BlockPyEnglish.html">BlockPyEnglish</a></li><li><a href="BlockPyFeedback.html">BlockPyFeedback</a></li><li><a href="BlockPyHistory.html">BlockPyHistory</a></li><li><a href="BlockPyPresentation.html">BlockPyPresentation</a></li><li><a href="BlockPyPrinter.html">BlockPyPrinter</a></li><li><a href="BlockPyServer.html">BlockPyServer</a></li><li><a href="BlockPyToolbar.html">BlockPyToolbar</a></li><li><a href="LocalStorageWrapper.html">LocalStorageWrapper</a></li><li><a href="PythonToBlocks.html">PythonToBlocks</a></li></ul><h3>Global</h3><ul><li><a href="global.html#BlockPyInterface">BlockPyInterface</a></li><li><a href="global.html#cloneNode">cloneNode</a></li><li><a href="global.html#encodeHTML">encodeHTML</a></li><li><a href="global.html#expandArray">expandArray</a></li><li><a href="global.html#EXTENDED_ERROR_EXPLANATION">EXTENDED_ERROR_EXPLANATION</a></li><li><a href="global.html#indent">indent</a></li><li><a href="global.html#instructor_module">instructor_module</a></li><li><a href="global.html#prettyPrintDateTime">prettyPrintDateTime</a></li><li><a href="global.html#randomInteger">randomInteger</a></li><li><a href="global.html#set_button_loaded">set_button_loaded</a></li><li><a href="global.html#timerGuard">timerGuard</a></li></ul>
  142. </nav>
  143. <br class="clear">
  144. <footer>
  145. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Sun Mar 26 2017 09:45:03 GMT-0400 (Eastern Daylight Time)
  146. </footer>
  147. <script> prettyPrint(); </script>
  148. <script src="scripts/linenumber.js"> </script>
  149. </body>
  150. </html>