Skip to content

Commit

Permalink
feat: Format seq expressions without seq
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Sep 20, 2020
1 parent bce5973 commit 5c0cec2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 15 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions book/src/syntax-and-semantics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions format/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
27 changes: 16 additions & 11 deletions format/src/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
25 changes: 24 additions & 1 deletion format/tests/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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());
}
2 changes: 1 addition & 1 deletion std/parser.glu
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 5c0cec2

Please sign in to comment.