state.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});
  2. var _keywords = require('./keywords');
  3. var _types = require('./types');
  4. class Scope {
  5. constructor(startTokenIndex, endTokenIndex, isFunctionScope) {
  6. this.startTokenIndex = startTokenIndex;
  7. this.endTokenIndex = endTokenIndex;
  8. this.isFunctionScope = isFunctionScope;
  9. }
  10. } exports.Scope = Scope;
  11. class StateSnapshot {
  12. constructor(
  13. potentialArrowAt,
  14. noAnonFunctionType,
  15. inDisallowConditionalTypesContext,
  16. tokensLength,
  17. scopesLength,
  18. pos,
  19. type,
  20. contextualKeyword,
  21. start,
  22. end,
  23. isType,
  24. scopeDepth,
  25. error,
  26. ) {;this.potentialArrowAt = potentialArrowAt;this.noAnonFunctionType = noAnonFunctionType;this.inDisallowConditionalTypesContext = inDisallowConditionalTypesContext;this.tokensLength = tokensLength;this.scopesLength = scopesLength;this.pos = pos;this.type = type;this.contextualKeyword = contextualKeyword;this.start = start;this.end = end;this.isType = isType;this.scopeDepth = scopeDepth;this.error = error;}
  27. } exports.StateSnapshot = StateSnapshot;
  28. class State {constructor() { State.prototype.__init.call(this);State.prototype.__init2.call(this);State.prototype.__init3.call(this);State.prototype.__init4.call(this);State.prototype.__init5.call(this);State.prototype.__init6.call(this);State.prototype.__init7.call(this);State.prototype.__init8.call(this);State.prototype.__init9.call(this);State.prototype.__init10.call(this);State.prototype.__init11.call(this);State.prototype.__init12.call(this);State.prototype.__init13.call(this); }
  29. // Used to signify the start of a potential arrow function
  30. __init() {this.potentialArrowAt = -1}
  31. // Used by Flow to handle an edge case involving function type parsing.
  32. __init2() {this.noAnonFunctionType = false}
  33. // Used by TypeScript to handle ambiguities when parsing conditional types.
  34. __init3() {this.inDisallowConditionalTypesContext = false}
  35. // Token store.
  36. __init4() {this.tokens = []}
  37. // Array of all observed scopes, ordered by their ending position.
  38. __init5() {this.scopes = []}
  39. // The current position of the tokenizer in the input.
  40. __init6() {this.pos = 0}
  41. // Information about the current token.
  42. __init7() {this.type = _types.TokenType.eof}
  43. __init8() {this.contextualKeyword = _keywords.ContextualKeyword.NONE}
  44. __init9() {this.start = 0}
  45. __init10() {this.end = 0}
  46. __init11() {this.isType = false}
  47. __init12() {this.scopeDepth = 0}
  48. /**
  49. * If the parser is in an error state, then the token is always tt.eof and all functions can
  50. * keep executing but should be written so they don't get into an infinite loop in this situation.
  51. *
  52. * This approach, combined with the ability to snapshot and restore state, allows us to implement
  53. * backtracking without exceptions and without needing to explicitly propagate error states
  54. * everywhere.
  55. */
  56. __init13() {this.error = null}
  57. snapshot() {
  58. return new StateSnapshot(
  59. this.potentialArrowAt,
  60. this.noAnonFunctionType,
  61. this.inDisallowConditionalTypesContext,
  62. this.tokens.length,
  63. this.scopes.length,
  64. this.pos,
  65. this.type,
  66. this.contextualKeyword,
  67. this.start,
  68. this.end,
  69. this.isType,
  70. this.scopeDepth,
  71. this.error,
  72. );
  73. }
  74. restoreFromSnapshot(snapshot) {
  75. this.potentialArrowAt = snapshot.potentialArrowAt;
  76. this.noAnonFunctionType = snapshot.noAnonFunctionType;
  77. this.inDisallowConditionalTypesContext = snapshot.inDisallowConditionalTypesContext;
  78. this.tokens.length = snapshot.tokensLength;
  79. this.scopes.length = snapshot.scopesLength;
  80. this.pos = snapshot.pos;
  81. this.type = snapshot.type;
  82. this.contextualKeyword = snapshot.contextualKeyword;
  83. this.start = snapshot.start;
  84. this.end = snapshot.end;
  85. this.isType = snapshot.isType;
  86. this.scopeDepth = snapshot.scopeDepth;
  87. this.error = snapshot.error;
  88. }
  89. } exports.default = State;