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

[tree_sitter] Add sum type to tree node #87

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 2 additions & 0 deletions tree_sitter_v/bindings/node_types.v
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pub enum NodeType {
struct_declaration
struct_field_declaration
struct_field_scope
sum_type
thread_type
type_declaration
type_initializer
Expand Down Expand Up @@ -464,6 +465,7 @@ const node_type_name_to_enum = {
'struct_declaration': NodeType.struct_declaration
'struct_field_declaration': NodeType.struct_field_declaration
'struct_field_scope': NodeType.struct_field_scope
'sum_type': NodeType.sum_type
'thread_type': NodeType.thread_type
'type_declaration': NodeType.type_declaration
'type_initializer': NodeType.type_initializer
Expand Down
35 changes: 19 additions & 16 deletions tree_sitter_v/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const list_separator = choice(semi, ',');
module.exports = grammar({
name: 'v',

extras: ($) => [/\s/, $.line_comment, $.block_comment],
extras: ($) => [/[ \t]+|(\r?\n)+/, $.line_comment, $.block_comment],
Lycs-D marked this conversation as resolved.
Show resolved Hide resolved

word: ($) => $.identifier,

Expand All @@ -104,7 +104,6 @@ module.exports = grammar({
[$.fixed_array_type, $.literal],
[$.reference_expression, $.type_reference_expression],
[$.is_expression],
[$._type_union_list],
[$._expression_without_blocks, $.element_list],
],

Expand Down Expand Up @@ -209,19 +208,18 @@ module.exports = grammar({
_global_var_value: ($) => seq('=', field('value', $._expression)),

type_declaration: ($) =>
seq(
optional($.visibility_modifiers),
'type',
field('name', $.identifier),
optional(field('generic_parameters', $.generic_parameters)),
'=',
field('types', $._type_union_list),
prec.right(
PREC.resolve,
seq(
optional($.visibility_modifiers),
'type',
field('name', $.identifier),
optional(field('generic_parameters', $.generic_parameters)),
'=',
field('types', choice($.sum_type, $.plain_type)),
),
),

// int | string | Foo
_type_union_list: ($) =>
seq($.plain_type, repeat(seq(optional(terminator), '|', $.plain_type))),

function_declaration: ($) =>
prec.right(
PREC.resolve,
Expand Down Expand Up @@ -534,7 +532,7 @@ module.exports = grammar({
),

type_initializer: ($) =>
prec(
prec.right(
PREC.type_initializer,
seq(field('type', $.plain_type), field('body', $.type_initializer_body)),
),
Expand Down Expand Up @@ -957,6 +955,12 @@ module.exports = grammar({

// ==================== TYPES ====================

// int | string | Foo
sum_type: ($) =>
prec.right(
seq($.plain_type, repeat1(seq(optional(/\s+/), token.immediate('|'), $.plain_type))),
),

plain_type: ($) =>
prec.right(
PREC.primary,
Expand Down Expand Up @@ -1003,8 +1007,7 @@ module.exports = grammar({
field('element', $.plain_type),
),

array_type: ($) =>
prec(PREC.primary, prec.dynamic(2, seq('[', ']', field('element', $.plain_type)))),
array_type: ($) => prec.right(PREC.primary, seq('[', ']', field('element', $.plain_type))),

variadic_type: ($) => seq('...', $.plain_type),

Expand Down
4 changes: 2 additions & 2 deletions tree_sitter_v/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"install": "node-gyp-build",
"prebuildify": "prebuildify --napi --strip",
"lint": "eslint \"**/*.js\"",
"format": "prettier --write \"**/*.js\"",
"format:check": "prettier --check \"**/*.js\""
"format": "prettier --write \"grammar.js\"",
"format:check": "prettier --check \"grammar.js\""
Lycs-D marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"node-addon-api": "^8.0.0",
Expand Down
Loading