Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Integrate open PRs and update instructions #239

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
aspeddro merged 14 commits into rescript-lang:main from Emilios1995:main
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
c85307e
Fix infinite loop in scanner.
cristianoc Oct 11, 2023
b83d7eb
feat: dynamic module import
zweimach Feb 9, 2024
b50f638
feat: record type spread
zweimach Feb 9, 2024
3dfdf8a
fix: negative decimal integer literal
zweimach Feb 14, 2024
c454e09
Fix injections
Emilios1995 Mar 1, 2024
5ca149c
Support GraphQA injection for rescript-relay
Emilios1995 Mar 1, 2024
c379199
feat(plugin): installation
TheFedaikin Aug 4, 2023
c03220f
Update instructions
Emilios1995 Mar 11, 2024
309fd25
Fix generic type spread
Emilios1995 Mar 17, 2024
9af3e1f
Add support for the explicit partial application syntax
Emilios1995 Mar 20, 2024
6651fae
Support optional fields in record patterns
Emilios1995 Mar 20, 2024
e0bc632
Fix error with JSX element inside pipe
Emilios1995 Mar 20, 2024
e39df74
Support aliased inline types
Emilios1995 Mar 20, 2024
857272e
Add separate named node for type spreads
Emilios1995 Mar 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,62 @@

This repository contains a parser definition of the [ReScript](https://rescript-lang.org/) language for the [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) parser generator tool.

Athough Tree-sitter has many applications, the main intent of this parser is powering the [`nvim-treesitter-rescript`](https://github.com/nkrkv/nvim-tree-sitter-rescript/) NeoVim plugin which may be used to improve development experience in the NeoVim + ReScript combo.

Queries for text objects are also included which help you to navigate, select, and modify ReScript code syntactically. For NeoVim, the [`nvim-treesitter-textobjects`](https://github.com/nvim-treesitter/nvim-treesitter-textobjects) plugin is required to use Tree-sitter text objects.

## Installation

- If you want ReScript Tree-sitter in NeoVim, refer to [`nvim-treesitter-rescript`](https://github.com/nkrkv/nvim-tree-sitter-rescript/) installation notes;
### Neovim

If you want ReScript Tree-sitter in NeoVim, you will first need to register a new parser for it like so:

```lua
local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
parser_config.rescript = {
install_info = {
url = "https://github.com/rescript-lang/tree-sitter-rescript",
branch = "main",
files = { "src/scanner.c" },
generate_requires_npm = false,
requires_generate_from_grammar = true,
use_makefile = true, -- macOS specific instruction
},
}
```

This will make `TSInstall rescript` globally available. For more persistent approach you should add this parser to your Lua configuration.

Default configuration detects `.res` and `.resi` files. You can confirm that it's correctly installed by invoking `:InspectTree` when you are in the ReScript file.

- Notice that by default you will not see the highlighting! To enable highlighting, you will need to install this package either as a dependency or directly.

If you are using `lazy.nvim` example configuration will look like so:

```lua
{
"nvim-treesitter/nvim-treesitter",
dependencies = {
"rescript-lang/tree-sitter-rescript"
},
opts = function(_, opts) -- this is needed so you won't override your default nvim-treesitter configuration
vim.list_extend(opts.ensure_installed, {
"rescript",
})

local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
parser_config.rescript = {
install_info = {
url = "https://github.com/rescript-lang/tree-sitter-rescript",
branch = "main",
files = { "src/scanner.c" },
generate_requires_npm = false,
requires_generate_from_grammar = true,
use_makefile = true, -- macOS specific instruction
},
}
end,
}
```

- If you want it for other purposes, you probably know what to do.

## Contributing
Expand Down
53 changes: 39 additions & 14 deletions grammar.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="tree-sitter-cli/dsl" />

module.exports = grammar({
name: 'rescript',

Expand Down Expand Up @@ -105,7 +107,9 @@ module.exports = grammar({
[$._reserved_identifier, $.function],
[$.exception_pattern, $.or_pattern],
[$.type_binding, $._inline_type],
[$._module_structure, $.parenthesized_module_expression]
[$._module_structure, $.parenthesized_module_expression],
[$.record_type_field, $.object_type_field],
[$._record_type_member, $._object_type_member],
],

rules: {
Expand Down Expand Up @@ -174,6 +178,7 @@ module.exports = grammar({
)),
optional(seq(
'=',
optional('await'),
field('definition', $._module_definition),
)),
)),
Expand Down Expand Up @@ -314,6 +319,7 @@ module.exports = grammar({
$.module_pack,
$.unit,
$.polymorphic_type,
alias($._as_aliasing_non_function_inline_type, $.as_aliasing_type)
),

polymorphic_type: $ => seq(
Expand Down Expand Up @@ -377,37 +383,48 @@ module.exports = grammar({

record_type: $ => seq(
'{',
commaSept($.record_type_field),
commaSept($._record_type_member),
'}',
),

record_type_field: $ => seq(
optional('mutable'),
alias($.value_identifier, $.property_identifier),
optional('?'),
$.type_annotation,
record_type_field: $ =>
seq(
optional('mutable'),
alias($.value_identifier, $.property_identifier),
optional('?'),
$.type_annotation,
),

type_spread: $ =>
seq('...', choice($.type_identifier, $.generic_type, $.type_identifier_path)),

_record_type_member: $ => choice(
$.record_type_field,
$.type_spread
),

object_type: $ => prec.left(seq(
'{',
choice(
commaSep1t($._object_type_field),
seq('.', commaSept($._object_type_field)),
seq('..', commaSept($._object_type_field)),
commaSep1t($._object_type_member),
seq('.', commaSept($._object_type_member)),
seq('..', commaSept($._object_type_member)),
),
'}',
)),

_object_type_field: $ => alias($.object_type_field, $.field),
_object_type_member: $ =>
choice(
alias($.object_type_field, $.field),
$.type_spread
),

object_type_field: $ => choice(
seq('...', choice($.type_identifier, $.type_identifier_path)),
seq(
alias($.string, $.property_identifier),
':',
$._type,
),

),

generic_type: $ => prec.left(seq(
Expand Down Expand Up @@ -521,6 +538,7 @@ module.exports = grammar({
$.module_pack,
$.extension_expression,
$.lazy_expression,
$._jsx_element
),

parenthesized_expression: $ => seq(
Expand Down Expand Up @@ -698,6 +716,9 @@ module.exports = grammar({

as_aliasing_type: $ => seq($._type, 'as', $.type_identifier),

_as_aliasing_non_function_inline_type: $ =>
prec(2, seq($._non_function_inline_type, 'as', $.type_identifier)),

assert_expression: $ => prec.left(seq('assert', $.expression)),

call_expression: $ => prec('call', seq(
Expand Down Expand Up @@ -726,6 +747,7 @@ module.exports = grammar({
'(',
optional($.uncurry),
optional(commaSep1t($._call_argument)),
optional($.partial_application_spread),
')'
),

Expand All @@ -737,6 +759,8 @@ module.exports = grammar({
$.labeled_argument,
),

partial_application_spread: $ => "...",

labeled_argument: $ => seq(
'~',
field('label', $.value_identifier),
Expand Down Expand Up @@ -876,6 +900,7 @@ module.exports = grammar({
record_pattern: $ => seq(
'{',
commaSep1t(seq(
optional("?"),
choice(
$.value_identifier,
$.value_identifier_path,
Expand Down Expand Up @@ -1331,7 +1356,7 @@ module.exports = grammar({
const bigint_literal = seq(choice(hex_literal, binary_literal, octal_literal, decimal_digits), 'n')

const decimal_integer_literal = choice(
repeat('0'),
repeat1('0'),
seq(repeat('0'), /[1-9]/, optional(seq(optional('_'), decimal_digits)))
)

Expand Down
14 changes: 13 additions & 1 deletion package.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@
"res",
"resi"
],
"injection-regex": "rescript"
"injection-regex": "rescript",
"highlights": [
"queries/rescript/highlights.scm"
],
"locals": [
"queries/rescript/locals.scm"
],
"injections": [
"queries/rescript/injections.scm"
],
"textobjects": [
"queries/rescript/textobjects.scm"
]
}
]
}
21 changes: 0 additions & 21 deletions queries/injections.scm
View file Open in desktop

This file was deleted.

File renamed without changes.
29 changes: 29 additions & 0 deletions queries/rescript/injections.scm
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
((comment) @injection.content (#set! injection.language "comment"))

; %re
(extension_expression
(extension_identifier) @_name
(#eq? @_name "re")
(expression_statement (_) @injection.content (#set! injection.language "regex")))

; %raw
(extension_expression
(extension_identifier) @_name
(#eq? @_name "raw")
(expression_statement
(_ (_) @injection.content (#set! injection.language "javascript"))))

; %graphql
(extension_expression
(extension_identifier) @_name
(#eq? @_name "graphql")
(expression_statement
(_ (_) @injection.content (#set! injection.language "graphql"))))

; %relay
(extension_expression
(extension_identifier) @_name
(#eq? @_name "relay")
(expression_statement
(_ (_) @injection.content (#set! injection.language "graphql") )))

File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/scanner.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static bool scan_comment(TSLexer *lexer) {
// Single-line comment
do {
advance(lexer);
} while (lexer->lookahead != '\n');
} while (lexer->lookahead != '\n' && !lexer->eof(lexer));
return true;

case '*':
Expand Down
30 changes: 27 additions & 3 deletions test/corpus/expressions.txt
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ blocky(
~third={3},
)
f(raise)
f(1, ...)

--------------------------------------------------------------------------------

Expand Down Expand Up @@ -191,7 +192,13 @@ f(raise)
(call_expression
function: (value_identifier)
arguments: (arguments
(value_identifier)))))
(value_identifier))))
(expression_statement
(call_expression
function: (value_identifier)
arguments: (arguments
(number)
(partial_application_spread)))))

================================================================================
Pipe
Expand Down Expand Up @@ -755,6 +762,7 @@ switch person {
}) => 20
| Student({status: Sick}) => 30
| Student({name}) => 40
| Student({?age}) => 50
}

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -818,7 +826,16 @@ switch person {
(value_identifier))))
(sequence_expression
(expression_statement
(number)))))))
(number))))
(switch_match
(variant_pattern
(variant_identifier)
(formal_parameters
(record_pattern
(value_identifier))))
(sequence_expression
(expression_statement
(number)))))))

================================================================================
Switch of lists
Expand Down Expand Up @@ -1149,6 +1166,8 @@ Math operators
- 1 + 2 / 3
-. 1.0 +. 2.0 /. 3.0
2.0 ** 3.0
-0l
-ln

--------------------------------------------------------------------------------

Expand All @@ -1170,7 +1189,12 @@ Math operators
(expression_statement
(binary_expression
(number)
(number))))
(number)))
(expression_statement
(number))
(expression_statement
(unary_expression
(value_identifier))))

================================================================================
Boolean operators
Expand Down
17 changes: 17 additions & 0 deletions test/corpus/jsx.txt
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,20 @@ Spread props
(value_identifier))))
(jsx_closing_element
(jsx_identifier)))))

================================================================================
Elements in pipes
================================================================================

<Comp />->Some
Copy link
Contributor Author

@Emilios1995 Emilios1995 Mar 20, 2024

Choose a reason for hiding this comment

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

A similar snippet was failing in the wild tests (but I forgot to add it to the list in the description.)

The problem was that pipe expressions only accept primary_expressions as their members, and JSX elements wasn't one. I fixed it by simply adding it to the list of primary_expressions. I looked at the grammar and this shouldn't have any negative repercussions.


--------------------------------------------------------------------------------

(source_file
(expression_statement
(pipe_expression
(jsx_self_closing_element
(jsx_identifier))
(variant
(variant_identifier)))))

Loading

AltStyle によって変換されたページ (->オリジナル) /