Skip to content

Commit

Permalink
[tree_sitter_v] Support short lambda (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lycs-D authored Mar 25, 2024
1 parent 029cd12 commit 7f91919
Show file tree
Hide file tree
Showing 11 changed files with 195,264 additions and 194,422 deletions.
35 changes: 27 additions & 8 deletions tree_sitter_v/grammar.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const PREC = {
or: 1,
resolve: 1,
composite_literal: -1,
strictly_expression_list: -3,
strictly_expression_list: -2,
};

const multiplicative_operators = ["*", "/", "%", "<<", ">>", ">>>", "&", "&^"];
Expand Down Expand Up @@ -578,7 +578,17 @@ module.exports = grammar({
prec.dynamic(2, seq(token.immediate("["), comma_sep1($.plain_type), "]")),

argument_list: ($) =>
seq("(", repeat(seq($.argument, optional(list_separator))), ")"),
seq(
"(",
choice(
repeat(seq($.argument, optional(list_separator))),
$.short_lambda,
),
")",
),

short_lambda: ($) =>
seq("|", comma_sep($.reference_expression), "|", $._expression_without_blocks),

argument: ($) =>
choice(
Expand Down Expand Up @@ -627,14 +637,14 @@ module.exports = grammar({
),

short_element_list: ($) =>
repeat1(seq($.element, optional(list_separator))),

element: ($) => $._expression,
repeat1(seq(alias($._expression, $.element), optional(list_separator))),

keyed_element: ($) =>
seq(field("key", $.field_name), ":", field("value", $._expression)),

field_name: ($) => $.reference_expression,
seq(
field("key", alias($.reference_expression, $.field_name)),
":",
field("value", $._expression),
),

function_literal: ($) =>
prec.right(
Expand Down Expand Up @@ -1505,14 +1515,23 @@ module.exports = grammar({
},
});

/**
* @param {RuleOrLiteral} rule
*/
function comp_time(rule) {
return seq("$", rule);
}

/**
* @param {RuleOrLiteral} rules
*/
function comma_sep1(rules) {
return seq(rules, repeat(seq(",", rules)));
}

/**
* @param {RuleOrLiteral} rule
*/
function comma_sep(rule) {
return optional(comma_sep1(rule));
}
4 changes: 2 additions & 2 deletions tree_sitter_v/node_types.v
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pub enum NodeType {
continue_statement
dec_expression
defer_statement
element
element_list
else_branch
embedded_definition
Expand Down Expand Up @@ -141,6 +140,7 @@ pub enum NodeType {
send_statement
shared_type
short_element_list
short_lambda
signature
simple_statement
slice_expression
Expand Down Expand Up @@ -355,7 +355,6 @@ const node_type_name_to_enum = {
'continue_statement': NodeType.continue_statement
'dec_expression': NodeType.dec_expression
'defer_statement': NodeType.defer_statement
'element': NodeType.element
'element_list': NodeType.element_list
'else_branch': NodeType.else_branch
'embedded_definition': NodeType.embedded_definition
Expand Down Expand Up @@ -451,6 +450,7 @@ const node_type_name_to_enum = {
'send_statement': NodeType.send_statement
'shared_type': NodeType.shared_type
'short_element_list': NodeType.short_element_list
'short_lambda': NodeType.short_lambda
'signature': NodeType.signature
'simple_statement': NodeType.simple_statement
'slice_expression': NodeType.slice_expression
Expand Down
4 changes: 3 additions & 1 deletion tree_sitter_v/queries/helix.highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
(interface_method_definition
name: (identifier) @function.method)

(short_lambda
(reference_expression) @parameter)
(call_expression
name: (selector_expression
name: (selector_expression
field: (reference_expression) @function.method))

(call_expression
Expand Down
4 changes: 3 additions & 1 deletion tree_sitter_v/queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
receiver: (receiver)
name: (identifier) @method)

(short_lambda
(reference_expression) @parameter)
(call_expression
name: (selector_expression
name: (selector_expression
field: (reference_expression) @method))

(type_reference_expression) @type
Expand Down
137 changes: 99 additions & 38 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 7f91919

Please sign in to comment.