Skip to content

Commit

Permalink
Add support for let assert ... as (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
GearsDatapacks authored Nov 26, 2024
1 parent 2702fe8 commit e451746
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
38 changes: 24 additions & 14 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,27 @@ module.exports = grammar({
optional(field("arguments", $.arguments))
),
todo: ($) =>
seq(
"todo",
optional(
choice(
// DEPRECATED: The 'as' syntax was introduced in v0.30.
seq("(", field("message", $.string), ")"),
seq("as", field("message", $._expression))
prec.left(
seq(
"todo",
optional(
choice(
// DEPRECATED: The 'as' syntax was introduced in v0.30.
seq("(", field("message", $.string), ")"),
seq("as", field("message", $._expression))
)
)
)
),
panic: ($) =>
seq(
"panic",
optional(
choice(
seq("(", field("message", $.string), ")"),
seq("as", field("message", $._expression))
prec.left(
seq(
"panic",
optional(
choice(
seq("(", field("message", $.string), ")"),
seq("as", field("message", $._expression))
)
)
)
),
Expand Down Expand Up @@ -488,7 +492,13 @@ module.exports = grammar({
),
_case_clause_tuple_access: ($) =>
seq(field("tuple", $.identifier), ".", field("index", $.integer)),
let_assert: ($) => seq("let", "assert", $._assignment),
let_assert: ($) =>
seq(
"let",
"assert",
$._assignment,
optional(seq("as", field("message", $._expression)))
),
let: ($) => seq("let", $._assignment),
use: ($) =>
seq(
Expand Down
26 changes: 25 additions & 1 deletion test/corpus/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ fn assert_assignment() {
}
let assert y = x
let assert #(x, _) = #(1, 2)
let assert Ok(_) = Ok(10) as "This never fails"
let assert 10 = 11 as { "message1" <> "message2" }
}
fn assignment() {
let x = {
Expand Down Expand Up @@ -504,7 +506,29 @@ fn field_access(x) {
(discard))
value: (tuple
(integer)
(integer)))))
(integer)))
(let_assert
pattern: (record_pattern
name: (constructor_name)
arguments: (record_pattern_arguments
(record_pattern_argument
pattern: (discard))))
value: (record
name: (constructor_name)
arguments: (arguments
(argument
value: (integer))))
message: (string
(quoted_content)))
(let_assert
pattern: (integer)
value: (integer)
message: (block
(binary_expression
left: (string
(quoted_content))
right: (string
(quoted_content)))))))
(function
name: (identifier)
parameters: (function_parameters)
Expand Down

0 comments on commit e451746

Please sign in to comment.