diff options
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 8e86742d0..2f21ad6e0 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -26,7 +26,11 @@ static TOKEN_MAP_COUNTER: AtomicU32 = AtomicU32::new(0); | |||
26 | 26 | ||
27 | /// Generate an unique token map id for each instance | 27 | /// Generate an unique token map id for each instance |
28 | fn make_uniq_token_map_id() -> u32 { | 28 | fn make_uniq_token_map_id() -> u32 { |
29 | TOKEN_MAP_COUNTER.fetch_add(1, Ordering::SeqCst) | 29 | let res = TOKEN_MAP_COUNTER.fetch_add(1, Ordering::SeqCst); |
30 | if res == std::u32::MAX { | ||
31 | panic!("TOKEN_MAP_COUNTER is overflowed"); | ||
32 | } | ||
33 | res | ||
30 | } | 34 | } |
31 | 35 | ||
32 | impl std::default::Default for TokenMap { | 36 | impl std::default::Default for TokenMap { |
@@ -35,10 +39,9 @@ impl std::default::Default for TokenMap { | |||
35 | } | 39 | } |
36 | } | 40 | } |
37 | 41 | ||
38 | /// Maps Relative range of the expanded syntax node to `tt::TokenId` | 42 | /// Maps relative range of the expanded syntax node to `tt::TokenId` |
39 | #[derive(Debug, PartialEq, Eq, Default)] | 43 | #[derive(Debug, PartialEq, Eq, Default)] |
40 | pub struct ExpandedRangeMap { | 44 | pub struct ExpandedRangeMap { |
41 | /// Maps `tt::TokenId` to the *relative* source range. | ||
42 | ranges: Vec<(TextRange, tt::TokenId)>, | 45 | ranges: Vec<(TextRange, tt::TokenId)>, |
43 | } | 46 | } |
44 | 47 | ||
@@ -85,14 +88,13 @@ fn fragment_to_syntax_node( | |||
85 | }; | 88 | }; |
86 | let buffer = TokenBuffer::new(&tokens); | 89 | let buffer = TokenBuffer::new(&tokens); |
87 | let mut token_source = SubtreeTokenSource::new(&buffer); | 90 | let mut token_source = SubtreeTokenSource::new(&buffer); |
88 | let mut range_map = ExpandedRangeMap::default(); | 91 | let mut tree_sink = TtTreeSink::new(buffer.begin()); |
89 | let mut tree_sink = TtTreeSink::new(buffer.begin(), &mut range_map); | ||
90 | ra_parser::parse_fragment(&mut token_source, &mut tree_sink, fragment_kind); | 92 | ra_parser::parse_fragment(&mut token_source, &mut tree_sink, fragment_kind); |
91 | if tree_sink.roots.len() != 1 { | 93 | if tree_sink.roots.len() != 1 { |
92 | return Err(ExpandError::ConversionError); | 94 | return Err(ExpandError::ConversionError); |
93 | } | 95 | } |
94 | //FIXME: would be cool to report errors | 96 | //FIXME: would be cool to report errors |
95 | let parse = tree_sink.inner.finish(); | 97 | let (parse, range_map) = tree_sink.finish(); |
96 | Ok((parse, range_map)) | 98 | Ok((parse, range_map)) |
97 | } | 99 | } |
98 | 100 | ||
@@ -320,7 +322,7 @@ struct TtTreeSink<'a> { | |||
320 | cursor: Cursor<'a>, | 322 | cursor: Cursor<'a>, |
321 | text_pos: TextUnit, | 323 | text_pos: TextUnit, |
322 | inner: SyntaxTreeBuilder, | 324 | inner: SyntaxTreeBuilder, |
323 | range_map: &'a mut ExpandedRangeMap, | 325 | range_map: ExpandedRangeMap, |
324 | 326 | ||
325 | // Number of roots | 327 | // Number of roots |
326 | // Use for detect ill-form tree which is not single root | 328 | // Use for detect ill-form tree which is not single root |
@@ -328,16 +330,20 @@ struct TtTreeSink<'a> { | |||
328 | } | 330 | } |
329 | 331 | ||
330 | impl<'a> TtTreeSink<'a> { | 332 | impl<'a> TtTreeSink<'a> { |
331 | fn new(cursor: Cursor<'a>, range_map: &'a mut ExpandedRangeMap) -> Self { | 333 | fn new(cursor: Cursor<'a>) -> Self { |
332 | TtTreeSink { | 334 | TtTreeSink { |
333 | buf: String::new(), | 335 | buf: String::new(), |
334 | cursor, | 336 | cursor, |
335 | text_pos: 0.into(), | 337 | text_pos: 0.into(), |
336 | inner: SyntaxTreeBuilder::default(), | 338 | inner: SyntaxTreeBuilder::default(), |
337 | roots: smallvec::SmallVec::new(), | 339 | roots: smallvec::SmallVec::new(), |
338 | range_map, | 340 | range_map: ExpandedRangeMap::default(), |
339 | } | 341 | } |
340 | } | 342 | } |
343 | |||
344 | fn finish(self) -> (Parse<SyntaxNode>, ExpandedRangeMap) { | ||
345 | (self.inner.finish(), self.range_map) | ||
346 | } | ||
341 | } | 347 | } |
342 | 348 | ||
343 | fn delim_to_str(d: tt::Delimiter, closing: bool) -> SmolStr { | 349 | fn delim_to_str(d: tt::Delimiter, closing: bool) -> SmolStr { |