parsingrustpegpest

Using Pest.rs, is there a way to store comments as tokens?


Pest.rs gives us a method of doing comments,

COMMENT - runs between rules and sub-rules

But if we're building a linter we may want the comments. Is there a way to have them saved on the tree?


Solution

  • You'd use COMMENT but not make it silent.

    For example this will handle C-like comments but they won't appear in the output (what most people want most of the time):

    COMMENT = _{ "/*" ~ (!"*/" ~ ANY)* ~ "*/" }
    

    While this will make them present in the output:

    COMMENT = { "/*" ~ (!"*/" ~ ANY)* ~ "*/" }