2025-02-08 13:06:44 -05:00
|
|
|
import { type MainModule } from '../lib/web-tree-sitter';
|
2025-01-20 02:43:52 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
|
import { ParseState, type Parser } from './parser';
|
2025-01-13 01:48:42 -05:00
|
|
|
|
2025-01-20 02:43:52 -05:00
|
|
|
/**
|
|
|
|
|
* A position in a multi-line text document, in terms of rows and columns.
|
|
|
|
|
*
|
|
|
|
|
* Rows and columns are zero-based.
|
|
|
|
|
*/
|
2025-01-13 01:48:42 -05:00
|
|
|
export interface Point {
|
2025-01-20 02:43:52 -05:00
|
|
|
/** The zero-based row number. */
|
2025-01-13 01:48:42 -05:00
|
|
|
row: number;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** The zero-based column number. */
|
2025-01-13 01:48:42 -05:00
|
|
|
column: number;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 02:43:52 -05:00
|
|
|
/**
|
|
|
|
|
* A range of positions in a multi-line text document, both in terms of bytes
|
|
|
|
|
* and of rows and columns.
|
|
|
|
|
*/
|
2025-01-13 01:48:42 -05:00
|
|
|
export interface Range {
|
2025-01-20 02:43:52 -05:00
|
|
|
/** The start position of the range. */
|
2025-01-13 01:48:42 -05:00
|
|
|
startPosition: Point;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** The end position of the range. */
|
2025-01-13 01:48:42 -05:00
|
|
|
endPosition: Point;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** The start index of the range. */
|
2025-01-13 01:48:42 -05:00
|
|
|
startIndex: number;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** The end index of the range. */
|
2025-01-13 01:48:42 -05:00
|
|
|
endIndex: number;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 02:43:52 -05:00
|
|
|
/**
|
|
|
|
|
* A summary of a change to a text document.
|
|
|
|
|
*/
|
2025-01-13 01:48:42 -05:00
|
|
|
export interface Edit {
|
2025-01-20 02:43:52 -05:00
|
|
|
/** The start position of the change. */
|
2025-01-13 01:48:42 -05:00
|
|
|
startPosition: Point;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** The end position of the change before the edit. */
|
2025-01-13 01:48:42 -05:00
|
|
|
oldEndPosition: Point;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** The end position of the change after the edit. */
|
2025-01-13 01:48:42 -05:00
|
|
|
newEndPosition: Point;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** The start index of the change. */
|
2025-01-13 01:48:42 -05:00
|
|
|
startIndex: number;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** The end index of the change before the edit. */
|
2025-01-13 01:48:42 -05:00
|
|
|
oldEndIndex: number;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** The end index of the change after the edit. */
|
2025-01-13 01:48:42 -05:00
|
|
|
newEndIndex: number;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 02:43:52 -05:00
|
|
|
/** @internal */
|
2025-01-13 01:48:42 -05:00
|
|
|
export const SIZE_OF_SHORT = 2;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** @internal */
|
2025-01-13 01:48:42 -05:00
|
|
|
export const SIZE_OF_INT = 4;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** @internal */
|
2025-01-13 01:48:42 -05:00
|
|
|
export const SIZE_OF_CURSOR = 4 * SIZE_OF_INT;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** @internal */
|
2025-01-13 01:48:42 -05:00
|
|
|
export const SIZE_OF_NODE = 5 * SIZE_OF_INT;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** @internal */
|
2025-01-13 01:48:42 -05:00
|
|
|
export const SIZE_OF_POINT = 2 * SIZE_OF_INT;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** @internal */
|
2025-01-13 01:48:42 -05:00
|
|
|
export const SIZE_OF_RANGE = 2 * SIZE_OF_INT + 2 * SIZE_OF_POINT;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/** @internal */
|
2025-01-13 01:48:42 -05:00
|
|
|
export const ZERO_POINT: Point = { row: 0, column: 0 };
|
|
|
|
|
|
2025-01-20 02:43:52 -05:00
|
|
|
/**
|
|
|
|
|
* A callback for parsing that takes an index and point, and should return a string.
|
|
|
|
|
*/
|
2025-01-19 23:06:02 -05:00
|
|
|
export type ParseCallback = (index: number, position: Point) => string | undefined;
|
2025-01-20 02:43:52 -05:00
|
|
|
|
|
|
|
|
/**
|
2025-07-14 22:50:04 +12:00
|
|
|
* A callback that receives the parse state during parsing.
|
|
|
|
|
*/
|
2025-01-20 02:43:52 -05:00
|
|
|
export type ProgressCallback = (progress: ParseState) => boolean;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A callback for logging messages.
|
|
|
|
|
*
|
|
|
|
|
* If `isLex` is `true`, the message is from the lexer, otherwise it's from the parser.
|
|
|
|
|
*/
|
2025-01-13 01:48:42 -05:00
|
|
|
export type LogCallback = (message: string, isLex: boolean) => void;
|
|
|
|
|
|
|
|
|
|
// Helper type for internal use
|
2025-01-20 02:43:52 -05:00
|
|
|
/** @internal */
|
2025-01-13 01:48:42 -05:00
|
|
|
export const INTERNAL = Symbol('INTERNAL');
|
2025-01-20 02:43:52 -05:00
|
|
|
/** @internal */
|
2025-01-13 01:48:42 -05:00
|
|
|
export type Internal = typeof INTERNAL;
|
|
|
|
|
|
|
|
|
|
// Helper functions for type checking
|
2025-01-20 02:43:52 -05:00
|
|
|
/** @internal */
|
2025-01-13 01:48:42 -05:00
|
|
|
export function assertInternal(x: unknown): asserts x is Internal {
|
|
|
|
|
if (x !== INTERNAL) throw new Error('Illegal constructor');
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 02:43:52 -05:00
|
|
|
/** @internal */
|
2025-01-16 01:10:54 -05:00
|
|
|
export function isPoint(point?: Point): point is Point {
|
2025-01-13 01:48:42 -05:00
|
|
|
return (
|
|
|
|
|
!!point &&
|
2025-01-16 01:10:54 -05:00
|
|
|
typeof (point).row === 'number' &&
|
|
|
|
|
typeof (point).column === 'number'
|
2025-01-13 01:48:42 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 02:43:52 -05:00
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*
|
2025-08-19 12:32:46 +03:00
|
|
|
* Sets the Tree-sitter Wasm module. This should only be called by the {@link Parser} class via {@link Parser.init}.
|
2025-01-20 02:43:52 -05:00
|
|
|
*/
|
2025-01-19 15:15:01 -05:00
|
|
|
export function setModule(module: MainModule) {
|
|
|
|
|
C = module;
|
|
|
|
|
}
|
2025-01-13 01:48:42 -05:00
|
|
|
|
2025-01-20 02:43:52 -05:00
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*
|
2025-08-19 12:32:46 +03:00
|
|
|
* `C` is a convenient shorthand for the Tree-sitter Wasm module,
|
2025-01-20 02:43:52 -05:00
|
|
|
* which allows us to call all of the exported functions.
|
|
|
|
|
*/
|
2025-01-19 15:15:01 -05:00
|
|
|
export let C: MainModule;
|