diff options
author | Aleksey Kladov <[email protected]> | 2019-02-21 10:27:45 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-21 10:27:45 +0000 |
commit | d334b5a1db9ec6a57f54077d422a3f4b3c8c1178 (patch) | |
tree | 9d930fe43452e8188594c612de433a77524e4754 /crates | |
parent | 18b0c509f77a8e06141fee6668532cced1ebf5d8 (diff) |
move parser to a separate crate
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_parser/Cargo.toml | 9 | ||||
-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.rs | 64 | ||||
-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) | 0 | ||||
-rw-r--r-- | crates/ra_parser/src/token_set.rs (renamed from crates/ra_syntax/src/parsing/token_set.rs) | 0 |
23 files changed, 84 insertions, 18 deletions
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] | ||
2 | edition = "2018" | ||
3 | name = "ra_parser" | ||
4 | version = "0.1.0" | ||
5 | authors = ["rust-analyzer developers"] | ||
6 | publish = false | ||
7 | |||
8 | [dependencies] | ||
9 | drop_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 @@ | |||
10 | use std::mem; | 10 | use std::mem; |
11 | 11 | ||
12 | use crate::{ | 12 | use 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. |
87 | pub(super) fn process(sink: &mut impl TreeSink, mut events: Vec<Event>) { | 87 | pub(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 | ||
39 | use crate::{ | 39 | use 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 | ||
47 | pub(super) fn root(p: &mut Parser) { | 45 | pub(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 | ||
54 | pub(super) fn reparser( | 52 | pub(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..fbbac4c69 --- /dev/null +++ b/crates/ra_parser/src/lib.rs | |||
@@ -0,0 +1,64 @@ | |||
1 | #[macro_use] | ||
2 | mod token_set; | ||
3 | mod syntax_kind; | ||
4 | mod event; | ||
5 | mod parser; | ||
6 | mod grammar; | ||
7 | |||
8 | pub(crate) use token_set::TokenSet; | ||
9 | |||
10 | pub use syntax_kind::SyntaxKind; | ||
11 | |||
12 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
13 | pub struct ParseError(pub String); | ||
14 | |||
15 | /// `TreeSink` abstracts details of a particular syntax tree implementation. | ||
16 | pub 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! | ||
33 | pub 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 | |||
39 | pub 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 | |||
46 | pub struct Reparser(fn(&mut parser::Parser)); | ||
47 | |||
48 | impl 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 | |||
58 | pub fn reparse(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink, reparser: Reparser) { | ||
59 | let Reparser(r) = reparser; | ||
60 | let mut p = parser::Parser::new(token_source); | ||
61 | r(&mut p); | ||
62 | let events = p.finish(); | ||
63 | event::process(tree_sink, events); | ||
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 | ||
5 | use crate::{ | 5 | use 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 | ||
3 | use std::fmt; | 3 | use std::fmt; |
4 | 4 | ||
5 | use crate::SyntaxKind::*; | ||
6 | |||
7 | pub use self::generated::SyntaxKind; | 5 | pub use self::generated::SyntaxKind; |
8 | 6 | ||
9 | impl fmt::Debug for SyntaxKind { | 7 | impl fmt::Debug for SyntaxKind { |
@@ -20,7 +18,7 @@ pub(crate) struct SyntaxInfo { | |||
20 | impl SyntaxKind { | 18 | impl 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..837437136 100644 --- a/crates/ra_syntax/src/syntax_kinds/generated.rs.tera +++ b/crates/ra_parser/src/syntax_kind/generated.rs.tera | |||
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 | |||