|
| 1 | +//! Shortcuts that span lexer/parser abstraction. |
| 2 | +//! |
| 3 | +//! The way Rust works, parser doesn't necessary parse text, and you might |
| 4 | +//! tokenize text without parsing it further. So, it makes sense to keep |
| 5 | +//! abstract token parsing, and string tokenization as completely separate |
| 6 | +//! layers. |
| 7 | +//! |
| 8 | +//! However, often you do pares text into syntax trees and the glue code for |
| 9 | +//! that needs to live somewhere. Rather than putting it to lexer or parser, we |
| 10 | +//! use a separate shortcuts module for that. |
| 11 | +
|
| 12 | +use std::mem; |
| 13 | + |
| 14 | +use crate::{ |
| 15 | + LexedStr, Step, |
| 16 | + SyntaxKind::{self, *}, |
| 17 | +}; |
| 18 | + |
| 19 | +pub enum StrStep<'a> { |
| 20 | + Token { kind: SyntaxKind, text: &'a str }, |
| 21 | + Enter { kind: SyntaxKind }, |
| 22 | + Exit, |
| 23 | + Error { msg: &'a str, pos: usize }, |
| 24 | +} |
| 25 | + |
| 26 | +impl<'a> LexedStr<'a> { |
| 27 | + pub fn to_input(&self) -> crate::Input { |
| 28 | + let mut res = crate::Input::default(); |
| 29 | + let mut was_joint = false; |
| 30 | + for i in 0..self.len() { |
| 31 | + let kind = self.kind(i); |
| 32 | + if kind.is_trivia() { |
| 33 | + was_joint = false |
| 34 | + } else { |
| 35 | + if kind == SyntaxKind::IDENT { |
| 36 | + let token_text = self.text(i); |
| 37 | + let contextual_kw = SyntaxKind::from_contextual_keyword(token_text) |
| 38 | + .unwrap_or(SyntaxKind::IDENT); |
| 39 | + res.push_ident(contextual_kw); |
| 40 | + } else { |
| 41 | + if was_joint { |
| 42 | + res.was_joint(); |
| 43 | + } |
| 44 | + res.push(kind); |
| 45 | + } |
| 46 | + was_joint = true; |
| 47 | + } |
| 48 | + } |
| 49 | + res |
| 50 | + } |
| 51 | + |
| 52 | + pub fn intersperse_trivia( |
| 53 | + &self, |
| 54 | + output: &crate::Output, |
| 55 | + synthetic_root: bool, |
| 56 | + sink: &mut dyn FnMut(StrStep), |
| 57 | + ) -> bool { |
| 58 | + let mut builder = Builder { lexed: self, pos: 0, state: State::PendingEnter, sink }; |
| 59 | + |
| 60 | + if synthetic_root { |
| 61 | + builder.enter(SyntaxKind::SOURCE_FILE); |
| 62 | + } |
| 63 | + for event in output.iter() { |
| 64 | + match event { |
| 65 | + Step::Token { kind, n_input_tokens: n_raw_tokens } => { |
| 66 | + builder.token(kind, n_raw_tokens) |
| 67 | + } |
| 68 | + Step::Enter { kind } => builder.enter(kind), |
| 69 | + Step::Exit => builder.exit(), |
| 70 | + Step::Error { msg } => { |
| 71 | + let text_pos = builder.lexed.text_start(builder.pos); |
| 72 | + (builder.sink)(StrStep::Error { msg, pos: text_pos }); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + if synthetic_root { |
| 77 | + builder.exit(); |
| 78 | + } |
| 79 | + |
| 80 | + match mem::replace(&mut builder.state, State::Normal) { |
| 81 | + State::PendingExit => { |
| 82 | + builder.eat_trivias(); |
| 83 | + (builder.sink)(StrStep::Exit); |
| 84 | + } |
| 85 | + State::PendingEnter | State::Normal => unreachable!(), |
| 86 | + } |
| 87 | + |
| 88 | + let is_eof = builder.pos == builder.lexed.len(); |
| 89 | + is_eof |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +struct Builder<'a, 'b> { |
| 94 | + lexed: &'a LexedStr<'a>, |
| 95 | + pos: usize, |
| 96 | + state: State, |
| 97 | + sink: &'b mut dyn FnMut(StrStep<'_>), |
| 98 | +} |
| 99 | + |
| 100 | +enum State { |
| 101 | + PendingEnter, |
| 102 | + Normal, |
| 103 | + PendingExit, |
| 104 | +} |
| 105 | + |
| 106 | +impl Builder<'_, '_> { |
| 107 | + fn token(&mut self, kind: SyntaxKind, n_tokens: u8) { |
| 108 | + match mem::replace(&mut self.state, State::Normal) { |
| 109 | + State::PendingEnter => unreachable!(), |
| 110 | + State::PendingExit => (self.sink)(StrStep::Exit), |
| 111 | + State::Normal => (), |
| 112 | + } |
| 113 | + self.eat_trivias(); |
| 114 | + self.do_token(kind, n_tokens as usize); |
| 115 | + } |
| 116 | + |
| 117 | + fn enter(&mut self, kind: SyntaxKind) { |
| 118 | + match mem::replace(&mut self.state, State::Normal) { |
| 119 | + State::PendingEnter => { |
| 120 | + (self.sink)(StrStep::Enter { kind }); |
| 121 | + // No need to attach trivias to previous node: there is no |
| 122 | + // previous node. |
| 123 | + return; |
| 124 | + } |
| 125 | + State::PendingExit => (self.sink)(StrStep::Exit), |
| 126 | + State::Normal => (), |
| 127 | + } |
| 128 | + |
| 129 | + let n_trivias = |
| 130 | + (self.pos..self.lexed.len()).take_while(|&it| self.lexed.kind(it).is_trivia()).count(); |
| 131 | + let leading_trivias = self.pos..self.pos + n_trivias; |
| 132 | + let n_attached_trivias = n_attached_trivias( |
| 133 | + kind, |
| 134 | + leading_trivias.rev().map(|it| (self.lexed.kind(it), self.lexed.text(it))), |
| 135 | + ); |
| 136 | + self.eat_n_trivias(n_trivias - n_attached_trivias); |
| 137 | + (self.sink)(StrStep::Enter { kind }); |
| 138 | + self.eat_n_trivias(n_attached_trivias); |
| 139 | + } |
| 140 | + |
| 141 | + fn exit(&mut self) { |
| 142 | + match mem::replace(&mut self.state, State::PendingExit) { |
| 143 | + State::PendingEnter => unreachable!(), |
| 144 | + State::PendingExit => (self.sink)(StrStep::Exit), |
| 145 | + State::Normal => (), |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + fn eat_trivias(&mut self) { |
| 150 | + while self.pos < self.lexed.len() { |
| 151 | + let kind = self.lexed.kind(self.pos); |
| 152 | + if !kind.is_trivia() { |
| 153 | + break; |
| 154 | + } |
| 155 | + self.do_token(kind, 1); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + fn eat_n_trivias(&mut self, n: usize) { |
| 160 | + for _ in 0..n { |
| 161 | + let kind = self.lexed.kind(self.pos); |
| 162 | + assert!(kind.is_trivia()); |
| 163 | + self.do_token(kind, 1); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + fn do_token(&mut self, kind: SyntaxKind, n_tokens: usize) { |
| 168 | + let text = &self.lexed.range_text(self.pos..self.pos + n_tokens); |
| 169 | + self.pos += n_tokens; |
| 170 | + (self.sink)(StrStep::Token { kind, text }); |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +fn n_attached_trivias<'a>( |
| 175 | + kind: SyntaxKind, |
| 176 | + trivias: impl Iterator<Item = (SyntaxKind, &'a str)>, |
| 177 | +) -> usize { |
| 178 | + match kind { |
| 179 | + CONST | ENUM | FN | IMPL | MACRO_CALL | MACRO_DEF | MACRO_RULES | MODULE | RECORD_FIELD |
| 180 | + | STATIC | STRUCT | TRAIT | TUPLE_FIELD | TYPE_ALIAS | UNION | USE | VARIANT => { |
| 181 | + let mut res = 0; |
| 182 | + let mut trivias = trivias.enumerate().peekable(); |
| 183 | + |
| 184 | + while let Some((i, (kind, text))) = trivias.next() { |
| 185 | + match kind { |
| 186 | + WHITESPACE if text.contains("\n\n") => { |
| 187 | + // we check whether the next token is a doc-comment |
| 188 | + // and skip the whitespace in this case |
| 189 | + if let Some((COMMENT, peek_text)) = trivias.peek().map(|(_, pair)| pair) { |
| 190 | + if is_outer(peek_text) { |
| 191 | + continue; |
| 192 | + } |
| 193 | + } |
| 194 | + break; |
| 195 | + } |
| 196 | + COMMENT => { |
| 197 | + if is_inner(text) { |
| 198 | + break; |
| 199 | + } |
| 200 | + res = i + 1; |
| 201 | + } |
| 202 | + _ => (), |
| 203 | + } |
| 204 | + } |
| 205 | + res |
| 206 | + } |
| 207 | + _ => 0, |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +fn is_outer(text: &str) -> bool { |
| 212 | + if text.starts_with("////") || text.starts_with("/***") { |
| 213 | + return false; |
| 214 | + } |
| 215 | + text.starts_with("///") || text.starts_with("/**") |
| 216 | +} |
| 217 | + |
| 218 | +fn is_inner(text: &str) -> bool { |
| 219 | + text.starts_with("//!") || text.starts_with("/*!") |
| 220 | +} |
0 commit comments