-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
49 lines (46 loc) · 1.36 KB
/
types.ts
File metadata and controls
49 lines (46 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
export type MaybeArray<T> = T | T[]
export type ParseData = [
result: MaybeArray<string>,
/** Start index of match */
startIndex: number,
/**
* End index of match, this is useful if your end up parsing more than the tokeniser originally
* provided
*/
stopIndex: number,
/** Any data you wish to forward can be included here for later parsers to use */
data?: { [key in string | symbol]: unknown },
]
export type ParserFunction = (
/** Capture groups are here */
vars: { [key in string]: string },
state: {
/** Index of the token */
index: number
/** Source string */
src: string
/** Length of match */
length: number
/** Index of the last char of match, (equal to index + length) */
lastIndex: number
/** For recursive parsing of tokens */
parseParagraph: (str: string) => string
parseNext: (str: string, start: number) => ParseData
parseIter: (str: string) => IterableIterator<ParseData>
parse: (str: string) => string
},
/**
* If a string is returned, it is transformed into parseData using the index and lastIndex in
* state
*/
) => string | ParseData
export type ParserDef = {
/** Must be a unique name */
name: string
/**
* Regex must contain at least 1 named capture group,. these are parsed to as the vars in the
* ParserFunction
*/
regex: RegExp
handler: ParserFunction
}