Last active
September 11, 2019 08:36
Revisions
-
xuwei-k revised this gist
Feb 20, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,2 +1,2 @@ - inspired by [macrocosm](https://github.com/retronym/macrocosm/blob/521dfc0bcd1/src/main/scala/com/github/retronym/macrocosm/Macrocosm.scala#L102-L128) and [inkytonik's blog post](http://hootenannylas.blogspot.com.au/2013/02/syntax-checking-in-scala-string.html) - Can not use if `$` character contains. Does anyone have a solution? :( -
xuwei-k created this gist
Feb 20, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ 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) } val Apply(fun, _) = reify(new Regex("")).tree c.Expr[Regex](Apply.apply(fun, c.literal(str).tree :: Nil)) } } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ 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 } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ - inspired [macrocosm](https://github.com/retronym/macrocosm/blob/521dfc0bcd1/src/main/scala/com/github/retronym/macrocosm/Macrocosm.scala#L102-L128) and [inkytonik's blog post](http://hootenannylas.blogspot.com.au/2013/02/syntax-checking-in-scala-string.html) - Can not use if `$` character contains. Does anyone have a solution? :(