Skip to content

Instantly share code, notes, and snippets.

@lihaoyi
Created May 5, 2014 06:57
Show Gist options
  • Save lihaoyi/dc63796497638d56fc2e to your computer and use it in GitHub Desktop.
Save lihaoyi/dc63796497638d56fc2e to your computer and use it in GitHub Desktop.
--- a/spray-util/src/main/scala/spray/util/pimps/PimpedRegex.scala
+++ b/spray-util/src/main/scala/spray/util/pimps/PimpedRegex.scala
@@ -20,12 +20,9 @@ import java.util.regex.Pattern
import scala.util.matching.Regex
class PimpedRegex(regex: Regex) {
def groupCount = {
try {
- val field = classOf[Pattern].getDeclaredField("capturingGroupCount")
- field.setAccessible(true)
- field.getInt(regex.pattern) - 1
+ regex.pattern.matcher("").groupCount()
} catch {
case t: Throwable ⇒
throw new RuntimeException("Could not determine regex group count: " + regex.pattern.pattern
@lihaoyi
Copy link
Author

lihaoyi commented Dec 2, 2014

case class StringLiteral(str: String, holes: Seq[Hole], start: Int)
case class Hole(tree: Tree, start: Int, end: Int)

class QuasiquoteParser(val input: String, holes: Seq[Hole]) extends Parser{
  val holeMap = holes.map(h => h.start -> h).toMap
  def MaybeHole = 
    if (!holeMap.contains(cursor)) rule( MISMATHCH ) 
    else rule( ANY.times(holeMap(cursor).end - holeMap(cursor).start ~ push(holeMap(cursor)) 

  ... use MaybeHole elsewhere in the grammar anywhere a hole could possibly appear ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment