/
DirectiveValueParser.scala
183 lines (162 loc) · 5.34 KB
/
DirectiveValueParser.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
180
181
182
183
package scala.build.directives
import com.virtuslab.using_directives.custom.model.{BooleanValue, EmptyValue, StringValue, Value}
import scala.build.Positioned.apply
import scala.build.errors.{
BuildException,
CompositeBuildException,
MalformedDirectiveError,
UsingDirectiveValueNumError,
UsingDirectiveWrongValueTypeError
}
import scala.build.preprocessing.ScopePath
import scala.build.preprocessing.directives.DirectiveUtil
import scala.build.{Position, Positioned}
abstract class DirectiveValueParser[+T] {
def parse(
values: Seq[Value[_]],
scopePath: ScopePath,
path: Either[String, os.Path]
): Either[BuildException, T]
final def map[U](f: T => U): DirectiveValueParser[U] =
new DirectiveValueParser.Mapped[T, U](this, f)
}
object DirectiveValueParser {
private final class Mapped[T, +U](underlying: DirectiveValueParser[T], f: T => U)
extends DirectiveValueParser[U] {
def parse(
values: Seq[Value[_]],
scopePath: ScopePath,
path: Either[String, os.Path]
): Either[BuildException, U] =
underlying.parse(values, scopePath, path).map(f)
}
abstract class DirectiveSingleValueParser[+T] extends DirectiveValueParser[T] {
def parseValue(
value: Value[_],
cwd: ScopePath,
path: Either[String, os.Path]
): Either[BuildException, T]
final def parse(
values: Seq[Value[_]],
scopePath: ScopePath,
path: Either[String, os.Path]
): Either[BuildException, T] =
values.filter(!_.isEmpty) match {
case Seq(value) => parseValue(value, scopePath, path)
case _ =>
Left(
new UsingDirectiveValueNumError(
path,
"",
"1",
values.length
)
)
}
}
given DirectiveValueParser[Unit] = { (values, scopePath, path) =>
values match {
case Seq() => Right(())
case Seq(value, _*) =>
val pos = value.position(path)
Left(new MalformedDirectiveError("Expected no value in directive", Seq(pos)))
}
}
extension (value: Value[_]) {
def isEmpty: Boolean =
value match {
case _: EmptyValue => true
case _ => false
}
def isString: Boolean =
value match {
case _: StringValue => true
case _ => false
}
def asString: Option[String] =
value match {
case s: StringValue => Some(s.get())
case _ => None
}
def isBoolean: Boolean =
value match {
case _: BooleanValue => true
case _ => false
}
def asBoolean: Option[Boolean] =
value match {
case s: BooleanValue => Some(s.get())
case _ => None
}
def position(path: Either[String, os.Path]): Position =
DirectiveUtil.position(value, path, skipQuotes = isString)
}
given DirectiveValueParser[Boolean] = { (values, scopePath, path) =>
values.filter(!_.isEmpty) match {
case Seq() => Right(true)
case Seq(v) =>
v.asBoolean.toRight {
new UsingDirectiveWrongValueTypeError(
path,
"",
Seq("boolean"),
""
)
}
case values0 =>
Left(
new MalformedDirectiveError(
s"Unexpected values ${values0.map(_.toString).mkString(", ")}",
values0.map(_.position(path))
)
)
}
}
given DirectiveSingleValueParser[String] =
(value, scopePath, path) =>
value.asString.toRight {
val pos = value.position(path)
new MalformedDirectiveError(
s"Expected a string, got '${value.getRelatedASTNode.toString}'",
Seq(pos)
)
}.map(DirectiveSpecialSyntax.handlingSpecialPathSyntax(_, path))
final case class MaybeNumericalString(value: String)
given DirectiveSingleValueParser[MaybeNumericalString] =
(value, scopePath, path) =>
value.asString.map(MaybeNumericalString(_)).toRight {
val pos = value.position(path)
new MalformedDirectiveError(
s"Expected a string value, got '${value.getRelatedASTNode.toString}'",
Seq(pos)
)
}
final case class WithScopePath[+T](scopePath: ScopePath, value: T)
object WithScopePath {
def empty[T](value: T): WithScopePath[T] =
WithScopePath(ScopePath(Left("invalid"), os.sub), value)
}
given [T](using underlying: DirectiveValueParser[T]): DirectiveValueParser[WithScopePath[T]] = {
(values, scopePath, path) =>
underlying.parse(values, scopePath, path)
.map(WithScopePath(scopePath, _))
}
given [T](using
underlying: DirectiveSingleValueParser[T]
): DirectiveSingleValueParser[Positioned[T]] = {
(value, scopePath, path) =>
underlying.parseValue(value, scopePath, path)
.map(Positioned(value.position(path), _))
}
given [T](using underlying: DirectiveValueParser[T]): DirectiveValueParser[Option[T]] =
underlying.map(Some(_))
given [T](using underlying: DirectiveSingleValueParser[T]): DirectiveValueParser[List[T]] = {
(values, scopePath, path) =>
val res = values.filter(!_.isEmpty).map(underlying.parseValue(_, scopePath, path))
val errors = res.collect {
case Left(e) => e
}
if (errors.isEmpty) Right(res.collect { case Right(v) => v }.toList)
else Left(CompositeBuildException(errors))
}
}