* Parse a slice of UTF8 text. * * @param {string | ParseCallback} callback - The UTF8-encoded text to parse or a callback function. * * @param {Tree | null} [oldTree] - A previous syntax tree parsed from the same document. If the text of the * document has changed since `oldTree` wa
(callback, oldTree, options)
| 3243 | * - The progress callback returned true. |
| 3244 | */ |
| 3245 | parse(callback, oldTree, options) { |
| 3246 | if (typeof callback === "string") { |
| 3247 | C.currentParseCallback = (index) => callback.slice(index); |
| 3248 | } else if (typeof callback === "function") { |
| 3249 | C.currentParseCallback = callback; |
| 3250 | } else { |
| 3251 | throw new Error("Argument must be a string or a function"); |
| 3252 | } |
| 3253 | if (options?.progressCallback) { |
| 3254 | C.currentProgressCallback = options.progressCallback; |
| 3255 | } else { |
| 3256 | C.currentProgressCallback = null; |
| 3257 | } |
| 3258 | if (this.logCallback) { |
| 3259 | C.currentLogCallback = this.logCallback; |
| 3260 | C._ts_parser_enable_logger_wasm(this[0], 1); |
| 3261 | } else { |
| 3262 | C.currentLogCallback = null; |
| 3263 | C._ts_parser_enable_logger_wasm(this[0], 0); |
| 3264 | } |
| 3265 | let rangeCount = 0; |
| 3266 | let rangeAddress = 0; |
| 3267 | if (options?.includedRanges) { |
| 3268 | rangeCount = options.includedRanges.length; |
| 3269 | rangeAddress = C._calloc(rangeCount, SIZE_OF_RANGE); |
| 3270 | let address = rangeAddress; |
| 3271 | for (let i2 = 0; i2 < rangeCount; i2++) { |
| 3272 | marshalRange(address, options.includedRanges[i2]); |
| 3273 | address += SIZE_OF_RANGE; |
| 3274 | } |
| 3275 | } |
| 3276 | const treeAddress = C._ts_parser_parse_wasm( |
| 3277 | this[0], |
| 3278 | this[1], |
| 3279 | oldTree ? oldTree[0] : 0, |
| 3280 | rangeAddress, |
| 3281 | rangeCount |
| 3282 | ); |
| 3283 | if (!treeAddress) { |
| 3284 | C.currentParseCallback = null; |
| 3285 | C.currentLogCallback = null; |
| 3286 | C.currentProgressCallback = null; |
| 3287 | return null; |
| 3288 | } |
| 3289 | if (!this.language) { |
| 3290 | throw new Error("Parser must have a language to parse"); |
| 3291 | } |
| 3292 | const result = new Tree(INTERNAL, treeAddress, this.language, C.currentParseCallback); |
| 3293 | C.currentParseCallback = null; |
| 3294 | C.currentLogCallback = null; |
| 3295 | C.currentProgressCallback = null; |
| 3296 | return result; |
| 3297 | } |
| 3298 | /** |
| 3299 | * Instruct the parser to start the next parse from the beginning. |
| 3300 | * |