This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserUtils.scala
179 lines (169 loc) · 5.14 KB
/
ParserUtils.scala
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package fmv1992.scala_cli_parser.conf
import scala.util.Failure
import scala.util.Success
object ParserUtils {
def or[A, B](
p1: ParserWithTry[A, B],
p2: ParserWithTry[A, B]
): ParserWithTry[A, B] = {
ParserWithTryImpl((x: A) => p1.parse(x).orElse(p2.parse(x)).get)
}
def or[A <: Seq[_], B](
p1: ParserPartial[A, B],
p2: ParserPartial[A, B]
): ParserPartial[A, B] = {
ParserPartialImpl((a: A) => {
val (rest1, parsed1) = p1.partialParse(a)
if (rest1 == a) {
p2.partialParse(a)
} else {
(rest1, parsed1)
}
})
}
def and[A <: Seq[_], B](
p1: ParserPartial[A, B],
p2: ParserPartial[A, B]
)(implicit combiner: (B, B) => B): ParserPartial[A, B] = {
ParserPartialImpl((a: A) => {
val (rest1, parsed1) = p1.partialParse(a)
if (rest1 == a) {
throw new ParseException(a.toString)
} else {
val (rest2, parsed2) = p2.partialParse(rest1)
(rest2, combiner(parsed1, parsed2))
}
})
}
// def and[A, B, C, D](
// p1: ParserWithTry[Seq[A], B],
// p2: ParserWithTry[Seq[A], C],
// combiner: (B, C) => D
// ): ParserWithTry[Seq[A], D] = {
// // Tries to find the longest valid sequence for `p1`.
// ParserImpl(
// (x: Seq[A]) => {
// val parsedOpt1 = p1.getValidSubSequence(x)
// parsedOpt1 match {
// case Some(x1) => {
// val inputRemaining = x.drop(x1.length)
// val parsedOpt2 = p2.getValidSubSequence(inputRemaining)
// parsedOpt2 match {
// case Some(x2) if inputRemaining.drop(x2.length).length == 0 =>
// combiner(p1.transform(x1), p2.transform(x2))
// case Some(_) =>
// throw ParseException.fromInput(inputRemaining, p2)
// case None =>
// throw ParseException.fromInput(inputRemaining, p2)
// }
// }
// case None => throw ParseException.fromInput(x, p1)
// }
// }
// )
// }
//
// def tryAll[A, B](
// input: ParserWithTry[Seq[A], B]*
// ): ParserWithTry[Seq[A], Seq[B]] = {
// val parserSet = input.toSet
// def go(
// currentSegment: Seq[A],
// remainingSegment: Seq[A],
// validParsers: Set[ParserWithTry[Seq[A], B]],
// acc: Seq[B] = Seq.empty
// ): Seq[B] = {
// if (remainingSegment.isEmpty) {
// val correctedAcc: Seq[B] = if (currentSegment.isEmpty) {
// acc
// } else {
// acc.appended(
// (validParsers.head
// .parse(currentSegment)
// .getOrElse(throw new Exception()): B)
// )
// }
// if (correctedAcc.isEmpty) {
// Seq.empty
// } else {
// correctedAcc
// }
// } else {
// val newCurrentSegment = currentSegment.appended(remainingSegment.head)
// val newValidParsers =
// validParsers.filter(_.parse(newCurrentSegment).isRight)
// if (newValidParsers.size > 0) {
// go(newCurrentSegment, remainingSegment.tail, newValidParsers, acc)
// } else {
// go(
// Seq.empty,
// remainingSegment,
// parserSet,
// validParsers.head.parse(currentSegment) match {
// case Left(_) => acc
// case Right(r) => acc.appended(r)
// }
// )
// }
// }
// }
// ParserImpl((x: Seq[A]) => {
// go(Seq.empty, x, parserSet)
// })
// }
//
// // ???: I believe there is a lot of redundancy in this method.
def many[A <: Seq[_], B, C](
p: ParserPartial[A, B]
)(implicit
mapper: Seq[B] => Seq[C],
combiner: (C, C) => C
): ParserPartial[A, C] = {
def go(input: A, acc: Seq[B]): (A, Seq[B]) = {
if (input.isEmpty) {
(input, acc)
} else {
val (rest, res) = p.partialParse(input)
// If no progress.
if (rest == input) {
(input, acc)
} else {
go(rest, acc.appended(res))
}
}
}
ParserPartialImpl((x: A) => {
val (rest, acc) = (go(x, Seq.empty): (A, Seq[B]))
if (rest.isEmpty) {
(rest, mapper(acc).reduce(combiner))
} else {
// ???: This is undefined at the time being. How should this fail?
throw new ParseException(s"Undefined case in `many`: '${rest}'.")
}
})
}
def newLines: PP =
ParserPartialImpl((x: Seq[Char]) => {
val leadingNewLines = x.takeWhile(_ == '\n')
val rest = x.drop(leadingNewLines.length)
require(x == leadingNewLines ++ rest)
if (leadingNewLines.isEmpty) {
(x, Failure(ParseException(x.mkString)))
} else {
(rest, Success(ParsedResult(leadingNewLines, emptyMapSS)))
}
})
def fullConfigParser =
ParserUtils.many(
ParserUtils.or(
newLines,
ParserUtils.or(
CommentConfParser,
ParserUtils.or(
SolidLineConfParser,
ParserUtils.or(MultiLineConfParser, SpaceConfParser)
)
)
)
)(MapperFullConfigParser, CombinerFullConfigParser)
}