Skip to content

Commit

Permalink
Merge pull request #736 from tweag/let-blocks
Browse files Browse the repository at this point in the history
Support let blocks in nickel
  • Loading branch information
yannham authored Sep 25, 2024
2 parents c0a2105 + cf7697a commit 98aa80f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ This name should be decided amongst the team before the release.
- [#711](https://github.com/tweag/topiary/pull/711) Feature gate all grammars, with supported and contributed languages built by default.
- [#716](https://github.com/tweag/topiary/pull/716) Dynamicly fetch, compile, and load language grammars. Topiary now no longer ships with statically linked grammars.
- [#732](https://github.com/tweag/topiary/pull/732) Change how function application and parenthesized expressions are treated in Nickel to reduce the overall noise and indentation
- [#736](https://github.com/tweag/topiary/pull/668) Updates our Nickel grammar, and adds support for let blocks.

## v0.4.0 - Exquisite Elm - 2024-05-15

Expand Down
10 changes: 10 additions & 0 deletions topiary-cli/tests/samples/expected/nickel.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@
in
foo
@ [],
let x = 1, y = 2 in x + y,
let
x = 1,
y = 2,
in x + y,
let rec
x = 1,
y = 2,
in
x + y,
],

# Nickel standard library as of 44aef1672a09a76a71946fbf822713747ab7b9df
Expand Down
12 changes: 12 additions & 0 deletions topiary-cli/tests/samples/input/nickel.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ else
in
foo
@ [],

let x = 1, y = 2 in x + y,

let x = 1,
y = 2,
in x + y,

let
rec x = 1,
y = 2,
in
x + y,
],

# Nickel standard library as of 44aef1672a09a76a71946fbf822713747ab7b9df
Expand Down
2 changes: 1 addition & 1 deletion topiary-config/languages.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
extensions = ["ncl"],
grammar = {
git = "https://github.com/nickel-lang/tree-sitter-nickel",
rev = "43433d8477b24cd13acaac20a66deda49b7e2547",
rev = "88d836a24b3b11c8720874a1a9286b8ae838d30a",
},
},

Expand Down
64 changes: 61 additions & 3 deletions topiary-queries/queries/nickel.scm
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,14 @@
;
; let [rec] IDENT = EXPR in EXPR
;
; or
;
; let [rec]
; IDENT = EXPR,
; IDENT = EXPR,
; in
; EXPR
;
; The formatting for the bound expression is handled by the above rules,
; which also apply to record field values. The "in" should appear on a
; new line, if the entire let expression is multi-line. The result
Expand All @@ -303,14 +311,43 @@
; expression, to avoid long diagonals in a series of let expressions
; (which is idiomatic).

; A let block containing multiple bindings. If this is multiline, the first
; binding should appear on a new line, and the bindings should be indented.
(let_expr
(#scope_id! "let_result")
(let_in_block
"let"
.
; Prepend before the first binding instead of appending after the "let",
; so that in the case of a "let rec" the line break goes after the "rec".
(let_binding) @prepend_spaced_softline @prepend_indent_start
(let_binding)
"in" @prepend_indent_end @prepend_begin_scope @prepend_spaced_softline
)
(term) @append_end_scope
)

; A let with a single binding. The binding should be on the same line as the "let".
(let_expr
(#scope_id! "let_result")
(let_in_block
"let"
.
(let_binding)
.
"in" @prepend_begin_scope @prepend_spaced_softline
)
(term) @append_end_scope
)

; When binding multiple values in a let block, allow new lines between the bindings.
(let_expr
(#scope_id! "let_result")
(let_in_block
("," @append_spaced_softline)
)
)

(let_expr
(#scope_id! "let_result")
(term) @prepend_spaced_scoped_softline
Expand Down Expand Up @@ -603,12 +640,33 @@
.
)

; Allow newlines after the comma (or semicolon) following a container
; element.
(
(#scope_id! "container")
[
","
";"
] @append_spaced_scoped_softline
(record_field)
(record_last_field)
(field_pattern)
(last_field_pat)
(match_branch)
(term)
(pattern)
(last_elem_pat)
(enum)
]
.
["," ";"] @append_spaced_scoped_softline
.
(comment)? @do_nothing
)

; Enums and records can have a `;` at the very beginning; allow spaces after
; these ones also.
(_
(#scope_id! "container")
.
";" @append_spaced_scoped_softline
.
(comment)? @do_nothing
)
Binary file modified web-playground/public/scripts/tree-sitter-nickel.wasm
Binary file not shown.

0 comments on commit 98aa80f

Please sign in to comment.