Scala: parser help

I am learning to write a simple parser-combinator. I write the rules from the bottom up and write the block tests to check how I am going. However, I am blocked when using repsep () with a space as a separator.

object MyParser extends RegexParsers {
  lazy val listVal:Parser[List[String]]=elem('{')<~repsep("""\d+""".r,"""\s+""".r)~>elem('}')
}

This rule has been simplified to illustrate the problem. When I feed the parser "{1 2 3}", it always complains that it does not match:

[1.4] Error: `} 'is expected, but 2 found

I am wondering how to write a rule correctly, as I described?

thank

+3
source share
1 answer

, RegexParsers - . , . (), , - (\s+), val whiteSpace: Regex = ... RegexParsers. - , override def skipWhitespace = false.

: , , :

repsep("""\d+""".r,"""\s+""".r)

:

rep("""\d+""".r)

, RegexParsers , , .

, repsep , , , , , ( AST).

+6

Source: https://habr.com/ru/post/1766688/


All Articles