Skip to content

Optional braces, part 2 #62

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

Merged
merged 13 commits into from
Jan 3, 2023
Merged

Conversation

eed3si9n
Copy link
Collaborator

@eed3si9n eed3si9n commented Dec 15, 2022

Ref #43

Problem

Currently Scala 3 optional braces syntax does not work.
Specifically, in this PR I want to address decls/defns not properly grouped under the right owner in part 1 (#61).

Solution

  1. This brings in indent/outdent impl by @keynmol
  2. Introduce _indentable_expression.
    This allows us to limit where indent/outdent shows up without
    confusing tree-sitter too much.
    Internally I've made a change to track last_indentation_size in the payload to indicate last
    outdent (default: -1). If it's not -1 that means there was an outdent.
    This then checks if the last_indentation_size qualified for another outdent.
  3. Add support for control strctures:
    • if expression
    • try-catch expression
    • match expression
    • while expression
    • for expression

The test cases for these are:

=======================================
Top-level Definitions (Scala 3 syntax)
=======================================

class A:
  def a() =
    ()

def a() = 1

---

(compilation_unit
  (class_definition
    (identifier)
    (template_body
      (function_definition
        (identifier)
        (parameters)
        (indented_block
          (unit)))))
  (function_definition
    (identifier)
    (parameters)
    (integer_literal)))
  =======================================
  Value declarations (Scala 3 syntax)
  =======================================
  
  class A:
-   val b: String
+   val b, c : Int
+   val d : String

Demo

Here's a demonstration of before and after using Sonokai. The theme is a bit too color happy, but it's good for checking what changed:

Before #61

sonokai_main_before

Note: def run is not recognized as a method.

After this PR

sonokai_main_after2

def run is highlighted as a method. The body of def dealiasBaseDirectory improved as well, since val is now acceptable without { ... }.

Problem
-------
Currently Scala 3 optional braces syntax does not work.

Solution
--------
This implements initial attempt on dealing with colon
instead of braces.
Note that this does not implement any indent/outdent tracking
so the second `val` onwards will not be added in `template_body`
but it should improve the highlighting a bit.
Problem
-------
Currently CI is failing on windows-latest.

Solution
--------
Downgrade to windows-2019 so node-gyp can be installed.
Problem
-------
Currently Scala 3 optional braces syntax does not work.
Specifically, in this PR I want to address decls/defns not
properly grouped under the right owner in part 1.

Solution
--------
1. This brings in indent/outdent impl by keynmol
2. Introduce `_indentable_expression`.
   This allows us to limit where indent/outdent shows up without
   confusing tree-sitter too much. To start, val/def/var defn
   and if-expressions are given indentable expression.
@eed3si9n eed3si9n force-pushed the wip/braces2 branch 3 times, most recently from 95269aa to d1287dd Compare December 20, 2022 16:10
Problem
-------
Given something like
    class A:
      def a() =
        ()

    def a() = 1
the scanner only outdents once, so it fails to parse the above.

Solution
--------
Track `last_indentation_size` in the payload to indicate last
outdent (default: -1). If it's not -1 that means there was an outdent.
This then checks if the last_indentation_size qualified for another
outdent.
Problem
-------
Given something like
  class A:
    def l: Int =
      1

    def m = ()
the scanner doesn't inject automatic semicolon since the newline
has been consumed by outdenting, and it fails to parse the code.

Solution
--------
Track `last_newline_count` in the payload, which represents the
newline_count at the time out outdent.
This then is recovered so automatic semicolon can use it
only if the column position hasn't moved.
Copy link
Collaborator

@ckipp01 ckipp01 left a comment

Choose a reason for hiding this comment

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

I don't have much to add apart from what @keynmol already mentioned. I think we're all on the same page that to fully handle the new syntax would need some more C work, but this is a significant improvement over what we had before.

Thanks @eed3si9n

@eed3si9n
Copy link
Collaborator Author

eed3si9n commented Jan 3, 2023

For later record keeping, I described some of the changes in this PR in https://eed3si9n.com/fast-scala3-parsing-with-tree-sitter/ post as well.

I think the next challenge would be handling fewer braces syntax, since likely newlines starts to add semantics, and thus can't be ignored globally.

@eed3si9n eed3si9n merged commit 0ceab66 into tree-sitter:master Jan 3, 2023
@eed3si9n eed3si9n deleted the wip/braces2 branch January 3, 2023 15:33
@keynmol
Copy link
Collaborator

keynmol commented Jan 3, 2023

I think before that (=fewer braces) we'll need to run this on a larger codebases - I.e. I've been using your fork and it still has problems in quite a few places, so I would suggest ignoring fewerBraces completely and instead getting the number of correctly parsed files up on something like Dotty codebase.

There are several of my PRs for Scala 3 I will now revive and ensure that they work with your changes as well, which should improve things.

Additionally, would be good to keep track of Scala 2 as well, for which we can also use the code of Scala 2 compiler as a bench.

To get that locally, you can use this command (thanks to @ckipp01 for showing me the light):

> tree-sitter parse ~/projects/dotty/compiler/**/*.scala --quiet --stat | tail -n2
Total parses: 745; successful parses: 282; failed parses: 463; success percentage: 37.85%

@ckipp01
Copy link
Collaborator

ckipp01 commented Jan 3, 2023

I think before that (=fewer braces) we'll need to run this on a larger codebases - I.e. I've been using your fork and it still has problems in quite a few places, so I would suggest ignoring fewerBraces completely and instead getting the number of correctly parsed files up on something like Dotty codebase.

Yea I'm in agreement here. Even without fewer braces I still hit on a lot of issues. I think if we get your stuff revived and in @keynmol I can start paying way more attention to errors in the tree and reporting them now that I feel more confident that we can get things merged in.

@eed3si9n
Copy link
Collaborator Author

eed3si9n commented Jan 3, 2023

Yea. Generally I agree that we should try to go after lower hanging Scala 3 constructs first, but I was replying to the "need some more C work" part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/scala3 Scala 3 syntax
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants