javadba javadba - 3 months ago 14
Scala Question

Running a parser within parboiled2

The docs for parboiled2 mention the following to get results:

https://github.com/sirthias/parboiled2#access-to-parser-results

val parser = new MyParser(input)
val result = parser.rootRule.run()


However I get a compilation error when attemping what seems to that approach:

Here is the outline of the parser:

case class CsvParser(input: ParserInput, delimiter: String = ",") extends Parser {
..
def file = zeroOrMore(line) ~ EOI
}


The code to attempt to run it

val in = new StringBasedParserInput(readFile(fname))
val p = new CsvParser(in)
println(p.toString)
p.file.run


But the "run" is not accepted:


Error:(81, 12) too few argument lists for macro invocation
p.file.run



^

Answer

Looks like that the problem inside the following line:

case class CsvParser(input: ParserInput, delimiter: String = ",") 

and it can be fixed by explicitly declaring parserInput as a val

case class CsvParser(val input: ParserInput, delimiter: String = ",")