lark-parser / lark

Lark is a parsing toolkit for Python, built with a focus on ergonomics, performance and modularity.
MIT License
4.75k stars 401 forks source link

Comment Parsing Support #1289

Closed ShreyW closed 1 year ago

ShreyW commented 1 year ago

I have written an extensive grammar for SQL and use the transformer to format SQL code. I had two issues with which I need help:

1) Used the %ignore directive for matching and ignoring comments, so when I reconstruct the AST, I lose info on comments. Is there a way to incorporate that into the AST that is generated so that when I reconstruct the AST, I can put the comments alongside the text maybe via using some callback_function or developing custom Tokens. 2) This is only a slight issue but after transforming the entire tree and reconstructing it, is there a way to fix slight indentation issues present?

I'll be happy to share snippets from my code when I'm completely done

erezsh commented 1 year ago

Sorry, missed the question.

  1. Yes - https://lark-parser.readthedocs.io/en/stable/recipes.html#collect-all-comments-with-lexer-callbacks
  2. That's a bit more complicated. Easiest solution is use an SQL linter.