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

Add support for line comments of the form // #243

Closed
wants to merge 14 commits into from
Closed

Conversation

dxu
Copy link
Contributor

@dxu dxu commented Mar 30, 2016

This adds support for line comments of the form // comment.

  1. It updates the lexer to recognize //, by maintaining a single_line_comment bool indicating whether we're currently parsing a single line comment. this differentiation prevents cases like // */ let x = 10 from executing, so that the */ does not get parsed as an end of comment. It also maintains a separate line_comment_start_loc to keep track of the line comment locations to separate the logic for checking for unmatched comments, so that newlines and nested */ comment endings can be differentiated. It also updates the pretty printer to handle these cases accordingly.
  2. There is an edge case with regards to interleaved comments having to maintain their "multiline" status.

For example, in the wrappingTest.re cases in facebook/Reason,

/*A*/
let named
    /* a::a */
    a::a
    /* b::b */
    b::b =>
  /* a + b */
  a + b;

/*B*/
let namedAlias
    /* a::aa */
    a::aa
    /* b::bb */
    b::bb =>
  /* aa + bb */
  aa + bb;

must be compiled into

//A
let named /* a::a */ a::a /* b::b */ b::b => /* a + b */ a + b;

//B
let namedAlias
    /* a::aa */
    a::aa
    /* b::bb */
    b::bb => /* aa + bb */ aa + bb;

as opposed to

//A
let named // a::a  a::a // b::b  b::b => // a + b  a + b;

//B
let namedAlias
    // a::aa 
    a::aa
    // b::bb 
    b::bb => // aa + bb  aa + bb;

to fix this, I check if the listConfig every defines its break attribute to be Never or IfNeed, it will force a multiline comment.

  1. As per the discussion on facebook/ReasonSyntax#41, there is also the edge case of nested inline comments within a line comment. To handle this, there is a new error within the lexer, Unterminated_nested_comments that get thrown when a nested comment is detected to not have been matched.

There are a few open questions that aren't answered by this pull request, both of which can be found in greater detail in the discussion on facebook/reasonsyntax#41:

  1. How should we handle line breaking? Should it be handled through easy_format (which is causing the current edge cases)?
  2. Converting from ML <-> reason, you could potentially run into problems with nested comments - for example, if you had strings of either /* or (* converting from one to the other, it could potentially cause issues.

@dxu dxu force-pushed the comments branch 2 times, most recently from 6a7b8ed to cbf37c2 Compare April 12, 2016 23:58
@dxu dxu changed the title add tests for single line comments Add support for line comments of the form // Apr 13, 2016
@dxu
Copy link
Contributor Author

dxu commented Apr 13, 2016

This is ready for review, sorry for the delay.

@dxu
Copy link
Contributor Author

dxu commented Apr 13, 2016

There is currently a failing test on master, i think a result of the docstring issue #308 . This PR does not commit the new expected output of all the tests for fear that broken tests get changed accidentally.

@dxu dxu force-pushed the comments branch 2 times, most recently from 9adc474 to d1a2070 Compare April 13, 2016 01:45
@jordwalke
Copy link
Member

I realized that failing test is blocking other tests. I'll update the test.sh to make sure it doesn't block other tests from running. And I'll make sure the file name clearly indicates it's intended to be failing (I wanted it as a constant reminder for us to fix it :) )

@jordwalke
Copy link
Member

Btw: I think this diff provides a really cool feature, and we should merge it in, but these two other comment-related issues are blocking launch:

#313
#308

I propose we merge this diff in after those other ones are handled.

@dxu
Copy link
Contributor Author

dxu commented Apr 13, 2016

No problem, this can wait. I'm still looking at #308 right now, it's been giving me a bit of trouble.

@dxu dxu force-pushed the comments branch 2 times, most recently from 1a0d7c7 to 9ee9901 Compare April 17, 2016 01:46
@dxu
Copy link
Contributor Author

dxu commented Apr 17, 2016

I've updated this review with the new test output, since a lot of comments were converted into single line comments

@dxu
Copy link
Contributor Author

dxu commented Apr 24, 2016

This rebase is failing a few tests due to an issue with empty line comments of the form //<newline>, I'm fixing it now and cleaning up this review.

I was thinking about extending the current lexer token COMMENT structure to include a boolean indicating whether or not it's a line comment, so that we can differentiate when printing it - if the developer specified comments of the form /* */ it would be kept in that form, if they used // then they would change it to that. My impression is that the only changes that would be necessary is updating the token definition in parser.mly and the creation in .mll, as well as the places we match on it in lexer.mll, toolchain.ml, and refmt_impl.ml. We'd probably also have to extend the Ocaml_lexer's comments() to return comments of the new form as well.

Is this something that would be useful/is there a better way to address this?

@dxu dxu force-pushed the comments branch 3 times, most recently from 3e562cd to a6ee21d Compare April 24, 2016 23:23
@dxu
Copy link
Contributor Author

dxu commented Apr 24, 2016

This is ready for review. I cleaned up the code a bit, and also added the extra boolean to the COMMENT definition, so I reverted all the tests back to the originals, since they're now just kept as multiline comments. I kept the commit history in case we decide this isn't intended behavior.

@jordwalke
Copy link
Member

Cool, I'll take a look tonight! I greatly simplified the comment interleaving code (will submit a diff) but it looks like the rebase conflicts won't be too bad with this one.

@jordwalke
Copy link
Member

Sorry for the delay, I'll take a look as soon as I land the other launch blocking comment diff (Yunxing can feel free to accept/comment).

@dxu
Copy link
Contributor Author

dxu commented Apr 25, 2016

No worries, take your time!

@jordwalke
Copy link
Member

I'll be getting back to this. I think we should open source with support for these.

@bobzhang
Copy link
Contributor

bobzhang commented May 3, 2016

This is nice to have

@dxu
Copy link
Contributor Author

dxu commented May 7, 2016

rebased, but i noticed that there are a few ml tests that are failing, also on master.

Failed tests:
./mlVariants.ml -- typecheck_test
./mlVariants.ml -- unit_test
./mlVariants.ml -- idempotent_test

@bslatkin
Copy link

Did this ever land? This page says that Reason supports line comments in the form // blah blah, but in practice I can't get them to work. /* blah blah */ comments work fine.

@dxu
Copy link
Contributor Author

dxu commented Jun 27, 2016

This unfortunately was never landed, and I think not fully reviewed. There was another issue regarding the comment interleaving logic introduced by another commit that is referenced above, which was causing issues at the time (#302). Due to some other responsibilities that cropped up in my personal life, I haven't gotten a chance to look at or address either in a while, so I'm not whether or not it's been fixed.

Also, this hasn't been rebased (as is apparent from the garbled history), but this PR also became bugged when the repo changed its name from facebook/Reason to facebook/reason (it caused my previous repo, dxu/Reason, to no longer be recognized as a fork), so there's a slightly more up-to-date fork at https://github.com/dxu/reason/tree/comments. It still isn't rebased with the most recent commits, but

Sorry for the delay! @yunxing, let me know if you wanted to take this over, otherwise I might not be able to get to this until a few weeks from now.

This pull request should also be closed and replaced with one from a repository that is detected as a fork.

@yunxing
Copy link
Contributor

yunxing commented Jun 27, 2016

@dxu Thanks for the update. I will take over this diff as soon as I get a cycle next Thursday.

@ghost ghost added the CLA Signed label Jul 12, 2016
@yunxing yunxing self-assigned this Aug 1, 2016
@ghost ghost added the CLA Signed label Aug 1, 2016
@jaredly
Copy link
Contributor

jaredly commented Sep 2, 2016

bump! This would be super nice

@yunxing
Copy link
Contributor

yunxing commented Sep 2, 2016

We have the necessary support in our comment interleaving logic now. Should be pretty easy, gimme some more days.

@tekknolagi
Copy link
Contributor

@yunxing any update on this?

@dxu dxu unassigned yunxing Apr 24, 2017
@dxu dxu closed this Jan 10, 2018
@chenglou
Copy link
Member

Nooooooooo

@tekknolagi
Copy link
Contributor

Does this work now?

@chenglou
Copy link
Member

Nope

@jordwalke
Copy link
Member

cc @let-def :D

@jordwalke
Copy link
Member

jordwalke commented Feb 13, 2018

:D
#1800

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

Successfully merging this pull request may close these issues.