UNPKG

2.49 kBJavaScriptView Raw
1import defineInspect from "../jsutils/defineInspect.mjs";
2
3/**
4 * Contains a range of UTF-8 character offsets and token references that
5 * identify the region of the source from which the AST derived.
6 */
7export var Location = /*#__PURE__*/function () {
8 /**
9 * The character offset at which this Node begins.
10 */
11
12 /**
13 * The character offset at which this Node ends.
14 */
15
16 /**
17 * The Token at which this Node begins.
18 */
19
20 /**
21 * The Token at which this Node ends.
22 */
23
24 /**
25 * The Source document the AST represents.
26 */
27 function Location(startToken, endToken, source) {
28 this.start = startToken.start;
29 this.end = endToken.end;
30 this.startToken = startToken;
31 this.endToken = endToken;
32 this.source = source;
33 }
34
35 var _proto = Location.prototype;
36
37 _proto.toJSON = function toJSON() {
38 return {
39 start: this.start,
40 end: this.end
41 };
42 };
43
44 return Location;
45}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
46
47defineInspect(Location);
48/**
49 * Represents a range of characters represented by a lexical token
50 * within a Source.
51 */
52
53export var Token = /*#__PURE__*/function () {
54 /**
55 * The kind of Token.
56 */
57
58 /**
59 * The character offset at which this Node begins.
60 */
61
62 /**
63 * The character offset at which this Node ends.
64 */
65
66 /**
67 * The 1-indexed line number on which this Token appears.
68 */
69
70 /**
71 * The 1-indexed column number at which this Token begins.
72 */
73
74 /**
75 * For non-punctuation tokens, represents the interpreted value of the token.
76 */
77
78 /**
79 * Tokens exist as nodes in a double-linked-list amongst all tokens
80 * including ignored tokens. <SOF> is always the first node and <EOF>
81 * the last.
82 */
83 function Token(kind, start, end, line, column, prev, value) {
84 this.kind = kind;
85 this.start = start;
86 this.end = end;
87 this.line = line;
88 this.column = column;
89 this.value = value;
90 this.prev = prev;
91 this.next = null;
92 }
93
94 var _proto2 = Token.prototype;
95
96 _proto2.toJSON = function toJSON() {
97 return {
98 kind: this.kind,
99 value: this.value,
100 line: this.line,
101 column: this.column
102 };
103 };
104
105 return Token;
106}(); // Print a simplified form when appearing in `inspect` and `util.inspect`.
107
108defineInspect(Token);
109/**
110 * @internal
111 */
112
113export function isNode(maybeNode) {
114 return maybeNode != null && typeof maybeNode.kind === 'string';
115}
116/**
117 * The list of all possible AST node types.
118 */