2

I'd like to write a PEG that matches filesystem paths. A path element is any character except / in posix linux.

There is an expression in PEG to match any character, but I cannot figure out how to match any character except one.

The peg parser I'm using is PEST for rust.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
marathon
  • 7,881
  • 17
  • 74
  • 137

1 Answers1

5

You could find the PEST syntax in https://docs.rs/pest/0.4.1/pest/macro.grammar.html#syntax, in particular there is a "negative lookahead"

!a — matches if a doesn't match without making progress

So you could write

!["/"] ~ any

Example:

// cargo-deps: pest

#[macro_use] extern crate pest;
use pest::*;

fn main() {
    impl_rdp! {
        grammar! {
            path = @{ soi ~ (["/"] ~ component)+ ~ eoi }
            component = @{ (!["/"] ~ any)+ }
        }
    }

    println!("should be true: {}", Rdp::new(StringInput::new("/bcc/cc/v")).path());
    println!("should be false: {}", Rdp::new(StringInput::new("/bcc/cc//v")).path());
}
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005