hermes-parser 0.4.8 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,10 +4,22 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
+ *
7
8
  * @format
8
9
  */
9
10
  'use strict';
10
11
 
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports["default"] = void 0;
16
+
17
+ var _HermesParserDecodeUTF8String = _interopRequireDefault(require("./HermesParserDecodeUTF8String"));
18
+
19
+ var _HermesParserNodeDeserializers = _interopRequireDefault(require("./HermesParserNodeDeserializers"));
20
+
21
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
22
+
11
23
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
24
 
13
25
  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); } }
@@ -16,14 +28,28 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
16
28
 
17
29
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
18
30
 
19
- var HermesParserDecodeUTF8String = require('./HermesParserDecodeUTF8String');
20
-
21
- var NODE_DESERIALIZERS = require('./HermesParserNodeDeserializers');
22
-
23
31
  var HermesParserDeserializer = /*#__PURE__*/function () {
32
+ // Matches StoredComment::Kind enum in JSLexer.h
33
+ // Matches TokenType enum in HermesParserJSSerializer.h
24
34
  function HermesParserDeserializer(programBuffer, positionBuffer, positionBufferSize, wasmParser, options) {
25
35
  _classCallCheck(this, HermesParserDeserializer);
26
36
 
37
+ _defineProperty(this, "programBufferIdx", void 0);
38
+
39
+ _defineProperty(this, "positionBufferIdx", void 0);
40
+
41
+ _defineProperty(this, "positionBufferSize", void 0);
42
+
43
+ _defineProperty(this, "locMap", void 0);
44
+
45
+ _defineProperty(this, "HEAPU8", void 0);
46
+
47
+ _defineProperty(this, "HEAPU32", void 0);
48
+
49
+ _defineProperty(this, "HEAPF64", void 0);
50
+
51
+ _defineProperty(this, "options", void 0);
52
+
27
53
  _defineProperty(this, "commentTypes", ['CommentLine', 'CommentBlock', 'InterpreterDirective']);
28
54
 
29
55
  _defineProperty(this, "tokenTypes", ['Boolean', 'Identifier', 'Keyword', 'Null', 'Numeric', 'Punctuator', 'String', 'RegularExpression', 'Template', 'JSXText']);
@@ -60,7 +86,7 @@ var HermesParserDeserializer = /*#__PURE__*/function () {
60
86
  comments: this.deserializeComments()
61
87
  };
62
88
 
63
- if (this.options.tokens) {
89
+ if (this.options.tokens === true) {
64
90
  program.tokens = this.deserializeTokens();
65
91
  }
66
92
 
@@ -113,7 +139,7 @@ var HermesParserDeserializer = /*#__PURE__*/function () {
113
139
  }
114
140
 
115
141
  var size = this.next();
116
- return HermesParserDecodeUTF8String(ptr, size, this.HEAPU8);
142
+ return (0, _HermesParserDecodeUTF8String["default"])(ptr, size, this.HEAPU8);
117
143
  }
118
144
  /**
119
145
  * Nodes are serialized as a 4-byte integer denoting their node kind,
@@ -132,7 +158,8 @@ var HermesParserDeserializer = /*#__PURE__*/function () {
132
158
  return null;
133
159
  }
134
160
 
135
- var nodeDeserializer = NODE_DESERIALIZERS[nodeType - 1].bind(this);
161
+ var nodeDeserializer = _HermesParserNodeDeserializers["default"][nodeType - 1].bind(this);
162
+
136
163
  return nodeDeserializer();
137
164
  }
138
165
  /**
@@ -151,16 +178,15 @@ var HermesParserDeserializer = /*#__PURE__*/function () {
151
178
  }
152
179
 
153
180
  return nodeList;
154
- } // Matches StoredComment::Kind enum in JSLexer.h
155
-
156
- }, {
157
- key: "deserializeComments",
158
-
181
+ }
159
182
  /**
160
183
  * Comments are serialized as a node list, where each comment is serialized
161
184
  * as a 4-byte integer denoting comment type, followed by a 4-byte value
162
185
  * denoting the loc ID, followed by a serialized string for the comment value.
163
186
  */
187
+
188
+ }, {
189
+ key: "deserializeComments",
164
190
  value: function deserializeComments() {
165
191
  var size = this.next();
166
192
  var comments = [];
@@ -206,6 +232,7 @@ var HermesParserDeserializer = /*#__PURE__*/function () {
206
232
  }, {
207
233
  key: "addEmptyLoc",
208
234
  value: function addEmptyLoc() {
235
+ // $FlowExpectedError
209
236
  var loc = {};
210
237
  this.locMap[this.next()] = loc;
211
238
  return loc;
@@ -247,4 +274,4 @@ var HermesParserDeserializer = /*#__PURE__*/function () {
247
274
  return HermesParserDeserializer;
248
275
  }();
249
276
 
250
- module.exports = HermesParserDeserializer;
277
+ exports["default"] = HermesParserDeserializer;
@@ -0,0 +1,260 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ import type {
14
+ HermesSourceLocation,
15
+ HermesNode,
16
+ HermesToken,
17
+ HermesComment,
18
+ } from './HermesAST';
19
+ import typeof HermesParserWASM from './HermesParserWASM';
20
+ import type {ParserOptions} from './ParserOptions';
21
+
22
+ import HermesParserDecodeUTF8String from './HermesParserDecodeUTF8String';
23
+ import NODE_DESERIALIZERS from './HermesParserNodeDeserializers';
24
+
25
+ export default class HermesParserDeserializer {
26
+ programBufferIdx: number;
27
+ positionBufferIdx: number;
28
+ +positionBufferSize: number;
29
+ +locMap: {[number]: HermesSourceLocation};
30
+ +HEAPU8: HermesParserWASM['HEAPU8'];
31
+ +HEAPU32: HermesParserWASM['HEAPU32'];
32
+ +HEAPF64: HermesParserWASM['HEAPF64'];
33
+ +options: ParserOptions;
34
+
35
+ // Matches StoredComment::Kind enum in JSLexer.h
36
+ +commentTypes: $ReadOnlyArray<HermesComment['type']> = [
37
+ 'CommentLine',
38
+ 'CommentBlock',
39
+ 'InterpreterDirective',
40
+ ];
41
+
42
+ // Matches TokenType enum in HermesParserJSSerializer.h
43
+ +tokenTypes: $ReadOnlyArray<HermesToken['type']> = [
44
+ 'Boolean',
45
+ 'Identifier',
46
+ 'Keyword',
47
+ 'Null',
48
+ 'Numeric',
49
+ 'Punctuator',
50
+ 'String',
51
+ 'RegularExpression',
52
+ 'Template',
53
+ 'JSXText',
54
+ ];
55
+
56
+ constructor(
57
+ programBuffer: number,
58
+ positionBuffer: number,
59
+ positionBufferSize: number,
60
+ wasmParser: HermesParserWASM,
61
+ options: ParserOptions,
62
+ ) {
63
+ // Program and position buffer are memory addresses, so we must convert
64
+ // into indices into HEAPU32 (an array of 4-byte integers).
65
+ this.programBufferIdx = programBuffer / 4;
66
+ this.positionBufferIdx = positionBuffer / 4;
67
+
68
+ this.positionBufferSize = positionBufferSize;
69
+ this.locMap = {};
70
+
71
+ this.HEAPU8 = wasmParser.HEAPU8;
72
+ this.HEAPU32 = wasmParser.HEAPU32;
73
+ this.HEAPF64 = wasmParser.HEAPF64;
74
+
75
+ this.options = options;
76
+ }
77
+
78
+ /**
79
+ * Consume and return the next 4 bytes in the program buffer.
80
+ */
81
+ next(): number {
82
+ const num = this.HEAPU32[this.programBufferIdx++];
83
+ return num;
84
+ }
85
+
86
+ deserialize(): HermesNode {
87
+ const program: HermesNode = {
88
+ type: 'Program',
89
+ loc: this.addEmptyLoc(),
90
+ body: this.deserializeNodeList(),
91
+ comments: this.deserializeComments(),
92
+ };
93
+
94
+ if (this.options.tokens === true) {
95
+ program.tokens = this.deserializeTokens();
96
+ }
97
+
98
+ this.fillLocs();
99
+
100
+ return program;
101
+ }
102
+
103
+ /**
104
+ * Booleans are serialized as a single 4-byte integer.
105
+ */
106
+ deserializeBoolean(): boolean {
107
+ return Boolean(this.next());
108
+ }
109
+
110
+ /**
111
+ * Numbers are serialized directly into program buffer, taking up 8 bytes
112
+ * preceded by 4 bytes of alignment padding if necessary.
113
+ */
114
+ deserializeNumber(): number {
115
+ let floatIdx;
116
+
117
+ // Numbers are aligned on 8-byte boundaries, so skip padding if we are at
118
+ // an odd index into the 4-byte aligned program buffer.
119
+ if (this.programBufferIdx % 2 === 0) {
120
+ floatIdx = this.programBufferIdx / 2;
121
+ this.programBufferIdx += 2;
122
+ } else {
123
+ floatIdx = (this.programBufferIdx + 1) / 2;
124
+ this.programBufferIdx += 3;
125
+ }
126
+
127
+ return this.HEAPF64[floatIdx];
128
+ }
129
+
130
+ /**
131
+ * Strings are serialized as a 4-byte pointer into the heap, followed
132
+ * by their size as a 4-byte integer. The size is only present if the
133
+ * pointer is non-null.
134
+ */
135
+ deserializeString(): ?string {
136
+ const ptr = this.next();
137
+ if (ptr === 0) {
138
+ return null;
139
+ }
140
+
141
+ const size = this.next();
142
+
143
+ return HermesParserDecodeUTF8String(ptr, size, this.HEAPU8);
144
+ }
145
+
146
+ /**
147
+ * Nodes are serialized as a 4-byte integer denoting their node kind,
148
+ * followed by a 4-byte loc ID, followed by serialized node properties.
149
+ *
150
+ * If the node kind is 0 the node is null, otherwise the node kind - 1 is an
151
+ * index into the array of node deserialization functions.
152
+ */
153
+ deserializeNode(): ?HermesNode {
154
+ const nodeType = this.next();
155
+ if (nodeType === 0) {
156
+ return null;
157
+ }
158
+
159
+ const nodeDeserializer = NODE_DESERIALIZERS[nodeType - 1].bind(this);
160
+ return nodeDeserializer();
161
+ }
162
+
163
+ /**
164
+ * Node lists are serialized as a 4-byte integer denoting the number of
165
+ * elements in the list, followed by the serialized elements.
166
+ */
167
+ deserializeNodeList(): Array<?HermesNode> {
168
+ const size = this.next();
169
+ const nodeList = [];
170
+
171
+ for (let i = 0; i < size; i++) {
172
+ nodeList.push(this.deserializeNode());
173
+ }
174
+
175
+ return nodeList;
176
+ }
177
+
178
+ /**
179
+ * Comments are serialized as a node list, where each comment is serialized
180
+ * as a 4-byte integer denoting comment type, followed by a 4-byte value
181
+ * denoting the loc ID, followed by a serialized string for the comment value.
182
+ */
183
+ deserializeComments(): Array<HermesComment> {
184
+ const size = this.next();
185
+ const comments = [];
186
+
187
+ for (let i = 0; i < size; i++) {
188
+ const commentType = this.commentTypes[this.next()];
189
+ const loc = this.addEmptyLoc();
190
+ const value = this.deserializeString();
191
+ comments.push({
192
+ type: commentType,
193
+ loc,
194
+ value,
195
+ });
196
+ }
197
+
198
+ return comments;
199
+ }
200
+
201
+ deserializeTokens(): Array<HermesToken> {
202
+ const size = this.next();
203
+ const tokens = [];
204
+
205
+ for (let i = 0; i < size; i++) {
206
+ const tokenType = this.tokenTypes[this.next()];
207
+ const loc = this.addEmptyLoc();
208
+ const value = this.deserializeString();
209
+ tokens.push({
210
+ type: tokenType,
211
+ loc,
212
+ value,
213
+ });
214
+ }
215
+
216
+ return tokens;
217
+ }
218
+
219
+ /**
220
+ * While deserializing the AST locations are represented by
221
+ * a 4-byte loc ID. This is used to create a map of loc IDs to empty loc
222
+ * objects that are filled after the AST has been deserialized.
223
+ */
224
+ addEmptyLoc(): HermesSourceLocation {
225
+ // $FlowExpectedError
226
+ const loc: HermesSourceLocation = {};
227
+ this.locMap[this.next()] = loc;
228
+ return loc;
229
+ }
230
+
231
+ /**
232
+ * Positions are serialized as a loc ID which denotes which loc it is associated with,
233
+ * followed by kind which denotes whether it is a start or end position,
234
+ * followed by line, column, and offset (4-bytes each).
235
+ */
236
+ fillLocs(): void {
237
+ for (let i = 0; i < this.positionBufferSize; i++) {
238
+ const locId = this.HEAPU32[this.positionBufferIdx++];
239
+ const kind = this.HEAPU32[this.positionBufferIdx++];
240
+ const line = this.HEAPU32[this.positionBufferIdx++];
241
+ const column = this.HEAPU32[this.positionBufferIdx++];
242
+ const offset = this.HEAPU32[this.positionBufferIdx++];
243
+
244
+ const loc = this.locMap[locId];
245
+ if (kind === 0) {
246
+ loc.start = {
247
+ line,
248
+ column,
249
+ };
250
+ loc.rangeStart = offset;
251
+ } else {
252
+ loc.end = {
253
+ line,
254
+ column,
255
+ };
256
+ loc.rangeEnd = offset;
257
+ }
258
+ }
259
+ }
260
+ }
@@ -4,6 +4,7 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
+ *
7
8
  * @format
8
9
  */
9
10
  'use strict';
@@ -703,7 +704,7 @@ function deserializeImportDeclaration() {
703
704
  loc: this.addEmptyLoc(),
704
705
  specifiers: this.deserializeNodeList(),
705
706
  source: this.deserializeNode(),
706
- attributes: this.deserializeNodeList(),
707
+ assertions: this.deserializeNodeList(),
707
708
  importKind: this.deserializeString()
708
709
  };
709
710
  }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ import type {HermesNode} from './HermesAST';
12
+ import type HermesParserDeserializer from './HermesParserDeserializer';
13
+
14
+ declare module.exports: $ReadOnlyArray<
15
+ (this: HermesParserDeserializer) => HermesNode,
16
+ >;