aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parsing.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/parsing.rs')
-rw-r--r--crates/ra_syntax/src/parsing.rs50
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;
8mod reparsing; 8mod reparsing;
9 9
10use crate::{ 10use 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.
28trait 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!
52trait 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)]
59pub(crate) struct TokenPos(pub u32);
60
61impl 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
69impl std::ops::AddAssign<u32> for TokenPos {
70 fn add_assign(&mut self, rhs: u32) {
71 self.0 += rhs
72 }
73}