I have written a parser as follows:
class LogParser extends JavaTokenParsers {
def invertedIndex: Parser[Array[Array[(Int, Int)]]] = {
num ~> num ~> num ~> rep(postingsList) ^^ {
_.toArray
}
}
def postingsList: Parser[Array[(Int, Int)]] = {
num ~> rep(entry) ^^ {
_.toArray
}
}
def entry = {
num ~ "," ~ num ^^ {
case docID ~ "," ~ count => (docID.toInt, count.toInt)
}
}
def num = wholeNumber ^^ (_.toInt)
}
If I parse from a (270MB) file with a FileReader as follows:
val index = parseAll(invertedIndex, new FileReader("path/to/file")).get
I get an Exception in thread "main" java.lang.StackOverflowError
(I have also tried wrapping in a BufferedReader
) but I can fix it by first reading the file into a String like so:
val input = io.Source.fromFile("path/to/file")
val str = input.mkString
input.close()
val index = parseAll(invertedIndex, str).get
Why is this the case? Is there any way to avoid reading it as a String first, it seems a waste?
scala -J-Xss16M
) – LogwayPagedSeq
bug, so you're probably best off just reading it into a string. – Logway