diff options
Diffstat (limited to 'crates/ra_parser/src')
-rw-r--r-- | crates/ra_parser/src/event.rs | 7 | ||||
-rw-r--r-- | crates/ra_parser/src/lib.rs | 46 | ||||
-rw-r--r-- | crates/ra_parser/src/token_set.rs | 1 |
3 files changed, 39 insertions, 15 deletions
diff --git a/crates/ra_parser/src/event.rs b/crates/ra_parser/src/event.rs index d6e8454d4..6361d5d86 100644 --- a/crates/ra_parser/src/event.rs +++ b/crates/ra_parser/src/event.rs | |||
@@ -113,12 +113,11 @@ pub(super) fn process(sink: &mut dyn TreeSink, mut events: Vec<Event>) { | |||
113 | // append `B`'s forward_parent `C` in the next stage. | 113 | // append `B`'s forward_parent `C` in the next stage. |
114 | } | 114 | } |
115 | 115 | ||
116 | for (j, kind) in forward_parents.drain(..).rev().enumerate() { | 116 | for kind in forward_parents.drain(..).rev() { |
117 | let is_root_node = i == 0 && j == 0; | 117 | sink.start_branch(kind); |
118 | sink.start_branch(kind, is_root_node); | ||
119 | } | 118 | } |
120 | } | 119 | } |
121 | Event::Finish => sink.finish_branch(i == events.len() - 1), | 120 | Event::Finish => sink.finish_branch(), |
122 | Event::Token { kind, n_raw_tokens } => { | 121 | Event::Token { kind, n_raw_tokens } => { |
123 | sink.leaf(kind, n_raw_tokens); | 122 | sink.leaf(kind, n_raw_tokens); |
124 | } | 123 | } |
diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs index 7931b5189..ddc08e462 100644 --- a/crates/ra_parser/src/lib.rs +++ b/crates/ra_parser/src/lib.rs | |||
@@ -1,3 +1,17 @@ | |||
1 | //! The Rust parser. | ||
2 | //! | ||
3 | //! The parser doesn't know about concrete representation of tokens and syntax | ||
4 | //! trees. Abstract `TokenSource` and `TreeSink` traits are used instead. As a | ||
5 | //! consequence, this crates does not contain a lexer. | ||
6 | //! | ||
7 | //! The `Parser` struct from the `parser` module is a cursor into the sequence | ||
8 | //! of tokens. Parsing routines use `Parser` to inspect current state and | ||
9 | //! advance the parsing. | ||
10 | //! | ||
11 | //! The actual parsing happens in the `grammar` module. | ||
12 | //! | ||
13 | //! Tests for this crate live in `ra_syntax` crate. | ||
14 | |||
1 | #[macro_use] | 15 | #[macro_use] |
2 | mod token_set; | 16 | mod token_set; |
3 | mod syntax_kind; | 17 | mod syntax_kind; |
@@ -12,30 +26,34 @@ pub use syntax_kind::SyntaxKind; | |||
12 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 26 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
13 | pub struct ParseError(pub String); | 27 | pub struct ParseError(pub String); |
14 | 28 | ||
29 | /// `TokenSource` abstracts the source of the tokens parser operates one. | ||
30 | /// | ||
31 | /// Hopefully this will allow us to treat text and token trees in the same way! | ||
32 | pub trait TokenSource { | ||
33 | /// What is the current token? | ||
34 | fn token_kind(&self, pos: usize) -> SyntaxKind; | ||
35 | /// Is the current token joined to the next one (`> >` vs `>>`). | ||
36 | fn is_token_joint_to_next(&self, pos: usize) -> bool; | ||
37 | /// Is the current token a specified keyword? | ||
38 | fn is_keyword(&self, pos: usize, kw: &str) -> bool; | ||
39 | } | ||
40 | |||
15 | /// `TreeSink` abstracts details of a particular syntax tree implementation. | 41 | /// `TreeSink` abstracts details of a particular syntax tree implementation. |
16 | pub trait TreeSink { | 42 | pub trait TreeSink { |
17 | /// Adds new leaf to the current branch. | 43 | /// Adds new leaf to the current branch. |
18 | fn leaf(&mut self, kind: SyntaxKind, n_tokens: u8); | 44 | fn leaf(&mut self, kind: SyntaxKind, n_tokens: u8); |
19 | 45 | ||
20 | /// Start new branch and make it current. | 46 | /// Start new branch and make it current. |
21 | fn start_branch(&mut self, kind: SyntaxKind, root: bool); | 47 | fn start_branch(&mut self, kind: SyntaxKind); |
22 | 48 | ||
23 | /// Finish current branch and restore previous | 49 | /// Finish current branch and restore previous |
24 | /// branch as current. | 50 | /// branch as current. |
25 | fn finish_branch(&mut self, root: bool); | 51 | fn finish_branch(&mut self); |
26 | 52 | ||
27 | fn error(&mut self, error: ParseError); | 53 | fn error(&mut self, error: ParseError); |
28 | } | 54 | } |
29 | 55 | ||
30 | /// `TokenSource` abstracts the source of the tokens parser operates one. | 56 | /// Parse given tokens into the given sink as a rust file. |
31 | /// | ||
32 | /// Hopefully this will allow us to treat text and token trees in the same way! | ||
33 | pub trait TokenSource { | ||
34 | fn token_kind(&self, pos: usize) -> SyntaxKind; | ||
35 | fn is_token_joint_to_next(&self, pos: usize) -> bool; | ||
36 | fn is_keyword(&self, pos: usize, kw: &str) -> bool; | ||
37 | } | ||
38 | |||
39 | pub fn parse(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { | 57 | pub fn parse(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { |
40 | let mut p = parser::Parser::new(token_source); | 58 | let mut p = parser::Parser::new(token_source); |
41 | grammar::root(&mut p); | 59 | grammar::root(&mut p); |
@@ -43,9 +61,11 @@ pub fn parse(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { | |||
43 | event::process(tree_sink, events); | 61 | event::process(tree_sink, events); |
44 | } | 62 | } |
45 | 63 | ||
64 | /// A parsing function for a specific braced-block. | ||
46 | pub struct Reparser(fn(&mut parser::Parser)); | 65 | pub struct Reparser(fn(&mut parser::Parser)); |
47 | 66 | ||
48 | impl Reparser { | 67 | impl Reparser { |
68 | /// If the node is a braced block, return the corresponding `Reparser`. | ||
49 | pub fn for_node( | 69 | pub fn for_node( |
50 | node: SyntaxKind, | 70 | node: SyntaxKind, |
51 | first_child: Option<SyntaxKind>, | 71 | first_child: Option<SyntaxKind>, |
@@ -54,6 +74,10 @@ impl Reparser { | |||
54 | grammar::reparser(node, first_child, parent).map(Reparser) | 74 | grammar::reparser(node, first_child, parent).map(Reparser) |
55 | } | 75 | } |
56 | 76 | ||
77 | /// Re-parse given tokens using this `Reparser`. | ||
78 | /// | ||
79 | /// Tokens must start with `{`, end with `}` and form a valid brace | ||
80 | /// sequence. | ||
57 | pub fn parse(self, token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { | 81 | pub fn parse(self, token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) { |
58 | let Reparser(r) = self; | 82 | let Reparser(r) = self; |
59 | let mut p = parser::Parser::new(token_source); | 83 | let mut p = parser::Parser::new(token_source); |
diff --git a/crates/ra_parser/src/token_set.rs b/crates/ra_parser/src/token_set.rs index 24152a38a..79121b35f 100644 --- a/crates/ra_parser/src/token_set.rs +++ b/crates/ra_parser/src/token_set.rs | |||
@@ -1,5 +1,6 @@ | |||
1 | use crate::SyntaxKind; | 1 | use crate::SyntaxKind; |
2 | 2 | ||
3 | /// A bit-set of `SyntaxKind`s | ||
3 | #[derive(Clone, Copy)] | 4 | #[derive(Clone, Copy)] |
4 | pub(crate) struct TokenSet(u128); | 5 | pub(crate) struct TokenSet(u128); |
5 | 6 | ||