aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ssr/src/parsing.rs
blob: 1ae166d196cf70e0fbc3258faa211f2e478dc099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! This file contains code for parsing SSR rules, which look something like `foo($a) ==>> bar($b)`.
//! We first split everything before and after the separator `==>>`. Next, both the search pattern
//! and the replacement template get tokenized by the Rust tokenizer. Tokens are then searched for
//! placeholders, which start with `$`. For replacement templates, this is the final form. For
//! search patterns, we go further and parse the pattern as each kind of thing that we can match.
//! e.g. expressions, type references etc.

use crate::{SsrError, SsrPattern, SsrRule};
use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind};
use rustc_hash::{FxHashMap, FxHashSet};
use std::str::FromStr;

/// Returns from the current function with an error, supplied by arguments as for format!
macro_rules! bail {
    ($e:expr) => {return Err($crate::SsrError::new($e))};
    ($fmt:expr, $($arg:tt)+) => {return Err($crate::SsrError::new(format!($fmt, $($arg)+)))}
}

#[derive(Clone, Debug)]
pub(crate) struct SsrTemplate {
    pub(crate) tokens: Vec<PatternElement>,
}

#[derive(Debug)]
pub(crate) struct RawSearchPattern {
    tokens: Vec<PatternElement>,
}

// Part of a search or replace pattern.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum PatternElement {
    Token(Token),
    Placeholder(Placeholder),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Placeholder {
    /// The name of this placeholder. e.g. for "$a", this would be "a"
    pub(crate) ident: SmolStr,
    /// A unique name used in place of this placeholder when we parse the pattern as Rust code.
    stand_in_name: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Token {
    kind: SyntaxKind,
    pub(crate) text: SmolStr,
}

impl FromStr for SsrRule {
    type Err = SsrError;

    fn from_str(query: &str) -> Result<SsrRule, SsrError> {
        let mut it = query.split("==>>");
        let pattern = it.next().expect("at least empty string").trim();
        let template = it
            .next()
            .ok_or_else(|| SsrError("Cannot find delemiter `==>>`".into()))?
            .trim()
            .to_string();
        if it.next().is_some() {
            return Err(SsrError("More than one delimiter found".into()));
        }
        let rule = SsrRule { pattern: pattern.parse()?, template: template.parse()? };
        validate_rule(&rule)?;
        Ok(rule)
    }
}

impl FromStr for RawSearchPattern {
    type Err = SsrError;

    fn from_str(pattern_str: &str) -> Result<RawSearchPattern, SsrError> {
        Ok(RawSearchPattern { tokens: parse_pattern(pattern_str)? })
    }
}

impl RawSearchPattern {
    /// Returns this search pattern as Rust source code that we can feed to the Rust parser.
    fn as_rust_code(&self) -> String {
        let mut res = String::new();
        for t in &self.tokens {
            res.push_str(match t {
                PatternElement::Token(token) => token.text.as_str(),
                PatternElement::Placeholder(placeholder) => placeholder.stand_in_name.as_str(),
            });
        }
        res
    }

    fn placeholders_by_stand_in(&self) -> FxHashMap<SmolStr, Placeholder> {
        let mut res = FxHashMap::default();
        for t in &self.tokens {
            if let PatternElement::Placeholder(placeholder) = t {
                res.insert(SmolStr::new(placeholder.stand_in_name.clone()), placeholder.clone());
            }
        }
        res
    }
}

impl FromStr for SsrPattern {
    type Err = SsrError;

    fn from_str(pattern_str: &str) -> Result<SsrPattern, SsrError> {
        let raw: RawSearchPattern = pattern_str.parse()?;
        let raw_str = raw.as_rust_code();
        let res = SsrPattern {
            expr: ast::Expr::parse(&raw_str).ok().map(|n| n.syntax().clone()),
            type_ref: ast::TypeRef::parse(&raw_str).ok().map(|n| n.syntax().clone()),
            item: ast::ModuleItem::parse(&raw_str).ok().map(|n| n.syntax().clone()),
            path: ast::Path::parse(&raw_str).ok().map(|n| n.syntax().clone()),
            pattern: ast::Pat::parse(&raw_str).ok().map(|n| n.syntax().clone()),
            placeholders_by_stand_in: raw.placeholders_by_stand_in(),
            raw,
        };
        if res.expr.is_none()
            && res.type_ref.is_none()
            && res.item.is_none()
            && res.path.is_none()
            && res.pattern.is_none()
        {
            bail!("Pattern is not a valid Rust expression, type, item, path or pattern");
        }
        Ok(res)
    }
}

impl FromStr for SsrTemplate {
    type Err = SsrError;

    fn from_str(pattern_str: &str) -> Result<SsrTemplate, SsrError> {
        let tokens = parse_pattern(pattern_str)?;
        // Validate that the template is a valid fragment of Rust code. We reuse the validation
        // logic for search patterns since the only thing that differs is the error message.
        if SsrPattern::from_str(pattern_str).is_err() {
            bail!("Replacement is not a valid Rust expression, type, item, path or pattern");
        }
        // Our actual template needs to preserve whitespace, so we can't reuse `tokens`.
        Ok(SsrTemplate { tokens })
    }
}

/// Returns `pattern_str`, parsed as a search or replace pattern. If `remove_whitespace` is true,
/// then any whitespace tokens will be removed, which we do for the search pattern, but not for the
/// replace pattern.
fn parse_pattern(pattern_str: &str) -> Result<Vec<PatternElement>, SsrError> {
    let mut res = Vec::new();
    let mut placeholder_names = FxHashSet::default();
    let mut tokens = tokenize(pattern_str)?.into_iter();
    while let Some(token) = tokens.next() {
        if token.kind == SyntaxKind::DOLLAR {
            let placeholder = parse_placeholder(&mut tokens)?;
            if !placeholder_names.insert(placeholder.ident.clone()) {
                bail!("Name `{}` repeats more than once", placeholder.ident);
            }
            res.push(PatternElement::Placeholder(placeholder));
        } else {
            res.push(PatternElement::Token(token));
        }
    }
    Ok(res)
}

/// Checks for errors in a rule. e.g. the replace pattern referencing placeholders that the search
/// pattern didn't define.
fn validate_rule(rule: &SsrRule) -> Result<(), SsrError> {
    let mut defined_placeholders = FxHashSet::default();
    for p in &rule.pattern.raw.tokens {
        if let PatternElement::Placeholder(placeholder) = p {
            defined_placeholders.insert(&placeholder.ident);
        }
    }
    let mut undefined = Vec::new();
    for p in &rule.template.tokens {
        if let PatternElement::Placeholder(placeholder) = p {
            if !defined_placeholders.contains(&placeholder.ident) {
                undefined.push(format!("${}", placeholder.ident));
            }
        }
    }
    if !undefined.is_empty() {
        bail!("Replacement contains undefined placeholders: {}", undefined.join(", "));
    }
    Ok(())
}

fn tokenize(source: &str) -> Result<Vec<Token>, SsrError> {
    let mut start = 0;
    let (raw_tokens, errors) = ra_syntax::tokenize(source);
    if let Some(first_error) = errors.first() {
        bail!("Failed to parse pattern: {}", first_error);
    }
    let mut tokens: Vec<Token> = Vec::new();
    for raw_token in raw_tokens {
        let token_len = usize::from(raw_token.len);
        tokens.push(Token {
            kind: raw_token.kind,
            text: SmolStr::new(&source[start..start + token_len]),
        });
        start += token_len;
    }
    Ok(tokens)
}

fn parse_placeholder(tokens: &mut std::vec::IntoIter<Token>) -> Result<Placeholder, SsrError> {
    let mut name = None;
    if let Some(token) = tokens.next() {
        match token.kind {
            SyntaxKind::IDENT => {
                name = Some(token.text);
            }
            _ => {
                bail!("Placeholders should be $name");
            }
        }
    }
    let name = name.ok_or_else(|| SsrError::new("Placeholder ($) with no name"))?;
    Ok(Placeholder::new(name))
}

impl Placeholder {
    fn new(name: SmolStr) -> Self {
        Self { stand_in_name: format!("__placeholder_{}", name), ident: name }
    }
}

impl SsrError {
    fn new(message: impl Into<String>) -> SsrError {
        SsrError(message.into())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parser_happy_case() {
        fn token(kind: SyntaxKind, text: &str) -> PatternElement {
            PatternElement::Token(Token { kind, text: SmolStr::new(text) })
        }
        fn placeholder(name: &str) -> PatternElement {
            PatternElement::Placeholder(Placeholder::new(SmolStr::new(name)))
        }
        let result: SsrRule = "foo($a, $b) ==>> bar($b, $a)".parse().unwrap();
        assert_eq!(
            result.pattern.raw.tokens,
            vec![
                token(SyntaxKind::IDENT, "foo"),
                token(SyntaxKind::L_PAREN, "("),
                placeholder("a"),
                token(SyntaxKind::COMMA, ","),
                token(SyntaxKind::WHITESPACE, " "),
                placeholder("b"),
                token(SyntaxKind::R_PAREN, ")"),
            ]
        );
        assert_eq!(
            result.template.tokens,
            vec![
                token(SyntaxKind::IDENT, "bar"),
                token(SyntaxKind::L_PAREN, "("),
                placeholder("b"),
                token(SyntaxKind::COMMA, ","),
                token(SyntaxKind::WHITESPACE, " "),
                placeholder("a"),
                token(SyntaxKind::R_PAREN, ")"),
            ]
        );
    }
}