diff options
author | Florian Diebold <[email protected]> | 2019-02-09 18:07:35 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-02-09 20:37:30 +0000 |
commit | f1afc933530a87bd0cc7b25726c9a7fff3f3e007 (patch) | |
tree | cfa9a55a6d9df93129502be655d6c26dee1bc5c1 /crates/ra_syntax/src | |
parent | 7ebde241c00cd9eb816b1aa7cb212a946afb0d3e (diff) |
Fix handling of literal patterns
Wrap them in a LiteralPat node so they can be distinguished from literal
expressions.
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast/generated.rs | 37 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar.ron | 3 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar/patterns.rs | 41 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_kinds/generated.rs | 2 |
4 files changed, 67 insertions, 16 deletions
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 60314d245..256277609 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs | |||
@@ -1821,6 +1821,38 @@ impl LiteralExpr { | |||
1821 | 1821 | ||
1822 | impl LiteralExpr {} | 1822 | impl LiteralExpr {} |
1823 | 1823 | ||
1824 | // LiteralPat | ||
1825 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
1826 | #[repr(transparent)] | ||
1827 | pub struct LiteralPat { | ||
1828 | pub(crate) syntax: SyntaxNode, | ||
1829 | } | ||
1830 | unsafe impl TransparentNewType for LiteralPat { | ||
1831 | type Repr = rowan::SyntaxNode<RaTypes>; | ||
1832 | } | ||
1833 | |||
1834 | impl AstNode for LiteralPat { | ||
1835 | fn cast(syntax: &SyntaxNode) -> Option<&Self> { | ||
1836 | match syntax.kind() { | ||
1837 | LITERAL_PAT => Some(LiteralPat::from_repr(syntax.into_repr())), | ||
1838 | _ => None, | ||
1839 | } | ||
1840 | } | ||
1841 | fn syntax(&self) -> &SyntaxNode { &self.syntax } | ||
1842 | } | ||
1843 | |||
1844 | impl ToOwned for LiteralPat { | ||
1845 | type Owned = TreeArc<LiteralPat>; | ||
1846 | fn to_owned(&self) -> TreeArc<LiteralPat> { TreeArc::cast(self.syntax.to_owned()) } | ||
1847 | } | ||
1848 | |||
1849 | |||
1850 | impl LiteralPat { | ||
1851 | pub fn literal(&self) -> Option<&Literal> { | ||
1852 | super::child_opt(self) | ||
1853 | } | ||
1854 | } | ||
1855 | |||
1824 | // LoopExpr | 1856 | // LoopExpr |
1825 | #[derive(Debug, PartialEq, Eq, Hash)] | 1857 | #[derive(Debug, PartialEq, Eq, Hash)] |
1826 | #[repr(transparent)] | 1858 | #[repr(transparent)] |
@@ -2594,6 +2626,7 @@ pub enum PatKind<'a> { | |||
2594 | TuplePat(&'a TuplePat), | 2626 | TuplePat(&'a TuplePat), |
2595 | SlicePat(&'a SlicePat), | 2627 | SlicePat(&'a SlicePat), |
2596 | RangePat(&'a RangePat), | 2628 | RangePat(&'a RangePat), |
2629 | LiteralPat(&'a LiteralPat), | ||
2597 | } | 2630 | } |
2598 | 2631 | ||
2599 | impl AstNode for Pat { | 2632 | impl AstNode for Pat { |
@@ -2607,7 +2640,8 @@ impl AstNode for Pat { | |||
2607 | | TUPLE_STRUCT_PAT | 2640 | | TUPLE_STRUCT_PAT |
2608 | | TUPLE_PAT | 2641 | | TUPLE_PAT |
2609 | | SLICE_PAT | 2642 | | SLICE_PAT |
2610 | | RANGE_PAT => Some(Pat::from_repr(syntax.into_repr())), | 2643 | | RANGE_PAT |
2644 | | LITERAL_PAT => Some(Pat::from_repr(syntax.into_repr())), | ||
2611 | _ => None, | 2645 | _ => None, |
2612 | } | 2646 | } |
2613 | } | 2647 | } |
@@ -2631,6 +2665,7 @@ impl Pat { | |||
2631 | TUPLE_PAT => PatKind::TuplePat(TuplePat::cast(&self.syntax).unwrap()), | 2665 | TUPLE_PAT => PatKind::TuplePat(TuplePat::cast(&self.syntax).unwrap()), |
2632 | SLICE_PAT => PatKind::SlicePat(SlicePat::cast(&self.syntax).unwrap()), | 2666 | SLICE_PAT => PatKind::SlicePat(SlicePat::cast(&self.syntax).unwrap()), |
2633 | RANGE_PAT => PatKind::RangePat(RangePat::cast(&self.syntax).unwrap()), | 2667 | RANGE_PAT => PatKind::RangePat(RangePat::cast(&self.syntax).unwrap()), |
2668 | LITERAL_PAT => PatKind::LiteralPat(LiteralPat::cast(&self.syntax).unwrap()), | ||
2634 | _ => unreachable!(), | 2669 | _ => unreachable!(), |
2635 | } | 2670 | } |
2636 | } | 2671 | } |
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 046db5885..d428bc595 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron | |||
@@ -161,6 +161,7 @@ Grammar( | |||
161 | "TUPLE_PAT", | 161 | "TUPLE_PAT", |
162 | "SLICE_PAT", | 162 | "SLICE_PAT", |
163 | "RANGE_PAT", | 163 | "RANGE_PAT", |
164 | "LITERAL_PAT", | ||
164 | 165 | ||
165 | // atoms | 166 | // atoms |
166 | "TUPLE_EXPR", | 167 | "TUPLE_EXPR", |
@@ -524,6 +525,7 @@ Grammar( | |||
524 | "TuplePat": ( collections: [["args", "Pat"]] ), | 525 | "TuplePat": ( collections: [["args", "Pat"]] ), |
525 | "SlicePat": (), | 526 | "SlicePat": (), |
526 | "RangePat": (), | 527 | "RangePat": (), |
528 | "LiteralPat": (options: ["Literal"]), | ||
527 | 529 | ||
528 | "Pat": ( | 530 | "Pat": ( |
529 | enum: [ | 531 | enum: [ |
@@ -536,6 +538,7 @@ Grammar( | |||
536 | "TuplePat", | 538 | "TuplePat", |
537 | "SlicePat", | 539 | "SlicePat", |
538 | "RangePat", | 540 | "RangePat", |
541 | "LiteralPat", | ||
539 | ], | 542 | ], |
540 | ), | 543 | ), |
541 | 544 | ||
diff --git a/crates/ra_syntax/src/grammar/patterns.rs b/crates/ra_syntax/src/grammar/patterns.rs index f3f400ae0..9d7da639d 100644 --- a/crates/ra_syntax/src/grammar/patterns.rs +++ b/crates/ra_syntax/src/grammar/patterns.rs | |||
@@ -43,21 +43,8 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> { | |||
43 | return Some(path_pat(p)); | 43 | return Some(path_pat(p)); |
44 | } | 44 | } |
45 | 45 | ||
46 | // test literal_pattern | 46 | if is_literal_pat_start(p) { |
47 | // fn main() { | 47 | return Some(literal_pat(p)); |
48 | // match () { | ||
49 | // -1 => (), | ||
50 | // 92 => (), | ||
51 | // 'c' => (), | ||
52 | // "hello" => (), | ||
53 | // } | ||
54 | // } | ||
55 | if p.at(MINUS) && (p.nth(1) == INT_NUMBER || p.nth(1) == FLOAT_NUMBER) { | ||
56 | p.bump(); | ||
57 | } | ||
58 | |||
59 | if let Some(m) = expressions::literal(p) { | ||
60 | return Some(m); | ||
61 | } | 48 | } |
62 | 49 | ||
63 | let m = match la0 { | 50 | let m = match la0 { |
@@ -73,6 +60,30 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> { | |||
73 | Some(m) | 60 | Some(m) |
74 | } | 61 | } |
75 | 62 | ||
63 | fn is_literal_pat_start(p: &mut Parser) -> bool { | ||
64 | p.at(MINUS) && (p.nth(1) == INT_NUMBER || p.nth(1) == FLOAT_NUMBER) | ||
65 | || p.at_ts(expressions::LITERAL_FIRST) | ||
66 | } | ||
67 | |||
68 | // test literal_pattern | ||
69 | // fn main() { | ||
70 | // match () { | ||
71 | // -1 => (), | ||
72 | // 92 => (), | ||
73 | // 'c' => (), | ||
74 | // "hello" => (), | ||
75 | // } | ||
76 | // } | ||
77 | fn literal_pat(p: &mut Parser) -> CompletedMarker { | ||
78 | assert!(is_literal_pat_start(p)); | ||
79 | let m = p.start(); | ||
80 | if p.at(MINUS) { | ||
81 | p.bump(); | ||
82 | } | ||
83 | expressions::literal(p); | ||
84 | m.complete(p, LITERAL_PAT) | ||
85 | } | ||
86 | |||
76 | // test path_part | 87 | // test path_part |
77 | // fn foo() { | 88 | // fn foo() { |
78 | // let foo::Bar = (); | 89 | // let foo::Bar = (); |
diff --git a/crates/ra_syntax/src/syntax_kinds/generated.rs b/crates/ra_syntax/src/syntax_kinds/generated.rs index fea513458..266b95bbb 100644 --- a/crates/ra_syntax/src/syntax_kinds/generated.rs +++ b/crates/ra_syntax/src/syntax_kinds/generated.rs | |||
@@ -157,6 +157,7 @@ pub enum SyntaxKind { | |||
157 | TUPLE_PAT, | 157 | TUPLE_PAT, |
158 | SLICE_PAT, | 158 | SLICE_PAT, |
159 | RANGE_PAT, | 159 | RANGE_PAT, |
160 | LITERAL_PAT, | ||
160 | TUPLE_EXPR, | 161 | TUPLE_EXPR, |
161 | ARRAY_EXPR, | 162 | ARRAY_EXPR, |
162 | PAREN_EXPR, | 163 | PAREN_EXPR, |
@@ -493,6 +494,7 @@ impl SyntaxKind { | |||
493 | TUPLE_PAT => &SyntaxInfo { name: "TUPLE_PAT" }, | 494 | TUPLE_PAT => &SyntaxInfo { name: "TUPLE_PAT" }, |
494 | SLICE_PAT => &SyntaxInfo { name: "SLICE_PAT" }, | 495 | SLICE_PAT => &SyntaxInfo { name: "SLICE_PAT" }, |
495 | RANGE_PAT => &SyntaxInfo { name: "RANGE_PAT" }, | 496 | RANGE_PAT => &SyntaxInfo { name: "RANGE_PAT" }, |
497 | LITERAL_PAT => &SyntaxInfo { name: "LITERAL_PAT" }, | ||
496 | TUPLE_EXPR => &SyntaxInfo { name: "TUPLE_EXPR" }, | 498 | TUPLE_EXPR => &SyntaxInfo { name: "TUPLE_EXPR" }, |
497 | ARRAY_EXPR => &SyntaxInfo { name: "ARRAY_EXPR" }, | 499 | ARRAY_EXPR => &SyntaxInfo { name: "ARRAY_EXPR" }, |
498 | PAREN_EXPR => &SyntaxInfo { name: "PAREN_EXPR" }, | 500 | PAREN_EXPR => &SyntaxInfo { name: "PAREN_EXPR" }, |