How to instantiate lexical.Scanner in a JavaTokenParsers class?

376 views Asked by At

I am writing a parser which inherits from JavaTokenParsers in that I have a function as follow:

 import scala.util.parsing.combinator.lexical._
 import scala.util.parsing._
 import scala.util.parsing.combinator.RegexParsers;
 import scala.util.parsing.combinator.syntactical.StdTokenParsers
 import scala.util.parsing.combinator.token.StdTokens
 import scala.util.parsing.combinator.lexical.StdLexical
 import scala.util.parsing.combinator.lexical.Scanners
 import scala.util.parsing.combinator.lexical.Lexical
 import scala.util.parsing.input._
 import scala.util.parsing.combinator.syntactical._
 import scala.util.parsing.combinator.token
 import scala.util.parsing.combinator._

 class ParseExp extends JavaTokenParsers{
  //some code for parsing 
  def parse(s:String) = {
     val tokens = new lexical.Scanner(s)
     phrase(expr)(tokens)
   }
}

I am getting the following error :

type Scanner is not a member of package scala.util.parsing.combinator.lexical
[error]         val tokens = new lexical.Scanner(s)
[error]                                  ^

Why I have this error while I have imported all packages?

1

There are 1 answers

0
Alexis C. On BEST ANSWER

The JavaTokenParsers does not implement the Scanners trait. So you would need to extends also from this trait (or a trait that extends it) in order to have access to this class.

Unless your expr parser accepts the Reader as a parameter (not from its apply method), you'd need to override the type of elements and the input type if I'm not mistaken to make this working.

Also is there any reason you need to have a Reader[Token]?.

If you don't need a Reader[Token]and since you give your input in a plain string,

phrase(expr)(new CharSequenceReader(s))

should work.