]> git.lizzy.rs Git - rust.git/blobdiff - crates/parser/src/grammar/patterns.rs
Replace SyntaxKind usage with T! macro where applicable
[rust.git] / crates / parser / src / grammar / patterns.rs
index 716bdc978402bc07346e8acc5925f91d18931158..da71498a8f9211465f902fb84765fe1c43dea8e2 100644 (file)
@@ -2,9 +2,18 @@
 
 use super::*;
 
-pub(super) const PATTERN_FIRST: TokenSet = expressions::LITERAL_FIRST
-    .union(paths::PATH_FIRST)
-    .union(token_set![T![box], T![ref], T![mut], T!['('], T!['['], T![&], T![_], T![-], T![.]]);
+pub(super) const PATTERN_FIRST: TokenSet =
+    expressions::LITERAL_FIRST.union(paths::PATH_FIRST).union(TokenSet::new(&[
+        T![box],
+        T![ref],
+        T![mut],
+        T!['('],
+        T!['['],
+        T![&],
+        T![_],
+        T![-],
+        T![.],
+    ]));
 
 pub(crate) fn pattern(p: &mut Parser) {
     pattern_r(p, PAT_RECOVERY_SET);
@@ -74,18 +83,19 @@ fn pattern_single_r(p: &mut Parser, recovery_set: TokenSet) {
 }
 
 const PAT_RECOVERY_SET: TokenSet =
-    token_set![LET_KW, IF_KW, WHILE_KW, LOOP_KW, MATCH_KW, R_PAREN, COMMA];
+    TokenSet::new(&[T![let], T![if], T![while], T![loop], T![match], T![')'], T![,]]);
 
 fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> {
     let m = match p.nth(0) {
         T![box] => box_pat(p),
-        T![ref] | T![mut] => bind_pat(p, true),
+        T![ref] | T![mut] => ident_pat(p, true),
+        T![const] => const_block_pat(p),
         IDENT => match p.nth(1) {
             // Checks the token after an IDENT to see if a pattern is a path (Struct { .. }) or macro
             // (T![x]).
             T!['('] | T!['{'] | T![!] => path_or_macro_pat(p),
             T![:] if p.nth_at(1, T![::]) => path_or_macro_pat(p),
-            _ => bind_pat(p, true),
+            _ => ident_pat(p, true),
         },
 
         // test type_path_in_pattern
@@ -93,8 +103,8 @@ fn atom_pat(p: &mut Parser, recovery_set: TokenSet) -> Option<CompletedMarker> {
         _ if paths::is_path_start(p) => path_or_macro_pat(p),
         _ if is_literal_pat_start(p) => literal_pat(p),
 
-        T![.] if p.at(T![..]) => dot_dot_pat(p),
-        T![_] => placeholder_pat(p),
+        T![.] if p.at(T![..]) => rest_pat(p),
+        T![_] => wildcard_pat(p),
         T![&] => ref_pat(p),
         T!['('] => tuple_pat(p),
         T!['['] => slice_pat(p),
@@ -149,7 +159,7 @@ fn path_or_macro_pat(p: &mut Parser) -> CompletedMarker {
             TUPLE_STRUCT_PAT
         }
         T!['{'] => {
-            record_field_pat_list(p);
+            record_pat_field_list(p);
             RECORD_PAT
         }
         // test marco_pat
@@ -179,14 +189,14 @@ fn tuple_pat_fields(p: &mut Parser) {
     p.expect(T![')']);
 }
 
-// test record_field_pat_list
+// test record_pat_field_list
 // fn foo() {
 //     let S {} = ();
 //     let S { f, ref mut g } = ();
 //     let S { h: _, ..} = ();
 //     let S { h: _, } = ();
 // }
-fn record_field_pat_list(p: &mut Parser) {
+fn record_pat_field_list(p: &mut Parser) {
     assert!(p.at(T!['{']));
     let m = p.start();
     p.bump(T!['{']);
@@ -199,7 +209,7 @@ fn record_field_pat_list(p: &mut Parser) {
             c => {
                 let m = p.start();
                 match c {
-                    // test record_field_pat
+                    // test record_pat_field
                     // fn foo() {
                     //     let S { 0: 1 } = ();
                     //     let S { x: 1 } = ();
@@ -214,7 +224,7 @@ fn record_field_pat_list(p: &mut Parser) {
                         box_pat(p);
                     }
                     _ => {
-                        bind_pat(p, false);
+                        ident_pat(p, false);
                     }
                 }
                 m.complete(p, RECORD_PAT_FIELD);
@@ -230,7 +240,7 @@ fn record_field_pat_list(p: &mut Parser) {
 
 // test placeholder_pat
 // fn main() { let _ = (); }
-fn placeholder_pat(p: &mut Parser) -> CompletedMarker {
+fn wildcard_pat(p: &mut Parser) -> CompletedMarker {
     assert!(p.at(T![_]));
     let m = p.start();
     p.bump(T![_]);
@@ -263,7 +273,7 @@ fn placeholder_pat(p: &mut Parser) -> CompletedMarker {
 //     let [head, .., mid, tail @ ..] = ();
 //     let [head, .., mid, .., cons] = ();
 // }
-fn dot_dot_pat(p: &mut Parser) -> CompletedMarker {
+fn rest_pat(p: &mut Parser) -> CompletedMarker {
     assert!(p.at(T![..]));
     let m = p.start();
     p.bump(T![..]);
@@ -353,7 +363,7 @@ fn pat_list(p: &mut Parser, ket: SyntaxKind) {
 //     let e @ _ = ();
 //     let ref mut f @ g @ _ = ();
 // }
-fn bind_pat(p: &mut Parser, with_at: bool) -> CompletedMarker {
+fn ident_pat(p: &mut Parser, with_at: bool) -> CompletedMarker {
     let m = p.start();
     p.eat(T![ref]);
     p.eat(T![mut]);
@@ -377,3 +387,16 @@ fn box_pat(p: &mut Parser) -> CompletedMarker {
     pattern_single(p);
     m.complete(p, BOX_PAT)
 }
+
+// test const_block_pat
+// fn main() {
+//     let const { 15 } = ();
+//     let const { foo(); bar() } = ();
+// }
+fn const_block_pat(p: &mut Parser) -> CompletedMarker {
+    assert!(p.at(T![const]));
+    let m = p.start();
+    p.bump(T![const]);
+    expressions::block_expr(p);
+    m.complete(p, CONST_BLOCK_PAT)
+}