-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternativeParser.js
More file actions
129 lines (117 loc) · 3.42 KB
/
AlternativeParser.js
File metadata and controls
129 lines (117 loc) · 3.42 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import Parser from "./Parser.js"
import Reply from "../Reply.js"
import StringParser from "./StringParser.js"
import SuccessParser from "./SuccessParser.js"
/**
* @template {Parser<any>[]} T
* @extends Parser<ParserValue<T>>
*/
export default class AlternativeParser extends Parser {
#backtracking = false
get backtracking() {
return this.#backtracking
}
#parsers
get parsers() {
return this.#parsers
}
/** @param {T} parsers */
constructor(...parsers) {
super()
this.#parsers = parsers
if (this.#parsers.length === 1) {
this.isActualParser = false
}
}
/** @protected */
doMatchesEmpty() {
return this.#parsers.some(p => p.matchesEmpty())
}
/**
* @protected
* @param {Context} context
*/
doStarterList(context, additional = /** @type {Parser<any>[]} */([])) {
return this.#parsers
.flatMap(p => p.starterList(context))
.reduce(
(acc, cur) => acc.some(p => p.equals(context, cur, true)) ? acc : (acc.push(cur), acc),
/** @type {Parser<any>[]} */([])
)
}
unwrap() {
return [...this.#parsers]
}
/**
* @template {Parser<any>[]} T
* @param {T} parsers
*/
wrap(...parsers) {
const result = new AlternativeParser(...parsers)
if (this.#backtracking) {
result.#backtracking = true
}
return result
}
asBacktracking() {
const result = this.wrap(...this.#parsers)
result.#backtracking = true
return result
}
/**
* @param {Context} context
* @param {Number} position
*/
parse(context, position) {
let result
for (let i = 0; i < this.#parsers.length; ++i) {
result = this.#parsers[i].parse(context, position)
if (result.status) {
return result
}
}
return Reply.makeFailure(position)
}
/**
* @protected
* @param {Context} context
* @param {Parser<any>} other
* @param {Boolean} strict
*/
doEquals(context, other, strict) {
if (
!(other instanceof AlternativeParser)
|| this.#parsers.length != other.#parsers.length
|| this.#backtracking !== other.#backtracking
) {
return false
}
for (let i = 0; i < this.#parsers.length; ++i) {
if (!this.#parsers[i].equals(context, other.#parsers[i], strict)) {
return false
}
}
return true
}
/**
* @protected
* @param {Context} context
*/
doToString(context, indent = 0) {
const indentation = Parser.indentation.repeat(indent)
const deeperIndentation = Parser.indentation.repeat(indent + 1)
if (this.#parsers.length === 2 && this.#parsers[1] instanceof SuccessParser) {
let result = this.#parsers[0].toString(context, indent)
if (!(this.#parsers[0] instanceof StringParser) && !context.visited.has(this.#parsers[0])) {
result = "<" + result + ">"
}
result += "?"
return result
}
return "ALT<\n"
+ deeperIndentation + this.#parsers
.map(p => p.toString(context, indent + 1))
.join("\n" + deeperIndentation + "| ")
+ "\n" + indentation + ">"
}
}