Add support for label shorthand syntax

pull/844/head
Giacomo Cavalieri 2024-07-25 15:48:40 +07:00
parent 84fa6b0d53
commit f8a6614480
No known key found for this signature in database
GPG Key ID: 4A196008E732F17E
1 changed files with 22 additions and 10 deletions

@ -139,10 +139,13 @@ module.exports = grammar({
")" ")"
), ),
constant_record_argument: ($) => constant_record_argument: ($) =>
choice(
seq( seq(
optional(seq(field("label", $.label), ":")), optional(seq(field("label", $.label), ":")),
field("value", $._constant_value) field("value", $._constant_value)
), ),
seq(field("label", $.label), ":")
),
// This rule exists to parse remote function references which are generally // This rule exists to parse remote function references which are generally
// indistinguishable from field accesses and so share an AST node. // indistinguishable from field accesses and so share an AST node.
constant_field_access: ($) => constant_field_access: ($) =>
@ -510,7 +513,10 @@ module.exports = grammar({
), ),
record_update_arguments: ($) => series_of($.record_update_argument, ","), record_update_arguments: ($) => series_of($.record_update_argument, ","),
record_update_argument: ($) => record_update_argument: ($) =>
choice(
seq(field("label", $.label), ":", field("value", $._expression)), seq(field("label", $.label), ":", field("value", $._expression)),
seq(field("label", $.label), ":")
),
// As with other AST nodes in this section, `_maybe_record_expression`, // As with other AST nodes in this section, `_maybe_record_expression`,
// `_maybe_tuple_expression`, and `_maybe_function_expresssion` have no // `_maybe_tuple_expression`, and `_maybe_function_expresssion` have no
// corollaries in the Gleam parser. These anonymous AST node denote any // corollaries in the Gleam parser. These anonymous AST node denote any
@ -578,10 +584,13 @@ module.exports = grammar({
// record arguments, hence the ambiguous name. // record arguments, hence the ambiguous name.
arguments: ($) => seq("(", optional(series_of($.argument, ",")), ")"), arguments: ($) => seq("(", optional(series_of($.argument, ",")), ")"),
argument: ($) => argument: ($) =>
choice(
seq( seq(
optional(seq(field("label", $.label), ":")), optional(seq(field("label", $.label), ":")),
field("value", choice($.hole, $._expression)) field("value", choice($.hole, $._expression))
), ),
seq(field("label", $.label), ":")
),
hole: ($) => $._discard_name, hole: ($) => $._discard_name,
function_call: ($) => function_call: ($) =>
seq( seq(
@ -624,10 +633,13 @@ module.exports = grammar({
")" ")"
), ),
record_pattern_argument: ($) => record_pattern_argument: ($) =>
choice(
seq( seq(
optional(seq(field("label", $.label), ":")), optional(seq(field("label", $.label), ":")),
field("pattern", $._pattern) field("pattern", $._pattern)
), ),
seq(field("label", $.label), ":")
),
pattern_spread: ($) => seq("..", optional(",")), pattern_spread: ($) => seq("..", optional(",")),
tuple_pattern: ($) => tuple_pattern: ($) =>
seq("#", "(", optional(series_of($._pattern, ",")), ")"), seq("#", "(", optional(series_of($._pattern, ",")), ")"),