diff options
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 3 | ||||
-rw-r--r-- | crates/ra_syntax/src/parsing.rs | 32 | ||||
-rw-r--r-- | crates/ra_syntax/src/parsing/builder.rs | 15 | ||||
-rw-r--r-- | crates/ra_syntax/src/parsing/input.rs | 7 | ||||
-rw-r--r-- | crates/ra_syntax/src/parsing/reparsing.rs | 21 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_error.rs | 4 |
6 files changed, 30 insertions, 52 deletions
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index b12282b39..c788bddec 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs | |||
@@ -16,7 +16,6 @@ | |||
16 | #![allow(missing_docs)] | 16 | #![allow(missing_docs)] |
17 | //#![warn(unreachable_pub)] // rust-lang/rust#47816 | 17 | //#![warn(unreachable_pub)] // rust-lang/rust#47816 |
18 | 18 | ||
19 | mod syntax_kinds; | ||
20 | mod syntax_node; | 19 | mod syntax_node; |
21 | mod syntax_text; | 20 | mod syntax_text; |
22 | mod syntax_error; | 21 | mod syntax_error; |
@@ -31,9 +30,9 @@ pub mod ast; | |||
31 | pub mod utils; | 30 | pub mod utils; |
32 | 31 | ||
33 | pub use rowan::{SmolStr, TextRange, TextUnit}; | 32 | pub use rowan::{SmolStr, TextRange, TextUnit}; |
33 | pub use ra_parser::SyntaxKind; | ||
34 | pub use crate::{ | 34 | pub use crate::{ |
35 | ast::AstNode, | 35 | ast::AstNode, |
36 | syntax_kinds::SyntaxKind, | ||
37 | syntax_error::{SyntaxError, SyntaxErrorKind, Location}, | 36 | syntax_error::{SyntaxError, SyntaxErrorKind, Location}, |
38 | syntax_text::SyntaxText, | 37 | syntax_text::SyntaxText, |
39 | syntax_node::{Direction, SyntaxNode, WalkEvent, TreeArc}, | 38 | syntax_node::{Direction, SyntaxNode, WalkEvent, TreeArc}, |
diff --git a/crates/ra_syntax/src/parsing.rs b/crates/ra_syntax/src/parsing.rs index 7e1b32035..0a11600e1 100644 --- a/crates/ra_syntax/src/parsing.rs +++ b/crates/ra_syntax/src/parsing.rs | |||
@@ -1,50 +1,28 @@ | |||
1 | #[macro_use] | ||
2 | mod token_set; | ||
3 | mod builder; | 1 | mod builder; |
4 | mod lexer; | 2 | mod lexer; |
5 | mod event; | ||
6 | mod input; | 3 | mod input; |
7 | mod parser; | ||
8 | mod grammar; | ||
9 | mod reparsing; | 4 | mod reparsing; |
10 | 5 | ||
6 | use ra_parser::{parse, ParseError}; | ||
7 | |||
11 | use crate::{ | 8 | use crate::{ |
12 | SyntaxKind, SyntaxError, | 9 | SyntaxKind, SyntaxError, |
13 | parsing::{ | 10 | parsing::{ |
14 | builder::TreeBuilder, | 11 | builder::TreeBuilder, |
15 | input::ParserInput, | 12 | input::ParserInput, |
16 | event::process, | ||
17 | parser::Parser, | ||
18 | }, | 13 | }, |
19 | syntax_node::GreenNode, | 14 | syntax_node::GreenNode, |
20 | }; | 15 | }; |
21 | 16 | ||
22 | pub use self::lexer::{tokenize, Token}; | 17 | pub use self::lexer::{tokenize, Token}; |
23 | 18 | ||
24 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
25 | pub struct ParseError(pub String); | ||
26 | |||
27 | pub(crate) use self::reparsing::incremental_reparse; | 19 | pub(crate) use self::reparsing::incremental_reparse; |
28 | 20 | ||
29 | pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) { | 21 | pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) { |
30 | let tokens = tokenize(&text); | 22 | let tokens = tokenize(&text); |
31 | let tree_sink = TreeBuilder::new(text, &tokens); | 23 | let token_source = ParserInput::new(text, &tokens); |
32 | parse_with(tree_sink, text, &tokens, grammar::root) | 24 | let mut tree_sink = TreeBuilder::new(text, &tokens); |
33 | } | 25 | parse(&token_source, &mut tree_sink); |
34 | |||
35 | fn parse_with<S: TreeSink>( | ||
36 | mut tree_sink: S, | ||
37 | text: &str, | ||
38 | tokens: &[Token], | ||
39 | f: fn(&mut Parser), | ||
40 | ) -> S::Tree { | ||
41 | let events = { | ||
42 | let input = ParserInput::new(text, &tokens); | ||
43 | let mut p = Parser::new(&input); | ||
44 | f(&mut p); | ||
45 | p.finish() | ||
46 | }; | ||
47 | process(&mut tree_sink, events); | ||
48 | tree_sink.finish() | 26 | tree_sink.finish() |
49 | } | 27 | } |
50 | 28 | ||
diff --git a/crates/ra_syntax/src/parsing/builder.rs b/crates/ra_syntax/src/parsing/builder.rs index 1041c6a7b..0775b0900 100644 --- a/crates/ra_syntax/src/parsing/builder.rs +++ b/crates/ra_syntax/src/parsing/builder.rs | |||
@@ -1,7 +1,9 @@ | |||
1 | use ra_parser::{TreeSink, ParseError}; | ||
2 | |||
1 | use crate::{ | 3 | use crate::{ |
2 | SmolStr, SyntaxError, SyntaxErrorKind, TextUnit, TextRange, | 4 | SmolStr, SyntaxError, SyntaxErrorKind, TextUnit, TextRange, |
3 | SyntaxKind::{self, *}, | 5 | SyntaxKind::{self, *}, |
4 | parsing::{TreeSink, ParseError, Token}, | 6 | parsing::Token, |
5 | syntax_node::{GreenNode, RaTypes}, | 7 | syntax_node::{GreenNode, RaTypes}, |
6 | }; | 8 | }; |
7 | 9 | ||
@@ -17,8 +19,6 @@ pub(crate) struct TreeBuilder<'a> { | |||
17 | } | 19 | } |
18 | 20 | ||
19 | impl<'a> TreeSink for TreeBuilder<'a> { | 21 | impl<'a> TreeSink for TreeBuilder<'a> { |
20 | type Tree = (GreenNode, Vec<SyntaxError>); | ||
21 | |||
22 | fn leaf(&mut self, kind: SyntaxKind, n_tokens: u8) { | 22 | fn leaf(&mut self, kind: SyntaxKind, n_tokens: u8) { |
23 | self.eat_trivias(); | 23 | self.eat_trivias(); |
24 | let n_tokens = n_tokens as usize; | 24 | let n_tokens = n_tokens as usize; |
@@ -65,10 +65,6 @@ impl<'a> TreeSink for TreeBuilder<'a> { | |||
65 | let error = SyntaxError::new(SyntaxErrorKind::ParseError(error), self.text_pos); | 65 | let error = SyntaxError::new(SyntaxErrorKind::ParseError(error), self.text_pos); |
66 | self.errors.push(error) | 66 | self.errors.push(error) |
67 | } | 67 | } |
68 | |||
69 | fn finish(self) -> (GreenNode, Vec<SyntaxError>) { | ||
70 | (self.inner.finish(), self.errors) | ||
71 | } | ||
72 | } | 68 | } |
73 | 69 | ||
74 | impl<'a> TreeBuilder<'a> { | 70 | impl<'a> TreeBuilder<'a> { |
@@ -82,6 +78,11 @@ impl<'a> TreeBuilder<'a> { | |||
82 | inner: GreenNodeBuilder::new(), | 78 | inner: GreenNodeBuilder::new(), |
83 | } | 79 | } |
84 | } | 80 | } |
81 | |||
82 | pub(super) fn finish(self) -> (GreenNode, Vec<SyntaxError>) { | ||
83 | (self.inner.finish(), self.errors) | ||
84 | } | ||
85 | |||
85 | fn eat_trivias(&mut self) { | 86 | fn eat_trivias(&mut self) { |
86 | while let Some(&token) = self.tokens.get(self.token_pos) { | 87 | while let Some(&token) = self.tokens.get(self.token_pos) { |
87 | if !token.kind.is_trivia() { | 88 | if !token.kind.is_trivia() { |
diff --git a/crates/ra_syntax/src/parsing/input.rs b/crates/ra_syntax/src/parsing/input.rs index 96c03bb11..58be795bc 100644 --- a/crates/ra_syntax/src/parsing/input.rs +++ b/crates/ra_syntax/src/parsing/input.rs | |||
@@ -1,9 +1,8 @@ | |||
1 | use ra_parser::TokenSource; | ||
2 | |||
1 | use crate::{ | 3 | use crate::{ |
2 | SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit, | 4 | SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit, |
3 | parsing::{ | 5 | parsing::lexer::Token, |
4 | TokenSource, | ||
5 | lexer::Token, | ||
6 | }, | ||
7 | }; | 6 | }; |
8 | 7 | ||
9 | impl<'t> TokenSource for ParserInput<'t> { | 8 | impl<'t> TokenSource for ParserInput<'t> { |
diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index 2c860b3df..ffcb512ad 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs | |||
@@ -1,18 +1,18 @@ | |||
1 | use ra_text_edit::AtomTextEdit; | ||
2 | use ra_parser::Reparser; | ||
3 | |||
1 | use crate::{ | 4 | use crate::{ |
2 | SyntaxKind::*, TextRange, TextUnit, | 5 | SyntaxKind::*, TextRange, TextUnit, |
3 | algo, | 6 | algo, |
4 | syntax_node::{GreenNode, SyntaxNode}, | 7 | syntax_node::{GreenNode, SyntaxNode}, |
5 | syntax_error::SyntaxError, | 8 | syntax_error::SyntaxError, |
6 | parsing::{ | 9 | parsing::{ |
7 | grammar, parse_with, | 10 | input::ParserInput, |
8 | builder::TreeBuilder, | 11 | builder::TreeBuilder, |
9 | parser::Parser, | ||
10 | lexer::{tokenize, Token}, | 12 | lexer::{tokenize, Token}, |
11 | } | 13 | } |
12 | }; | 14 | }; |
13 | 15 | ||
14 | use ra_text_edit::AtomTextEdit; | ||
15 | |||
16 | pub(crate) fn incremental_reparse( | 16 | pub(crate) fn incremental_reparse( |
17 | node: &SyntaxNode, | 17 | node: &SyntaxNode, |
18 | edit: &AtomTextEdit, | 18 | edit: &AtomTextEdit, |
@@ -61,8 +61,10 @@ fn reparse_block<'node>( | |||
61 | if !is_balanced(&tokens) { | 61 | if !is_balanced(&tokens) { |
62 | return None; | 62 | return None; |
63 | } | 63 | } |
64 | let tree_sink = TreeBuilder::new(&text, &tokens); | 64 | let token_source = ParserInput::new(&text, &tokens); |
65 | let (green, new_errors) = parse_with(tree_sink, &text, &tokens, reparser); | 65 | let mut tree_sink = TreeBuilder::new(&text, &tokens); |
66 | reparser.parse(&token_source, &mut tree_sink); | ||
67 | let (green, new_errors) = tree_sink.finish(); | ||
66 | Some((node, green, new_errors)) | 68 | Some((node, green, new_errors)) |
67 | } | 69 | } |
68 | 70 | ||
@@ -78,15 +80,12 @@ fn is_contextual_kw(text: &str) -> bool { | |||
78 | } | 80 | } |
79 | } | 81 | } |
80 | 82 | ||
81 | fn find_reparsable_node( | 83 | fn find_reparsable_node(node: &SyntaxNode, range: TextRange) -> Option<(&SyntaxNode, Reparser)> { |
82 | node: &SyntaxNode, | ||
83 | range: TextRange, | ||
84 | ) -> Option<(&SyntaxNode, fn(&mut Parser))> { | ||
85 | let node = algo::find_covering_node(node, range); | 84 | let node = algo::find_covering_node(node, range); |
86 | node.ancestors().find_map(|node| { | 85 | node.ancestors().find_map(|node| { |
87 | let first_child = node.first_child().map(|it| it.kind()); | 86 | let first_child = node.first_child().map(|it| it.kind()); |
88 | let parent = node.parent().map(|it| it.kind()); | 87 | let parent = node.parent().map(|it| it.kind()); |
89 | grammar::reparser(node.kind(), first_child, parent).map(|r| (node, r)) | 88 | Reparser::for_node(node.kind(), first_child, parent).map(|r| (node, r)) |
90 | }) | 89 | }) |
91 | } | 90 | } |
92 | 91 | ||
diff --git a/crates/ra_syntax/src/syntax_error.rs b/crates/ra_syntax/src/syntax_error.rs index 1a00fcc27..bdd431742 100644 --- a/crates/ra_syntax/src/syntax_error.rs +++ b/crates/ra_syntax/src/syntax_error.rs | |||
@@ -1,6 +1,8 @@ | |||
1 | use std::fmt; | 1 | use std::fmt; |
2 | 2 | ||
3 | use crate::{TextRange, TextUnit, parsing::ParseError}; | 3 | use ra_parser::ParseError; |
4 | |||
5 | use crate::{TextRange, TextUnit}; | ||
4 | 6 | ||
5 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 7 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
6 | pub struct SyntaxError { | 8 | pub struct SyntaxError { |