diff --git a/compiler-core/src/parse.rs b/compiler-core/src/parse.rs index b4322997ea8..61e9d946ea3 100644 --- a/compiler-core/src/parse.rs +++ b/compiler-core/src/parse.rs @@ -465,15 +465,6 @@ where Ok(()) } - fn parse_expression_unit_collapsing_single_value_blocks( - &mut self, - ) -> Result, ParseError> { - match self.parse_expression_unit()? { - Some(expression) => Ok(Some(expression)), - None => Ok(None), - } - } - // examples: // 1 // "one" @@ -545,6 +536,16 @@ where } } + Some((start, Token::Echo, end)) => { + self.advance(); + let expression = self.parse_expression_unit()?; + let end = expression.as_ref().map_or(end, |e| e.location().end); + UntypedExpr::Echo { + location: SrcSpan { start, end }, + expression: expression.map(Box::new), + } + } + Some((start, Token::Hash, _)) => { self.advance(); let _ = self.expect_one(&Token::LeftParen)?; @@ -646,7 +647,7 @@ where &|s| { Parser::parse_bit_array_segment( s, - &Parser::parse_expression_unit_collapsing_single_value_blocks, + &Parser::parse_expression_unit, &Parser::expect_expression, &bit_array_expr_int, ) diff --git a/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_followed_by_expression_ends_where_expression_ends.snap b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_followed_by_expression_ends_where_expression_ends.snap new file mode 100644 index 00000000000..4a877aee788 --- /dev/null +++ b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_followed_by_expression_ends_where_expression_ends.snap @@ -0,0 +1,23 @@ +--- +source: compiler-core/src/parse/tests.rs +expression: echo wibble +--- +[ + Expression( + Echo { + location: SrcSpan { + start: 0, + end: 11, + }, + expression: Some( + Var { + location: SrcSpan { + start: 5, + end: 11, + }, + name: "wibble", + }, + ), + }, + ), +] diff --git a/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_has_greater_precedence_than_binop.snap b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_has_greater_precedence_than_binop.snap new file mode 100644 index 00000000000..c19d4462f04 --- /dev/null +++ b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_has_greater_precedence_than_binop.snap @@ -0,0 +1,37 @@ +--- +source: compiler-core/src/parse/tests.rs +expression: echo 1 + 1 +--- +[ + Expression( + BinOp { + location: SrcSpan { + start: 0, + end: 10, + }, + name: AddInt, + left: Echo { + location: SrcSpan { + start: 0, + end: 6, + }, + expression: Some( + Int { + location: SrcSpan { + start: 5, + end: 6, + }, + value: "1", + }, + ), + }, + right: Int { + location: SrcSpan { + start: 9, + end: 10, + }, + value: "1", + }, + }, + ), +] diff --git a/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_in_a_pipeline.snap b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_in_a_pipeline.snap new file mode 100644 index 00000000000..72f20d6e365 --- /dev/null +++ b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_in_a_pipeline.snap @@ -0,0 +1,34 @@ +--- +source: compiler-core/src/parse/tests.rs +expression: "[] |> echo |> wibble" +--- +[ + Expression( + PipeLine { + expressions: [ + List { + location: SrcSpan { + start: 0, + end: 2, + }, + elements: [], + tail: None, + }, + Echo { + location: SrcSpan { + start: 6, + end: 10, + }, + expression: None, + }, + Var { + location: SrcSpan { + start: 14, + end: 20, + }, + name: "wibble", + }, + ], + }, + ), +] diff --git a/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_in_a_pipeline_with_an_expression_ater_it.snap b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_in_a_pipeline_with_an_expression_ater_it.snap new file mode 100644 index 00000000000..04a567ddd83 --- /dev/null +++ b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_in_a_pipeline_with_an_expression_ater_it.snap @@ -0,0 +1,42 @@ +--- +source: compiler-core/src/parse/tests.rs +expression: "[] |> echo fun |> wibble" +--- +[ + Expression( + PipeLine { + expressions: [ + List { + location: SrcSpan { + start: 0, + end: 2, + }, + elements: [], + tail: None, + }, + Echo { + location: SrcSpan { + start: 6, + end: 14, + }, + expression: Some( + Var { + location: SrcSpan { + start: 11, + end: 14, + }, + name: "fun", + }, + ), + }, + Var { + location: SrcSpan { + start: 18, + end: 24, + }, + name: "wibble", + }, + ], + }, + ), +] diff --git a/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_with_block.snap b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_with_block.snap new file mode 100644 index 00000000000..6fccc7be25b --- /dev/null +++ b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_with_block.snap @@ -0,0 +1,47 @@ +--- +source: compiler-core/src/parse/tests.rs +expression: "echo { 1 + 1 }" +--- +[ + Expression( + Echo { + location: SrcSpan { + start: 0, + end: 14, + }, + expression: Some( + Block { + location: SrcSpan { + start: 5, + end: 14, + }, + statements: [ + Expression( + BinOp { + location: SrcSpan { + start: 7, + end: 12, + }, + name: AddInt, + left: Int { + location: SrcSpan { + start: 7, + end: 8, + }, + value: "1", + }, + right: Int { + location: SrcSpan { + start: 11, + end: 12, + }, + value: "1", + }, + }, + ), + ], + }, + ), + }, + ), +] diff --git a/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_with_no_expressions_after_it.snap b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_with_no_expressions_after_it.snap new file mode 100644 index 00000000000..2d6c960faeb --- /dev/null +++ b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_with_no_expressions_after_it.snap @@ -0,0 +1,15 @@ +--- +source: compiler-core/src/parse/tests.rs +expression: echo +--- +[ + Expression( + Echo { + location: SrcSpan { + start: 0, + end: 4, + }, + expression: None, + }, + ), +] diff --git a/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_with_panic.snap b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_with_panic.snap new file mode 100644 index 00000000000..6c4b6010365 --- /dev/null +++ b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__echo_with_panic.snap @@ -0,0 +1,31 @@ +--- +source: compiler-core/src/parse/tests.rs +expression: "echo panic as \"a\"" +--- +[ + Expression( + Echo { + location: SrcSpan { + start: 0, + end: 17, + }, + expression: Some( + Panic { + location: SrcSpan { + start: 5, + end: 17, + }, + message: Some( + String { + location: SrcSpan { + start: 14, + end: 17, + }, + value: "a", + }, + ), + }, + ), + }, + ), +] diff --git a/compiler-core/src/parse/snapshots/gleam_core__parse__tests__panic_with_echo.snap b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__panic_with_echo.snap new file mode 100644 index 00000000000..cc367fd86a7 --- /dev/null +++ b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__panic_with_echo.snap @@ -0,0 +1,31 @@ +--- +source: compiler-core/src/parse/tests.rs +expression: "panic as echo \"string\"" +--- +[ + Expression( + Panic { + location: SrcSpan { + start: 0, + end: 22, + }, + message: Some( + Echo { + location: SrcSpan { + start: 9, + end: 22, + }, + expression: Some( + String { + location: SrcSpan { + start: 14, + end: 22, + }, + value: "string", + }, + ), + }, + ), + }, + ), +] diff --git a/compiler-core/src/parse/snapshots/gleam_core__parse__tests__repeated_echos.snap b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__repeated_echos.snap new file mode 100644 index 00000000000..70b9afbb9e5 --- /dev/null +++ b/compiler-core/src/parse/snapshots/gleam_core__parse__tests__repeated_echos.snap @@ -0,0 +1,39 @@ +--- +source: compiler-core/src/parse/tests.rs +expression: echo echo echo 1 +--- +[ + Expression( + Echo { + location: SrcSpan { + start: 0, + end: 16, + }, + expression: Some( + Echo { + location: SrcSpan { + start: 5, + end: 16, + }, + expression: Some( + Echo { + location: SrcSpan { + start: 10, + end: 16, + }, + expression: Some( + Int { + location: SrcSpan { + start: 15, + end: 16, + }, + value: "1", + }, + ), + }, + ), + }, + ), + }, + ), +] diff --git a/compiler-core/src/parse/tests.rs b/compiler-core/src/parse/tests.rs index d90e78b9e3e..a6fdeee8e14 100644 --- a/compiler-core/src/parse/tests.rs +++ b/compiler-core/src/parse/tests.rs @@ -466,6 +466,51 @@ fn no_eq_after_binding1() { ); } +#[test] +fn echo_followed_by_expression_ends_where_expression_ends() { + assert_parse!("echo wibble"); +} + +#[test] +fn echo_with_no_expressions_after_it() { + assert_parse!("echo"); +} + +#[test] +fn echo_with_block() { + assert_parse!("echo { 1 + 1 }"); +} + +#[test] +fn echo_has_greater_precedence_than_binop() { + assert_parse!("echo 1 + 1"); +} + +#[test] +fn echo_in_a_pipeline() { + assert_parse!("[] |> echo |> wibble"); +} + +#[test] +fn echo_in_a_pipeline_with_an_expression_ater_it() { + assert_parse!("[] |> echo fun |> wibble"); +} + +#[test] +fn panic_with_echo() { + assert_parse!("panic as echo \"string\""); +} + +#[test] +fn echo_with_panic() { + assert_parse!("echo panic as \"a\""); +} + +#[test] +fn repeated_echos() { + assert_parse!("echo echo echo 1"); +} + #[test] fn no_let_binding_snapshot_1() { assert_error!("wibble = 4");