Skip to content

Add reserved words #117

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ddickstein
Copy link

Tree-sitter now supports reserved keywords for better error recovery. This commit updates the OCaml grammar to mark reserved words. For example, before

let x =

type t = int

was parsed as

(compilation_unit ; [0, 0] - [4, 0]
  (value_definition ; [0, 0] - [2, 12]
    "let" ; [0, 0] - [0, 3]
    (let_binding ; [0, 4] - [2, 12]
      pattern: (value_name) ; [0, 4] - [0, 5]
      "=" ; [0, 6] - [0, 7]
      body: (infix_expression ; [2, 0] - [2, 12]
        left: (application_expression ; [2, 0] - [2, 6]
          function: (value_path ; [2, 0] - [2, 4]
            (value_name)) ; [2, 0] - [2, 4]
          argument: (value_path ; [2, 5] - [2, 6]
            (value_name))) ; [2, 5] - [2, 6]
        operator: (rel_operator) ; [2, 7] - [2, 8]
        right: (value_path ; [2, 9] - [2, 12]
          (value_name)))))) ; [2, 9] - [2, 12]

and now it is parsed as

(compilation_unit ; [0, 0] - [4, 0]
  (value_definition ; [0, 0] - [0, 5]
    "let" ; [0, 0] - [0, 3]
    (let_binding ; [0, 4] - [0, 5]
      pattern: (value_name))) ; [0, 4] - [0, 5]
  (ERROR ; [0, 6] - [0, 7]
    "=") ; [0, 6] - [0, 7]
  (type_definition ; [2, 0] - [2, 12]
    "type" ; [2, 0] - [2, 4]
    (type_binding ; [2, 5] - [2, 12]
      name: (type_constructor) ; [2, 5] - [2, 6]
      "=" ; [2, 7] - [2, 8]
      equation: (type_constructor_path ; [2, 9] - [2, 12]
        (type_constructor))))) ; [2, 9] - [2, 12]

Tree-sitter now supports reserved keywords for better error recovery.
This commit updates the OCaml grammar to mark reserved words. For
example, before

```ocaml
let x =

type t = int
```

was parsed as

```
(compilation_unit ; [0, 0] - [4, 0]
  (value_definition ; [0, 0] - [2, 12]
    "let" ; [0, 0] - [0, 3]
    (let_binding ; [0, 4] - [2, 12]
      pattern: (value_name) ; [0, 4] - [0, 5]
      "=" ; [0, 6] - [0, 7]
      body: (infix_expression ; [2, 0] - [2, 12]
        left: (application_expression ; [2, 0] - [2, 6]
          function: (value_path ; [2, 0] - [2, 4]
            (value_name)) ; [2, 0] - [2, 4]
          argument: (value_path ; [2, 5] - [2, 6]
            (value_name))) ; [2, 5] - [2, 6]
        operator: (rel_operator) ; [2, 7] - [2, 8]
        right: (value_path ; [2, 9] - [2, 12]
          (value_name)))))) ; [2, 9] - [2, 12]
```

and now it is parsed as

```
(compilation_unit ; [0, 0] - [4, 0]
  (value_definition ; [0, 0] - [0, 5]
    "let" ; [0, 0] - [0, 3]
    (let_binding ; [0, 4] - [0, 5]
      pattern: (value_name))) ; [0, 4] - [0, 5]
  (ERROR ; [0, 6] - [0, 7]
    "=") ; [0, 6] - [0, 7]
  (type_definition ; [2, 0] - [2, 12]
    "type" ; [2, 0] - [2, 4]
    (type_binding ; [2, 5] - [2, 12]
      name: (type_constructor) ; [2, 5] - [2, 6]
      "=" ; [2, 7] - [2, 8]
      equation: (type_constructor_path ; [2, 9] - [2, 12]
        (type_constructor))))) ; [2, 9] - [2, 12]
```
@ddickstein
Copy link
Author

List of reserved words taken from https://ocaml.org/manual/5.3/lex.html#sss:keywords

@aryx
Copy link
Contributor

aryx commented Apr 21, 2025

nice!

@314eter
Copy link
Collaborator

314eter commented Apr 21, 2025

I was working on this myself at 314eter/tree-sitter-ocaml. But the tests are failing because the Python bindings don't support 0.25 yet, so I was waiting on that to get released to create a PR.

Some things I did that are missing here:

  • Upgraded the dependencies to tree-sitter 0.25
  • Excluded the nonrec keyword. It's new since OCaml 4.02, so old code may be using it as a variable.
  • Included the binary operators or, lor, lxor, mod, land, lsl, lsr and asr by making them tokens in the grammar.
  • Used a different set of keywords for attribute_id.

@ddickstein
Copy link
Author

ddickstein commented Apr 21, 2025 via email

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.

3 participants