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 all commits
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
33 changes: 18 additions & 15 deletions tree_sitter_v/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('type', 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
244 changes: 122 additions & 122 deletions tree_sitter_v/src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading