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

fix: Handle newlines and comments in the middle of expressions #217

Merged
merged 2 commits into from
May 24, 2023

Conversation

susliko
Copy link
Collaborator

@susliko susliko commented May 22, 2023

Resolves #188, #199

Problem:

Code like

if true ()

else ()

or

a
 .b
 // comment
 .c

is not parsed correctly, for extra $._automated_semicolon tokens,
which are produced after newlines

Solution:

Tweak scanner.c to stop producing AUTOMATED_SEMICOLON tokens when
encountering comments, else/catch clauses, etc.

Extra changes:

corpus/expressions.txt formatted with tree-sitter test -u in the first commit

Note:

The following code still won't be parsed

a
  /* comment */ .b

as I haven't figured out a way both to parse it and code like

val a = 1
/* comment */ val b = 2

where AUTOMATED_SEMICOLON is needed to distinguish consecutive blocks

@susliko susliko marked this pull request as draft May 23, 2023 07:59
@susliko susliko force-pushed the less-automated-semicolons branch 5 times, most recently from b224541 to 60243c0 Compare May 23, 2023 22:31
Problem:
-------
Code like
```scala
if true ()

else ()
```
or
```scala
a
 .b
 // comment
 .c
```
is not parsed correctly, for extra `$._automated_semicolon` tokens,
which are produced after newlines

Solution:
-------
Tweak `scanner.c` to stop producing AUTOMATED_SEMICOLON tokens when
encountering comments, else/catch clauses, etc.
@susliko susliko marked this pull request as ready for review May 23, 2023 22:36
@susliko
Copy link
Collaborator Author

susliko commented May 23, 2023

@eed3si9n this one is ready for review
It would be great if you could take a look as you have an experience with the external parser


lexer->mark_end(lexer);
lexer->result_symbol = AUTOMATIC_SEMICOLON;

if (newline_count > 1) return true;
// Probably, a multi-line field expression, e.g.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this comment here.

Copy link
Collaborator

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM

@eed3si9n eed3si9n merged commit cd514fd into tree-sitter:master May 24, 2023
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

Successfully merging this pull request may close these issues.

Incorrect parse for if_expression with extra line between alternative
2 participants