J-F-Liu / pom

PEG parser combinators using operator overloading without macros.
MIT License
496 stars 30 forks source link

Not operator needs 'static #27

Closed Cokemonkey11 closed 5 years ago

Cokemonkey11 commented 5 years ago

Means if you want to parse a string sourced from IO, you need to use lazy_static. Not very convenient

    lazy_static! {
        pub static ref STDIN_BUF: String = {
            let mut buf = String::new();

            std::io::stdin().lock().read_to_string(&mut buf).expect(
                "Failed to read stdin!"
            );

            buf
        };
    }

    println!(
        "{}",
        serde_yaml::to_string(&my_parser().parse(STDIN_BUF.as_bytes())?)?
    );
J-F-Liu commented 5 years ago

Use pom::parser::Parser instead of pom::Parser, there is lifetime parameter, e.g.

pub fn json<'a>() -> Parser<'a, u8, JsonValue> {
    space() * value() - end()
}
Cokemonkey11 commented 5 years ago

I'm having some problems with seq(&' static str) if I try that.

https://github.com/Cokemonkey11/wurstdoktor/tree/nostatic

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> src\main.rs:92:7
   |
92 |     ).map(|(((d, (e, n)), p), r)| {
   |       ^^^
   |
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 75:13...
  --> src\main.rs:75:13
   |
75 | fn class_fn<'a>() -> Parser<'a, u8, WurstFunction> {
   |             ^^
   = note: ...so that the expression is assignable:
           expected pom::parser::Parser<'a, _, _>
              found pom::parser::Parser<'_, _, _>
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `&[u8]` will meet its required lifetime bounds
  --> src\main.rs:79:13
   |
79 |             !seq(PRIVATE) *
   |             ^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0495`.
error: Could not compile `wurstdoktor`.
J-F-Liu commented 5 years ago

I have no problem when doing this in examples/json.rs

static NULL: &[u8; 4] = b"null";

fn value<'a>() -> Parser<'a, u8, JsonValue> {
    ( seq(NULL).map(|_|JsonValue::Null)
Cokemonkey11 commented 5 years ago

You're right - I think the problem is actually related to ! operator:

@@ -52,7 +52,7 @@ fn object<'a>() -> Parser<'a, u8, HashMap<String, JsonValue>> {
 }

 fn value<'a>() -> Parser<'a, u8, JsonValue> {
-       ( seq(b"null").map(|_|JsonValue::Null)
+       ( !seq(b"*") * seq(b"null").map(|_|JsonValue::Null)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
  --> examples\json.rs:63:4
   |
63 |     ) - space()
   |       ^
   |
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 55:10...
  --> examples\json.rs:55:10
   |
55 | fn value<'a>() -> Parser<'a, u8, JsonValue> {
   |          ^^
   = note: ...so that the expression is assignable:
           expected pom::parser::Parser<'a, _, _>
              found pom::parser::Parser<'_, _, _>
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `&[u8]` will meet its required lifetime bounds
  --> examples\json.rs:56:4
   |
56 |     ( !seq(b"*") * seq(b"null").map(|_|JsonValue::Null)
   |       ^^^^^^^^^^
J-F-Liu commented 5 years ago

Yes, fixed.