Skip to content

Commit

Permalink
Merge pull request #239 from Emilios1995/main
Browse files Browse the repository at this point in the history
Integrate open PRs and update instructions
  • Loading branch information
aspeddro authored Mar 20, 2024
2 parents 6376fa0 + 857272e commit 5cb02bb
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 47 deletions.
55 changes: 52 additions & 3 deletions README.md
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
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
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

This file was deleted.

File renamed without changes.
29 changes: 29 additions & 0 deletions queries/rescript/injections.scm
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
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
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
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

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

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

Loading

0 comments on commit 5cb02bb

Please sign in to comment.