com-lihaoyi / fastparse

Writing Fast Parsers Fast in Scala
https://com-lihaoyi.github.io/fastparse
MIT License
1.09k stars 164 forks source link

CharIn whitespace problem #57

Closed ihji closed 8 years ago

ihji commented 8 years ago

I found that overriding WhitespaceApi also affects CharIn's behavior. Is this a bug?

import fastparse.WhitespaceApi
import fastparse.noApi._

val White = WhitespaceApi.Wrapper{
  import fastparse.all._
  NoTrace(" ".rep)
}
import White._

val x = P("abc" ~ CharIn("def").rep.!)
x.parse("abcdd d")

// result: res0: fastparse.core.Result[String] = Success(dd d,7)
ihji commented 8 years ago

repX can solve this problem. I found it while I read the source code. Thanks!

lihaoyi commented 8 years ago

Yeah, would be nice to document it =D This is more to do with .rep than with CharIn, since anything with .rep will exhibit this behavior.

FWIW, CharIn("def").rep could also be rewritten as CharsWhile("def".contains(_)), which is equivalent but a lot faster and doesn't suffer the effects of .rep