3
3
/// interface, although it contains some code to bridge `SyntaxNode`s and
4
4
/// `TokenTree`s as well!
5
5
6
- macro_rules! impl_froms {
7
- ( $e: ident: $( $v: ident) , * ) => {
8
- $(
9
- impl From <$v> for $e {
10
- fn from( it: $v) -> $e {
11
- $e:: $v( it)
12
- }
13
- }
14
- ) *
15
- }
16
- }
17
-
18
- mod mbe_parser;
6
+ mod parser;
19
7
mod mbe_expander;
20
8
mod syntax_bridge;
21
- mod tt_cursor ;
9
+ mod tt_iter ;
22
10
mod subtree_source;
23
- mod subtree_parser;
24
-
25
- use ra_syntax:: SmolStr ;
26
- use smallvec:: SmallVec ;
27
11
28
12
pub use tt:: { Delimiter , Punct } ;
29
13
14
+ use crate :: {
15
+ parser:: { parse_pattern, Op } ,
16
+ tt_iter:: TtIter ,
17
+ } ;
18
+
30
19
#[ derive( Debug , PartialEq , Eq ) ]
31
20
pub enum ParseError {
32
21
Expected ( String ) ,
@@ -38,6 +27,7 @@ pub enum ExpandError {
38
27
UnexpectedToken ,
39
28
BindingError ( String ) ,
40
29
ConversionError ,
30
+ InvalidRepeat ,
41
31
}
42
32
43
33
pub use crate :: syntax_bridge:: {
@@ -54,97 +44,72 @@ pub struct MacroRules {
54
44
pub ( crate ) rules : Vec < Rule > ,
55
45
}
56
46
47
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
48
+ pub ( crate ) struct Rule {
49
+ pub ( crate ) lhs : tt:: Subtree ,
50
+ pub ( crate ) rhs : tt:: Subtree ,
51
+ }
52
+
57
53
impl MacroRules {
58
54
pub fn parse ( tt : & tt:: Subtree ) -> Result < MacroRules , ParseError > {
59
- mbe_parser:: parse ( tt)
55
+ let mut src = TtIter :: new ( tt) ;
56
+ let mut rules = Vec :: new ( ) ;
57
+ while src. len ( ) > 0 {
58
+ let rule = Rule :: parse ( & mut src) ?;
59
+ rules. push ( rule) ;
60
+ if let Err ( ( ) ) = src. expect_char ( ';' ) {
61
+ if src. len ( ) > 0 {
62
+ return Err ( ParseError :: Expected ( "expected `:`" . to_string ( ) ) ) ;
63
+ }
64
+ break ;
65
+ }
66
+ }
67
+ Ok ( MacroRules { rules } )
60
68
}
61
69
pub fn expand ( & self , tt : & tt:: Subtree ) -> Result < tt:: Subtree , ExpandError > {
62
70
mbe_expander:: expand ( self , tt)
63
71
}
64
72
}
65
73
66
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
67
- pub ( crate ) struct Rule {
68
- pub ( crate ) lhs : Subtree ,
69
- pub ( crate ) rhs : Subtree ,
70
- }
71
-
72
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
73
- pub ( crate ) enum TokenTree {
74
- Leaf ( Leaf ) ,
75
- Subtree ( Subtree ) ,
76
- Repeat ( Repeat ) ,
77
- }
78
- impl_froms ! ( TokenTree : Leaf , Subtree , Repeat ) ;
79
-
80
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
81
- pub ( crate ) enum Leaf {
82
- Literal ( Literal ) ,
83
- Punct ( Punct ) ,
84
- Ident ( Ident ) ,
85
- Var ( Var ) ,
86
- }
87
- impl_froms ! ( Leaf : Literal , Punct , Ident , Var ) ;
88
-
89
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
90
- pub ( crate ) struct Subtree {
91
- pub ( crate ) delimiter : Delimiter ,
92
- pub ( crate ) token_trees : Vec < TokenTree > ,
93
- }
94
-
95
- #[ derive( Clone , Debug , Eq ) ]
96
- pub ( crate ) enum Separator {
97
- Literal ( tt:: Literal ) ,
98
- Ident ( tt:: Ident ) ,
99
- Puncts ( SmallVec < [ tt:: Punct ; 3 ] > ) ,
74
+ impl Rule {
75
+ fn parse ( src : & mut TtIter ) -> Result < Rule , ParseError > {
76
+ let mut lhs = src
77
+ . expect_subtree ( )
78
+ . map_err ( |( ) | ParseError :: Expected ( "expected subtree" . to_string ( ) ) ) ?
79
+ . clone ( ) ;
80
+ validate ( & lhs) ?;
81
+ lhs. delimiter = tt:: Delimiter :: None ;
82
+ src. expect_char ( '=' ) . map_err ( |( ) | ParseError :: Expected ( "expected `=`" . to_string ( ) ) ) ?;
83
+ src. expect_char ( '>' ) . map_err ( |( ) | ParseError :: Expected ( "expected `>`" . to_string ( ) ) ) ?;
84
+ let mut rhs = src
85
+ . expect_subtree ( )
86
+ . map_err ( |( ) | ParseError :: Expected ( "expected subtree" . to_string ( ) ) ) ?
87
+ . clone ( ) ;
88
+ rhs. delimiter = tt:: Delimiter :: None ;
89
+ Ok ( crate :: Rule { lhs, rhs } )
90
+ }
100
91
}
101
92
102
- // Note that when we compare a Separator, we just care about its textual value.
103
- impl PartialEq for crate :: Separator {
104
- fn eq ( & self , other : & crate :: Separator ) -> bool {
105
- use crate :: Separator :: * ;
106
-
107
- match ( self , other) {
108
- ( Ident ( ref a) , Ident ( ref b) ) => a. text == b. text ,
109
- ( Literal ( ref a) , Literal ( ref b) ) => a. text == b. text ,
110
- ( Puncts ( ref a) , Puncts ( ref b) ) if a. len ( ) == b. len ( ) => {
111
- let a_iter = a. iter ( ) . map ( |a| a. char ) ;
112
- let b_iter = b. iter ( ) . map ( |b| b. char ) ;
113
- a_iter. eq ( b_iter)
93
+ fn validate ( pattern : & tt:: Subtree ) -> Result < ( ) , ParseError > {
94
+ for op in parse_pattern ( pattern) {
95
+ let op = match op {
96
+ Ok ( it) => it,
97
+ Err ( e) => {
98
+ let msg = match e {
99
+ ExpandError :: InvalidRepeat => "invalid repeat" . to_string ( ) ,
100
+ _ => "invalid macro definition" . to_string ( ) ,
101
+ } ;
102
+ return Err ( ParseError :: Expected ( msg) ) ;
103
+ }
104
+ } ;
105
+ match op {
106
+ Op :: TokenTree ( tt:: TokenTree :: Subtree ( subtree) ) | Op :: Repeat { subtree, .. } => {
107
+ validate ( subtree) ?
114
108
}
115
- _ => false ,
109
+ _ => ( ) ,
116
110
}
117
111
}
118
- }
119
-
120
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
121
- pub ( crate ) struct Repeat {
122
- pub ( crate ) subtree : Subtree ,
123
- pub ( crate ) kind : RepeatKind ,
124
- pub ( crate ) separator : Option < Separator > ,
125
- }
126
-
127
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
128
- pub ( crate ) enum RepeatKind {
129
- ZeroOrMore ,
130
- OneOrMore ,
131
- ZeroOrOne ,
132
- }
133
-
134
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
135
- pub ( crate ) struct Literal {
136
- pub ( crate ) text : SmolStr ,
137
- }
138
-
139
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
140
- pub ( crate ) struct Ident {
141
- pub ( crate ) text : SmolStr ,
142
- }
143
-
144
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
145
- pub ( crate ) struct Var {
146
- pub ( crate ) text : SmolStr ,
147
- pub ( crate ) kind : Option < SmolStr > ,
112
+ Ok ( ( ) )
148
113
}
149
114
150
115
#[ cfg( test) ]
0 commit comments