1

I have the following fastparse parser named "variable":

val alphabet = 'A' to 'z'
val variable: Parser[String] = P(CharsWhileIn(alphabet).!)

I would like for this parser to fail on a specific word like "end", while still returning a Parser[String].

Anton
  • 1,314
  • 1
  • 12
  • 27

1 Answers1

2

Try with negative lookahead:

val alphabet = 'A' to 'Z'
val variable: P[String] = P(!"end" ~ CharIn(alphabet).rep(min = 1)).!

where this will succeed:

println( variable.parse("ABCend") )   // Success(ABC,3)

but this won't:

println( variable.parse("endABC") )   // Failure(!("end"):1:4 ..."ABC")
insan-e
  • 3,883
  • 3
  • 18
  • 43