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
|
package mill.util
import fastparse.all._
import mill.define.{Segment, Segments}
object ParseArgs {
def apply(scriptArgs: Seq[String],
multiSelect: Boolean): Either[String, (List[(Option[Segments], Segments)], Seq[String])] = {
val (selectors, args) = extractSelsAndArgs(scriptArgs, multiSelect)
for {
_ <- validateSelectors(selectors)
expandedSelectors <- EitherOps
.sequence(selectors.map(expandBraces))
.map(_.flatten)
selectors <- EitherOps.sequence(expandedSelectors.map(extractSegments))
} yield (selectors.toList, args)
}
def extractSelsAndArgs(scriptArgs: Seq[String],
multiSelect: Boolean): (Seq[String], Seq[String]) = {
if (multiSelect) {
val dd = scriptArgs.indexOf("--")
val selectors = if (dd == -1) scriptArgs else scriptArgs.take(dd)
val args = if (dd == -1) Seq.empty else scriptArgs.drop(dd + 1)
(selectors, args)
} else {
(scriptArgs.take(1), scriptArgs.drop(1))
}
}
private def validateSelectors(selectors: Seq[String]): Either[String, Unit] = {
if (selectors.isEmpty || selectors.exists(_.isEmpty))
Left("Selector cannot be empty")
else Right(())
}
def expandBraces(selectorString: String): Either[String, List[String]] = {
parseBraceExpansion(selectorString) match {
case f: Parsed.Failure => Left(s"Parsing exception ${f.msg}")
case Parsed.Success(expanded, _) => Right(expanded.toList)
}
}
private sealed trait Fragment
private object Fragment {
case class Keep(value: String) extends Fragment
case class Expand(values: List[List[Fragment]]) extends Fragment
def unfold(fragments: List[Fragment]): Seq[String] = {
fragments match {
case head :: rest =>
val prefixes = head match {
case Keep(v) => Seq(v)
case Expand(Nil) => Seq("{}")
case Expand(List(vs)) => unfold(vs).map("{" + _ + "}")
case Expand(vss) => vss.flatMap(unfold)
}
for {
prefix <- prefixes
suffix <- unfold(rest)
} yield prefix + suffix
case Nil => Seq("")
}
}
}
private object BraceExpansionParser {
val plainChars =
P(CharsWhile(c => c != ',' && c != '{' && c != '}')).!.map(Fragment.Keep)
val toExpand: P[Fragment] =
P("{" ~ braceParser.rep(1).rep(sep = ",") ~ "}").map(
x => Fragment.Expand(x.toList.map(_.toList))
)
val braceParser = P(toExpand | plainChars)
val parser = P(braceParser.rep(1).rep(sep = ",") ~ End)
}
private def parseBraceExpansion(input: String) = {
def unfold(vss: List[Seq[String]]): Seq[String] = {
vss match {
case Nil => Seq("")
case head :: rest =>
for {
str <- head
r <- unfold(rest)
} yield
r match {
case "" => str
case _ => str + "," + r
}
}
}
BraceExpansionParser.parser
.map { vss =>
val stringss = vss.map(x => Fragment.unfold(x.toList)).toList
unfold(stringss)
}
.parse(input)
}
def extractSegments(selectorString: String): Either[String, (Option[Segments], Segments)] =
parseSelector(selectorString) match {
case f: Parsed.Failure => Left(s"Parsing exception ${f.msg}")
case Parsed.Success(selector, _) => Right(selector)
}
private val identChars = ('a' to 'z') ++ ('A' to 'Z') ++ ('0' to '9') ++ Seq('_', '-')
private val ident = P( CharsWhileIn(identChars) ).!
def isLegalIdentifier(identifier: String): Boolean =
(Start ~ ident ~ End).parse(identifier).isInstanceOf[Parsed.Success[_]]
private def parseSelector(input: String) = {
val ident2 = P( CharsWhileIn(identChars ++ ".") ).!
val segment = P( ident ).map( Segment.Label)
val crossSegment = P("[" ~ ident2.rep(1, sep = ",") ~ "]").map(Segment.Cross)
val simpleQuery = P(segment ~ ("." ~ segment | crossSegment).rep).map {
case (h, rest) => Segments(h :: rest.toList:_*)
}
val query = P( simpleQuery ~ ("/" ~/ simpleQuery).?).map{
case (q, None) => (None, q)
case (q, Some(q2)) => (Some(q), q2)
}
query.parse(input)
}
}
|