0
votes

I've just started using Scala a few days ago and wanted to write my first parser.

the problematic code is as follows:

  val withoutZero: Parser[List[String]] = ("1" | "2" | "3" | "4").+
  val withZero: Parser[String] = "0" | withoutZero

the number string I'd like to parse can have multiple zeros and other numbers but I want to define two functions for it.

the combined withZero has to stay String (not List[String] or whatever else). at the moment I'm only parsing one zero because ("0").+ didn't work out.

my question: how am I able to adjust the withZero Parser to multiple zeros AND combining it with the withoutZero Parser still staying a Parser[String]

1
Can you give some examples of grammatical strings? It's not entirely clear to me from your description. - Travis Brown
it should parse: 0+ | (1|2|3|4)+ - 12dollar
The stuff on either side of the | has to be the same type, so I still don't understand—it guess I'd still like to see actual strings. - Travis Brown

1 Answers

1
votes

I think this should work for 0+ | (1|2|3|4)+:

val withZero: Parser[String] = (literal("0").+ | withoutZero) ^^ (_.mkString)

It joins the list of Strings from some parser into a single String