state.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
  4. */
  5. 'use strict';
  6. /**
  7. * A generic FA State class (base for NFA and DFA).
  8. *
  9. * Maintains the transition map, and the flag whether
  10. * the state is accepting.
  11. */
  12. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  13. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  14. var State = function () {
  15. function State() {
  16. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  17. _ref$accepting = _ref.accepting,
  18. accepting = _ref$accepting === undefined ? false : _ref$accepting;
  19. _classCallCheck(this, State);
  20. /**
  21. * Outgoing transitions to other states.
  22. */
  23. this._transitions = new Map();
  24. /**
  25. * Whether the state is accepting.
  26. */
  27. this.accepting = accepting;
  28. }
  29. /**
  30. * Returns transitions for this state.
  31. */
  32. _createClass(State, [{
  33. key: 'getTransitions',
  34. value: function getTransitions() {
  35. return this._transitions;
  36. }
  37. /**
  38. * Creates a transition on symbol.
  39. */
  40. }, {
  41. key: 'addTransition',
  42. value: function addTransition(symbol, toState) {
  43. this.getTransitionsOnSymbol(symbol).add(toState);
  44. return this;
  45. }
  46. /**
  47. * Returns transitions set on symbol.
  48. */
  49. }, {
  50. key: 'getTransitionsOnSymbol',
  51. value: function getTransitionsOnSymbol(symbol) {
  52. var transitions = this._transitions.get(symbol);
  53. if (!transitions) {
  54. transitions = new Set();
  55. this._transitions.set(symbol, transitions);
  56. }
  57. return transitions;
  58. }
  59. }]);
  60. return State;
  61. }();
  62. module.exports = State;