rustpegpest

Building a grammar for variables with pest parser


Using pest parser I am trying to build a grammar that can recognize variable names, but I can't get the variables to end at the next space/non-alpha character. I tried using...

var_name = {!reserved ~ ASCII_ALPHA+} which works perfectly for single-letter variables

var_name = {!reserved ~ ASCII_ALPHA+} but this includes a space when I do x := 1, my parser sees var_name as "x ". And even if I was okay with that, it doesn't work for parsing larger expressions while true do { if a < b then b := b - a else a := a - b } => parse error

var_name = {!reserved ~ ASCII_ALPHA | ASCII_ALPHA+} reacts similar to the single character option.

I also tried using 'a'..'z', alphanumeric, and other options as well, but no change. I may be missing something from the book, but I can't seem to find anything that works.

Additional information:

Thanks, any help is appreciated


Solution

  • I figured out the answer.

    var_name = @{!reserved ~ ASCII_ALPHA ~ ASCII_ALPHANUMERIC*}

    From the book: 'Both kinds of atomic rule prevent implicit whitespace: inside an atomic rule, the tilde ~ means "immediately followed by"'

    I had tried this earlier and gotten stuck, it's pretty simple though.

    @ says no whitespace, !reserved keeps reserved words out of variable. ASCII_ALPHA starts the variable with an alphabetical character, then there can be 0 or more alphanumeric characters immediately following it.

    Hope this helps anyone else that got stuck on this.