@@ -7,6 +7,7 @@ import scala.build.errors.{
7
7
BuildException ,
8
8
CompositeBuildException ,
9
9
MalformedDirectiveError ,
10
+ ToolkitDirectiveMissingVersionError ,
10
11
UsingDirectiveValueNumError ,
11
12
UsingDirectiveWrongValueTypeError
12
13
}
@@ -16,6 +17,7 @@ import scala.build.{Position, Positioned}
16
17
17
18
abstract class DirectiveValueParser [+ T ] {
18
19
def parse (
20
+ key : String ,
19
21
values : Seq [Value [_]],
20
22
scopePath : ScopePath ,
21
23
path : Either [String , os.Path ]
@@ -30,40 +32,46 @@ object DirectiveValueParser {
30
32
private final class Mapped [T , + U ](underlying : DirectiveValueParser [T ], f : T => U )
31
33
extends DirectiveValueParser [U ] {
32
34
def parse (
35
+ key : String ,
33
36
values : Seq [Value [_]],
34
37
scopePath : ScopePath ,
35
38
path : Either [String , os.Path ]
36
39
): Either [BuildException , U ] =
37
- underlying.parse(values, scopePath, path).map(f)
40
+ underlying.parse(key, values, scopePath, path).map(f)
38
41
}
39
42
40
43
abstract class DirectiveSingleValueParser [+ T ] extends DirectiveValueParser [T ] {
41
44
def parseValue (
45
+ key : String ,
42
46
value : Value [_],
43
47
cwd : ScopePath ,
44
48
path : Either [String , os.Path ]
45
49
): Either [BuildException , T ]
46
50
47
51
final def parse (
52
+ key : String ,
48
53
values : Seq [Value [_]],
49
54
scopePath : ScopePath ,
50
55
path : Either [String , os.Path ]
51
56
): Either [BuildException , T ] =
52
- values.filter(! _.isEmpty) match {
53
- case Seq (value) => parseValue(value, scopePath, path)
54
- case _ =>
57
+ values match {
58
+ case Seq (value) if ! value.isEmpty => parseValue(key, value, scopePath, path)
59
+ case Seq (value) if value.isEmpty && (key == " toolkit" || key == " test.toolkit" ) =>
60
+ // FIXME: handle similar parsing errors in the directive declaration instead of hacks like this one
61
+ Left (ToolkitDirectiveMissingVersionError (maybePath = path, key = key))
62
+ case resultValues @ _ =>
55
63
Left (
56
64
new UsingDirectiveValueNumError (
57
- path,
58
- " " ,
59
- " 1 " ,
60
- values.length
65
+ maybePath = path,
66
+ key = key ,
67
+ expectedValueNum = 1 ,
68
+ providedValueNum = resultValues.count( ! _.isEmpty)
61
69
)
62
70
)
63
71
}
64
72
}
65
73
66
- given DirectiveValueParser [Unit ] = { (values, scopePath, path) =>
74
+ given DirectiveValueParser [Unit ] = { (key, values, scopePath, path) =>
67
75
values match {
68
76
case Seq () => Right (())
69
77
case Seq (value, _* ) =>
@@ -105,16 +113,16 @@ object DirectiveValueParser {
105
113
DirectiveUtil .position(value, path, skipQuotes = isString)
106
114
}
107
115
108
- given DirectiveValueParser [Boolean ] = { (values, scopePath, path) =>
116
+ given DirectiveValueParser [Boolean ] = { (key, values, scopePath, path) =>
109
117
values.filter(! _.isEmpty) match {
110
118
case Seq () => Right (true )
111
119
case Seq (v) =>
112
120
v.asBoolean.toRight {
113
121
new UsingDirectiveWrongValueTypeError (
114
- path,
115
- " " ,
116
- Seq (" boolean" ),
117
- " "
122
+ maybePath = path,
123
+ key = key ,
124
+ expectedTypes = Seq (" boolean" ),
125
+ hint = " "
118
126
)
119
127
}
120
128
case values0 =>
@@ -128,23 +136,26 @@ object DirectiveValueParser {
128
136
}
129
137
130
138
given DirectiveSingleValueParser [String ] =
131
- (value, scopePath, path) =>
139
+ (key, value, scopePath, path) =>
132
140
value.asString.toRight {
133
141
val pos = value.position(path)
134
142
new MalformedDirectiveError (
135
- s " Expected a string, got ' ${value.getRelatedASTNode.toString}' " ,
136
- Seq (pos)
143
+ message =
144
+ s """ Encountered an error for the $key using directive.
145
+ |Expected a string, got ' ${value.getRelatedASTNode.toString}' """ .stripMargin,
146
+ positions = Seq (pos)
137
147
)
138
148
}.map(DirectiveSpecialSyntax .handlingSpecialPathSyntax(_, path))
139
149
140
150
final case class MaybeNumericalString (value : String )
141
151
142
152
given DirectiveSingleValueParser [MaybeNumericalString ] =
143
- (value, scopePath, path) =>
153
+ (key, value, scopePath, path) =>
144
154
value.asString.map(MaybeNumericalString (_)).toRight {
145
155
val pos = value.position(path)
146
156
new MalformedDirectiveError (
147
- s " Expected a string value, got ' ${value.getRelatedASTNode.toString}' " ,
157
+ s """ Encountered an error for the $key using directive.
158
+ |Expected a string value, got ' ${value.getRelatedASTNode.toString}' """ .stripMargin,
148
159
Seq (pos)
149
160
)
150
161
}
@@ -157,22 +168,22 @@ object DirectiveValueParser {
157
168
}
158
169
159
170
given [T ](using underlying : DirectiveValueParser [T ]): DirectiveValueParser [WithScopePath [T ]] = {
160
- (values, scopePath, path) =>
161
- underlying.parse(values, scopePath, path)
171
+ (key, values, scopePath, path) =>
172
+ underlying.parse(key, values, scopePath, path)
162
173
.map(WithScopePath (scopePath, _))
163
174
}
164
175
given [T ](using
165
176
underlying : DirectiveSingleValueParser [T ]
166
177
): DirectiveSingleValueParser [Positioned [T ]] = {
167
- (value, scopePath, path) =>
168
- underlying.parseValue(value, scopePath, path)
178
+ (key, value, scopePath, path) =>
179
+ underlying.parseValue(key, value, scopePath, path)
169
180
.map(Positioned (value.position(path), _))
170
181
}
171
182
given [T ](using underlying : DirectiveValueParser [T ]): DirectiveValueParser [Option [T ]] =
172
183
underlying.map(Some (_))
173
184
given [T ](using underlying : DirectiveSingleValueParser [T ]): DirectiveValueParser [List [T ]] = {
174
- (values, scopePath, path) =>
175
- val res = values.filter(! _.isEmpty).map(underlying.parseValue(_, scopePath, path))
185
+ (key, values, scopePath, path) =>
186
+ val res = values.filter(! _.isEmpty).map(underlying.parseValue(key, _, scopePath, path))
176
187
val errors = res.collect {
177
188
case Left (e) => e
178
189
}
0 commit comments