@@ -71,23 +71,13 @@ pub trait TreeSink {
71
71
fn error ( & mut self , error : ParseError ) ;
72
72
}
73
73
74
- fn parse_from_tokens < F > ( token_source : & mut dyn TokenSource , tree_sink : & mut dyn TreeSink , f : F )
75
- where
76
- F : FnOnce ( & mut parser:: Parser ) ,
77
- {
78
- let mut p = parser:: Parser :: new ( token_source) ;
79
- f ( & mut p) ;
80
- let events = p. finish ( ) ;
81
- event:: process ( tree_sink, events) ;
82
- }
83
-
84
- /// Parse given tokens into the given sink as a rust file.
85
- pub fn parse ( token_source : & mut dyn TokenSource , tree_sink : & mut dyn TreeSink ) {
86
- parse_from_tokens ( token_source, tree_sink, grammar:: root) ;
87
- }
88
-
74
+ /// rust-analyzer parser allows you to choose one of the possible entry points.
75
+ ///
76
+ /// The primary consumer of this API are declarative macros, `$x:expr` matchers
77
+ /// are implemented by calling into the parser with non-standard entry point.
89
78
#[ derive( Debug , Clone , Copy , Eq , PartialEq , Hash ) ]
90
- pub enum FragmentKind {
79
+ pub enum ParserEntryPoint {
80
+ SourceFile ,
91
81
Path ,
92
82
Expr ,
93
83
Statement ,
@@ -103,27 +93,37 @@ pub enum FragmentKind {
103
93
Attr ,
104
94
}
105
95
106
- pub fn parse_fragment (
96
+ /// Parse given tokens into the given sink as a rust file.
97
+ pub fn parse_source_file ( token_source : & mut dyn TokenSource , tree_sink : & mut dyn TreeSink ) {
98
+ parse ( token_source, tree_sink, ParserEntryPoint :: SourceFile ) ;
99
+ }
100
+
101
+ pub fn parse (
107
102
token_source : & mut dyn TokenSource ,
108
103
tree_sink : & mut dyn TreeSink ,
109
- fragment_kind : FragmentKind ,
104
+ entry_point : ParserEntryPoint ,
110
105
) {
111
- let parser: fn ( & ' _ mut parser:: Parser ) = match fragment_kind {
112
- FragmentKind :: Path => grammar:: fragments:: path,
113
- FragmentKind :: Expr => grammar:: fragments:: expr,
114
- FragmentKind :: Type => grammar:: fragments:: type_,
115
- FragmentKind :: Pattern => grammar:: fragments:: pattern_single,
116
- FragmentKind :: Item => grammar:: fragments:: item,
117
- FragmentKind :: Block => grammar:: fragments:: block_expr,
118
- FragmentKind :: Visibility => grammar:: fragments:: opt_visibility,
119
- FragmentKind :: MetaItem => grammar:: fragments:: meta_item,
120
- FragmentKind :: Statement => grammar:: fragments:: stmt,
121
- FragmentKind :: StatementOptionalSemi => grammar:: fragments:: stmt_optional_semi,
122
- FragmentKind :: Items => grammar:: fragments:: macro_items,
123
- FragmentKind :: Statements => grammar:: fragments:: macro_stmts,
124
- FragmentKind :: Attr => grammar:: fragments:: attr,
106
+ let entry_point: fn ( & ' _ mut parser:: Parser ) = match entry_point {
107
+ ParserEntryPoint :: SourceFile => grammar:: entry_points:: source_file,
108
+ ParserEntryPoint :: Path => grammar:: entry_points:: path,
109
+ ParserEntryPoint :: Expr => grammar:: entry_points:: expr,
110
+ ParserEntryPoint :: Type => grammar:: entry_points:: type_,
111
+ ParserEntryPoint :: Pattern => grammar:: entry_points:: pattern,
112
+ ParserEntryPoint :: Item => grammar:: entry_points:: item,
113
+ ParserEntryPoint :: Block => grammar:: entry_points:: block_expr,
114
+ ParserEntryPoint :: Visibility => grammar:: entry_points:: visibility,
115
+ ParserEntryPoint :: MetaItem => grammar:: entry_points:: meta_item,
116
+ ParserEntryPoint :: Statement => grammar:: entry_points:: stmt,
117
+ ParserEntryPoint :: StatementOptionalSemi => grammar:: entry_points:: stmt_optional_semi,
118
+ ParserEntryPoint :: Items => grammar:: entry_points:: macro_items,
119
+ ParserEntryPoint :: Statements => grammar:: entry_points:: macro_stmts,
120
+ ParserEntryPoint :: Attr => grammar:: entry_points:: attr,
125
121
} ;
126
- parse_from_tokens ( token_source, tree_sink, parser)
122
+
123
+ let mut p = parser:: Parser :: new ( token_source) ;
124
+ entry_point ( & mut p) ;
125
+ let events = p. finish ( ) ;
126
+ event:: process ( tree_sink, events) ;
127
127
}
128
128
129
129
/// A parsing function for a specific braced-block.
0 commit comments