diff options
author | Marcus Klaas de Vries <[email protected]> | 2019-01-14 18:30:21 +0000 |
---|---|---|
committer | Marcus Klaas de Vries <[email protected]> | 2019-01-14 18:30:21 +0000 |
commit | a9a6a50c759302e8a8d59bf6c53c72ec804324b3 (patch) | |
tree | ecd22d01bca8bfb2419732621140c3271277973d /crates/ra_syntax/src | |
parent | 606d66a714bb8fe07f35a6af83d04ab576b9a0e1 (diff) |
Fixup tests
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 46 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 132 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar.ron | 11 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_kinds/generated.rs | 2 |
5 files changed, 148 insertions, 45 deletions
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 { | |||
609 | } | 609 | } |
610 | } | 610 | } |
611 | 611 | ||
612 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
613 | pub enum LiteralFlavor { | ||
614 | String, | ||
615 | ByteString, | ||
616 | Char, | ||
617 | Byte, | ||
618 | IntNumber { suffix: Option<SmolStr> }, | ||
619 | FloatNumber { suffix: Option<SmolStr> }, | ||
620 | Bool, | ||
621 | } | ||
622 | |||
623 | impl LiteralExpr { | ||
624 | pub fn flavor(&self) -> LiteralFlavor { | ||
625 | let syntax = self.syntax(); | ||
626 | match syntax.kind() { | ||
627 | INT_NUMBER => { | ||
628 | let allowed_suffix_list = [ | ||
629 | "isize", "i128", "i64", "i32", "i16", "i8", "usize", "u128", "u64", "u32", | ||
630 | "u16", "u8", | ||
631 | ]; | ||
632 | let text = syntax.text().to_string(); | ||
633 | let suffix = allowed_suffix_list | ||
634 | .iter() | ||
635 | .find(|&s| text.ends_with(s)) | ||
636 | .map(|&suf| SmolStr::new(suf)); | ||
637 | LiteralFlavor::IntNumber { suffix: suffix } | ||
638 | } | ||
639 | FLOAT_NUMBER => { | ||
640 | let allowed_suffix_list = ["f64", "f32"]; | ||
641 | let text = syntax.text().to_string(); | ||
642 | let suffix = allowed_suffix_list | ||
643 | .iter() | ||
644 | .find(|&s| text.ends_with(s)) | ||
645 | .map(|&suf| SmolStr::new(suf)); | ||
646 | LiteralFlavor::FloatNumber { suffix: suffix } | ||
647 | } | ||
648 | STRING | RAW_STRING => LiteralFlavor::String, | ||
649 | TRUE_KW | FALSE_KW => LiteralFlavor::Bool, | ||
650 | BYTE_STRING | RAW_BYTE_STRING => LiteralFlavor::ByteString, | ||
651 | CHAR => LiteralFlavor::Char, | ||
652 | BYTE => LiteralFlavor::Byte, | ||
653 | _ => unreachable!(), | ||
654 | } | ||
655 | } | ||
656 | } | ||
657 | |||
612 | #[test] | 658 | #[test] |
613 | fn test_doc_comment_of_items() { | 659 | fn test_doc_comment_of_items() { |
614 | let file = SourceFile::parse( | 660 | 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> { | |||
664 | PrefixExpr(&'a PrefixExpr), | 664 | PrefixExpr(&'a PrefixExpr), |
665 | RangeExpr(&'a RangeExpr), | 665 | RangeExpr(&'a RangeExpr), |
666 | BinExpr(&'a BinExpr), | 666 | BinExpr(&'a BinExpr), |
667 | LiteralExpr(&'a LiteralExpr), | 667 | Literal(&'a Literal), |
668 | } | 668 | } |
669 | 669 | ||
670 | impl AstNode for Expr { | 670 | impl AstNode for Expr { |
@@ -696,7 +696,7 @@ impl AstNode for Expr { | |||
696 | | PREFIX_EXPR | 696 | | PREFIX_EXPR |
697 | | RANGE_EXPR | 697 | | RANGE_EXPR |
698 | | BIN_EXPR | 698 | | BIN_EXPR |
699 | | LITERAL_EXPR => Some(Expr::from_repr(syntax.into_repr())), | 699 | | LITERAL => Some(Expr::from_repr(syntax.into_repr())), |
700 | _ => None, | 700 | _ => None, |
701 | } | 701 | } |
702 | } | 702 | } |
@@ -733,7 +733,7 @@ impl Expr { | |||
733 | PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr::cast(&self.syntax).unwrap()), | 733 | PREFIX_EXPR => ExprKind::PrefixExpr(PrefixExpr::cast(&self.syntax).unwrap()), |
734 | RANGE_EXPR => ExprKind::RangeExpr(RangeExpr::cast(&self.syntax).unwrap()), | 734 | RANGE_EXPR => ExprKind::RangeExpr(RangeExpr::cast(&self.syntax).unwrap()), |
735 | BIN_EXPR => ExprKind::BinExpr(BinExpr::cast(&self.syntax).unwrap()), | 735 | BIN_EXPR => ExprKind::BinExpr(BinExpr::cast(&self.syntax).unwrap()), |
736 | LITERAL_EXPR => ExprKind::LiteralExpr(LiteralExpr::cast(&self.syntax).unwrap()), | 736 | LITERAL => ExprKind::Literal(Literal::cast(&self.syntax).unwrap()), |
737 | _ => unreachable!(), | 737 | _ => unreachable!(), |
738 | } | 738 | } |
739 | } | 739 | } |
@@ -793,6 +793,31 @@ impl AstNode for ExternCrateItem { | |||
793 | 793 | ||
794 | impl ExternCrateItem {} | 794 | impl ExternCrateItem {} |
795 | 795 | ||
796 | // FalseKw | ||
797 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
798 | #[repr(transparent)] | ||
799 | pub struct FalseKw { | ||
800 | pub(crate) syntax: SyntaxNode, | ||
801 | } | ||
802 | unsafe impl TransparentNewType for FalseKw { | ||
803 | type Repr = rowan::SyntaxNode<RaTypes>; | ||
804 | } | ||
805 | |||
806 | impl AstNode for FalseKw { | ||
807 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | ||
808 | match syntax.kind() { | ||
809 | FALSE_KW => Some(FalseKw::from_repr(syntax.into_repr())), | ||
810 | _ => None, | ||
811 | } | ||
812 | } | ||
813 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
814 | fn to_owned(&self) -> TreeArc<FalseKw> { TreeArc::cast(self.syntax.to_owned()) } | ||
815 | } | ||
816 | |||
817 | |||
818 | impl ast::AstToken for FalseKw {} | ||
819 | impl FalseKw {} | ||
820 | |||
796 | // FieldExpr | 821 | // FieldExpr |
797 | #[derive(Debug, PartialEq, Eq, Hash)] | 822 | #[derive(Debug, PartialEq, Eq, Hash)] |
798 | #[repr(transparent)] | 823 | #[repr(transparent)] |
@@ -867,7 +892,7 @@ impl AstNode for FloatNumber { | |||
867 | } | 892 | } |
868 | } | 893 | } |
869 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | 894 | fn syntax(&self) -> &SyntaxNode { &self.syntax } |
870 | fn to_owned(&self) -> TreePtr<FloatNumber> { TreePtr::cast(self.syntax.to_owned()) } | 895 | fn to_owned(&self) -> TreeArc<FloatNumber> { TreeArc::cast(self.syntax.to_owned()) } |
871 | } | 896 | } |
872 | 897 | ||
873 | 898 | ||
@@ -1173,7 +1198,7 @@ impl AstNode for IntNumber { | |||
1173 | } | 1198 | } |
1174 | } | 1199 | } |
1175 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | 1200 | fn syntax(&self) -> &SyntaxNode { &self.syntax } |
1176 | fn to_owned(&self) -> TreePtr<IntNumber> { TreePtr::cast(self.syntax.to_owned()) } | 1201 | fn to_owned(&self) -> TreeArc<IntNumber> { TreeArc::cast(self.syntax.to_owned()) } |
1177 | } | 1202 | } |
1178 | 1203 | ||
1179 | 1204 | ||
@@ -1365,25 +1390,10 @@ unsafe impl TransparentNewType for Literal { | |||
1365 | type Repr = rowan::SyntaxNode<RaTypes>; | 1390 | type Repr = rowan::SyntaxNode<RaTypes>; |
1366 | } | 1391 | } |
1367 | 1392 | ||
1368 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
1369 | pub enum LiteralKind<'a> { | ||
1370 | String(&'a String), | ||
1371 | ByteString(&'a ByteString), | ||
1372 | Char(&'a Char), | ||
1373 | Byte(&'a Byte), | ||
1374 | IntNumber(&'a IntNumber), | ||
1375 | FloatNumber(&'a FloatNumber), | ||
1376 | } | ||
1377 | |||
1378 | impl AstNode for Literal { | 1393 | impl AstNode for Literal { |
1379 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | 1394 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { |
1380 | match syntax.kind() { | 1395 | match syntax.kind() { |
1381 | | STRING | 1396 | LITERAL => Some(Literal::from_repr(syntax.into_repr())), |
1382 | | BYTE_STRING | ||
1383 | | CHAR | ||
1384 | | BYTE | ||
1385 | | INT_NUMBER | ||
1386 | | FLOAT_NUMBER => Some(Literal::from_repr(syntax.into_repr())), | ||
1387 | _ => None, | 1397 | _ => None, |
1388 | } | 1398 | } |
1389 | } | 1399 | } |
@@ -1391,22 +1401,13 @@ impl AstNode for Literal { | |||
1391 | fn to_owned(&self) -> TreeArc<Literal> { TreeArc::cast(self.syntax.to_owned()) } | 1401 | fn to_owned(&self) -> TreeArc<Literal> { TreeArc::cast(self.syntax.to_owned()) } |
1392 | } | 1402 | } |
1393 | 1403 | ||
1404 | |||
1394 | impl Literal { | 1405 | impl Literal { |
1395 | pub fn kind(&self) -> LiteralKind { | 1406 | pub fn literal_expr(&self) -> Option<&LiteralExpr> { |
1396 | match self.syntax.kind() { | 1407 | super::child_opt(self) |
1397 | STRING => LiteralKind::String(String::cast(&self.syntax).unwrap()), | ||
1398 | BYTE_STRING => LiteralKind::ByteString(ByteString::cast(&self.syntax).unwrap()), | ||
1399 | CHAR => LiteralKind::Char(Char::cast(&self.syntax).unwrap()), | ||
1400 | BYTE => LiteralKind::Byte(Byte::cast(&self.syntax).unwrap()), | ||
1401 | INT_NUMBER => LiteralKind::IntNumber(IntNumber::cast(&self.syntax).unwrap()), | ||
1402 | FLOAT_NUMBER => LiteralKind::FloatNumber(FloatNumber::cast(&self.syntax).unwrap()), | ||
1403 | _ => unreachable!(), | ||
1404 | } | ||
1405 | } | 1408 | } |
1406 | } | 1409 | } |
1407 | 1410 | ||
1408 | impl Literal {} | ||
1409 | |||
1410 | // LiteralExpr | 1411 | // LiteralExpr |
1411 | #[derive(Debug, PartialEq, Eq, Hash)] | 1412 | #[derive(Debug, PartialEq, Eq, Hash)] |
1412 | #[repr(transparent)] | 1413 | #[repr(transparent)] |
@@ -1417,24 +1418,54 @@ unsafe impl TransparentNewType for LiteralExpr { | |||
1417 | type Repr = rowan::SyntaxNode<RaTypes>; | 1418 | type Repr = rowan::SyntaxNode<RaTypes>; |
1418 | } | 1419 | } |
1419 | 1420 | ||
1421 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
1422 | pub enum LiteralExprKind<'a> { | ||
1423 | String(&'a String), | ||
1424 | ByteString(&'a ByteString), | ||
1425 | Char(&'a Char), | ||
1426 | Byte(&'a Byte), | ||
1427 | IntNumber(&'a IntNumber), | ||
1428 | FloatNumber(&'a FloatNumber), | ||
1429 | TrueKw(&'a TrueKw), | ||
1430 | FalseKw(&'a FalseKw), | ||
1431 | } | ||
1432 | |||
1420 | impl AstNode for LiteralExpr { | 1433 | impl AstNode for LiteralExpr { |
1421 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | 1434 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { |
1422 | match syntax.kind() { | 1435 | match syntax.kind() { |
1423 | LITERAL_EXPR => Some(LiteralExpr::from_repr(syntax.into_repr())), | 1436 | | STRING |
1437 | | BYTE_STRING | ||
1438 | | CHAR | ||
1439 | | BYTE | ||
1440 | | INT_NUMBER | ||
1441 | | FLOAT_NUMBER | ||
1442 | | TRUE_KW | ||
1443 | | FALSE_KW => Some(LiteralExpr::from_repr(syntax.into_repr())), | ||
1424 | _ => None, | 1444 | _ => None, |
1425 | } | 1445 | } |
1426 | } | 1446 | } |
1427 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | 1447 | fn syntax(&self) -> &SyntaxNode { &self.syntax } |
1428 | fn to_owned(&self) -> TreePtr<LiteralExpr> { TreePtr::cast(self.syntax.to_owned()) } | 1448 | fn to_owned(&self) -> TreeArc<LiteralExpr> { TreeArc::cast(self.syntax.to_owned()) } |
1429 | } | 1449 | } |
1430 | 1450 | ||
1431 | |||
1432 | impl LiteralExpr { | 1451 | impl LiteralExpr { |
1433 | pub fn literal(&self) -> Option<&Literal> { | 1452 | pub fn kind(&self) -> LiteralExprKind { |
1434 | super::child_opt(self) | 1453 | match self.syntax.kind() { |
1454 | STRING => LiteralExprKind::String(String::cast(&self.syntax).unwrap()), | ||
1455 | BYTE_STRING => LiteralExprKind::ByteString(ByteString::cast(&self.syntax).unwrap()), | ||
1456 | CHAR => LiteralExprKind::Char(Char::cast(&self.syntax).unwrap()), | ||
1457 | BYTE => LiteralExprKind::Byte(Byte::cast(&self.syntax).unwrap()), | ||
1458 | INT_NUMBER => LiteralExprKind::IntNumber(IntNumber::cast(&self.syntax).unwrap()), | ||
1459 | FLOAT_NUMBER => LiteralExprKind::FloatNumber(FloatNumber::cast(&self.syntax).unwrap()), | ||
1460 | TRUE_KW => LiteralExprKind::TrueKw(TrueKw::cast(&self.syntax).unwrap()), | ||
1461 | FALSE_KW => LiteralExprKind::FalseKw(FalseKw::cast(&self.syntax).unwrap()), | ||
1462 | _ => unreachable!(), | ||
1463 | } | ||
1435 | } | 1464 | } |
1436 | } | 1465 | } |
1437 | 1466 | ||
1467 | impl LiteralExpr {} | ||
1468 | |||
1438 | // LoopExpr | 1469 | // LoopExpr |
1439 | #[derive(Debug, PartialEq, Eq, Hash)] | 1470 | #[derive(Debug, PartialEq, Eq, Hash)] |
1440 | #[repr(transparent)] | 1471 | #[repr(transparent)] |
@@ -3025,6 +3056,31 @@ impl ast::AttrsOwner for TraitDef {} | |||
3025 | impl ast::DocCommentsOwner for TraitDef {} | 3056 | impl ast::DocCommentsOwner for TraitDef {} |
3026 | impl TraitDef {} | 3057 | impl TraitDef {} |
3027 | 3058 | ||
3059 | // TrueKw | ||
3060 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
3061 | #[repr(transparent)] | ||
3062 | pub struct TrueKw { | ||
3063 | pub(crate) syntax: SyntaxNode, | ||
3064 | } | ||
3065 | unsafe impl TransparentNewType for TrueKw { | ||
3066 | type Repr = rowan::SyntaxNode<RaTypes>; | ||
3067 | } | ||
3068 | |||
3069 | impl AstNode for TrueKw { | ||
3070 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | ||
3071 | match syntax.kind() { | ||
3072 | TRUE_KW => Some(TrueKw::from_repr(syntax.into_repr())), | ||
3073 | _ => None, | ||
3074 | } | ||
3075 | } | ||
3076 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
3077 | fn to_owned(&self) -> TreeArc<TrueKw> { TreeArc::cast(self.syntax.to_owned()) } | ||
3078 | } | ||
3079 | |||
3080 | |||
3081 | impl ast::AstToken for TrueKw {} | ||
3082 | impl TrueKw {} | ||
3083 | |||
3028 | // TryExpr | 3084 | // TryExpr |
3029 | #[derive(Debug, PartialEq, Eq, Hash)] | 3085 | #[derive(Debug, PartialEq, Eq, Hash)] |
3030 | #[repr(transparent)] | 3086 | #[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( | |||
215 | "PATH", | 215 | "PATH", |
216 | "PATH_SEGMENT", | 216 | "PATH_SEGMENT", |
217 | "LITERAL", | 217 | "LITERAL", |
218 | "LITERAL_EXPR", | ||
219 | "ALIAS", | 218 | "ALIAS", |
220 | "VISIBILITY", | 219 | "VISIBILITY", |
221 | "WHERE_CLAUSE", | 220 | "WHERE_CLAUSE", |
@@ -434,7 +433,9 @@ Grammar( | |||
434 | "Byte": ( traits: ["AstToken"] ), | 433 | "Byte": ( traits: ["AstToken"] ), |
435 | "ByteString": ( traits: ["AstToken"] ), | 434 | "ByteString": ( traits: ["AstToken"] ), |
436 | "Char": ( traits: ["AstToken"] ), | 435 | "Char": ( traits: ["AstToken"] ), |
437 | "Literal": ( | 436 | "TrueKw": ( traits: ["AstToken"] ), |
437 | "FalseKw": ( traits: ["AstToken"] ), | ||
438 | "LiteralExpr": ( | ||
438 | enum: [ | 439 | enum: [ |
439 | "String", | 440 | "String", |
440 | "ByteString", | 441 | "ByteString", |
@@ -442,9 +443,11 @@ Grammar( | |||
442 | "Byte", | 443 | "Byte", |
443 | "IntNumber", | 444 | "IntNumber", |
444 | "FloatNumber", | 445 | "FloatNumber", |
446 | "TrueKw", | ||
447 | "FalseKw", | ||
445 | ] | 448 | ] |
446 | ), | 449 | ), |
447 | "LiteralExpr": (options: ["Literal"]), | 450 | "Literal": (options: ["LiteralExpr"]), |
448 | 451 | ||
449 | "Expr": ( | 452 | "Expr": ( |
450 | enum: [ | 453 | enum: [ |
@@ -474,7 +477,7 @@ Grammar( | |||
474 | "PrefixExpr", | 477 | "PrefixExpr", |
475 | "RangeExpr", | 478 | "RangeExpr", |
476 | "BinExpr", | 479 | "BinExpr", |
477 | "LiteralExpr", | 480 | "Literal", |
478 | ], | 481 | ], |
479 | ), | 482 | ), |
480 | 483 | ||
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 { | |||
59 | assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); | 59 | assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); |
60 | TreeArc::cast(root) | 60 | TreeArc::cast(root) |
61 | } | 61 | } |
62 | 62 | ||
63 | pub fn parse(text: &str) -> TreeArc<SourceFile> { | 63 | pub fn parse(text: &str) -> TreeArc<SourceFile> { |
64 | let tokens = tokenize(&text); | 64 | let tokens = tokenize(&text); |
65 | let (green, errors) = | 65 | 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 { | |||
205 | PATH, | 205 | PATH, |
206 | PATH_SEGMENT, | 206 | PATH_SEGMENT, |
207 | LITERAL, | 207 | LITERAL, |
208 | LITERAL_EXPR, | ||
209 | ALIAS, | 208 | ALIAS, |
210 | VISIBILITY, | 209 | VISIBILITY, |
211 | WHERE_CLAUSE, | 210 | WHERE_CLAUSE, |
@@ -468,7 +467,6 @@ impl SyntaxKind { | |||
468 | PATH => &SyntaxInfo { name: "PATH" }, | 467 | PATH => &SyntaxInfo { name: "PATH" }, |
469 | PATH_SEGMENT => &SyntaxInfo { name: "PATH_SEGMENT" }, | 468 | PATH_SEGMENT => &SyntaxInfo { name: "PATH_SEGMENT" }, |
470 | LITERAL => &SyntaxInfo { name: "LITERAL" }, | 469 | LITERAL => &SyntaxInfo { name: "LITERAL" }, |
471 | LITERAL_EXPR => &SyntaxInfo { name: "LITERAL_EXPR" }, | ||
472 | ALIAS => &SyntaxInfo { name: "ALIAS" }, | 470 | ALIAS => &SyntaxInfo { name: "ALIAS" }, |
473 | VISIBILITY => &SyntaxInfo { name: "VISIBILITY" }, | 471 | VISIBILITY => &SyntaxInfo { name: "VISIBILITY" }, |
474 | WHERE_CLAUSE => &SyntaxInfo { name: "WHERE_CLAUSE" }, | 472 | WHERE_CLAUSE => &SyntaxInfo { name: "WHERE_CLAUSE" }, |