Question

a try to add non-backtracking ~> and <~ to my parser based on RegexParsers.

RegexParsers implement implicit def literal(s: String): Parser[String] witch transphorm implicitly a String to a Parser[String]

now i add :

`object implicits{

       implicit class helper[T](parser  : Parser[T])  { 
           def ~>! [U](q: => Parser[U]): Parser[U] = parser ~! q ^^ {case a ~ b => b} named ("~>!") 
           def <~! [U](q: => Parser[U]): Parser[T] = parser ~! q ^^ {case a ~ b => a} named ("<~!") 
       }  
}

Witch is my implicit class.

But i seen a strange thing

def groupe : Parser[Group] =  "group(" ~>! identifier <~!  ")"

identifier is a Parser

Here i got an error when the string is pass in class parameter "group(" ~>! identifier because the implicit is not made and the compiler look for ~>! inside String.

but when it's passed in the method parameter identifier <~! ")" the String->Parser[String] implicit work.

Is it a Scala bug or i just miss something?

Was it helpful?

Solution

It isn't a bug. The problem is that to resolve ~>! on String the compiler would have to chain two implicit conversions: from String to Parser and from Parser to helper, which isn't allowed. So you need to add a direct conversion from String to helper:

implicit def stringToHelper(s: String): helper[String] = new helper(literal(s))

and also from Regex to helper if necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top