3

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?

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • 1
    As far as I can tell, you'd use [`COMMENT`](https://pest.rs/book/grammars/syntax.html#implicit-whitespace) but not make it [silent](https://pest.rs/book/grammars/syntax.html#silent-and-atomic-rules). Haven't actually tried it so not an answer. – mcarton Mar 19 '21 at 14:50
  • @mcarton I never even thought that at... Good thinking my man, make it an answer. That was easy as hell. I read the docs 3 times and never thought syntax. No one better to write a parser, amirite? – Evan Carroll Mar 19 '21 at 14:58

1 Answers1

4

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)* ~ "*/" }
mcarton
  • 27,633
  • 5
  • 85
  • 95