-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegexStringContext.scala
78 lines (58 loc) · 2.09 KB
/
RegexStringContext.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
import scala.language.experimental.macros
import java.util.regex.{Pattern => JRegex}
import scala.reflect.macros.Context
import scala.util.matching.Regex
object RegexStringContext {
implicit class StringContextExt(sc: StringContext) {
object r {
def apply(argExprs: String*): Regex = macro r_apply_impl
def unapplySeq(targetExpr: String): Option[Seq[String]] = macro r_unapplySeq_impl
}
}
def r_apply_impl(c: Context)(argExprs: c.Expr[String]*): c.Expr[Regex] = {
import c.universe._
checkRegexSyntax(c, extractParts(c).mkString("X"))
def mkSeqTree(xs: Seq[c.universe.Tree]): c.universe.Tree = {
// TODO: Improve this somehow!
Apply(TypeApply(Select(Select(Select(Ident(nme.ROOTPKG), newTermName("scala")), newTermName("collection")), newTermName("Seq")), List(Ident(newTypeName("String")))), xs.toList)
}
val scExpr = c.Expr[StringContext](extractStringContext(c))
val argListExpr = c.Expr[Seq[String]](mkSeqTree(argExprs.map(_.tree)))
reify {
new Regex(scExpr.splice.raw(argListExpr.splice.map(JRegex.quote): _*))
}
}
def r_unapplySeq_impl(c: Context)(targetExpr: c.Expr[String]): c.Expr[Option[Seq[String]]] = {
import c.universe._
val regex = extractParts(c).mkString("(.*)")
checkRegexSyntax(c, regex)
val regexExpr = c.Expr[String](Literal(Constant(regex)))
reify {
regexExpr.splice.r.unapplySeq(targetExpr.splice)
}
}
private def extractStringContext(c: Context): c.universe.Tree = {
import c.universe._
c.prefix.tree match {
case Select(Apply(_, List(scTree)), _) => scTree
}
}
private def extractParts(c: Context): Seq[String] = {
import c.universe._
c.prefix.tree match {
case Select(Apply(_, List(Apply(_, xs))), _) => xs map {
case Literal(Constant(x: String)) => x
}
}
}
private def checkRegexSyntax(c: Context, re: String) {
try {
JRegex.compile(re)
}
catch {
case e: java.util.regex.PatternSyntaxException => {
c.abort(c.enclosingPosition, f"Syntax error in regex: ${e.getMessage}")
}
}
}
}