Skip to content

Commit

Permalink
Add support for unit if/else blocks
Browse files Browse the repository at this point in the history
Closes #144.
  • Loading branch information
bbannier committed Oct 3, 2024
1 parent 7356e7a commit bf0c88f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.lock

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

12 changes: 12 additions & 0 deletions corpus/unit.spicy
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,15 @@ type Test = unit {
on b %error {}
};
on Test::b(msg: string) %error {}

# Unit if/else blocks.
type X = unit {
if (True) {
a1: bytes &size=2;
} &max-size=42 &requires=True;

if (True) {
a1: bytes &size=2;
a1: bytes &size=2;
} else {} &max-size=42 &requires=True;
};
13 changes: 13 additions & 0 deletions corpus/unit.spicy.expected
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,16 @@ type Test = unit {
on b %error {}
};
on Test::b(msg: string) %error {}

# Unit if/else blocks.
type X = unit {
if (True) {
a1: bytes &size=2;
} &max-size=42 &requires=True;

if (True) {
a1: bytes &size=2;
a1: bytes &size=2;
} else {
} &max-size=42 &requires=True;
};
20 changes: 20 additions & 0 deletions src/query.scm
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@
(comment)
(type_decl)
(field_decl)
(unit_if_else)
(unit_switch)
(struct_decl)
(sink_decl)
Expand Down Expand Up @@ -371,6 +372,25 @@
) @delete
)

; Unit `if`/`else` blocks.
(unit_if_else
"if" @append_space
"{" @prepend_space
("else" @prepend_space @append_space)?
)
(unit_if_else
"{" @append_indent_start @append_hardline
.
(field_decl)* ; List explicitly so we match the correct {} pair.
.
"}" @prepend_indent_end @prepend_hardline
)
(unit_if_else
(field_decl) @append_empty_softline
.
(comment)? @do_nothing
)

; Unit `switch` statement.
(unit_switch (expression) ")" @append_space)

Expand Down

0 comments on commit bf0c88f

Please sign in to comment.