- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
"Not followed by" parser?
Fri, 2009-10-09, 11:51
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
Fri, 2009-10-09, 14:17
#2
Re: "Not followed by" parser?
Thanks Tony,
On Fri, Oct 9, 2009 at 10:04 PM, Tony Sloane <inkytonik@gmail.com> wrote:
On Fri, Oct 9, 2009 at 10:04 PM, Tony Sloane <inkytonik@gmail.com> wrote:
On 09/10/2009, at 9:51 PM, John Ky wrote:In fact, there is a combinator called "not" in the Scala library. Here's the doc: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.
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
Fri, 2009-10-09, 15:07
#3
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
Fri, 2009-10-09, 15:27
#4
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
Mon, 2009-10-12, 11:37
#5
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@sonic.net> wrote:
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@sonic.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
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