From 606d66a714bb8fe07f35a6af83d04ab576b9a0e1 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Fri, 11 Jan 2019 11:27:07 +0100 Subject: Start moving literal interpretation to the AST (WIP) --- crates/ra_syntax/src/ast/generated.rs | 114 ++++++++++++++++++++++++++++++++-- 1 file changed, 110 insertions(+), 4 deletions(-) (limited to 'crates/ra_syntax/src/ast') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 94842a514..442659aee 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -664,7 +664,7 @@ pub enum ExprKind<'a> { PrefixExpr(&'a PrefixExpr), RangeExpr(&'a RangeExpr), BinExpr(&'a BinExpr), - Literal(&'a Literal), + LiteralExpr(&'a LiteralExpr), } impl AstNode for Expr { @@ -696,7 +696,7 @@ impl AstNode for Expr { | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR - | LITERAL => Some(Expr::from_repr(syntax.into_repr())), + | LITERAL_EXPR => Some(Expr::from_repr(syntax.into_repr())), _ => None, } } @@ -733,7 +733,7 @@ impl Expr { PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr::cast(&self.syntax).unwrap()), RANGE_EXPR => ExprKind::RangeExpr(RangeExpr::cast(&self.syntax).unwrap()), BIN_EXPR => ExprKind::BinExpr(BinExpr::cast(&self.syntax).unwrap()), - LITERAL => ExprKind::Literal(Literal::cast(&self.syntax).unwrap()), + LITERAL_EXPR => ExprKind::LiteralExpr(LiteralExpr::cast(&self.syntax).unwrap()), _ => unreachable!(), } } @@ -849,6 +849,31 @@ impl AstNode for FieldPatList { impl FieldPatList {} +// FloatNumber +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct FloatNumber { + pub(crate) syntax: SyntaxNode, +} +unsafe impl TransparentNewType for FloatNumber { + type Repr = rowan::SyntaxNode; +} + +impl AstNode for FloatNumber { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { + match syntax.kind() { + FLOAT_NUMBER => Some(FloatNumber::from_repr(syntax.into_repr())), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } +} + + +impl ast::AstToken for FloatNumber {} +impl FloatNumber {} + // FnDef #[derive(Debug, PartialEq, Eq, Hash)] #[repr(transparent)] @@ -1130,6 +1155,31 @@ impl AstNode for IndexExpr { impl IndexExpr {} +// IntNumber +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct IntNumber { + pub(crate) syntax: SyntaxNode, +} +unsafe impl TransparentNewType for IntNumber { + type Repr = rowan::SyntaxNode; +} + +impl AstNode for IntNumber { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { + match syntax.kind() { + INT_NUMBER => Some(IntNumber::from_repr(syntax.into_repr())), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } +} + + +impl ast::AstToken for IntNumber {} +impl IntNumber {} + // ItemList #[derive(Debug, PartialEq, Eq, Hash)] #[repr(transparent)] @@ -1315,10 +1365,25 @@ unsafe impl TransparentNewType for Literal { type Repr = rowan::SyntaxNode; } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum LiteralKind<'a> { + String(&'a String), + ByteString(&'a ByteString), + Char(&'a Char), + Byte(&'a Byte), + IntNumber(&'a IntNumber), + FloatNumber(&'a FloatNumber), +} + impl AstNode for Literal { fn cast(syntax: &SyntaxNode) -> Option<&Self> { match syntax.kind() { - LITERAL => Some(Literal::from_repr(syntax.into_repr())), + | STRING + | BYTE_STRING + | CHAR + | BYTE + | INT_NUMBER + | FLOAT_NUMBER => Some(Literal::from_repr(syntax.into_repr())), _ => None, } } @@ -1326,9 +1391,50 @@ impl AstNode for Literal { fn to_owned(&self) -> TreeArc { TreeArc::cast(self.syntax.to_owned()) } } +impl Literal { + pub fn kind(&self) -> LiteralKind { + match self.syntax.kind() { + STRING => LiteralKind::String(String::cast(&self.syntax).unwrap()), + BYTE_STRING => LiteralKind::ByteString(ByteString::cast(&self.syntax).unwrap()), + CHAR => LiteralKind::Char(Char::cast(&self.syntax).unwrap()), + BYTE => LiteralKind::Byte(Byte::cast(&self.syntax).unwrap()), + INT_NUMBER => LiteralKind::IntNumber(IntNumber::cast(&self.syntax).unwrap()), + FLOAT_NUMBER => LiteralKind::FloatNumber(FloatNumber::cast(&self.syntax).unwrap()), + _ => unreachable!(), + } + } +} impl Literal {} +// LiteralExpr +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct LiteralExpr { + pub(crate) syntax: SyntaxNode, +} +unsafe impl TransparentNewType for LiteralExpr { + type Repr = rowan::SyntaxNode; +} + +impl AstNode for LiteralExpr { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { + match syntax.kind() { + LITERAL_EXPR => Some(LiteralExpr::from_repr(syntax.into_repr())), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } +} + + +impl LiteralExpr { + pub fn literal(&self) -> Option<&Literal> { + super::child_opt(self) + } +} + // LoopExpr #[derive(Debug, PartialEq, Eq, Hash)] #[repr(transparent)] -- cgit v1.2.3 From a9a6a50c759302e8a8d59bf6c53c72ec804324b3 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Mon, 14 Jan 2019 19:30:21 +0100 Subject: Fixup tests --- crates/ra_syntax/src/ast/generated.rs | 132 ++++++++++++++++++++++++---------- 1 file changed, 94 insertions(+), 38 deletions(-) (limited to 'crates/ra_syntax/src/ast') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 442659aee..cad845ec0 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -664,7 +664,7 @@ pub enum ExprKind<'a> { PrefixExpr(&'a PrefixExpr), RangeExpr(&'a RangeExpr), BinExpr(&'a BinExpr), - LiteralExpr(&'a LiteralExpr), + Literal(&'a Literal), } impl AstNode for Expr { @@ -696,7 +696,7 @@ impl AstNode for Expr { | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR - | LITERAL_EXPR => Some(Expr::from_repr(syntax.into_repr())), + | LITERAL => Some(Expr::from_repr(syntax.into_repr())), _ => None, } } @@ -733,7 +733,7 @@ impl Expr { PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr::cast(&self.syntax).unwrap()), RANGE_EXPR => ExprKind::RangeExpr(RangeExpr::cast(&self.syntax).unwrap()), BIN_EXPR => ExprKind::BinExpr(BinExpr::cast(&self.syntax).unwrap()), - LITERAL_EXPR => ExprKind::LiteralExpr(LiteralExpr::cast(&self.syntax).unwrap()), + LITERAL => ExprKind::Literal(Literal::cast(&self.syntax).unwrap()), _ => unreachable!(), } } @@ -793,6 +793,31 @@ impl AstNode for ExternCrateItem { impl ExternCrateItem {} +// FalseKw +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct FalseKw { + pub(crate) syntax: SyntaxNode, +} +unsafe impl TransparentNewType for FalseKw { + type Repr = rowan::SyntaxNode; +} + +impl AstNode for FalseKw { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { + match syntax.kind() { + FALSE_KW => Some(FalseKw::from_repr(syntax.into_repr())), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreeArc { TreeArc::cast(self.syntax.to_owned()) } +} + + +impl ast::AstToken for FalseKw {} +impl FalseKw {} + // FieldExpr #[derive(Debug, PartialEq, Eq, Hash)] #[repr(transparent)] @@ -867,7 +892,7 @@ impl AstNode for FloatNumber { } } fn syntax(&self) -> &SyntaxNode { &self.syntax } - fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } + fn to_owned(&self) -> TreeArc { TreeArc::cast(self.syntax.to_owned()) } } @@ -1173,7 +1198,7 @@ impl AstNode for IntNumber { } } fn syntax(&self) -> &SyntaxNode { &self.syntax } - fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } + fn to_owned(&self) -> TreeArc { TreeArc::cast(self.syntax.to_owned()) } } @@ -1365,25 +1390,10 @@ unsafe impl TransparentNewType for Literal { type Repr = rowan::SyntaxNode; } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum LiteralKind<'a> { - String(&'a String), - ByteString(&'a ByteString), - Char(&'a Char), - Byte(&'a Byte), - IntNumber(&'a IntNumber), - FloatNumber(&'a FloatNumber), -} - impl AstNode for Literal { fn cast(syntax: &SyntaxNode) -> Option<&Self> { match syntax.kind() { - | STRING - | BYTE_STRING - | CHAR - | BYTE - | INT_NUMBER - | FLOAT_NUMBER => Some(Literal::from_repr(syntax.into_repr())), + LITERAL => Some(Literal::from_repr(syntax.into_repr())), _ => None, } } @@ -1391,22 +1401,13 @@ impl AstNode for Literal { fn to_owned(&self) -> TreeArc { TreeArc::cast(self.syntax.to_owned()) } } + impl Literal { - pub fn kind(&self) -> LiteralKind { - match self.syntax.kind() { - STRING => LiteralKind::String(String::cast(&self.syntax).unwrap()), - BYTE_STRING => LiteralKind::ByteString(ByteString::cast(&self.syntax).unwrap()), - CHAR => LiteralKind::Char(Char::cast(&self.syntax).unwrap()), - BYTE => LiteralKind::Byte(Byte::cast(&self.syntax).unwrap()), - INT_NUMBER => LiteralKind::IntNumber(IntNumber::cast(&self.syntax).unwrap()), - FLOAT_NUMBER => LiteralKind::FloatNumber(FloatNumber::cast(&self.syntax).unwrap()), - _ => unreachable!(), - } + pub fn literal_expr(&self) -> Option<&LiteralExpr> { + super::child_opt(self) } } -impl Literal {} - // LiteralExpr #[derive(Debug, PartialEq, Eq, Hash)] #[repr(transparent)] @@ -1417,24 +1418,54 @@ unsafe impl TransparentNewType for LiteralExpr { type Repr = rowan::SyntaxNode; } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum LiteralExprKind<'a> { + String(&'a String), + ByteString(&'a ByteString), + Char(&'a Char), + Byte(&'a Byte), + IntNumber(&'a IntNumber), + FloatNumber(&'a FloatNumber), + TrueKw(&'a TrueKw), + FalseKw(&'a FalseKw), +} + impl AstNode for LiteralExpr { fn cast(syntax: &SyntaxNode) -> Option<&Self> { match syntax.kind() { - LITERAL_EXPR => Some(LiteralExpr::from_repr(syntax.into_repr())), + | STRING + | BYTE_STRING + | CHAR + | BYTE + | INT_NUMBER + | FLOAT_NUMBER + | TRUE_KW + | FALSE_KW => Some(LiteralExpr::from_repr(syntax.into_repr())), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } - fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } + fn to_owned(&self) -> TreeArc { TreeArc::cast(self.syntax.to_owned()) } } - impl LiteralExpr { - pub fn literal(&self) -> Option<&Literal> { - super::child_opt(self) + pub fn kind(&self) -> LiteralExprKind { + match self.syntax.kind() { + STRING => LiteralExprKind::String(String::cast(&self.syntax).unwrap()), + BYTE_STRING => LiteralExprKind::ByteString(ByteString::cast(&self.syntax).unwrap()), + CHAR => LiteralExprKind::Char(Char::cast(&self.syntax).unwrap()), + BYTE => LiteralExprKind::Byte(Byte::cast(&self.syntax).unwrap()), + INT_NUMBER => LiteralExprKind::IntNumber(IntNumber::cast(&self.syntax).unwrap()), + FLOAT_NUMBER => LiteralExprKind::FloatNumber(FloatNumber::cast(&self.syntax).unwrap()), + TRUE_KW => LiteralExprKind::TrueKw(TrueKw::cast(&self.syntax).unwrap()), + FALSE_KW => LiteralExprKind::FalseKw(FalseKw::cast(&self.syntax).unwrap()), + _ => unreachable!(), + } } } +impl LiteralExpr {} + // LoopExpr #[derive(Debug, PartialEq, Eq, Hash)] #[repr(transparent)] @@ -3025,6 +3056,31 @@ impl ast::AttrsOwner for TraitDef {} impl ast::DocCommentsOwner for TraitDef {} impl TraitDef {} +// TrueKw +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct TrueKw { + pub(crate) syntax: SyntaxNode, +} +unsafe impl TransparentNewType for TrueKw { + type Repr = rowan::SyntaxNode; +} + +impl AstNode for TrueKw { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { + match syntax.kind() { + TRUE_KW => Some(TrueKw::from_repr(syntax.into_repr())), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreeArc { TreeArc::cast(self.syntax.to_owned()) } +} + + +impl ast::AstToken for TrueKw {} +impl TrueKw {} + // TryExpr #[derive(Debug, PartialEq, Eq, Hash)] #[repr(transparent)] -- cgit v1.2.3 From d67eabb512a08a451a649c7f20e4e9ae1860a8a0 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Mon, 14 Jan 2019 20:56:14 +0100 Subject: Fix type inference for raw (byte) strings --- crates/ra_syntax/src/ast/generated.rs | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'crates/ra_syntax/src/ast') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index cad845ec0..3471d5226 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1422,6 +1422,8 @@ unsafe impl TransparentNewType for LiteralExpr { pub enum LiteralExprKind<'a> { String(&'a String), ByteString(&'a ByteString), + RawString(&'a RawString), + RawByteString(&'a RawByteString), Char(&'a Char), Byte(&'a Byte), IntNumber(&'a IntNumber), @@ -1435,6 +1437,8 @@ impl AstNode for LiteralExpr { match syntax.kind() { | STRING | BYTE_STRING + | RAW_STRING + | RAW_BYTE_STRING | CHAR | BYTE | INT_NUMBER @@ -1453,6 +1457,8 @@ impl LiteralExpr { match self.syntax.kind() { STRING => LiteralExprKind::String(String::cast(&self.syntax).unwrap()), BYTE_STRING => LiteralExprKind::ByteString(ByteString::cast(&self.syntax).unwrap()), + RAW_STRING => LiteralExprKind::RawString(RawString::cast(&self.syntax).unwrap()), + RAW_BYTE_STRING => LiteralExprKind::RawByteString(RawByteString::cast(&self.syntax).unwrap()), CHAR => LiteralExprKind::Char(Char::cast(&self.syntax).unwrap()), BYTE => LiteralExprKind::Byte(Byte::cast(&self.syntax).unwrap()), INT_NUMBER => LiteralExprKind::IntNumber(IntNumber::cast(&self.syntax).unwrap()), @@ -2543,6 +2549,56 @@ impl AstNode for RangePat { impl RangePat {} +// RawByteString +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct RawByteString { + pub(crate) syntax: SyntaxNode, +} +unsafe impl TransparentNewType for RawByteString { + type Repr = rowan::SyntaxNode; +} + +impl AstNode for RawByteString { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { + match syntax.kind() { + RAW_BYTE_STRING => Some(RawByteString::from_repr(syntax.into_repr())), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreeArc { TreeArc::cast(self.syntax.to_owned()) } +} + + +impl ast::AstToken for RawByteString {} +impl RawByteString {} + +// RawString +#[derive(Debug, PartialEq, Eq, Hash)] +#[repr(transparent)] +pub struct RawString { + pub(crate) syntax: SyntaxNode, +} +unsafe impl TransparentNewType for RawString { + type Repr = rowan::SyntaxNode; +} + +impl AstNode for RawString { + fn cast(syntax: &SyntaxNode) -> Option<&Self> { + match syntax.kind() { + RAW_STRING => Some(RawString::from_repr(syntax.into_repr())), + _ => None, + } + } + fn syntax(&self) -> &SyntaxNode { &self.syntax } + fn to_owned(&self) -> TreeArc { TreeArc::cast(self.syntax.to_owned()) } +} + + +impl ast::AstToken for RawString {} +impl RawString {} + // RefExpr #[derive(Debug, PartialEq, Eq, Hash)] #[repr(transparent)] -- cgit v1.2.3