diff options
author | Marcus Klaas de Vries <[email protected]> | 2019-01-05 20:28:30 +0000 |
---|---|---|
committer | Marcus Klaas de Vries <[email protected]> | 2019-01-05 20:28:30 +0000 |
commit | 4fc233a02e8dc07619a969400c445ec47c2b1a9d (patch) | |
tree | dae33907a38b7c4ed9d1a63ff38803e99f1ae6df /crates/ra_syntax | |
parent | 3e42a158787955ff9f2e81be43479dbe8f2b1bb6 (diff) |
Implement type inference for boolean operators
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 39 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 10 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar.ron | 7 |
3 files changed, 54 insertions, 2 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index c10169d90..1bce6fa40 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -489,6 +489,45 @@ impl<'a> PrefixExpr<'a> { | |||
489 | } | 489 | } |
490 | 490 | ||
491 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | 491 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] |
492 | pub enum BinOp { | ||
493 | /// The `||` operator for boolean OR | ||
494 | BooleanOr, | ||
495 | /// The `&&` operator for boolean AND | ||
496 | BooleanAnd, | ||
497 | /// The `==` operator for equality testing | ||
498 | EqualityTest, | ||
499 | /// The `<=` operator for lesser-equal testing | ||
500 | LesserEqualTest, | ||
501 | /// The `>=` operator for greater-equal testing | ||
502 | GreaterEqualTest, | ||
503 | /// The `<` operator for comparison | ||
504 | LesserTest, | ||
505 | /// The `>` operator for comparison | ||
506 | GreaterTest, | ||
507 | // TODO: lots of others | ||
508 | } | ||
509 | |||
510 | impl<'a> BinExpr<'a> { | ||
511 | pub fn op(&self) -> Option<BinOp> { | ||
512 | self.syntax() | ||
513 | .children() | ||
514 | .filter_map(|c| { | ||
515 | match c.kind() { | ||
516 | PIPEPIPE => Some(BinOp::BooleanOr), | ||
517 | AMPAMP => Some(BinOp::BooleanAnd), | ||
518 | EQEQ => Some(BinOp::EqualityTest), | ||
519 | LTEQ => Some(BinOp::LesserEqualTest), | ||
520 | GTEQ => Some(BinOp::GreaterEqualTest), | ||
521 | L_ANGLE => Some(BinOp::LesserTest), | ||
522 | R_ANGLE => Some(BinOp::GreaterTest), | ||
523 | _ => None, | ||
524 | } | ||
525 | }) | ||
526 | .next() | ||
527 | } | ||
528 | } | ||
529 | |||
530 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | ||
492 | pub enum SelfParamFlavor { | 531 | pub enum SelfParamFlavor { |
493 | /// self | 532 | /// self |
494 | Owned, | 533 | Owned, |
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 7df6a9c46..ac320606d 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -217,7 +217,15 @@ impl<R: TreeRoot<RaTypes>> BinExprNode<R> { | |||
217 | } | 217 | } |
218 | 218 | ||
219 | 219 | ||
220 | impl<'a> BinExpr<'a> {} | 220 | impl<'a> BinExpr<'a> { |
221 | pub fn lhs(self) -> Option<Expr<'a>> { | ||
222 | super::child_opt(self) | ||
223 | } | ||
224 | |||
225 | pub fn rhs(self) -> Option<Expr<'a>> { | ||
226 | super::child_opt(self) | ||
227 | } | ||
228 | } | ||
221 | 229 | ||
222 | // BindPat | 230 | // BindPat |
223 | #[derive(Debug, Clone, Copy,)] | 231 | #[derive(Debug, Clone, Copy,)] |
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index c55e9e07a..e59961da3 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron | |||
@@ -422,7 +422,12 @@ Grammar( | |||
422 | "RefExpr": (options: ["Expr"]), | 422 | "RefExpr": (options: ["Expr"]), |
423 | "PrefixExpr": (options: ["Expr"]), | 423 | "PrefixExpr": (options: ["Expr"]), |
424 | "RangeExpr": (), | 424 | "RangeExpr": (), |
425 | "BinExpr": (), | 425 | "BinExpr": ( |
426 | options: [ | ||
427 | ["lhs", "Expr"], | ||
428 | ["rhs", "Expr"] | ||
429 | ] | ||
430 | ), | ||
426 | "String": (), | 431 | "String": (), |
427 | "Byte": (), | 432 | "Byte": (), |
428 | "ByteString": (), | 433 | "ByteString": (), |