Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

code-path.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @fileoverview A class of the code path.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const CodePathState = require("./code-path-state");
  10. const IdGenerator = require("./id-generator");
  11. //------------------------------------------------------------------------------
  12. // Public Interface
  13. //------------------------------------------------------------------------------
  14. /**
  15. * A code path.
  16. */
  17. class CodePath {
  18. /**
  19. * @param {string} id - An identifier.
  20. * @param {CodePath|null} upper - The code path of the upper function scope.
  21. * @param {Function} onLooped - A callback function to notify looping.
  22. */
  23. constructor(id, upper, onLooped) {
  24. /**
  25. * The identifier of this code path.
  26. * Rules use it to store additional information of each rule.
  27. * @type {string}
  28. */
  29. this.id = id;
  30. /**
  31. * The code path of the upper function scope.
  32. * @type {CodePath|null}
  33. */
  34. this.upper = upper;
  35. /**
  36. * The code paths of nested function scopes.
  37. * @type {CodePath[]}
  38. */
  39. this.childCodePaths = [];
  40. // Initializes internal state.
  41. Object.defineProperty(
  42. this,
  43. "internal",
  44. { value: new CodePathState(new IdGenerator(`${id}_`), onLooped) }
  45. );
  46. // Adds this into `childCodePaths` of `upper`.
  47. if (upper) {
  48. upper.childCodePaths.push(this);
  49. }
  50. }
  51. /**
  52. * Gets the state of a given code path.
  53. *
  54. * @param {CodePath} codePath - A code path to get.
  55. * @returns {CodePathState} The state of the code path.
  56. */
  57. static getState(codePath) {
  58. return codePath.internal;
  59. }
  60. /**
  61. * The initial code path segment.
  62. * @type {CodePathSegment}
  63. */
  64. get initialSegment() {
  65. return this.internal.initialSegment;
  66. }
  67. /**
  68. * Final code path segments.
  69. * This array is a mix of `returnedSegments` and `thrownSegments`.
  70. * @type {CodePathSegment[]}
  71. */
  72. get finalSegments() {
  73. return this.internal.finalSegments;
  74. }
  75. /**
  76. * Final code path segments which is with `return` statements.
  77. * This array contains the last path segment if it's reachable.
  78. * Since the reachable last path returns `undefined`.
  79. * @type {CodePathSegment[]}
  80. */
  81. get returnedSegments() {
  82. return this.internal.returnedForkContext;
  83. }
  84. /**
  85. * Final code path segments which is with `throw` statements.
  86. * @type {CodePathSegment[]}
  87. */
  88. get thrownSegments() {
  89. return this.internal.thrownForkContext;
  90. }
  91. /**
  92. * Current code path segments.
  93. * @type {CodePathSegment[]}
  94. */
  95. get currentSegments() {
  96. return this.internal.currentSegments;
  97. }
  98. /**
  99. * Traverses all segments in this code path.
  100. *
  101. * codePath.traverseSegments(function(segment, controller) {
  102. * // do something.
  103. * });
  104. *
  105. * This method enumerates segments in order from the head.
  106. *
  107. * The `controller` object has two methods.
  108. *
  109. * - `controller.skip()` - Skip the following segments in this branch.
  110. * - `controller.break()` - Skip all following segments.
  111. *
  112. * @param {Object} [options] - Omittable.
  113. * @param {CodePathSegment} [options.first] - The first segment to traverse.
  114. * @param {CodePathSegment} [options.last] - The last segment to traverse.
  115. * @param {Function} callback - A callback function.
  116. * @returns {void}
  117. */
  118. traverseSegments(options, callback) {
  119. let resolvedOptions;
  120. let resolvedCallback;
  121. if (typeof options === "function") {
  122. resolvedCallback = options;
  123. resolvedOptions = {};
  124. } else {
  125. resolvedOptions = options || {};
  126. resolvedCallback = callback;
  127. }
  128. const startSegment = resolvedOptions.first || this.internal.initialSegment;
  129. const lastSegment = resolvedOptions.last;
  130. let item = null;
  131. let index = 0;
  132. let end = 0;
  133. let segment = null;
  134. const visited = Object.create(null);
  135. const stack = [[startSegment, 0]];
  136. let skippedSegment = null;
  137. let broken = false;
  138. const controller = {
  139. skip() {
  140. if (stack.length <= 1) {
  141. broken = true;
  142. } else {
  143. skippedSegment = stack[stack.length - 2][0];
  144. }
  145. },
  146. break() {
  147. broken = true;
  148. }
  149. };
  150. /**
  151. * Checks a given previous segment has been visited.
  152. * @param {CodePathSegment} prevSegment - A previous segment to check.
  153. * @returns {boolean} `true` if the segment has been visited.
  154. */
  155. function isVisited(prevSegment) {
  156. return (
  157. visited[prevSegment.id] ||
  158. segment.isLoopedPrevSegment(prevSegment)
  159. );
  160. }
  161. while (stack.length > 0) {
  162. item = stack[stack.length - 1];
  163. segment = item[0];
  164. index = item[1];
  165. if (index === 0) {
  166. // Skip if this segment has been visited already.
  167. if (visited[segment.id]) {
  168. stack.pop();
  169. continue;
  170. }
  171. // Skip if all previous segments have not been visited.
  172. if (segment !== startSegment &&
  173. segment.prevSegments.length > 0 &&
  174. !segment.prevSegments.every(isVisited)
  175. ) {
  176. stack.pop();
  177. continue;
  178. }
  179. // Reset the flag of skipping if all branches have been skipped.
  180. if (skippedSegment && segment.prevSegments.indexOf(skippedSegment) !== -1) {
  181. skippedSegment = null;
  182. }
  183. visited[segment.id] = true;
  184. // Call the callback when the first time.
  185. if (!skippedSegment) {
  186. resolvedCallback.call(this, segment, controller);
  187. if (segment === lastSegment) {
  188. controller.skip();
  189. }
  190. if (broken) {
  191. break;
  192. }
  193. }
  194. }
  195. // Update the stack.
  196. end = segment.nextSegments.length - 1;
  197. if (index < end) {
  198. item[1] += 1;
  199. stack.push([segment.nextSegments[index], 0]);
  200. } else if (index === end) {
  201. item[0] = segment.nextSegments[index];
  202. item[1] = 0;
  203. } else {
  204. stack.pop();
  205. }
  206. }
  207. }
  208. }
  209. module.exports = CodePath;