Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for comments #103

Open
cmtt opened this issue Nov 28, 2018 · 5 comments
Open

Support for comments #103

cmtt opened this issue Nov 28, 2018 · 5 comments

Comments

@cmtt
Copy link
Member

cmtt commented Nov 28, 2018

As follow-up for #99, this issue is about adding optional support for comment nodes.

As @maciejhirsz suggested, comments should be gathered by the lexer as internal list.
This way, f.e. all comments of a function can be pulled before the declaration.

Please note that there are three types of valid comments in JavaScript:

  • Line comments // foo
  • Block comments /* bar */
  • HTML comments <!-- baz --> (:angry:)
@matklad
Copy link

matklad commented Nov 29, 2018

If you want to support not only comments, but white space as well, and in general support loseless syntax trees, I suggest taking a look at Swift’s libsyntax architecture:
https://github.com/apple/swift/tree/master/lib/Syntax.

This might not be what you want though: such full fidelity syntax trees are heavier than ASTs.

@maciejhirsz
Copy link
Member

Oh this is clever! I like the idea of trivia being attached to nodes, I wouldn't necessarily do it on every node (although, given how fast Ratel is, it might not be that much of a burden), but doing it on things like functions should be sufficient. It would require some re-parsing of comments, but then any tool that wants to read documentation or JSDoc types from comments is likely going to have to parse those in full anyway.

@matklad
Copy link

matklad commented Nov 29, 2018

BTW, I have a crate with implenets a similar flavor of syntax trees here: https://github.com/rust-analyzer/rowan

It's main focus is loselessness, and not perf, so it has a ton of indirection and allocation.

@TitanNano
Copy link

I use ratel to do some JavaScript type analysis and I'm going to need comments, so I can parse JSDoc annotations. Is this something you are interested to implement in the near future?

@maciejhirsz
Copy link
Member

@TitanNano my plan right now (which is just in my head) is for Logos to expose trivia as just a string of bytes between the previous and current token, and have that string attached to few nodes where comments might be useful (functions, methods, variable declarations), so you could extract JSDoc comments out of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants