diff options
author | Aleksey Kladov <[email protected]> | 2019-02-20 19:19:12 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-20 19:19:12 +0000 |
commit | 2b5e336ce7172914686b33c8ac1522911366fcf0 (patch) | |
tree | e706eea2010ee6d05269512b4d5e3dab033c789a /crates/ra_syntax/src/parsing.rs | |
parent | d2bce118ae72ee5cf96b8c6ac687914cb842363c (diff) |
move abstract traits to top
Diffstat (limited to 'crates/ra_syntax/src/parsing.rs')
-rw-r--r-- | crates/ra_syntax/src/parsing.rs | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/crates/ra_syntax/src/parsing.rs b/crates/ra_syntax/src/parsing.rs index 761accd7b..6c2c5f78b 100644 --- a/crates/ra_syntax/src/parsing.rs +++ b/crates/ra_syntax/src/parsing.rs | |||
@@ -8,7 +8,7 @@ mod grammar; | |||
8 | mod reparsing; | 8 | mod reparsing; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | SyntaxError, | 11 | SyntaxError, SyntaxKind, SmolStr, |
12 | parsing::builder::GreenBuilder, | 12 | parsing::builder::GreenBuilder, |
13 | syntax_node::GreenNode, | 13 | syntax_node::GreenNode, |
14 | }; | 14 | }; |
@@ -23,3 +23,51 @@ pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) { | |||
23 | parser_impl::parse_with(GreenBuilder::new(), text, &tokens, grammar::root); | 23 | parser_impl::parse_with(GreenBuilder::new(), text, &tokens, grammar::root); |
24 | (green, errors) | 24 | (green, errors) |
25 | } | 25 | } |
26 | |||
27 | /// `TreeSink` abstracts details of a particular syntax tree implementation. | ||
28 | trait TreeSink { | ||
29 | type Tree; | ||
30 | |||
31 | /// Adds new leaf to the current branch. | ||
32 | fn leaf(&mut self, kind: SyntaxKind, text: SmolStr); | ||
33 | |||
34 | /// Start new branch and make it current. | ||
35 | fn start_branch(&mut self, kind: SyntaxKind); | ||
36 | |||
37 | /// Finish current branch and restore previous | ||
38 | /// branch as current. | ||
39 | fn finish_branch(&mut self); | ||
40 | |||
41 | fn error(&mut self, error: SyntaxError); | ||
42 | |||
43 | /// Complete tree building. Make sure that | ||
44 | /// `start_branch` and `finish_branch` calls | ||
45 | /// are paired! | ||
46 | fn finish(self) -> Self::Tree; | ||
47 | } | ||
48 | |||
49 | /// `TokenSource` abstracts the source of the tokens parser operates one. | ||
50 | /// | ||
51 | /// Hopefully this will allow us to treat text and token trees in the same way! | ||
52 | trait TokenSource { | ||
53 | fn token_kind(&self, pos: TokenPos) -> SyntaxKind; | ||
54 | fn is_token_joint_to_next(&self, pos: TokenPos) -> bool; | ||
55 | fn is_keyword(&self, pos: TokenPos, kw: &str) -> bool; | ||
56 | } | ||
57 | |||
58 | #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Default)] | ||
59 | pub(crate) struct TokenPos(pub u32); | ||
60 | |||
61 | impl std::ops::Add<u32> for TokenPos { | ||
62 | type Output = TokenPos; | ||
63 | |||
64 | fn add(self, rhs: u32) -> TokenPos { | ||
65 | TokenPos(self.0 + rhs) | ||
66 | } | ||
67 | } | ||
68 | |||
69 | impl std::ops::AddAssign<u32> for TokenPos { | ||
70 | fn add_assign(&mut self, rhs: u32) { | ||
71 | self.0 += rhs | ||
72 | } | ||
73 | } | ||