-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnchorParser.js
More file actions
77 lines (67 loc) · 1.62 KB
/
AnchorParser.js
File metadata and controls
77 lines (67 loc) · 1.62 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
66
67
68
69
70
71
72
73
74
75
76
77
import Parser from "./Parser.js"
import Reply from "../Reply.js"
/**
* @readonly
* @enum {String}
*/
export const AnchorType = {
LINE_START: "^",
LINE_END: "$",
WORD_BOUNDARY: "\\b",
}
/**
* @template {AnchorType} T
* @extends {Parser<"">}
*/
export default class AnchorParser extends Parser {
static isTerminal = true
#type
/** @param {T} type */
constructor(type) {
super()
this.#type = type
}
/** @protected */
doMatchesEmpty() {
return true
}
/**
* @protected
* @param {Context} context
*/
doStarterList(context, additional = /** @type {Parser<any>[]} */([])) {
return [this]
}
/**
* @param {Context} context
* @param {Number} position
*/
parse(context, position) {
let status = false
switch (this.#type) {
case AnchorType.LINE_START:
status = position === 0 || context.input[position - 1] === "\n"
break
case AnchorType.LINE_END:
status = position === context.input.length || context.input[position] === "\n"
break
}
return status ? Reply.makeSuccess(position, "") : Reply.makeFailure(position)
}
/**
* @protected
* @param {Context} context
* @param {Parser<any>} other
* @param {Boolean} strict
*/
doEquals(context, other, strict) {
return other instanceof AnchorParser && this.#type === other.#type
}
/**
* @protected
* @param {Context} context
*/
doToString(context, indent = 0) {
return this.#type
}
}