diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-27 08:28:13 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-27 08:28:13 +0100 |
commit | ce694ae11854a806031db98c51c068253f927519 (patch) | |
tree | 2996ecd85ff9aa57b6f208e83d42dd03a7370d1e /crates/ra_mbe/src/subtree_parser.rs | |
parent | 4f4e50db908ba44f113faeb356ae2b3d0788d308 (diff) | |
parent | 90764fc54b2be1e0fc5d6ac9c9e960d7bb059b14 (diff) |
Merge #1328
1328: Change TokenSource to iteration based r=matklad a=edwin0cheng
This PR change the `TokenSource` trait from random access to be an iteration based trait:
```rust
/// `TokenSource` abstracts the source of the tokens parser operates one.
///
/// Hopefully this will allow us to treat text and token trees in the same way!
pub trait TokenSource {
fn current(&self) -> Token;
/// Lookahead n token
fn lookahead_nth(&self, n: usize) -> Token;
/// bump cursor to next token
fn bump(&mut self);
/// Is the current token a specified keyword?
fn is_keyword(&self, kw: &str) -> bool;
}
/// `TokenCursor` abstracts the cursor of `TokenSource` operates one.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Token {
/// What is the current token?
pub kind: SyntaxKind,
/// Is the current token joined to the next one (`> >` vs `>>`).
pub is_jointed_to_next: bool,
}
```
Note that the refactoring based on this new trait will be separated to incoming PRs
Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_mbe/src/subtree_parser.rs')
-rw-r--r-- | crates/ra_mbe/src/subtree_parser.rs | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/crates/ra_mbe/src/subtree_parser.rs b/crates/ra_mbe/src/subtree_parser.rs index 709b87a38..9cc989b23 100644 --- a/crates/ra_mbe/src/subtree_parser.rs +++ b/crates/ra_mbe/src/subtree_parser.rs | |||
@@ -68,13 +68,13 @@ impl<'a> Parser<'a> { | |||
68 | 68 | ||
69 | fn parse<F>(self, f: F) -> Option<tt::TokenTree> | 69 | fn parse<F>(self, f: F) -> Option<tt::TokenTree> |
70 | where | 70 | where |
71 | F: FnOnce(&dyn TokenSource, &mut dyn TreeSink), | 71 | F: FnOnce(&mut dyn TokenSource, &mut dyn TreeSink), |
72 | { | 72 | { |
73 | let buffer = TokenBuffer::new(&self.subtree.token_trees[*self.cur_pos..]); | 73 | let buffer = TokenBuffer::new(&self.subtree.token_trees[*self.cur_pos..]); |
74 | let mut src = SubtreeTokenSource::new(&buffer); | 74 | let mut src = SubtreeTokenSource::new(&buffer); |
75 | let mut sink = OffsetTokenSink { token_pos: 0, error: false }; | 75 | let mut sink = OffsetTokenSink { token_pos: 0, error: false }; |
76 | 76 | ||
77 | f(&src, &mut sink); | 77 | f(&mut src, &mut sink); |
78 | 78 | ||
79 | let r = self.finish(sink.token_pos, &mut src); | 79 | let r = self.finish(sink.token_pos, &mut src); |
80 | if sink.error { | 80 | if sink.error { |