aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parser_impl
diff options
context:
space:
mode:
authorJeremy A. Kolb <[email protected]>2018-10-15 22:44:23 +0100
committerJeremy A. Kolb <[email protected]>2018-10-16 14:41:10 +0100
commit61f3a438d3a729a6be941bca1ff4c6a97a33f221 (patch)
tree6551967cc8c6e921b66071453ad7888a9121d326 /crates/ra_syntax/src/parser_impl
parent39cb6c6d3f78b193f5873c3492e530bbd24d5dd2 (diff)
Cargo Format
Run `cargo fmt` and ignore generated files
Diffstat (limited to 'crates/ra_syntax/src/parser_impl')
-rw-r--r--crates/ra_syntax/src/parser_impl/event.rs54
-rw-r--r--crates/ra_syntax/src/parser_impl/mod.rs13
2 files changed, 38 insertions, 29 deletions
diff --git a/crates/ra_syntax/src/parser_impl/event.rs b/crates/ra_syntax/src/parser_impl/event.rs
index 928d2cc7a..79fa21389 100644
--- a/crates/ra_syntax/src/parser_impl/event.rs
+++ b/crates/ra_syntax/src/parser_impl/event.rs
@@ -7,14 +7,14 @@
7//! tree builder: the parser produces a stream of events like 7//! tree builder: the parser produces a stream of events like
8//! `start node`, `finish node`, and `FileBuilder` converts 8//! `start node`, `finish node`, and `FileBuilder` converts
9//! this stream to a real tree. 9//! this stream to a real tree.
10use std::mem;
11use crate::{ 10use crate::{
12 TextUnit, TextRange, SmolStr,
13 lexer::Token, 11 lexer::Token,
14 parser_impl::Sink, 12 parser_impl::Sink,
13 SmolStr,
15 SyntaxKind::{self, *}, 14 SyntaxKind::{self, *},
15 TextRange, TextUnit,
16}; 16};
17 17use std::mem;
18 18
19/// `Parser` produces a flat list of `Event`s. 19/// `Parser` produces a flat list of `Event`s.
20/// They are converted to a tree-structure in 20/// They are converted to a tree-structure in
@@ -89,20 +89,28 @@ pub(super) struct EventProcessor<'a, S: Sink> {
89} 89}
90 90
91impl<'a, S: Sink> EventProcessor<'a, S> { 91impl<'a, S: Sink> EventProcessor<'a, S> {
92 pub(super) fn new(sink: S, text: &'a str, tokens: &'a[Token], events: &'a mut [Event]) -> EventProcessor<'a, S> { 92 pub(super) fn new(
93 sink: S,
94 text: &'a str,
95 tokens: &'a [Token],
96 events: &'a mut [Event],
97 ) -> EventProcessor<'a, S> {
93 EventProcessor { 98 EventProcessor {
94 sink, 99 sink,
95 text_pos: 0.into(), 100 text_pos: 0.into(),
96 text, 101 text,
97 token_pos: 0, 102 token_pos: 0,
98 tokens, 103 tokens,
99 events 104 events,
100 } 105 }
101 } 106 }
102 107
103 pub(super) fn process(mut self) -> S { 108 pub(super) fn process(mut self) -> S {
104 fn tombstone() -> Event { 109 fn tombstone() -> Event {
105 Event::Start { kind: TOMBSTONE, forward_parent: None } 110 Event::Start {
111 kind: TOMBSTONE,
112 forward_parent: None,
113 }
106 } 114 }
107 let mut forward_parents = Vec::new(); 115 let mut forward_parents = Vec::new();
108 116
@@ -112,7 +120,10 @@ impl<'a, S: Sink> EventProcessor<'a, S> {
112 kind: TOMBSTONE, .. 120 kind: TOMBSTONE, ..
113 } => (), 121 } => (),
114 122
115 Event::Start { kind, forward_parent } => { 123 Event::Start {
124 kind,
125 forward_parent,
126 } => {
116 forward_parents.push(kind); 127 forward_parents.push(kind);
117 let mut idx = i; 128 let mut idx = i;
118 let mut fp = forward_parent; 129 let mut fp = forward_parent;
@@ -125,7 +136,7 @@ impl<'a, S: Sink> EventProcessor<'a, S> {
125 } => { 136 } => {
126 forward_parents.push(kind); 137 forward_parents.push(kind);
127 forward_parent 138 forward_parent
128 }, 139 }
129 _ => unreachable!(), 140 _ => unreachable!(),
130 }; 141 };
131 } 142 }
@@ -136,7 +147,7 @@ impl<'a, S: Sink> EventProcessor<'a, S> {
136 Event::Finish => { 147 Event::Finish => {
137 let last = i == self.events.len() - 1; 148 let last = i == self.events.len() - 1;
138 self.finish(last); 149 self.finish(last);
139 }, 150 }
140 Event::Token { kind, n_raw_tokens } => { 151 Event::Token { kind, n_raw_tokens } => {
141 self.eat_ws(); 152 self.eat_ws();
142 let n_raw_tokens = n_raw_tokens as usize; 153 let n_raw_tokens = n_raw_tokens as usize;
@@ -162,19 +173,16 @@ impl<'a, S: Sink> EventProcessor<'a, S> {
162 .take_while(|it| it.kind.is_trivia()) 173 .take_while(|it| it.kind.is_trivia())
163 .count(); 174 .count();
164 let leading_trivias = &self.tokens[self.token_pos..self.token_pos + n_trivias]; 175 let leading_trivias = &self.tokens[self.token_pos..self.token_pos + n_trivias];
165 let mut trivia_end = self.text_pos + leading_trivias 176 let mut trivia_end =
166 .iter() 177 self.text_pos + leading_trivias.iter().map(|it| it.len).sum::<TextUnit>();
167 .map(|it| it.len)
168 .sum::<TextUnit>();
169 178
170 let n_attached_trivias = { 179 let n_attached_trivias = {
171 let leading_trivias = leading_trivias.iter().rev() 180 let leading_trivias = leading_trivias.iter().rev().map(|it| {
172 .map(|it| { 181 let next_end = trivia_end - it.len;
173 let next_end = trivia_end - it.len; 182 let range = TextRange::from_to(next_end, trivia_end);
174 let range = TextRange::from_to(next_end, trivia_end); 183 trivia_end = next_end;
175 trivia_end = next_end; 184 (it.kind, &self.text[range])
176 (it.kind, &self.text[range]) 185 });
177 });
178 n_attached_trivias(kind, leading_trivias) 186 n_attached_trivias(kind, leading_trivias)
179 }; 187 };
180 self.eat_n_trivias(n_trivias - n_attached_trivias); 188 self.eat_n_trivias(n_trivias - n_attached_trivias);
@@ -215,7 +223,10 @@ impl<'a, S: Sink> EventProcessor<'a, S> {
215 } 223 }
216} 224}
217 225
218fn n_attached_trivias<'a>(kind: SyntaxKind, trivias: impl Iterator<Item=(SyntaxKind, &'a str)>) -> usize { 226fn n_attached_trivias<'a>(
227 kind: SyntaxKind,
228 trivias: impl Iterator<Item = (SyntaxKind, &'a str)>,
229) -> usize {
219 match kind { 230 match kind {
220 STRUCT_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | MODULE => { 231 STRUCT_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | MODULE => {
221 let mut res = 0; 232 let mut res = 0;
@@ -236,5 +247,4 @@ fn n_attached_trivias<'a>(kind: SyntaxKind, trivias: impl Iterator<Item=(SyntaxK
236 } 247 }
237 _ => 0, 248 _ => 0,
238 } 249 }
239
240} 250}
diff --git a/crates/ra_syntax/src/parser_impl/mod.rs b/crates/ra_syntax/src/parser_impl/mod.rs
index c2a6448e7..2b026d61e 100644
--- a/crates/ra_syntax/src/parser_impl/mod.rs
+++ b/crates/ra_syntax/src/parser_impl/mod.rs
@@ -4,13 +4,13 @@ mod input;
4use std::cell::Cell; 4use std::cell::Cell;
5 5
6use crate::{ 6use crate::{
7 TextUnit, SmolStr,
8 lexer::Token, 7 lexer::Token,
9 parser_api::Parser, 8 parser_api::Parser,
10 parser_impl::{ 9 parser_impl::{
11 event::{EventProcessor, Event}, 10 event::{Event, EventProcessor},
12 input::{InputPosition, ParserInput}, 11 input::{InputPosition, ParserInput},
13 }, 12 },
13 SmolStr, TextUnit,
14}; 14};
15 15
16use crate::SyntaxKind::{self, EOF, TOMBSTONE}; 16use crate::SyntaxKind::{self, EOF, TOMBSTONE};
@@ -86,7 +86,9 @@ impl<'t> ParserImpl<'t> {
86 let c2 = self.inp.kind(self.pos + 1); 86 let c2 = self.inp.kind(self.pos + 1);
87 let c3 = self.inp.kind(self.pos + 2); 87 let c3 = self.inp.kind(self.pos + 2);
88 if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) 88 if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos)
89 && self.inp.start(self.pos + 2) == self.inp.start(self.pos + 1) + self.inp.len(self.pos + 1){ 89 && self.inp.start(self.pos + 2)
90 == self.inp.start(self.pos + 1) + self.inp.len(self.pos + 1)
91 {
90 Some((c1, c2, c3)) 92 Some((c1, c2, c3))
91 } else { 93 } else {
92 None 94 None
@@ -138,10 +140,7 @@ impl<'t> ParserImpl<'t> {
138 140
139 fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) { 141 fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) {
140 self.pos += u32::from(n_raw_tokens); 142 self.pos += u32::from(n_raw_tokens);
141 self.event(Event::Token { 143 self.event(Event::Token { kind, n_raw_tokens });
142 kind,
143 n_raw_tokens,
144 });
145 } 144 }
146 145
147 pub(super) fn error(&mut self, msg: String) { 146 pub(super) fn error(&mut self, msg: String) {