diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-21 09:25:20 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-21 09:25:20 +0000 |
commit | 18b0c509f77a8e06141fee6668532cced1ebf5d8 (patch) | |
tree | e6b1718e4557a33921bf6637e76bf74c0d3cfa96 /crates/ra_syntax/src/parsing.rs | |
parent | c84561bb624280b84eb2fe6c6b2a6b9fe3f1dbf7 (diff) | |
parent | cd0d2866fc61e03aeca7bd7d1bc652e0443d2b3d (diff) |
Merge #865
865: Parser decoupling r=matklad a=matklad
More work on making parser independent from text
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/parsing.rs')
-rw-r--r-- | crates/ra_syntax/src/parsing.rs | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/crates/ra_syntax/src/parsing.rs b/crates/ra_syntax/src/parsing.rs index 138d1394a..7e1b32035 100644 --- a/crates/ra_syntax/src/parsing.rs +++ b/crates/ra_syntax/src/parsing.rs | |||
@@ -9,11 +9,11 @@ mod grammar; | |||
9 | mod reparsing; | 9 | mod reparsing; |
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | SyntaxKind, SmolStr, SyntaxError, | 12 | SyntaxKind, SyntaxError, |
13 | parsing::{ | 13 | parsing::{ |
14 | builder::GreenBuilder, | 14 | builder::TreeBuilder, |
15 | input::ParserInput, | 15 | input::ParserInput, |
16 | event::EventProcessor, | 16 | event::process, |
17 | parser::Parser, | 17 | parser::Parser, |
18 | }, | 18 | }, |
19 | syntax_node::GreenNode, | 19 | syntax_node::GreenNode, |
@@ -28,22 +28,24 @@ pub(crate) use self::reparsing::incremental_reparse; | |||
28 | 28 | ||
29 | pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) { | 29 | pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) { |
30 | let tokens = tokenize(&text); | 30 | let tokens = tokenize(&text); |
31 | parse_with(GreenBuilder::default(), text, &tokens, grammar::root) | 31 | let tree_sink = TreeBuilder::new(text, &tokens); |
32 | parse_with(tree_sink, text, &tokens, grammar::root) | ||
32 | } | 33 | } |
33 | 34 | ||
34 | fn parse_with<S: TreeSink>( | 35 | fn parse_with<S: TreeSink>( |
35 | tree_sink: S, | 36 | mut tree_sink: S, |
36 | text: &str, | 37 | text: &str, |
37 | tokens: &[Token], | 38 | tokens: &[Token], |
38 | f: fn(&mut Parser), | 39 | f: fn(&mut Parser), |
39 | ) -> S::Tree { | 40 | ) -> S::Tree { |
40 | let mut events = { | 41 | let events = { |
41 | let input = ParserInput::new(text, &tokens); | 42 | let input = ParserInput::new(text, &tokens); |
42 | let mut p = Parser::new(&input); | 43 | let mut p = Parser::new(&input); |
43 | f(&mut p); | 44 | f(&mut p); |
44 | p.finish() | 45 | p.finish() |
45 | }; | 46 | }; |
46 | EventProcessor::new(tree_sink, text, tokens, &mut events).process().finish() | 47 | process(&mut tree_sink, events); |
48 | tree_sink.finish() | ||
47 | } | 49 | } |
48 | 50 | ||
49 | /// `TreeSink` abstracts details of a particular syntax tree implementation. | 51 | /// `TreeSink` abstracts details of a particular syntax tree implementation. |
@@ -51,14 +53,14 @@ trait TreeSink { | |||
51 | type Tree; | 53 | type Tree; |
52 | 54 | ||
53 | /// Adds new leaf to the current branch. | 55 | /// Adds new leaf to the current branch. |
54 | fn leaf(&mut self, kind: SyntaxKind, text: SmolStr); | 56 | fn leaf(&mut self, kind: SyntaxKind, n_tokens: u8); |
55 | 57 | ||
56 | /// Start new branch and make it current. | 58 | /// Start new branch and make it current. |
57 | fn start_branch(&mut self, kind: SyntaxKind); | 59 | fn start_branch(&mut self, kind: SyntaxKind, root: bool); |
58 | 60 | ||
59 | /// Finish current branch and restore previous | 61 | /// Finish current branch and restore previous |
60 | /// branch as current. | 62 | /// branch as current. |
61 | fn finish_branch(&mut self); | 63 | fn finish_branch(&mut self, root: bool); |
62 | 64 | ||
63 | fn error(&mut self, error: ParseError); | 65 | fn error(&mut self, error: ParseError); |
64 | 66 | ||