Skip to main content

squawk_parser/
shortcuts.rs

1// via https://github.com/rust-lang/rust-analyzer/blob/d8887c0758bbd2d5f752d5bd405d4491e90e7ed6/crates/parser/src/shortcuts.rs
2//
3// Permission is hereby granted, free of charge, to any
4// person obtaining a copy of this software and associated
5// documentation files (the "Software"), to deal in the
6// Software without restriction, including without
7// limitation the rights to use, copy, modify, merge,
8// publish, distribute, sublicense, and/or sell copies of
9// the Software, and to permit persons to whom the Software
10// is furnished to do so, subject to the following
11// conditions:
12//
13// The above copyright notice and this permission notice
14// shall be included in all copies or substantial portions
15// of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25// DEALINGS IN THE SOFTWARE.
26
27use std::mem;
28
29use crate::{
30    lexed_str::LexedStr,
31    output::{Output, Step},
32    syntax_kind::SyntaxKind,
33};
34
35#[derive(Debug)]
36pub enum StrStep<'a> {
37    Token { kind: SyntaxKind, text: &'a str },
38    Enter { kind: SyntaxKind },
39    Exit,
40    Error { msg: &'a str, pos: usize },
41}
42
43enum State {
44    PendingEnter,
45    Normal,
46    PendingExit,
47}
48
49struct Builder<'a, 'b> {
50    lexed: &'a LexedStr<'a>,
51    pos: usize,
52    state: State,
53    sink: &'b mut dyn FnMut(StrStep<'_>),
54}
55
56impl Builder<'_, '_> {
57    fn token(&mut self, kind: SyntaxKind, n_tokens: u8) {
58        match mem::replace(&mut self.state, State::Normal) {
59            State::PendingEnter => unreachable!(),
60            State::PendingExit => (self.sink)(StrStep::Exit),
61            State::Normal => (),
62        }
63        self.eat_trivias();
64        self.do_token(kind, n_tokens as usize);
65    }
66
67    fn enter(&mut self, kind: SyntaxKind) {
68        match mem::replace(&mut self.state, State::Normal) {
69            State::PendingEnter => {
70                (self.sink)(StrStep::Enter { kind });
71                // No need to attach trivias to previous node: there is no
72                // previous node.
73                return;
74            }
75            State::PendingExit => (self.sink)(StrStep::Exit),
76            State::Normal => (),
77        }
78
79        self.eat_trivias();
80        (self.sink)(StrStep::Enter { kind });
81    }
82
83    fn exit(&mut self) {
84        match mem::replace(&mut self.state, State::PendingExit) {
85            State::PendingEnter => unreachable!(),
86            State::PendingExit => (self.sink)(StrStep::Exit),
87            State::Normal => (),
88        }
89    }
90
91    fn eat_trivias(&mut self) {
92        while self.pos < self.lexed.len() {
93            let kind = self.lexed.kind(self.pos);
94            if !kind.is_trivia() {
95                break;
96            }
97            self.do_token(kind, 1);
98        }
99    }
100
101    fn do_token(&mut self, kind: SyntaxKind, n_tokens: usize) {
102        let text = &self.lexed.range_text(self.pos..self.pos + n_tokens);
103        self.pos += n_tokens;
104        (self.sink)(StrStep::Token { kind, text });
105    }
106}
107
108impl LexedStr<'_> {
109    pub fn to_input(&self) -> crate::Input {
110        let mut res = crate::Input::default();
111        let mut was_joint = false;
112        for i in 0..self.len() {
113            let kind = self.kind(i);
114            if kind.is_trivia() {
115                was_joint = false;
116                // skip over any triva since the parser shouldn't have to deal
117                // with it
118            }
119            // else if kind == SyntaxKind::IDENT {
120            //     let token_text = self.text(i);
121            //     let contextual_kw =
122            //         SyntaxKind::from_contextual_keyword(token_text).unwrap_or(SyntaxKind::IDENT);
123            //     res.push_ident(contextual_kw);
124            // }
125            else {
126                if was_joint {
127                    res.was_joint();
128                }
129                res.push(kind);
130                was_joint = true;
131            }
132        }
133        res
134    }
135
136    /// NB: only valid to call with Output from Reparser/TopLevelEntry.
137    pub fn intersperse_trivia(&self, output: &Output, sink: &mut dyn FnMut(StrStep<'_>)) -> bool {
138        let mut builder = Builder {
139            lexed: self,
140            pos: 0,
141            state: State::PendingEnter,
142            sink,
143        };
144
145        for event in output.iter() {
146            match event {
147                Step::Token {
148                    kind,
149                    n_input_tokens: n_raw_tokens,
150                } => builder.token(kind, n_raw_tokens),
151                Step::Enter { kind } => builder.enter(kind),
152                Step::Exit => builder.exit(),
153                Step::Error { msg } => {
154                    let text_pos = builder.lexed.text_start(builder.pos);
155                    (builder.sink)(StrStep::Error { msg, pos: text_pos });
156                }
157            }
158        }
159
160        match mem::replace(&mut builder.state, State::Normal) {
161            State::PendingExit => {
162                builder.eat_trivias();
163                (builder.sink)(StrStep::Exit);
164            }
165            State::PendingEnter | State::Normal => unreachable!(),
166        }
167
168        // is_eof?
169        builder.pos == builder.lexed.len()
170    }
171}