-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapturingGroupParser.js
More file actions
65 lines (56 loc) · 1.33 KB
/
CapturingGroupParser.js
File metadata and controls
65 lines (56 loc) · 1.33 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import Parser from "./Parser.js"
/**
* @template {Parser<any>} T
* @extends Parser<ParserValue<T>>
*/
export default class CapturingGroupParser extends Parser {
#parser
#id
get id() {
return this.#id
}
/**
* @param {T} parser
* @param {String | Symbol} id
*/
constructor(parser, id) {
super()
this.#parser = parser
this.#id = id
}
unwrap() {
return [this.#parser]
}
/**
* @template {Parser<any>[]} T
* @param {T} parsers
*/
wrap(...parsers) {
return new CapturingGroupParser(parsers[0], this.#id)
}
/**
* @param {Context} context
* @param {Number} position
*/
parse(context, position) {
return this.#parser.parse(context, position)
}
/**
* @protected
* @param {Context} context
* @param {Parser<any>} other
* @param {Boolean} strict
*/
doEquals(context, other, strict) {
return other instanceof CapturingGroupParser
&& this.#id == other.#id
&& this.#parser.equals(context, other.#parser, strict)
}
/**
* @protected
* @param {Context} context
*/
doToString(context, indent = 0) {
return "(" + (this.#id !== "" ? `?<${this.#id}>` : "") + this.#parser.toString(context, indent) + ")"
}
}