jsinger67 / parol

LL(k) and LALR(1) parser generator for Rust

Home Page:https://jsinger67.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Comment handling

dalance opened this issue · comments

Hi, this is very nice crate!

I want to use this to create the formatter of a language.
Is there any recommendation to handle comment?

I understand comment can be inserted to parser definition like below:

Expression: [Comment] Factor [Comment] { Op [Comment] Multiplication [Comment] }

But this is very complicated.
If the adjacent comment tokens are automatically captured to terminal tokens like below, it may be usefull.

pub struct Token<'t> {
    pub token_type: TerminalIndex,
    pub location: Location,
    pub comments: Vec<Token<'t>>,
    /* private fields */
}

Hi @dalance
If you want to handle comments in your grammar processing and not just skip them, there is currently no support from parol.
You have to formulate your grammar to include comments and to tie them to the correct syntactical element.
I used the same method in parol_ls language server which also provides formatting support.
I know this is cumbersome but it provides you the exact association between comments and grammar elements with AST types nicely generated similar to the one you mentioned above.

Thank you for your suggestion.
I'll try to add comment to my grammar definition.

Yeah 👍
There is no general assumption one can make about where the comments belong to. To the element below? To the element left of the comment?
Maybe one can define some tricky extension in the parol grammar to support this in a future version.
I think it's worth thinking about it because it might be a problem many users face somehow (including me 😉).