com-lihaoyi / fastparse

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

better negations #78

Closed antonkulaga closed 8 years ago

antonkulaga commented 8 years ago

Right now "!" is the most confusing sign in FastParse as it is used both for negation and for capturing. Moreover !(something) does not provide any output, so to have something very common and trivial, like a parser like "everything but space" I have to do something like this:

val notSpace = (!" ").flatMap(v => AnyChar)  
val stringWithoutSpaces = P( notSpace.rep.! ) 

I would be happy to have less verbose way to do this

antonkulaga commented 8 years ago

It is also not clear for me why

val notSpace1 = P( !(" ") )
val notSpaceRep = P( !(" ").rep )
val str = "zef8498494zefzef"
notSpace1.parse(str) //succedes
notSpaceRep.parse(str) //fails for uknozn reason
lihaoyi commented 8 years ago

What's wrong with

val notSpaceRep = (!" " ~ AnyChar).rep

?

Or

CharPred(_ != ' ').rep

?

You should hang out in the gitter channel for this kind of thing. They're not bugs or issues with fastparse

antonkulaga commented 8 years ago
val notSpaceRep = (!" " ~ AnyChar).rep

@lihaoyi Have you checked it yourself? Each time I run it in ammonite console it freezes it

lihaoyi commented 8 years ago
Welcome to the Ammonite Repl 0.5.5
(Scala 2.11.7 Java 1.8.0_25)
@ import fastparse.all._
import fastparse.all._
@ val notSpaceRep = (!" " ~ AnyChar).rep
notSpaceRep: Parser[Unit] = (!(" ") ~ AnyChar).rep
@ noSpaceRep.parse("zef8498494zefzef")
Main.scala:84: not found: value noSpaceRep
noSpaceRep.parse("zef8498494zefzef")
^
Compilation Failed
@ notSpaceRep.parse("zef8498494zefzef")
res2: Parsed[Unit] = Success((), 16)
@

@antonkulaga I hadn't tried it but I have now and it seems to work

antonkulaga commented 8 years ago

It is strange because on my PC it freezes ammonite.

Anyway CharPred( != ' ').rep worked for me, maybe it is worth of mentioning in docs that CharPred( != ' ') should be recommended approach to parse "everything but ..."

lihaoyi commented 8 years ago

No idea why it's freezing for you, seems to work great for me.

Docs could always be better, that's already tracked in https://github.com/lihaoyi/fastparse/issues/68. Feel free to post your own ad-hoc guidelines and maybe someone will package them nicely into the docs