Skip to content

Commit

Permalink
[tree_sitter] Add sum type to tree node (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lycs-D authored Apr 9, 2024
1 parent 395d22e commit 73b0d99
Show file tree
Hide file tree
Showing 7 changed files with 204,070 additions and 199,222 deletions.
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

0 comments on commit 73b0d99

Please sign in to comment.