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.rs | 46 +++++++++ crates/ra_syntax/src/ast/generated.rs | 132 ++++++++++++++++++------- crates/ra_syntax/src/grammar.ron | 11 ++- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/syntax_kinds/generated.rs | 2 - 5 files changed, 148 insertions(+), 45 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 8bf439b60..211ba31e5 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -609,6 +609,52 @@ impl SelfParam { } } +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum LiteralFlavor { + String, + ByteString, + Char, + Byte, + IntNumber { suffix: Option }, + FloatNumber { suffix: Option }, + Bool, +} + +impl LiteralExpr { + pub fn flavor(&self) -> LiteralFlavor { + let syntax = self.syntax(); + match syntax.kind() { + INT_NUMBER => { + let allowed_suffix_list = [ + "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32", + "u16", "u8", + ]; + let text = syntax.text().to_string(); + let suffix = allowed_suffix_list + .iter() + .find(|&s| text.ends_with(s)) + .map(|&suf| SmolStr::new(suf)); + LiteralFlavor::IntNumber { suffix: suffix } + } + FLOAT_NUMBER => { + let allowed_suffix_list = ["f64", "f32"]; + let text = syntax.text().to_string(); + let suffix = allowed_suffix_list + .iter() + .find(|&s| text.ends_with(s)) + .map(|&suf| SmolStr::new(suf)); + LiteralFlavor::FloatNumber { suffix: suffix } + } + STRING | RAW_STRING => LiteralFlavor::String, + TRUE_KW | FALSE_KW => LiteralFlavor::Bool, + BYTE_STRING | RAW_BYTE_STRING => LiteralFlavor::ByteString, + CHAR => LiteralFlavor::Char, + BYTE => LiteralFlavor::Byte, + _ => unreachable!(), + } + } +} + #[test] fn test_doc_comment_of_items() { let file = SourceFile::parse( 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)] diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 49b297696..34d2a27d1 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -215,7 +215,6 @@ Grammar( "PATH", "PATH_SEGMENT", "LITERAL", - "LITERAL_EXPR", "ALIAS", "VISIBILITY", "WHERE_CLAUSE", @@ -434,7 +433,9 @@ Grammar( "Byte": ( traits: ["AstToken"] ), "ByteString": ( traits: ["AstToken"] ), "Char": ( traits: ["AstToken"] ), - "Literal": ( + "TrueKw": ( traits: ["AstToken"] ), + "FalseKw": ( traits: ["AstToken"] ), + "LiteralExpr": ( enum: [ "String", "ByteString", @@ -442,9 +443,11 @@ Grammar( "Byte", "IntNumber", "FloatNumber", + "TrueKw", + "FalseKw", ] ), - "LiteralExpr": (options: ["Literal"]), + "Literal": (options: ["LiteralExpr"]), "Expr": ( enum: [ @@ -474,7 +477,7 @@ Grammar( "PrefixExpr", "RangeExpr", "BinExpr", - "LiteralExpr", + "Literal", ], ), diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 6181df9d7..bc311cbbc 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -59,7 +59,7 @@ impl SourceFile { assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); TreeArc::cast(root) } - + pub fn parse(text: &str) -> TreeArc { let tokens = tokenize(&text); let (green, errors) = diff --git a/crates/ra_syntax/src/syntax_kinds/generated.rs b/crates/ra_syntax/src/syntax_kinds/generated.rs index 46795d6e9..830fac9f4 100644 --- a/crates/ra_syntax/src/syntax_kinds/generated.rs +++ b/crates/ra_syntax/src/syntax_kinds/generated.rs @@ -205,7 +205,6 @@ pub enum SyntaxKind { PATH, PATH_SEGMENT, LITERAL, - LITERAL_EXPR, ALIAS, VISIBILITY, WHERE_CLAUSE, @@ -468,7 +467,6 @@ impl SyntaxKind { PATH => &SyntaxInfo { name: "PATH" }, PATH_SEGMENT => &SyntaxInfo { name: "PATH_SEGMENT" }, LITERAL => &SyntaxInfo { name: "LITERAL" }, - LITERAL_EXPR => &SyntaxInfo { name: "LITERAL_EXPR" }, ALIAS => &SyntaxInfo { name: "ALIAS" }, VISIBILITY => &SyntaxInfo { name: "VISIBILITY" }, WHERE_CLAUSE => &SyntaxInfo { name: "WHERE_CLAUSE" }, -- cgit v1.2.3