Skip to content

Commit

Permalink
Merge pull request #6 from starkware-libs/lucas/closure
Browse files Browse the repository at this point in the history
feat(grammar): add closures
  • Loading branch information
0xLucqs authored Jul 26, 2024
2 parents e3a0212 + f3cc322 commit d5733e0
Show file tree
Hide file tree
Showing 9 changed files with 45,376 additions and 40,094 deletions.
27 changes: 26 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const PREC = {
and: 3,
or: 2,
assign: 0,
closure: -1,
};
const TOKEN_TREE_NON_SPECIAL_PUNCTUATION = [
'+',
Expand Down Expand Up @@ -589,7 +590,7 @@ module.exports = grammar({
'_',
),
// for example in let (a, b) = c; it would be `(a, b)`
tuple_pattern: ($) => seq('(', sepBy(',', $._pattern), optional(','), ')'),
tuple_pattern: ($) => seq('(', sepBy(',', choice($._pattern, $.closure_expression)), optional(','), ')'),

// for example in let [a, b] = c; it would be `[a, b]`
slice_pattern: ($) => seq('[', sepBy(',', $._pattern), optional(','), ']'),
Expand Down Expand Up @@ -806,6 +807,7 @@ module.exports = grammar({
$.index_expression,
$.parenthesized_expression,
prec(1, $.macro_invocation),
$.closure_expression,
),
// for example in a::<A>(); it would be `a::<A>`
generic_function: ($) =>
Expand Down Expand Up @@ -1035,6 +1037,29 @@ module.exports = grammar({
while_expression: ($) =>
seq('while', field('condition', $._condition), field('body', $.block)),

// |x| { x + 1}
closure_expression: $ => prec(PREC.closure, seq(
optional('move'),
field('parameters', $.closure_parameters),
choice(
seq(
optional(seq('->', field('return_type', $._type))),
field('body', $.block),
),
field('body', choice($.expression, '_')),
),
)),

closure_parameters: $ => seq(
'|',
sepBy(',', choice(
$._pattern,
$.parameter,
)),
'|',
),

// for x in a.iter()
for_expression: $ => seq(
'for',
field('pattern', $._pattern),
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"build": "tree-sitter generate --no-bindings",
"build-wasm": "tree-sitter build --wasm",
"lint": "eslint grammar.js",
"fix": "eslint grammar.js --fix",
"parse": "tree-sitter parse",
"test": "tree-sitter test"
},
Expand Down Expand Up @@ -103,4 +104,4 @@
]
}
}
}
}
4 changes: 4 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
"<"
">"
] @punctuation.bracket)
(closure_parameters
"|" @punctuation.bracket)

; ---
; Variables
Expand All @@ -92,6 +94,8 @@
(parameter
pattern: (identifier) @variable.parameter)

(closure_parameters
(identifier) @variable.parameter)
; -------
; Keywords
; -------
Expand Down
10 changes: 10 additions & 0 deletions queries/indents.scm
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@
(#not-same-line? @expr-start @pattern-guard)
) @indent

; Align closure parameters if they span more than one line
(closure_parameters
"|"
.
(_) @anchor
(_) @expr-end
.
(#not-same-line? @anchor @expr-end)
) @align

(for_expression
"in" @in
.
Expand Down
3 changes: 3 additions & 0 deletions queries/locals.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(type_item)
(trait_item)
(impl_item)
(closure_expression)
(block)
] @local.scope

Expand All @@ -20,6 +21,8 @@
(constrained_type_parameter
left: (type_identifier) @local.definition)

(closure_parameters (identifier) @local.definition)

; References
(identifier) @local.reference
(type_identifier) @local.reference
6 changes: 6 additions & 0 deletions queries/textobjects.scm
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
(function_item
body: (_) @function.inside) @function.around

(closure_expression
body: (_) @function.inside) @function.around

(struct_item
body: (_) @class.inside) @class.around

Expand All @@ -16,6 +19,9 @@
(parameters
((_) @parameter.inside . ","? @parameter.around) @parameter.around)

(closure_parameters
((_) @parameter.inside . ","? @parameter.around) @parameter.around)

(type_parameters
((_) @parameter.inside . ","? @parameter.around) @parameter.around)

Expand Down
195 changes: 191 additions & 4 deletions 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 d5733e0

Please sign in to comment.