0
votes

For this input in a file: {right; left; straight} I have this parser:

def conf: Parser[Any] = "{"  ~> feat <~ "}"
def feat: Parser[Any] = repsep(expr, ";") 
def expr = """[a-zA-Z]([a-zA-Z0-9]|[a-zA-Z0-9])*""".r

which is giving to me this output: List(right, left, straight)

Now I need these elements, in this list, to iterate through them but I can't because when I am trying to use the for() loop it is saying that "value filter is not a member of com.xxx.ParseResult[Any]". How I can iterate and take the values from this parsed List??

1

1 Answers

1
votes

The : Parser[Any] type ascriptions make the result of your parsing Any, rather than the List[String] you need for the rest of your code. If you eliminate them, the compiler will infer the correct type of Parser[List[String]] on its own:

def conf = "{"  ~> feat <~ "}"
def feat = repsep(expr, ";") 
def expr = """[a-zA-Z]([a-zA-Z0-9]|[a-zA-Z0-9])*""".r

The second issue is that you are trying to use the ParseResult directly. You need to invoke .getOrElse on it to either get the List[String] contained inside or provide an alternative if parsing failed:

val ls = parseResult.getOrElse(throw new Exception("parsing failed"))