aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock8
-rw-r--r--crates/ra_parser/Cargo.toml9
-rw-r--r--crates/ra_parser/src/event.rs (renamed from crates/ra_syntax/src/parsing/event.rs)4
-rw-r--r--crates/ra_parser/src/grammar.rs (renamed from crates/ra_syntax/src/parsing/grammar.rs)10
-rw-r--r--crates/ra_parser/src/grammar/attributes.rs (renamed from crates/ra_syntax/src/parsing/grammar/attributes.rs)0
-rw-r--r--crates/ra_parser/src/grammar/expressions.rs (renamed from crates/ra_syntax/src/parsing/grammar/expressions.rs)0
-rw-r--r--crates/ra_parser/src/grammar/expressions/atom.rs (renamed from crates/ra_syntax/src/parsing/grammar/expressions/atom.rs)0
-rw-r--r--crates/ra_parser/src/grammar/items.rs (renamed from crates/ra_syntax/src/parsing/grammar/items.rs)0
-rw-r--r--crates/ra_parser/src/grammar/items/consts.rs (renamed from crates/ra_syntax/src/parsing/grammar/items/consts.rs)0
-rw-r--r--crates/ra_parser/src/grammar/items/nominal.rs (renamed from crates/ra_syntax/src/parsing/grammar/items/nominal.rs)0
-rw-r--r--crates/ra_parser/src/grammar/items/traits.rs (renamed from crates/ra_syntax/src/parsing/grammar/items/traits.rs)0
-rw-r--r--crates/ra_parser/src/grammar/items/use_item.rs (renamed from crates/ra_syntax/src/parsing/grammar/items/use_item.rs)0
-rw-r--r--crates/ra_parser/src/grammar/params.rs (renamed from crates/ra_syntax/src/parsing/grammar/params.rs)0
-rw-r--r--crates/ra_parser/src/grammar/paths.rs (renamed from crates/ra_syntax/src/parsing/grammar/paths.rs)0
-rw-r--r--crates/ra_parser/src/grammar/patterns.rs (renamed from crates/ra_syntax/src/parsing/grammar/patterns.rs)0
-rw-r--r--crates/ra_parser/src/grammar/type_args.rs (renamed from crates/ra_syntax/src/parsing/grammar/type_args.rs)0
-rw-r--r--crates/ra_parser/src/grammar/type_params.rs (renamed from crates/ra_syntax/src/parsing/grammar/type_params.rs)0
-rw-r--r--crates/ra_parser/src/grammar/types.rs (renamed from crates/ra_syntax/src/parsing/grammar/types.rs)0
-rw-r--r--crates/ra_parser/src/lib.rs64
-rw-r--r--crates/ra_parser/src/parser.rs (renamed from crates/ra_syntax/src/parsing/parser.rs)7
-rw-r--r--crates/ra_parser/src/syntax_kind.rs (renamed from crates/ra_syntax/src/syntax_kinds.rs)4
-rw-r--r--crates/ra_parser/src/syntax_kind/generated.rs (renamed from crates/ra_syntax/src/syntax_kinds/generated.rs)4
-rw-r--r--crates/ra_parser/src/syntax_kind/generated.rs.tera (renamed from crates/ra_syntax/src/syntax_kinds/generated.rs.tera)4
-rw-r--r--crates/ra_parser/src/token_set.rs (renamed from crates/ra_syntax/src/parsing/token_set.rs)0
-rw-r--r--crates/ra_syntax/Cargo.toml1
-rw-r--r--crates/ra_syntax/src/lib.rs3
-rw-r--r--crates/ra_syntax/src/parsing.rs32
-rw-r--r--crates/ra_syntax/src/parsing/builder.rs15
-rw-r--r--crates/ra_syntax/src/parsing/input.rs7
-rw-r--r--crates/ra_syntax/src/parsing/reparsing.rs21
-rw-r--r--crates/ra_syntax/src/syntax_error.rs4
-rw-r--r--crates/tools/src/lib.rs4
32 files changed, 127 insertions, 74 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 7b1412556..f09cf98c8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1072,6 +1072,13 @@ dependencies = [
1072] 1072]
1073 1073
1074[[package]] 1074[[package]]
1075name = "ra_parser"
1076version = "0.1.0"
1077dependencies = [
1078 "drop_bomb 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
1079]
1080
1081[[package]]
1075name = "ra_project_model" 1082name = "ra_project_model"
1076version = "0.1.0" 1083version = "0.1.0"
1077dependencies = [ 1084dependencies = [
@@ -1093,6 +1100,7 @@ dependencies = [
1093 "drop_bomb 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", 1100 "drop_bomb 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
1094 "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 1101 "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
1095 "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", 1102 "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
1103 "ra_parser 0.1.0",
1096 "ra_text_edit 0.1.0", 1104 "ra_text_edit 0.1.0",
1097 "rowan 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", 1105 "rowan 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
1098 "smol_str 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", 1106 "smol_str 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/crates/ra_parser/Cargo.toml b/crates/ra_parser/Cargo.toml
new file mode 100644
index 000000000..b110e2bc6
--- /dev/null
+++ b/crates/ra_parser/Cargo.toml
@@ -0,0 +1,9 @@
1[package]
2edition = "2018"
3name = "ra_parser"
4version = "0.1.0"
5authors = ["rust-analyzer developers"]
6publish = false
7
8[dependencies]
9drop_bomb = "0.1.4"
diff --git a/crates/ra_syntax/src/parsing/event.rs b/crates/ra_parser/src/event.rs
index d6cbdffe0..d6e8454d4 100644
--- a/crates/ra_syntax/src/parsing/event.rs
+++ b/crates/ra_parser/src/event.rs
@@ -10,8 +10,8 @@
10use std::mem; 10use std::mem;
11 11
12use crate::{ 12use crate::{
13 ParseError, TreeSink,
13 SyntaxKind::{self, *}, 14 SyntaxKind::{self, *},
14 parsing::{ParseError, TreeSink},
15}; 15};
16 16
17/// `Parser` produces a flat list of `Event`s. 17/// `Parser` produces a flat list of `Event`s.
@@ -84,7 +84,7 @@ impl Event {
84} 84}
85 85
86/// Generate the syntax tree with the control of events. 86/// Generate the syntax tree with the control of events.
87pub(super) fn process(sink: &mut impl TreeSink, mut events: Vec<Event>) { 87pub(super) fn process(sink: &mut dyn TreeSink, mut events: Vec<Event>) {
88 let mut forward_parents = Vec::new(); 88 let mut forward_parents = Vec::new();
89 89
90 for i in 0..events.len() { 90 for i in 0..events.len() {
diff --git a/crates/ra_syntax/src/parsing/grammar.rs b/crates/ra_parser/src/grammar.rs
index 800d5a4a2..15aab6c6f 100644
--- a/crates/ra_syntax/src/parsing/grammar.rs
+++ b/crates/ra_parser/src/grammar.rs
@@ -38,20 +38,18 @@ mod types;
38 38
39use crate::{ 39use crate::{
40 SyntaxKind::{self, *}, 40 SyntaxKind::{self, *},
41 parsing::{ 41 TokenSet,
42 token_set::TokenSet, 42 parser::{CompletedMarker, Marker, Parser},
43 parser::{CompletedMarker, Marker, Parser}
44 },
45}; 43};
46 44
47pub(super) fn root(p: &mut Parser) { 45pub(crate) fn root(p: &mut Parser) {
48 let m = p.start(); 46 let m = p.start();
49 p.eat(SHEBANG); 47 p.eat(SHEBANG);
50 items::mod_contents(p, false); 48 items::mod_contents(p, false);
51 m.complete(p, SOURCE_FILE); 49 m.complete(p, SOURCE_FILE);
52} 50}
53 51
54pub(super) fn reparser( 52pub(crate) fn reparser(
55 node: SyntaxKind, 53 node: SyntaxKind,
56 first_child: Option<SyntaxKind>, 54 first_child: Option<SyntaxKind>,
57 parent: Option<SyntaxKind>, 55 parent: Option<SyntaxKind>,
diff --git a/crates/ra_syntax/src/parsing/grammar/attributes.rs b/crates/ra_parser/src/grammar/attributes.rs
index cd30e8a45..cd30e8a45 100644
--- a/crates/ra_syntax/src/parsing/grammar/attributes.rs
+++ b/crates/ra_parser/src/grammar/attributes.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs
index d5a4f4d7b..d5a4f4d7b 100644
--- a/crates/ra_syntax/src/parsing/grammar/expressions.rs
+++ b/crates/ra_parser/src/grammar/expressions.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs
index e74305b6a..e74305b6a 100644
--- a/crates/ra_syntax/src/parsing/grammar/expressions/atom.rs
+++ b/crates/ra_parser/src/grammar/expressions/atom.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs
index 4b962c1f3..4b962c1f3 100644
--- a/crates/ra_syntax/src/parsing/grammar/items.rs
+++ b/crates/ra_parser/src/grammar/items.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/items/consts.rs b/crates/ra_parser/src/grammar/items/consts.rs
index 5a5852f83..5a5852f83 100644
--- a/crates/ra_syntax/src/parsing/grammar/items/consts.rs
+++ b/crates/ra_parser/src/grammar/items/consts.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/items/nominal.rs b/crates/ra_parser/src/grammar/items/nominal.rs
index ff9b38f9c..ff9b38f9c 100644
--- a/crates/ra_syntax/src/parsing/grammar/items/nominal.rs
+++ b/crates/ra_parser/src/grammar/items/nominal.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/items/traits.rs b/crates/ra_parser/src/grammar/items/traits.rs
index d5a8ccd98..d5a8ccd98 100644
--- a/crates/ra_syntax/src/parsing/grammar/items/traits.rs
+++ b/crates/ra_parser/src/grammar/items/traits.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/items/use_item.rs b/crates/ra_parser/src/grammar/items/use_item.rs
index 5111d37eb..5111d37eb 100644
--- a/crates/ra_syntax/src/parsing/grammar/items/use_item.rs
+++ b/crates/ra_parser/src/grammar/items/use_item.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/params.rs b/crates/ra_parser/src/grammar/params.rs
index 185386569..185386569 100644
--- a/crates/ra_syntax/src/parsing/grammar/params.rs
+++ b/crates/ra_parser/src/grammar/params.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/paths.rs b/crates/ra_parser/src/grammar/paths.rs
index 33a11886c..33a11886c 100644
--- a/crates/ra_syntax/src/parsing/grammar/paths.rs
+++ b/crates/ra_parser/src/grammar/paths.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/patterns.rs b/crates/ra_parser/src/grammar/patterns.rs
index 9d7da639d..9d7da639d 100644
--- a/crates/ra_syntax/src/parsing/grammar/patterns.rs
+++ b/crates/ra_parser/src/grammar/patterns.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/type_args.rs b/crates/ra_parser/src/grammar/type_args.rs
index f889419c5..f889419c5 100644
--- a/crates/ra_syntax/src/parsing/grammar/type_args.rs
+++ b/crates/ra_parser/src/grammar/type_args.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/type_params.rs b/crates/ra_parser/src/grammar/type_params.rs
index 40f998682..40f998682 100644
--- a/crates/ra_syntax/src/parsing/grammar/type_params.rs
+++ b/crates/ra_parser/src/grammar/type_params.rs
diff --git a/crates/ra_syntax/src/parsing/grammar/types.rs b/crates/ra_parser/src/grammar/types.rs
index adc189a29..adc189a29 100644
--- a/crates/ra_syntax/src/parsing/grammar/types.rs
+++ b/crates/ra_parser/src/grammar/types.rs
diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs
new file mode 100644
index 000000000..7931b5189
--- /dev/null
+++ b/crates/ra_parser/src/lib.rs
@@ -0,0 +1,64 @@
1#[macro_use]
2mod token_set;
3mod syntax_kind;
4mod event;
5mod parser;
6mod grammar;
7
8pub(crate) use token_set::TokenSet;
9
10pub use syntax_kind::SyntaxKind;
11
12#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13pub struct ParseError(pub String);
14
15/// `TreeSink` abstracts details of a particular syntax tree implementation.
16pub trait TreeSink {
17 /// Adds new leaf to the current branch.
18 fn leaf(&mut self, kind: SyntaxKind, n_tokens: u8);
19
20 /// Start new branch and make it current.
21 fn start_branch(&mut self, kind: SyntaxKind, root: bool);
22
23 /// Finish current branch and restore previous
24 /// branch as current.
25 fn finish_branch(&mut self, root: bool);
26
27 fn error(&mut self, error: ParseError);
28}
29
30/// `TokenSource` abstracts the source of the tokens parser operates one.
31///
32/// Hopefully this will allow us to treat text and token trees in the same way!
33pub 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
39pub fn parse(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
40 let mut p = parser::Parser::new(token_source);
41 grammar::root(&mut p);
42 let events = p.finish();
43 event::process(tree_sink, events);
44}
45
46pub struct Reparser(fn(&mut parser::Parser));
47
48impl Reparser {
49 pub fn for_node(
50 node: SyntaxKind,
51 first_child: Option<SyntaxKind>,
52 parent: Option<SyntaxKind>,
53 ) -> Option<Reparser> {
54 grammar::reparser(node, first_child, parent).map(Reparser)
55 }
56
57 pub fn parse(self, token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
58 let Reparser(r) = self;
59 let mut p = parser::Parser::new(token_source);
60 r(&mut p);
61 let events = p.finish();
62 event::process(tree_sink, events);
63 }
64}
diff --git a/crates/ra_syntax/src/parsing/parser.rs b/crates/ra_parser/src/parser.rs
index 923b0f2b2..a18458148 100644
--- a/crates/ra_syntax/src/parsing/parser.rs
+++ b/crates/ra_parser/src/parser.rs
@@ -4,11 +4,8 @@ use drop_bomb::DropBomb;
4 4
5use crate::{ 5use crate::{
6 SyntaxKind::{self, ERROR, EOF, TOMBSTONE}, 6 SyntaxKind::{self, ERROR, EOF, TOMBSTONE},
7 parsing::{ 7 TokenSource, ParseError, TokenSet,
8 TokenSource, ParseError, 8 event::Event,
9 token_set::TokenSet,
10 event::Event,
11 },
12}; 9};
13 10
14/// `Parser` struct provides the low-level API for 11/// `Parser` struct provides the low-level API for
diff --git a/crates/ra_syntax/src/syntax_kinds.rs b/crates/ra_parser/src/syntax_kind.rs
index c1118c5ab..a2353317f 100644
--- a/crates/ra_syntax/src/syntax_kinds.rs
+++ b/crates/ra_parser/src/syntax_kind.rs
@@ -2,8 +2,6 @@ mod generated;
2 2
3use std::fmt; 3use std::fmt;
4 4
5use crate::SyntaxKind::*;
6
7pub use self::generated::SyntaxKind; 5pub use self::generated::SyntaxKind;
8 6
9impl fmt::Debug for SyntaxKind { 7impl fmt::Debug for SyntaxKind {
@@ -20,7 +18,7 @@ pub(crate) struct SyntaxInfo {
20impl SyntaxKind { 18impl SyntaxKind {
21 pub fn is_trivia(self) -> bool { 19 pub fn is_trivia(self) -> bool {
22 match self { 20 match self {
23 WHITESPACE | COMMENT => true, 21 SyntaxKind::WHITESPACE | SyntaxKind::COMMENT => true,
24 _ => false, 22 _ => false,
25 } 23 }
26 } 24 }
diff --git a/crates/ra_syntax/src/syntax_kinds/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs
index 266b95bbb..1d8f988ae 100644
--- a/crates/ra_syntax/src/syntax_kinds/generated.rs
+++ b/crates/ra_parser/src/syntax_kind/generated.rs
@@ -568,7 +568,7 @@ impl SyntaxKind {
568 EOF => &SyntaxInfo { name: "EOF" }, 568 EOF => &SyntaxInfo { name: "EOF" },
569 } 569 }
570 } 570 }
571 pub(crate) fn from_keyword(ident: &str) -> Option<SyntaxKind> { 571 pub fn from_keyword(ident: &str) -> Option<SyntaxKind> {
572 let kw = match ident { 572 let kw = match ident {
573 "use" => USE_KW, 573 "use" => USE_KW,
574 "fn" => FN_KW, 574 "fn" => FN_KW,
@@ -610,7 +610,7 @@ impl SyntaxKind {
610 Some(kw) 610 Some(kw)
611 } 611 }
612 612
613 pub(crate) fn from_char(c: char) -> Option<SyntaxKind> { 613 pub fn from_char(c: char) -> Option<SyntaxKind> {
614 let tok = match c { 614 let tok = match c {
615 ';' => SEMI, 615 ';' => SEMI,
616 ',' => COMMA, 616 ',' => COMMA,
diff --git a/crates/ra_syntax/src/syntax_kinds/generated.rs.tera b/crates/ra_parser/src/syntax_kind/generated.rs.tera
index 837437136..f241a21a0 100644
--- a/crates/ra_syntax/src/syntax_kinds/generated.rs.tera
+++ b/crates/ra_parser/src/syntax_kind/generated.rs.tera
@@ -74,7 +74,7 @@ impl SyntaxKind {
74 EOF => &SyntaxInfo { name: "EOF" }, 74 EOF => &SyntaxInfo { name: "EOF" },
75 } 75 }
76 } 76 }
77 pub(crate) fn from_keyword(ident: &str) -> Option<SyntaxKind> { 77 pub fn from_keyword(ident: &str) -> Option<SyntaxKind> {
78 let kw = match ident { 78 let kw = match ident {
79{%- for kw in keywords %} 79{%- for kw in keywords %}
80 "{{kw}}" => {{kw | upper}}_KW, 80 "{{kw}}" => {{kw | upper}}_KW,
@@ -84,7 +84,7 @@ impl SyntaxKind {
84 Some(kw) 84 Some(kw)
85 } 85 }
86 86
87 pub(crate) fn from_char(c: char) -> Option<SyntaxKind> { 87 pub fn from_char(c: char) -> Option<SyntaxKind> {
88 let tok = match c { 88 let tok = match c {
89{%- for t in single_byte_tokens %} 89{%- for t in single_byte_tokens %}
90 '{{t.0}}' => {{t.1}}, 90 '{{t.0}}' => {{t.1}},
diff --git a/crates/ra_syntax/src/parsing/token_set.rs b/crates/ra_parser/src/token_set.rs
index 24152a38a..24152a38a 100644
--- a/crates/ra_syntax/src/parsing/token_set.rs
+++ b/crates/ra_parser/src/token_set.rs
diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml
index 7ce26b7c4..7e70dad3f 100644
--- a/crates/ra_syntax/Cargo.toml
+++ b/crates/ra_syntax/Cargo.toml
@@ -21,6 +21,7 @@ text_unit = { version = "0.1.6", features = ["serde"] }
21smol_str = { version = "0.1.9", features = ["serde"] } 21smol_str = { version = "0.1.9", features = ["serde"] }
22 22
23ra_text_edit = { path = "../ra_text_edit" } 23ra_text_edit = { path = "../ra_text_edit" }
24ra_parser = { path = "../ra_parser" }
24 25
25[dev-dependencies] 26[dev-dependencies]
26test_utils = { path = "../test_utils" } 27test_utils = { path = "../test_utils" }
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
19mod syntax_kinds;
20mod syntax_node; 19mod syntax_node;
21mod syntax_text; 20mod syntax_text;
22mod syntax_error; 21mod syntax_error;
@@ -31,9 +30,9 @@ pub mod ast;
31pub mod utils; 30pub mod utils;
32 31
33pub use rowan::{SmolStr, TextRange, TextUnit}; 32pub use rowan::{SmolStr, TextRange, TextUnit};
33pub use ra_parser::SyntaxKind;
34pub use crate::{ 34pub 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]
2mod token_set;
3mod builder; 1mod builder;
4mod lexer; 2mod lexer;
5mod event;
6mod input; 3mod input;
7mod parser;
8mod grammar;
9mod reparsing; 4mod reparsing;
10 5
6use ra_parser::{parse, ParseError};
7
11use crate::{ 8use 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
22pub use self::lexer::{tokenize, Token}; 17pub use self::lexer::{tokenize, Token};
23 18
24#[derive(Debug, Clone, PartialEq, Eq, Hash)]
25pub struct ParseError(pub String);
26
27pub(crate) use self::reparsing::incremental_reparse; 19pub(crate) use self::reparsing::incremental_reparse;
28 20
29pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) { 21pub(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
35fn 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 @@
1use ra_parser::{TreeSink, ParseError};
2
1use crate::{ 3use 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
19impl<'a> TreeSink for TreeBuilder<'a> { 21impl<'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
74impl<'a> TreeBuilder<'a> { 70impl<'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 @@
1use ra_parser::TokenSource;
2
1use crate::{ 3use 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
9impl<'t> TokenSource for ParserInput<'t> { 8impl<'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 @@
1use ra_text_edit::AtomTextEdit;
2use ra_parser::Reparser;
3
1use crate::{ 4use 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
14use ra_text_edit::AtomTextEdit;
15
16pub(crate) fn incremental_reparse( 16pub(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
81fn find_reparsable_node( 83fn 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 @@
1use std::fmt; 1use std::fmt;
2 2
3use crate::{TextRange, TextUnit, parsing::ParseError}; 3use ra_parser::ParseError;
4
5use crate::{TextRange, TextUnit};
4 6
5#[derive(Debug, Clone, PartialEq, Eq, Hash)] 7#[derive(Debug, Clone, PartialEq, Eq, Hash)]
6pub struct SyntaxError { 8pub struct SyntaxError {
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index 0a10d2737..3c23ed76e 100644
--- a/crates/tools/src/lib.rs
+++ b/crates/tools/src/lib.rs
@@ -14,11 +14,11 @@ pub use teraron::{Mode, Overwrite, Verify};
14pub type Result<T> = std::result::Result<T, failure::Error>; 14pub type Result<T> = std::result::Result<T, failure::Error>;
15 15
16pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron"; 16pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
17const GRAMMAR_DIR: &str = "crates/ra_syntax/src/parsing/grammar"; 17const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar";
18const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/ok"; 18const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/ok";
19const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/err"; 19const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/tests/data/parser/inline/err";
20 20
21pub const SYNTAX_KINDS: &str = "crates/ra_syntax/src/syntax_kinds/generated.rs.tera"; 21pub const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs.tera";
22pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera"; 22pub const AST: &str = "crates/ra_syntax/src/ast/generated.rs.tera";
23const TOOLCHAIN: &str = "stable"; 23const TOOLCHAIN: &str = "stable";
24 24