-
-
Save eed3si9n/4992129 to your computer and use it in GitHub Desktop.
Revisions
-
eed3si9n revised this gist
Feb 20, 2013 . 3 changed files with 10 additions and 13 deletions.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,17 +1,16 @@ import scala.reflect.macros.Context import scala.util.matching.Regex import java.util.regex.PatternSyntaxException object Macros { implicit class RegexContext(val c: String) { def regex(): Regex = macro regexImpl } def regexImpl(c: Context)(): c.Expr[Regex] = { import c.universe._ c.prefix.tree match { case Apply(Select(_, _), List(Literal(Constant(str: String)))) => try{ str.r }catch{ @@ -21,5 +20,4 @@ object Macros{ 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 @@ -1,7 +1,7 @@ import Macros._ object Main extends App{ println("foo|bar".regex) // compile success ! println("[foo".regex) // compile error ! println("foo$".regex) } 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 @@ - 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) -
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? :(