Rust/pest/'silent' <-- what does it mean?

Quoting: Syntax of pest parsers - A thoughtful introduction to the pest parser

Silent rules are just like normal rules — when run, they function the same way — except they do not produce pairs or tokens. If a rule is silent, it will never appear in a parse result.

To make a silent rule, precede the left curly bracket { with a low line (underscore) _.


silent = _{ ... }

Now, this does not make sense with regard to actual examples. For example: book/json.pest at master · pest-parser/book · GitHub

shows

value = _{ object | array | string | number | boolean | null }

yet we definitely match on it later on:

So what's going on here?

What exactly does 'silent', in the context of _{ ... }, mean ?

silent excludes the item from the generate syntax tree(kind of flattens the tree branches). If you remove _, pest returns extra tree level and the code in the example would look like this (not-compilable but you get the idea):

match pair.as_rule() {
      Rule::Value => JSONValue::Object(
               match pair.into_inner() {
                 // here goes the big `match` from L51 to L79
               }
      }
 }

Using _ allows you to skip check for the virtual Rule::Value(a compound type) and move to the real value

2 Likes