grammatik 0.1.1 → 0.1.2

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.
package/src/lexer.d.ts CHANGED
@@ -5,19 +5,60 @@
5
5
  * large win for punctuation-heavy grammars (JSON is ~60% punctuation by token count).
6
6
  *
7
7
  * @param {TokenDef[]} defs
8
- * @param {{ positions?: 'full' | 'offset' }} [options]
8
+ * @param {{ positions?: 'full' | 'offset', scanners?: boolean, fuse?: boolean, incremental?: boolean, recover?: boolean, errorToken?: string }} [options]
9
9
  * `full` (default) records line and column on every token. `offset` records
10
10
  * only `start`/`end`, which skips a scan of the entire input for newlines —
11
11
  * worth taking when errors do not need to name a line, or when positions are
12
12
  * resolved lazily from the offset afterwards.
13
- * @returns {{ tokenize: (text: string) => Token[], tokenNames: string[], positions: string }}
13
+ *
14
+ * `incremental` (default false) is opt-in: when set, the returned lexer also
15
+ * exposes `tokenizeIncremental` and `relex`, for editors that want to re-lex
16
+ * only the region touched by a single edit instead of the whole document.
17
+ * A lexer created without it is bit-for-bit the plain lexer — no overhead.
18
+ *
19
+ * `scanners` (default true) runs patterns of a few common shapes as
20
+ * table-driven loops instead of regex calls. The tokens are identical;
21
+ * `false` forces every pattern through its regex.
22
+ *
23
+ * `fuse` (default true) joins runs of three or more consecutive regex candidates
24
+ * into one alternation, so a position where several tokens could start costs one
25
+ * regex call rather than one per rejected token. Same tokens; `false` tries them
26
+ * one at a time.
27
+ *
28
+ * `recover` (default false, only meaningful with `incremental`) makes an
29
+ * unmatched character produce a single-character token of type `errorToken`
30
+ * (default `'Error'`) instead of throwing, so highlighting survives invalid
31
+ * or half-typed input.
32
+ * @returns {{ tokenize: (text: string) => Token[], tokenizeIncremental?: (text: string) => IncrementalResult, relex?: (text: string, prev: IncrementalResult, edit: { start: number, oldEnd: number, newEnd: number }) => IncrementalResult, tokenNames: string[], positions: string, modeNames: string[] }}
14
33
  */
15
34
  export function createLexer(defs: TokenDef[], options?: {
16
35
  positions?: "full" | "offset";
36
+ scanners?: boolean;
37
+ fuse?: boolean;
38
+ incremental?: boolean;
39
+ recover?: boolean;
40
+ errorToken?: string;
17
41
  }): {
18
42
  tokenize: (text: string) => Token[];
43
+ tokenizeIncremental?: (text: string) => IncrementalResult;
44
+ relex?: (text: string, prev: IncrementalResult, edit: {
45
+ start: number;
46
+ oldEnd: number;
47
+ newEnd: number;
48
+ }) => IncrementalResult;
19
49
  tokenNames: string[];
20
50
  positions: string;
51
+ modeNames: string[];
52
+ };
53
+ /**
54
+ * Opaque state returned by `tokenizeIncremental`/`relex` — pass it back into
55
+ * `relex` unmodified; only `.tokens` is meant to be read directly.
56
+ */
57
+ export type IncrementalResult = {
58
+ text: string;
59
+ tokens: Token[];
60
+ checkpoints: object[];
61
+ eof: object;
21
62
  };
22
63
  /**
23
64
  * A Chevrotain-style lexer. Token definitions are plain JSON, so a whole