Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.makeTransition = makeTransition;
  6. exports.FSM = void 0;
  7. function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  8. function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
  9. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  10. 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); } }
  11. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  12. var STOP = Symbol("STOP");
  13. function makeTransition(regex, nextState) {
  14. var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
  15. _ref$n = _ref.n,
  16. n = _ref$n === void 0 ? 1 : _ref$n,
  17. allowedSeparator = _ref.allowedSeparator;
  18. return function (instance) {
  19. if (allowedSeparator) {
  20. if (instance.input[instance.ptr] === allowedSeparator) {
  21. if (regex.test(instance.input.substring(instance.ptr - 1, instance.ptr))) {
  22. // Consume the separator and stay in current state
  23. return [instance.currentState, 1];
  24. } else {
  25. return [instance.terminatingState, 0];
  26. }
  27. }
  28. }
  29. if (regex.test(instance.input.substring(instance.ptr, instance.ptr + n))) {
  30. return [nextState, n];
  31. }
  32. return false;
  33. };
  34. }
  35. function combineTransitions(transitions) {
  36. return function () {
  37. var match = false;
  38. var currentTransitions = transitions[this.currentState] || [];
  39. for (var i = 0; i < currentTransitions.length; ++i) {
  40. match = currentTransitions[i](this);
  41. if (match !== false) {
  42. break;
  43. }
  44. }
  45. return match || [this.terminatingState, 0];
  46. };
  47. }
  48. var FSM =
  49. /*#__PURE__*/
  50. function () {
  51. function FSM(transitions, initialState) {
  52. var terminatingState = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : STOP;
  53. _classCallCheck(this, FSM);
  54. this.initialState = initialState;
  55. this.terminatingState = terminatingState;
  56. if (terminatingState === STOP || !transitions[terminatingState]) {
  57. transitions[terminatingState] = [];
  58. }
  59. this.transitionFunction = combineTransitions.call(this, transitions);
  60. }
  61. _createClass(FSM, [{
  62. key: "run",
  63. value: function run(input) {
  64. this.input = input;
  65. this.ptr = 0;
  66. this.currentState = this.initialState;
  67. var value = "";
  68. var eatLength, nextState;
  69. while (this.currentState !== this.terminatingState && this.ptr < this.input.length) {
  70. var _transitionFunction = this.transitionFunction();
  71. var _transitionFunction2 = _slicedToArray(_transitionFunction, 2);
  72. nextState = _transitionFunction2[0];
  73. eatLength = _transitionFunction2[1];
  74. value += this.input.substring(this.ptr, this.ptr += eatLength);
  75. this.currentState = nextState;
  76. }
  77. return value;
  78. }
  79. }]);
  80. return FSM;
  81. }();
  82. exports.FSM = FSM;