kevinmehall/rust-peg

How to escape the "

Closed this issue · 3 comments

Started using new version, but got compilation errors:

        string -> NodeElement
        = """ s:$([^"]*) """ { string_node(s) }
        / "'" s:$([^']*) "'" { string_node(s) }

error:

   --> src/fazic/parser.rs:133:12
    |
133 |         / "'" s:$([^']*) "'" { string_node(s) }
    |            ^^^^^^^^^^
    |
help: if you meant to write a `str` literal, use double quotes
    |
133 |         / ""\" s:$([^"]*) "'" { string_node(s) }
    |            ~~~~~~~~~~~

error: mismatched closing delimiter: `]`
   --> src/fazic/parser.rs:4:41
    |
4   | peg::parser!{grammar somename() for str {
    |                                         ^ unclosed delimiter
...
132 |         = """ s:$([^"]*) """ { string_node(s) }
    |                      ^ mismatched closing delimiter

error: mismatched closing delimiter: `)`
   --> src/fazic/parser.rs:4:13
    |
4   | peg::parser!{grammar somename() for str {
    |             ^ unclosed delimiter
...
132 |         = """ s:$([^"]*) """ { string_node(s) }
    |                        ^ mismatched closing delimiter

error: unexpected closing delimiter: `]`
   --> src/fazic/parser.rs:133:22
    |
132 |         = """ s:$([^"]*) """ { string_node(s) }
    |                      - - missing open `(` for this delimiter
    |                      |
    |                      missing open `[` for this delimiter
133 |         / "'" s:$([^']*) "'" { string_node(s) }
    |                      ^ unexpected closing delimiter

error: could not compile `fazic` (bin "fazic") due to 4 previous errors

Just guessing it's about """, but tried to escape it like """ with no luck.

What I'm doing wrong?

I think what you want is

rule string() -> NodeElement
        = "\"" s:$([^'"']*) "\"" { string_node(s) }
        / "'" s:$([^'\'']*) "'" { string_node(s) }
  • String literal expressions follow the same escaping as Rust string literals
  • Inside of [], you are writing a Rust pattern, just like in the arm of a match. It is matching on a char for a parser over str, therefore you want a single quoted char pattern, which follows the same escaping as in Rust.

Hey, one more thing. I spent second night on it ;)
Here is updated version:
https://github.com/fazic/fazic_mono/blob/update_deps/src/fazic/parser.rs
vs rustpeg:
https://github.com/fazic/fazic_mono/blob/master/src/fazic/parser.rustpeg

They are not doing the same:
for example PRINT 2+2 not working :(

I think is something about expression() rule. Rewrite from #infix.

Thanks in advance

In precedence!, the @ or (@) itself represents the recursive expression. It seems like the term() calls don't belong next to it in each case, but instead as an atom case at the end. e.g.

        rule expression() -> NodeElement = precedence! {
            l:(@) _ ("AND" / "and") _ r:@ { node("and", vec![l, r]) }
            // ... skipped ...
            l:@ _ "^" _ r:(@) { node("pow", vec![l, r]) }
            --
            t:term() { t }
        }

You could also call float, integer, string etc directly at the bottom there as atom cases if you don't need to have term as its own rule to use elsewhere.