This page is no longer maintained — Please continue to the home page at www.scala-lang.org

"Not followed by" parser?

5 replies
John Ky
Joined: 2009-10-06,
User offline. Last seen 42 years 45 weeks ago.

Hi,

I'm looking for a "not followed by" parser or some way to write one.  It only succeeds if the parser given to it as an argument fails, but either way, it consumes no input.

Cheers,

-John

Tony Sloane
Joined: 2009-01-07,
User offline. Last seen 2 years 32 weeks ago.
Re: "Not followed by" parser?
On 09/10/2009, at 9:51 PM, John Ky wrote:

I'm looking for a "not followed by" parser or some way to write one.  It only succeeds if the parser given to it as an argument fails, but either way, it consumes no input.

In fact, there is a combinator called "not" in the Scala library.  Here's the doc:
not [T](p : => Parser[T]) : Parser[Unit]
Wrap a parser so that its failures & errors become success and vice versa -- it never consumes any input.
cheers,Tony
John Ky
Joined: 2009-10-06,
User offline. Last seen 42 years 45 weeks ago.
Re: "Not followed by" parser?
Thanks Tony,

On Fri, Oct 9, 2009 at 10:04 PM, Tony Sloane <inkytonik [at] gmail [dot] com> wrote:
On 09/10/2009, at 9:51 PM, John Ky wrote:

I'm looking for a "not followed by" parser or some way to write one.  It only succeeds if the parser given to it as an argument fails, but either way, it consumes no input.

In fact, there is a combinator called "not" in the Scala library.  Here's the doc:
not [T](p : => Parser[T]) : Parser[Unit]
Wrap a parser so that its failures & errors become success and vice versa -- it never consumes any input.
cheers,Tony

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: "Not followed by" parser?

On Friday October 9 2009, Tony Sloane wrote:
> On 09/10/2009, at 9:51 PM, John Ky wrote:
> > I'm looking for a "not followed by" parser or some way to write
> > one. It only succeeds if the parser given to it as an argument
> > fails, but either way, it consumes no input.
>
> In fact, there is a combinator called "not" in the Scala library.
> Here's the doc:
>
> not [T](p : => Parser[T]) : Parser[Unit]
>
> Wrap a parser so that its failures & errors become success and vice
> versa -- it never consumes any input.
>
> cheers,
> Tony

You can use it as a non-consuming, look-ahead guard, too, by
double-negating.

Randall Schulz

David Biesack
Joined: 2008-11-18,
User offline. Last seen 2 years 38 weeks ago.
Re: "Not followed by" parser?

> From: Randall R Schulz
> Date: Fri, 9 Oct 2009 06:53:55 -0700
>
> On Friday October 9 2009, Tony Sloane wrote:
> >
> > In fact, there is a combinator called "not" in the Scala library.
> > Here's the doc:
> >
> > not [T](p : => Parser[T]) : Parser[Unit]
>
> You can use it as a non-consuming, look-ahead guard, too, by
> double-negating.

Looks like 2.8 adds an explicit guard combinator for this,

http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/scala/util/parsing/combinator/Parsers.html#guard%28%3D%3EParsers.this.Parser[T]%29

but nice to know one can get there via a workaround in 2.7

> Randall Schulz

John Ky
Joined: 2009-10-06,
User offline. Last seen 42 years 45 weeks ago.
Re: "Not followed by" parser?
Hi Randall,

Thank you so much.  You've answered my question before I even asked it.

As it turned out, the way I had intended to write the parser code with 'not' didn't work and what I really needed was the double not:

def identifier =
    ( lower
    ~ ( ( letter
        | digit
        | hyphen <~ not(not(letter | digit))
        ).*
      ).?
    )

Cheers,

-John

On Sat, Oct 10, 2009 at 12:53 AM, Randall R Schulz <rschulz [at] sonic [dot] net> wrote:
On Friday October 9 2009, Tony Sloane wrote:
> On 09/10/2009, at 9:51 PM, John Ky wrote:
> > I'm looking for a "not followed by" parser or some way to write
> > one.  It only succeeds if the parser given to it as an argument
> > fails, but either way, it consumes no input.
>
> In fact, there is a combinator called "not" in the Scala library.
> Here's the doc:
>
> not [T](p : => Parser[T]) : Parser[Unit]
>
> Wrap a parser so that its failures & errors become success and vice
> versa -- it never consumes any input.
>
> cheers,
> Tony

You can use it as a non-consuming, look-ahead guard, too, by
double-negating.


Randall Schulz

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland