aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parsing/parser_impl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/parsing/parser_impl.rs')
-rw-r--r--crates/ra_syntax/src/parsing/parser_impl.rs50
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 @@
1mod event; 1mod event;
2mod input; 2pub(crate) mod input;
3 3
4use std::cell::Cell; 4use 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
42pub(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
43pub(super) fn parse_with<S: TreeSink>( 49pub(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`.
62pub(super) struct ParserImpl<'t> { 68pub(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
69impl<'t> ParserImpl<'t> { 75impl<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.