Skip to content

Commit 752ae03

Browse files
author
Sara Alemanno
committed
Update Macros_1.scala
Remove code duplication
1 parent 1e6ce2a commit 752ae03

File tree

1 file changed

+47
-93
lines changed

1 file changed

+47
-93
lines changed

tests/run-macros/f-interpolator-neg/Macros_1.scala

Lines changed: 47 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -16,105 +16,59 @@ object Macro {
1616
def fooErrors(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]]) given (reflect: Reflection): Expr[List[(Boolean, Int, Int, Int, String)]] = {
1717
(strCtxExpr, argsExpr) match {
1818
case ('{ StringContext(${ExprSeq(parts)}: _*) }, ExprSeq(args)) =>
19-
val errors = List.newBuilder[Expr[(Boolean, Int, Int, Int, String)]]
20-
// true if error, false if warning
21-
// 0 if part, 1 if arg, 2 if strCtx, 3 if args
22-
// index in the list if arg or part, -1 otherwise
23-
// offset, 0 if strCtx, args or arg
24-
// message as given
25-
val reporter = new dotty.internal.StringContextMacro.Reporter{
26-
private[this] var reported = false
27-
private[this] var oldReported = false
28-
def partError(message : String, index : Int, offset : Int) : Unit = {
29-
reported = true
30-
errors += '{ Tuple5(true, 0, $index, $offset, $message) }
31-
}
32-
def partWarning(message : String, index : Int, offset : Int) : Unit = {
33-
reported = true
34-
errors += '{ Tuple5(false, 0, $index, $offset, $message) }
35-
}
36-
37-
def argError(message : String, index : Int) : Unit = {
38-
reported = true
39-
errors += '{ Tuple5(true, 1, $index, 0, $message) }
40-
}
41-
42-
def strCtxError(message : String) : Unit = {
43-
reported = true
44-
errors += '{ Tuple5(true, 2, -1, 0, $message) }
45-
}
46-
def argsError(message : String) : Unit = {
47-
reported = true
48-
errors += '{ Tuple5(true, 3, -1, 0, $message) }
49-
}
50-
51-
def hasReported() : Boolean = {
52-
reported
53-
}
54-
55-
def resetReported() : Unit = {
56-
oldReported = reported
57-
reported = false
58-
}
59-
60-
def restoreReported() : Unit = {
61-
reported = oldReported
62-
}
63-
}
64-
val partsExpr = dotty.internal.StringContextMacro.getPartsExprs(strCtxExpr)
65-
val args = dotty.internal.StringContextMacro.getArgsExprs(argsExpr)
66-
dotty.internal.StringContextMacro.interpolate(partsExpr, args, argsExpr, reporter) // Discard result
67-
errors.result().toExprOfList
19+
fooErrorsImpl(parts, args, argsExpr)
6820
case ('{ new StringContext(${ExprSeq(parts)}: _*) }, ExprSeq(args)) =>
69-
val errors = List.newBuilder[Expr[(Boolean, Int, Int, Int, String)]]
70-
// true if error, false if warning
71-
// 0 if part, 1 if arg, 2 if strCtx, 3 if args
72-
// index in the list if arg or part, -1 otherwise
73-
// offset, 0 if strCtx, args or arg
74-
// message as given
75-
val reporter = new dotty.internal.StringContextMacro.Reporter{
76-
private[this] var reported = false
77-
private[this] var oldReported = false
78-
def partError(message : String, index : Int, offset : Int) : Unit = {
79-
reported = true
80-
errors += '{ Tuple5(true, 0, $index, $offset, $message) }
81-
}
82-
def partWarning(message : String, index : Int, offset : Int) : Unit = {
83-
reported = true
84-
errors += '{ Tuple5(false, 0, $index, $offset, $message) }
85-
}
21+
fooErrorsImpl(parts, args, argsExpr)
22+
}
23+
}
24+
25+
def fooErrorsImpl(parts : Seq[Expr[String]], args: Seq[Expr[Any]], argsExpr: Expr[Seq[Any]]) given (reflect: Reflection)= {
26+
val errors = List.newBuilder[Expr[(Boolean, Int, Int, Int, String)]]
27+
// true if error, false if warning
28+
// 0 if part, 1 if arg, 2 if strCtx, 3 if args
29+
// index in the list if arg or part, -1 otherwise
30+
// offset, 0 if strCtx, args or arg
31+
// message as given
32+
val reporter = new dotty.internal.StringContextMacro.Reporter{
33+
private[this] var reported = false
34+
private[this] var oldReported = false
35+
def partError(message : String, index : Int, offset : Int) : Unit = {
36+
reported = true
37+
errors += '{ Tuple5(true, 0, $index, $offset, $message) }
38+
}
39+
def partWarning(message : String, index : Int, offset : Int) : Unit = {
40+
reported = true
41+
errors += '{ Tuple5(false, 0, $index, $offset, $message) }
42+
}
8643

87-
def argError(message : String, index : Int) : Unit = {
88-
reported = true
89-
errors += '{ Tuple5(true, 1, $index, 0, $message) }
90-
}
44+
def argError(message : String, index : Int) : Unit = {
45+
reported = true
46+
errors += '{ Tuple5(true, 1, $index, 0, $message) }
47+
}
9148

92-
def strCtxError(message : String) : Unit = {
93-
reported = true
94-
errors += '{ Tuple5(true, 2, -1, 0, $message) }
95-
}
96-
def argsError(message : String) : Unit = {
97-
reported = true
98-
errors += '{ Tuple5(true, 3, -1, 0, $message) }
99-
}
49+
def strCtxError(message : String) : Unit = {
50+
reported = true
51+
errors += '{ Tuple5(true, 2, -1, 0, $message) }
52+
}
53+
def argsError(message : String) : Unit = {
54+
reported = true
55+
errors += '{ Tuple5(true, 3, -1, 0, $message) }
56+
}
10057

101-
def hasReported() : Boolean = {
102-
reported
103-
}
58+
def hasReported() : Boolean = {
59+
reported
60+
}
10461

105-
def resetReported() : Unit = {
106-
oldReported = reported
107-
reported = false
108-
}
62+
def resetReported() : Unit = {
63+
oldReported = reported
64+
reported = false
65+
}
10966

110-
def restoreReported() : Unit = {
111-
reported = oldReported
112-
}
113-
}
114-
val partsExpr = dotty.internal.StringContextMacro.getPartsExprs(strCtxExpr)
115-
val args = dotty.internal.StringContextMacro.getArgsExprs(argsExpr)
116-
dotty.internal.StringContextMacro.interpolate(partsExpr, args, argsExpr, reporter) // Discard result
117-
errors.result().toExprOfList
67+
def restoreReported() : Unit = {
68+
reported = oldReported
69+
}
11870
}
71+
dotty.internal.StringContextMacro.interpolate(parts.toList, args.toList, argsExpr, reporter) // Discard result
72+
errors.result().toExprOfList
11973
}
12074
}

0 commit comments

Comments
 (0)