UNPKG

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