- inspired by macrocosm and inkytonik's blog post
- Can not use if
$
character contains. Does anyone have a solution? :(
-
-
Save lyricallogical/4993548 to your computer and use it in GitHub Desktop.
試してないけどこれでいいのでは
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.reflect.macros.Context | |
import scala.util.matching.Regex | |
import java.util.regex.PatternSyntaxException | |
object Macros{ | |
implicit class RegexContext(val c: StringContext) { | |
def r(): Regex = macro regexImpl | |
} | |
def regexImpl(c: Context)(): c.Expr[Regex] = { | |
import c.universe._ | |
c.prefix.tree match { | |
case Apply(_,List(Apply(_,List(Literal(Constant(str: String)))))) => | |
try str.r catch { | |
case e: PatternSyntaxException => c.abort(c.enclosingPosition, e.toString) | |
} | |
reify(c.literal(str).splice.r) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Macros._ | |
object Main extends App{ | |
println(r"foo|bar") // compile success ! | |
println(r"[foo") // compile error ! | |
println(r"foo$") // error: invalid string interpolation: `$$', `$'ident or `$'BlockExpr expected | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment