diff options
author | Aleksey Kladov <[email protected]> | 2019-02-20 18:50:07 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-20 18:50:07 +0000 |
commit | 0c81b9deeed81bfb2cf8142af9d748317d5d71a1 (patch) | |
tree | e9c0a1affabfa444611b762dc721d426e3a5bb56 /crates/ra_syntax/src/parsing/parser_impl.rs | |
parent | 3517c175ac537b47dd3e36cc7fb1edd60b02c039 (diff) |
route parsing via TokenSource trait
Diffstat (limited to 'crates/ra_syntax/src/parsing/parser_impl.rs')
-rw-r--r-- | crates/ra_syntax/src/parsing/parser_impl.rs | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/crates/ra_syntax/src/parsing/parser_impl.rs b/crates/ra_syntax/src/parsing/parser_impl.rs index 02baed76b..c0d2b6ec1 100644 --- a/crates/ra_syntax/src/parsing/parser_impl.rs +++ b/crates/ra_syntax/src/parsing/parser_impl.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | mod event; | 1 | mod event; |
2 | mod input; | 2 | pub(crate) mod input; |
3 | 3 | ||
4 | use std::cell::Cell; | 4 | use std::cell::Cell; |
5 | 5 | ||
@@ -11,7 +11,7 @@ use crate::{ | |||
11 | parser_api::Parser, | 11 | parser_api::Parser, |
12 | parser_impl::{ | 12 | parser_impl::{ |
13 | event::{Event, EventProcessor}, | 13 | event::{Event, EventProcessor}, |
14 | input::{InputPosition, ParserInput}, | 14 | input::InputPosition, |
15 | }, | 15 | }, |
16 | }, | 16 | }, |
17 | }; | 17 | }; |
@@ -39,6 +39,12 @@ pub(super) trait TreeSink { | |||
39 | fn finish(self) -> Self::Tree; | 39 | fn finish(self) -> Self::Tree; |
40 | } | 40 | } |
41 | 41 | ||
42 | pub(super) trait TokenSource { | ||
43 | fn token_kind(&self, pos: InputPosition) -> SyntaxKind; | ||
44 | fn is_token_joint_to_next(&self, pos: InputPosition) -> bool; | ||
45 | fn is_keyword(&self, pos: InputPosition, kw: &str) -> bool; | ||
46 | } | ||
47 | |||
42 | /// Parse a sequence of tokens into the representative node tree | 48 | /// Parse a sequence of tokens into the representative node tree |
43 | pub(super) fn parse_with<S: TreeSink>( | 49 | pub(super) fn parse_with<S: TreeSink>( |
44 | sink: S, | 50 | sink: S, |
@@ -48,7 +54,7 @@ pub(super) fn parse_with<S: TreeSink>( | |||
48 | ) -> S::Tree { | 54 | ) -> S::Tree { |
49 | let mut events = { | 55 | let mut events = { |
50 | let input = input::ParserInput::new(text, tokens); | 56 | let input = input::ParserInput::new(text, tokens); |
51 | let parser_impl = ParserImpl::new(&input); | 57 | let parser_impl = ParserImpl::new(input); |
52 | let mut parser_api = Parser(parser_impl); | 58 | let mut parser_api = Parser(parser_impl); |
53 | parser(&mut parser_api); | 59 | parser(&mut parser_api); |
54 | parser_api.0.into_events() | 60 | parser_api.0.into_events() |
@@ -59,17 +65,17 @@ pub(super) fn parse_with<S: TreeSink>( | |||
59 | /// Implementation details of `Parser`, extracted | 65 | /// Implementation details of `Parser`, extracted |
60 | /// to a separate struct in order not to pollute | 66 | /// to a separate struct in order not to pollute |
61 | /// the public API of the `Parser`. | 67 | /// the public API of the `Parser`. |
62 | pub(super) struct ParserImpl<'t> { | 68 | pub(super) struct ParserImpl<S> { |
63 | parser_input: &'t ParserInput<'t>, | 69 | token_source: S, |
64 | pos: InputPosition, | 70 | pos: InputPosition, |
65 | events: Vec<Event>, | 71 | events: Vec<Event>, |
66 | steps: Cell<u32>, | 72 | steps: Cell<u32>, |
67 | } | 73 | } |
68 | 74 | ||
69 | impl<'t> ParserImpl<'t> { | 75 | impl<S: TokenSource> ParserImpl<S> { |
70 | fn new(inp: &'t ParserInput<'t>) -> ParserImpl<'t> { | 76 | fn new(token_source: S) -> ParserImpl<S> { |
71 | ParserImpl { | 77 | ParserImpl { |
72 | parser_input: inp, | 78 | token_source, |
73 | pos: InputPosition::new(), | 79 | pos: InputPosition::new(), |
74 | events: Vec::new(), | 80 | events: Vec::new(), |
75 | steps: Cell::new(0), | 81 | steps: Cell::new(0), |
@@ -82,11 +88,9 @@ impl<'t> ParserImpl<'t> { | |||
82 | } | 88 | } |
83 | 89 | ||
84 | pub(super) fn current2(&self) -> Option<(SyntaxKind, SyntaxKind)> { | 90 | pub(super) fn current2(&self) -> Option<(SyntaxKind, SyntaxKind)> { |
85 | let c1 = self.parser_input.kind(self.pos); | 91 | let c1 = self.token_source.token_kind(self.pos); |
86 | let c2 = self.parser_input.kind(self.pos + 1); | 92 | let c2 = self.token_source.token_kind(self.pos + 1); |
87 | if self.parser_input.token_start_at(self.pos + 1) | 93 | if self.token_source.is_token_joint_to_next(self.pos) { |
88 | == self.parser_input.token_start_at(self.pos) + self.parser_input.token_len(self.pos) | ||
89 | { | ||
90 | Some((c1, c2)) | 94 | Some((c1, c2)) |
91 | } else { | 95 | } else { |
92 | None | 96 | None |
@@ -94,14 +98,11 @@ impl<'t> ParserImpl<'t> { | |||
94 | } | 98 | } |
95 | 99 | ||
96 | pub(super) fn current3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> { | 100 | pub(super) fn current3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> { |
97 | let c1 = self.parser_input.kind(self.pos); | 101 | let c1 = self.token_source.token_kind(self.pos); |
98 | let c2 = self.parser_input.kind(self.pos + 1); | 102 | let c2 = self.token_source.token_kind(self.pos + 1); |
99 | let c3 = self.parser_input.kind(self.pos + 2); | 103 | let c3 = self.token_source.token_kind(self.pos + 2); |
100 | if self.parser_input.token_start_at(self.pos + 1) | 104 | if self.token_source.is_token_joint_to_next(self.pos) |
101 | == self.parser_input.token_start_at(self.pos) + self.parser_input.token_len(self.pos) | 105 | && self.token_source.is_token_joint_to_next(self.pos + 1) |
102 | && self.parser_input.token_start_at(self.pos + 2) | ||
103 | == self.parser_input.token_start_at(self.pos + 1) | ||
104 | + self.parser_input.token_len(self.pos + 1) | ||
105 | { | 106 | { |
106 | Some((c1, c2, c3)) | 107 | Some((c1, c2, c3)) |
107 | } else { | 108 | } else { |
@@ -114,12 +115,11 @@ impl<'t> ParserImpl<'t> { | |||
114 | let steps = self.steps.get(); | 115 | let steps = self.steps.get(); |
115 | assert!(steps <= 10_000_000, "the parser seems stuck"); | 116 | assert!(steps <= 10_000_000, "the parser seems stuck"); |
116 | self.steps.set(steps + 1); | 117 | self.steps.set(steps + 1); |
117 | 118 | self.token_source.token_kind(self.pos + n) | |
118 | self.parser_input.kind(self.pos + n) | ||
119 | } | 119 | } |
120 | 120 | ||
121 | pub(super) fn at_kw(&self, t: &str) -> bool { | 121 | pub(super) fn at_kw(&self, kw: &str) -> bool { |
122 | self.parser_input.token_text(self.pos) == t | 122 | self.token_source.is_keyword(self.pos, kw) |
123 | } | 123 | } |
124 | 124 | ||
125 | /// Start parsing right behind the last event. | 125 | /// Start parsing right behind the last event. |