From 5c0cec2d29a0580a8e171e040f13427b282c4c1f Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Sun, 20 Sep 2020 01:00:30 +0200 Subject: [PATCH] feat: Format seq expressions without seq --- Cargo.lock | 11 +++++++++++ book/src/syntax-and-semantics.md | 13 +++++++++++-- format/Cargo.toml | 1 + format/src/pretty_print.rs | 27 ++++++++++++++++----------- format/tests/pretty_print.rs | 25 ++++++++++++++++++++++++- std/parser.glu | 2 +- 6 files changed, 64 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 280ea251af..de077e7601 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -810,6 +810,16 @@ dependencies = [ "version_check", ] +[[package]] +name = "expect-test" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ceb96f3eaa0d4e8769c52dacfd4eb60183b817ed2f176171b3c691d5022b0f2e" +dependencies = [ + "difference", + "once_cell", +] + [[package]] name = "failure" version = "0.1.8" @@ -1292,6 +1302,7 @@ dependencies = [ "codespan", "difference", "env_logger 0.7.1", + "expect-test", "futures 0.3.5", "gluon", "gluon_base", diff --git a/book/src/syntax-and-semantics.md b/book/src/syntax-and-semantics.md index 12ff816c4d..a2a60ee7c3 100644 --- a/book/src/syntax-and-semantics.md +++ b/book/src/syntax-and-semantics.md @@ -334,9 +334,18 @@ do { y } = Some { y = "" } Some y ``` -### Seq expressions +### Sequence expressions -`seq` expressions work just like `do` expressions, only they do not have a binding. +Sequence expressions work just like `do` expressions, only they do not have a binding. + +```f#,rust +let io @ { ? } = import! std.io +io.print "Hello" +io.print " " +io.println "world!" +``` + +For backwards compatibility it is also possible to write (`seq`) before each expression. ```f#,rust let io @ { ? } = import! std.io diff --git a/format/Cargo.toml b/format/Cargo.toml index f9b5e2e89a..cc3a8d7e69 100644 --- a/format/Cargo.toml +++ b/format/Cargo.toml @@ -22,6 +22,7 @@ gluon_base = { path = "../base", version = "0.17.1" } # GLUON [dev-dependencies] difference = "2" env_logger = "0.7" +expect-test = "1" futures = "0.3.1" pretty_assertions = "0.6" tokio = { version = "0.2", features = ["macros", "rt-core"] } diff --git a/format/src/pretty_print.rs b/format/src/pretty_print.rs index a9e4349e54..e76c367281 100644 --- a/format/src/pretty_print.rs +++ b/format/src/pretty_print.rs @@ -16,7 +16,7 @@ use base::{ metadata::Attribute, pos::{self, BytePos, HasSpan, Span, Spanned}, source, - types::{self, ArgType, AsId,Prec, Type}, + types::{self, ArgType, AsId, Prec, Type}, }; const INDENT: isize = 4; @@ -478,24 +478,29 @@ where ref bound, ref body, .. - }) => { - let from = match id { - Some(pattern) => chain![ + }) => match id { + Some(pattern) => { + let from = chain![ arena, "do", self.space_before(pattern.span.start()), self.pretty_pattern(pattern), self.space_after(pattern.span.end()), "=" - ], - None => arena.text("seq"), - }; - chain![ + ]; + chain![ + arena, + self.hang(from, (self.space_before(bound.span.start()), true), bound), + self.pretty_expr_(bound.span.end(), body) + ] + } + + None => chain![ arena, - self.hang(from, (self.space_before(bound.span.start()), true), bound), + self.pretty_expr_(bound.span.start(), bound), self.pretty_expr_(bound.span.end(), body) - ] - } + ], + }, Expr::MacroExpansion { ref original, .. } => { return self.pretty_expr_(previous_end, original); } diff --git a/format/tests/pretty_print.rs b/format/tests/pretty_print.rs index 856fa504ea..21aa9847f7 100644 --- a/format/tests/pretty_print.rs +++ b/format/tests/pretty_print.rs @@ -4,7 +4,7 @@ extern crate pretty_assertions; extern crate gluon_base as base; extern crate gluon_format as format; -use difference::assert_diff; +use {difference::assert_diff, expect_test::expect}; use gluon::{RootedThread, ThreadExt, VmBuilder}; @@ -813,3 +813,26 @@ let assert_success : [Show e] () "# } + +#[test] +fn sequence() { + let expr = r#" +// a +io.print "Hello" +// b +io.print " " +// c +io.println "World" +// d +"#; + expect![[r#" + + // a + io.print "Hello" + // b + io.print " " + // c + io.println "World" + // d + "#]].assert_eq(&format_expr(expr).unwrap()); +} diff --git a/std/parser.glu b/std/parser.glu index bb8c9fd7b9..5b8f8f1b01 100644 --- a/std/parser.glu +++ b/std/parser.glu @@ -218,7 +218,7 @@ rec let skip_many p : Parser a -> Parser () = skip_many1 p <|> wrap () /// Parses with `p` one or more times, ignoring the result of the parser let skip_many1 p : Parser a -> Parser () = - seq p + p skip_many p in /// Parses one of the characters of `s`