Reclassify 'let' and 'use' as statements

With the latest changes to the gleam parser, code that uses let/use as
expressions is now rejected. This change reorganizes let and use under
a new '_statement' rule. This restricts let/use from taking other
let/use statements since they only accept expressions.
pull/844/head
Michael Davis 2023-04-02 11:48:25 +07:00 committed by Jonathan Arnett
parent 9013c64605
commit c29d291424
2 changed files with 6 additions and 7 deletions

@ -229,7 +229,7 @@ module.exports = grammar({
field("parameters", $.function_parameters), field("parameters", $.function_parameters),
optional(seq("->", field("return_type", $._type))), optional(seq("->", field("return_type", $._type))),
"{", "{",
optional(field("body", alias($._expression_seq, $.function_body))), optional(field("body", alias($._statement_seq, $.function_body))),
"}" "}"
), ),
function_parameters: ($) => function_parameters: ($) =>
@ -271,7 +271,7 @@ module.exports = grammar({
// This makes sense for the parser, but (IMO) would be more confusing for // This makes sense for the parser, but (IMO) would be more confusing for
// users and tooling which don't think about `try`s as having a "then". Thus, // users and tooling which don't think about `try`s as having a "then". Thus,
// `try`s are essentially treated the same as any other expression. // `try`s are essentially treated the same as any other expression.
_expression_seq: ($) => repeat1(choice($._expression, $.try)), _statement_seq: ($) => repeat1(choice($._statement, $.try)),
try: ($) => try: ($) =>
seq( seq(
"try", "try",
@ -280,6 +280,7 @@ module.exports = grammar({
"=", "=",
field("value", $._expression) field("value", $._expression)
), ),
_statement: ($) => choice($._expression, $.let, $.use),
_expression: ($) => choice($._expression_unit, $.binary_expression), _expression: ($) => choice($._expression_unit, $.binary_expression),
binary_expression: ($) => binary_expression: ($) =>
choice( choice(
@ -333,8 +334,6 @@ module.exports = grammar({
$.expression_group, $.expression_group,
$.case, $.case,
$.let_assert, $.let_assert,
$.let,
$.use,
$.assert, $.assert,
$.negation, $.negation,
$.record_update, $.record_update,
@ -374,7 +373,7 @@ module.exports = grammar({
), ),
optional(seq("->", field("return_type", $._type))), optional(seq("->", field("return_type", $._type))),
"{", "{",
field("body", alias($._expression_seq, $.function_body)), field("body", alias($._statement_seq, $.function_body)),
"}" "}"
), ),
anonymous_function_parameters: ($) => anonymous_function_parameters: ($) =>
@ -393,7 +392,7 @@ module.exports = grammar({
choice($._discard_param, $._name_param), choice($._discard_param, $._name_param),
optional($._type_annotation) optional($._type_annotation)
), ),
expression_group: ($) => seq("{", $._expression_seq, "}"), expression_group: ($) => seq("{", $._statement_seq, "}"),
case: ($) => case: ($) =>
seq( seq(
"case", "case",

@ -482,7 +482,7 @@ fn field_access(x) {
name: (constructor_name) name: (constructor_name)
arguments: (arguments arguments: (arguments
(argument (argument
value: (integer))))) value: (integer)))))
(let_assert (let_assert
pattern: (identifier) pattern: (identifier)
value: (expression_group value: (expression_group