Change if-expr syntax

use else instead of ->
This commit is contained in:
greg 2019-10-08 18:16:32 -07:00
parent a48bb61eb3
commit 22efd39114
2 changed files with 28 additions and 28 deletions

View File

@ -96,26 +96,26 @@ fn full_if_matching() {
let source = r#" let source = r#"
type Option<T> = Some(T) | None type Option<T> = Some(T) | None
let a = Option::None let a = Option::None
if a { is Option::None -> 4, is Option::Some(x) -> x } if a { is Option::None then 4, is Option::Some(x) then x }
"#; "#;
test_in_fresh_env!(source, "4"); test_in_fresh_env!(source, "4");
let source = r#" let source = r#"
type Option<T> = Some(T) | None type Option<T> = Some(T) | None
let a = Option::Some(99) let a = Option::Some(99)
if a { is Option::None -> 4, is Option::Some(x) -> x } if a { is Option::None then 4, is Option::Some(x) then x }
"#; "#;
test_in_fresh_env!(source, "99"); test_in_fresh_env!(source, "99");
let source = r#" let source = r#"
let a = 10 let a = 10
if a { is 10 -> "x", is 4 -> "y" } if a { is 10 then "x", is 4 then "y" }
"#; "#;
test_in_fresh_env!(source, "\"x\""); test_in_fresh_env!(source, "\"x\"");
let source = r#" let source = r#"
let a = 10 let a = 10
if a { is 15 -> "x", is 10 -> "y" } if a { is 15 then "x", is 10 then "y" }
"#; "#;
test_in_fresh_env!(source, "\"y\""); test_in_fresh_env!(source, "\"y\"");
} }
@ -124,7 +124,7 @@ test_in_fresh_env!(source, "\"y\"");
fn string_pattern() { fn string_pattern() {
let source = r#" let source = r#"
let a = "foo" let a = "foo"
if a { is "foo" -> "x", is _ -> "y" } if a { is "foo" then "x", is _ then "y" }
"#; "#;
test_in_fresh_env!(source, "\"x\""); test_in_fresh_env!(source, "\"x\"");
} }
@ -134,8 +134,8 @@ fn boolean_pattern() {
let source = r#" let source = r#"
let a = true let a = true
if a { if a {
is true -> "x", is true then "x",
is false -> "y" is false then "y"
} }
"#; "#;
test_in_fresh_env!(source, "\"x\""); test_in_fresh_env!(source, "\"x\"");
@ -145,7 +145,7 @@ test_in_fresh_env!(source, "\"x\"");
fn boolean_pattern_2() { fn boolean_pattern_2() {
let source = r#" let source = r#"
let a = false let a = false
if a { is true -> "x", is false -> "y" } if a { is true then "x", is false then "y" }
"#; "#;
test_in_fresh_env!(source, "\"y\""); test_in_fresh_env!(source, "\"y\"");
} }
@ -155,7 +155,7 @@ fn ignore_pattern() {
let source = r#" let source = r#"
type Option<T> = Some(T) | None type Option<T> = Some(T) | None
if Option::Some(10) { if Option::Some(10) {
is _ -> "hella" is _ then "hella"
} }
"#; "#;
test_in_fresh_env!(source, "\"hella\""); test_in_fresh_env!(source, "\"hella\"");
@ -165,8 +165,8 @@ test_in_fresh_env!(source, "\"hella\"");
fn tuple_pattern() { fn tuple_pattern() {
let source = r#" let source = r#"
if (1, 2) { if (1, 2) {
is (1, x) -> x, is (1, x) then x,
is _ -> 99 is _ then 99
} }
"#; "#;
test_in_fresh_env!(source, 2); test_in_fresh_env!(source, 2);
@ -177,8 +177,8 @@ test_in_fresh_env!(source, 2);
fn tuple_pattern_2() { fn tuple_pattern_2() {
let source = r#" let source = r#"
if (1, 2) { if (1, 2) {
is (10, x) -> x, is (10, x) then x,
is (y, x) -> x + y is (y, x) then x + y
} }
"#; "#;
test_in_fresh_env!(source, 3); test_in_fresh_env!(source, 3);
@ -188,8 +188,8 @@ test_in_fresh_env!(source, 3);
fn tuple_pattern_3() { fn tuple_pattern_3() {
let source = r#" let source = r#"
if (1, 5) { if (1, 5) {
is (10, x) -> x, is (10, x) then x,
is (1, x) -> x is (1, x) then x
} }
"#; "#;
test_in_fresh_env!(source, 5); test_in_fresh_env!(source, 5);
@ -199,8 +199,8 @@ test_in_fresh_env!(source, 5);
fn tuple_pattern_4() { fn tuple_pattern_4() {
let source = r#" let source = r#"
if (1, 5) { if (1, 5) {
is (10, x) -> x, is (10, x) then x,
is (1, x) -> x, is (1, x) then x,
} }
"#; "#;
test_in_fresh_env!(source, 5); test_in_fresh_env!(source, 5);
@ -215,21 +215,21 @@ let b = Stuff::Jugs(1, "haha")
let c = Stuff::Mardok let c = Stuff::Mardok
let x = if a { let x = if a {
is Stuff::Mulch(20) -> "x", is Stuff::Mulch(20) then "x",
is _ -> "ERR" is _ then "ERR"
} }
let y = if b { let y = if b {
is Stuff::Mulch(n) -> "ERR", is Stuff::Mulch(n) then "ERR",
is Stuff::Jugs(2, _) -> "ERR", is Stuff::Jugs(2, _) then "ERR",
is Stuff::Jugs(1, s) -> s, is Stuff::Jugs(1, s) then s,
is _ -> "ERR", is _ then "ERR",
} }
let z = if c { let z = if c {
is Stuff::Jugs(_, _) -> "ERR", is Stuff::Jugs(_, _) then "ERR",
is Stuff::Mardok -> "NIGH", is Stuff::Mardok then "NIGH",
is _ -> "ERR", is _ then "ERR",
} }
(x, y, z) (x, y, z)

View File

@ -122,7 +122,7 @@
//! simple_pattern_match := pattern "then" conditional //! simple_pattern_match := pattern "then" conditional
//! else_clause := ε | "else" expr_or_block //! else_clause := ε | "else" expr_or_block
//! guard_block := "{" (guard_arm, ",")* "}" //! guard_block := "{" (guard_arm, ",")* "}"
//! guard_arm := guard "->" expr_or_block //! guard_arm := guard "then" expr_or_block
//! guard := "is" pattern | (operator)+ precedence_expr //! guard := "is" pattern | (operator)+ precedence_expr
//! ``` //! ```
//! //!
@ -894,7 +894,7 @@ impl Parser {
#[recursive_descent_method] #[recursive_descent_method]
fn guard_arm(&mut self) -> ParseResult<GuardArm> { fn guard_arm(&mut self) -> ParseResult<GuardArm> {
let guard = self.guard()?; let guard = self.guard()?;
expect!(self, Operator(ref c) if **c == "->"); expect!(self, Keyword(Kw::Then));
let body = self.expr_or_block()?; let body = self.expr_or_block()?;
Ok(GuardArm { guard, body }) Ok(GuardArm { guard, body })
} }