state.js 3.8 KB

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