From 4fc233a02e8dc07619a969400c445ec47c2b1a9d Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Sat, 5 Jan 2019 21:28:30 +0100 Subject: Implement type inference for boolean operators --- crates/ra_syntax/src/ast.rs | 39 +++++++++++++++++++++++++++++++++++ crates/ra_syntax/src/ast/generated.rs | 10 ++++++++- crates/ra_syntax/src/grammar.ron | 7 ++++++- 3 files changed, 54 insertions(+), 2 deletions(-) (limited to 'crates/ra_syntax/src') 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 @@ -488,6 +488,45 @@ impl<'a> PrefixExpr<'a> { } } +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +pub enum BinOp { + /// The `||` operator for boolean OR + BooleanOr, + /// The `&&` operator for boolean AND + BooleanAnd, + /// The `==` operator for equality testing + EqualityTest, + /// The `<=` operator for lesser-equal testing + LesserEqualTest, + /// The `>=` operator for greater-equal testing + GreaterEqualTest, + /// The `<` operator for comparison + LesserTest, + /// The `>` operator for comparison + GreaterTest, + // TODO: lots of others +} + +impl<'a> BinExpr<'a> { + pub fn op(&self) -> Option { + self.syntax() + .children() + .filter_map(|c| { + match c.kind() { + PIPEPIPE => Some(BinOp::BooleanOr), + AMPAMP => Some(BinOp::BooleanAnd), + EQEQ => Some(BinOp::EqualityTest), + LTEQ => Some(BinOp::LesserEqualTest), + GTEQ => Some(BinOp::GreaterEqualTest), + L_ANGLE => Some(BinOp::LesserTest), + R_ANGLE => Some(BinOp::GreaterTest), + _ => None, + } + }) + .next() + } +} + #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub enum SelfParamFlavor { /// self 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> BinExprNode { } -impl<'a> BinExpr<'a> {} +impl<'a> BinExpr<'a> { + pub fn lhs(self) -> Option> { + super::child_opt(self) + } + + pub fn rhs(self) -> Option> { + super::child_opt(self) + } +} // BindPat #[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( "RefExpr": (options: ["Expr"]), "PrefixExpr": (options: ["Expr"]), "RangeExpr": (), - "BinExpr": (), + "BinExpr": ( + options: [ + ["lhs", "Expr"], + ["rhs", "Expr"] + ] + ), "String": (), "Byte": (), "ByteString": (), -- cgit v1.2.3