4
4
//! placeholders, which start with `$`. For replacement templates, this is the final form. For
5
5
//! search patterns, we go further and parse the pattern as each kind of thing that we can match.
6
6
//! e.g. expressions, type references etc.
7
-
8
- use crate :: errors:: bail;
9
- use crate :: { SsrError , SsrPattern , SsrRule } ;
10
7
use rustc_hash:: { FxHashMap , FxHashSet } ;
11
8
use std:: { fmt:: Display , str:: FromStr } ;
12
- use syntax:: { ast, AstNode , SmolStr , SyntaxKind , SyntaxNode , T } ;
9
+ use syntax:: { SmolStr , SyntaxKind , SyntaxNode , T } ;
10
+
11
+ use crate :: errors:: bail;
12
+ use crate :: { fragments, SsrError , SsrPattern , SsrRule } ;
13
13
14
14
#[ derive( Debug ) ]
15
15
pub ( crate ) struct ParsedRule {
@@ -73,17 +73,16 @@ impl ParsedRule {
73
73
rules : Vec :: new ( ) ,
74
74
} ;
75
75
76
- let raw_template_stmt = raw_template. map ( ast :: Stmt :: parse ) ;
77
- if let raw_template_expr @ Some ( Ok ( _) ) = raw_template. map ( ast :: Expr :: parse ) {
78
- builder. try_add ( ast :: Expr :: parse ( & raw_pattern) , raw_template_expr) ;
76
+ let raw_template_stmt = raw_template. map ( fragments :: stmt ) ;
77
+ if let raw_template_expr @ Some ( Ok ( _) ) = raw_template. map ( fragments :: expr ) {
78
+ builder. try_add ( fragments :: expr ( & raw_pattern) , raw_template_expr) ;
79
79
} else {
80
- builder. try_add ( ast :: Expr :: parse ( & raw_pattern) , raw_template_stmt. clone ( ) ) ;
80
+ builder. try_add ( fragments :: expr ( & raw_pattern) , raw_template_stmt. clone ( ) ) ;
81
81
}
82
- builder. try_add ( ast:: Type :: parse ( & raw_pattern) , raw_template. map ( ast:: Type :: parse) ) ;
83
- builder. try_add ( ast:: Item :: parse ( & raw_pattern) , raw_template. map ( ast:: Item :: parse) ) ;
84
- builder. try_add ( ast:: Path :: parse ( & raw_pattern) , raw_template. map ( ast:: Path :: parse) ) ;
85
- builder. try_add ( ast:: Pat :: parse ( & raw_pattern) , raw_template. map ( ast:: Pat :: parse) ) ;
86
- builder. try_add ( ast:: Stmt :: parse ( & raw_pattern) , raw_template_stmt) ;
82
+ builder. try_add ( fragments:: ty ( & raw_pattern) , raw_template. map ( fragments:: ty) ) ;
83
+ builder. try_add ( fragments:: item ( & raw_pattern) , raw_template. map ( fragments:: item) ) ;
84
+ builder. try_add ( fragments:: pat ( & raw_pattern) , raw_template. map ( fragments:: pat) ) ;
85
+ builder. try_add ( fragments:: stmt ( & raw_pattern) , raw_template_stmt) ;
87
86
builder. build ( )
88
87
}
89
88
}
@@ -94,20 +93,20 @@ struct RuleBuilder {
94
93
}
95
94
96
95
impl RuleBuilder {
97
- fn try_add < T : AstNode , T2 : AstNode > (
96
+ fn try_add (
98
97
& mut self ,
99
- pattern : Result < T , ( ) > ,
100
- template : Option < Result < T2 , ( ) > > ,
98
+ pattern : Result < SyntaxNode , ( ) > ,
99
+ template : Option < Result < SyntaxNode , ( ) > > ,
101
100
) {
102
101
match ( pattern, template) {
103
102
( Ok ( pattern) , Some ( Ok ( template) ) ) => self . rules . push ( ParsedRule {
104
103
placeholders_by_stand_in : self . placeholders_by_stand_in . clone ( ) ,
105
- pattern : pattern . syntax ( ) . clone ( ) ,
106
- template : Some ( template. syntax ( ) . clone ( ) ) ,
104
+ pattern,
105
+ template : Some ( template) ,
107
106
} ) ,
108
107
( Ok ( pattern) , None ) => self . rules . push ( ParsedRule {
109
108
placeholders_by_stand_in : self . placeholders_by_stand_in . clone ( ) ,
110
- pattern : pattern . syntax ( ) . clone ( ) ,
109
+ pattern,
111
110
template : None ,
112
111
} ) ,
113
112
_ => { }
0 commit comments