-
Notifications
You must be signed in to change notification settings - Fork 25
Parse comments #106
base: master
Are you sure you want to change the base?
Parse comments #106
Conversation
Hi @pablohirafuji, wow, thanks! I'll review this, hopefully this week. I'd also like to validate this approach with folks:
Perhaps there are more insights to be gained. Thanks so much for the work you've done here! ❤️ |
|> keep myParser | ||
|> ignore spaces |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe I've seen keep / skip
naming somewhere, which mighit be nicer because the two functions have names of the same length? WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it is nicer, but I think it is harder to distinguish each one, eg.:
|> P.keep moduleType
|> P.skip spacesCommentsAndGreaterIndent
|> P.keep moduleName
|> P.skip spacesCommentsAndGreaterIndent
|> P.skip (P.keyword (P.Token "exposing" ExpectingExposingKeyword))
|> P.skip spacesCommentsAndGreaterIndent
|> P.keep exposingList
With the ignore
:
|> P.keep moduleType
|> P.ignore spacesCommentsAndGreaterIndent
|> P.keep moduleName
|> P.ignore spacesCommentsAndGreaterIndent
|> P.ignore (P.keyword (P.Token "exposing" ExpectingExposingKeyword))
|> P.ignore spacesCommentsAndGreaterIndent
|> P.keep exposingList
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, got it. The same length might actually be a disadvantage, not an advantage. Cool to see it on specific code snippets.
One of the feedback I've received is that Re other approaches: I'm still researching. elm-formatJudging from the source code, the way Take look at eg. declarations: https://github.com/avh4/elm-format/blob/master/parser/src/AST/Declaration.hs#L14-L31 Or Exprs: elm-format https://github.com/avh4/elm-format/blob/master/parser/src/AST/Expression.hs#L33 where many of the constructors contain the I'm not entirely sure this is the approach to pursue, although it would remove a need we'd probably have with the current approach of this PR ( Anyways, as said, I'm still researching and asking around. |
Another option might possibly be having Also I found this article: https://github.com/terrajobst/minsk/blob/master/docs/episode-24.md |
I tried this approach first. It produces code like this: P.succeed
(\moduleType_ comments1 moduleName_ comments2 comments3 exposing_ ->
{ content = ( moduleType_, moduleName_, exposing_ )
, comments = comments1 ++ comments2 ++ comments3
}
)
|= moduleType
|= spacesCommentsAndGreaterIndent
|= moduleName
|= spacesCommentsAndGreaterIndent
|. (P.keyword (P.Token "exposing" ExpectingExposingKeyword))
|= spacesCommentsAndGreaterIndent
|= exposingList In contrast, the P.succeed
(\moduleType_ moduleName_ exposing_ ->
( moduleType_, moduleName_, exposing_ )
)
|> P.keep moduleType
|> P.ignore spacesCommentsAndGreaterIndent
|> P.keep moduleName
|> P.ignore spacesCommentsAndGreaterIndent
|> P.ignore (P.keyword (P.Token "exposing" ExpectingExposingKeyword))
|> P.ignore spacesCommentsAndGreaterIndent
|> P.keep exposingList My thoughts were:
Interesting. Only the Frontend AST would have to look like this? What happens to the comments on the other stages?
I don't think this solves or facilitate anything. What happens with the comments between the |
I'll have to think about this a bit. I've been doing something similar before in the Weighing the pros and cons, also the goals of "being a learning resource" etc, I'm leaning towards not having our own parser library forks at the moment.
Yes, IMHO canonical + typed AST can throw all that away and be "pure". If we're after the "desugar" / lowering stage, we're free to tweak the AST as we see fit.
Yes it's unlikely that anything would come out of this. ❌ Re "comments on the side" vs "comments in every type",perhaps we should check how would working with each look like (after the parsing). Some real-ish tasks one might want to do with the AST and the comments, eg. for |
I agree. I love elm-format, it is very reliable. I thought no one would do such approach (didn't even consider it so much), but makes sense. Can I implement it? |
@pablohirafuji Could you try? 🙏 |
No worries, I am striving for the best code the package can have . I'm idealist, not pragmatist. That's why I love Elm so much 😄 |
We'll likely need to remember all whitespace "inside" expressions One thing that I've learned from today's discussions that I probably should disseminate here (apparently it was a lesson learned the hard way) is that expressions shouldn't eagerly eat whitespace that "doesn't belong to them". fn {- blabla -} x y
(YES) ^^ span of `Var fn`
(YES) ^ span of `Var x`
(YES) ^ span of `Var y`
^^^^^^^^^^^^^^^^^^^^^^ span of `FunctionCall`
fn {- blabla -} x y
(NOT) ^^^^^^^^^^^^^^^^ span of `Var fn`
(NOT) ^^^^^ span of `Var x`
(NOT) ^^^^^ span of `Var y` So... perhaps that will come in handy :) |
In essence, the parser is ready. The PR is missing some adjustments, like:
|
I'm storing the comments before any declaration with the declaration itself ( |
Woot woot! I'll take a look at this relatively-soonish 🙂 Thanks @pablohirafuji ! |
Add comment parsing and storing. This was the best strategy I could think of, but if you have any ideas (it would be nice to keep the
(|.)
and(|=)
infixes), let me know.