This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why doesn't Scala's parser combinator use Reader#atEnd ?

No replies
Kota Mizushima
Joined: 2009-09-07,
User offline. Last seen 42 years 45 weeks ago.

Hello. I often use Scala's parser combinator library to implement
DSLs and toy languages.
I would like to ask about the design choice which I'm wondering about.

In Scala, the following code causes infinite loop.

import scala.util.parsing.combinator._
import scala.util.parsing.input._
object InfiniteLoop extends Parsers {
type Elem = Char
val A = elem("", (e) => true).* // intend to represent .* in Regex
def main(args: Array[String]) {
A(new CharSequenceReader("foo"))
}
}

To fix the problem, I need to use CharSequenceReader.EofCh
as end of input such as the following:

import scala.util.parsing.combinator._
import scala.util.parsing.input._
object InfiniteLoop extends Parsers {
type Elem = Char
val A = elem("", (e) => e != CharSequenceReader.EofCh).*
def main(args: Array[String]) {
A(new CharSequenceReader("foo"))
}
}

However, this solution cannot be used if I want to take EofCh as a
normal character.
For example, EofCh in Java's identifier can be simply ignored (see
Character.isIdentifierIgnorable(int codepoint) ).

It seems that if the methods of the parser combinator library check
the result of scala.util.parsing.input.Reader#atEnd : Boolean,
the problem is resolved. However, now Reader#end is not used
for checking EOF. Is it design mistake, or am I missing something ?

Regards,
Kota Mizushima.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland