Skip to content

Commit 3d99978

Browse files
SSR: Add initial support for placeholder constraints
1 parent d34fd37 commit 3d99978

File tree

6 files changed

+156
-6
lines changed

6 files changed

+156
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_ide/src/ssr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ use ra_ssr::{MatchFinder, SsrError, SsrRule};
1010
// The syntax for a structural search replace command is `<search_pattern> ==>> <replace_pattern>`.
1111
// A `$<name>` placeholder in the search pattern will match any AST node and `$<name>` will reference it in the replacement.
1212
// Within a macro call, a placeholder will match up until whatever token follows the placeholder.
13+
//
14+
// Placeholders may be given constraints by writing them as `${<name>:<constraint1>:<constraint2>...}`.
15+
//
16+
// Supported constraints:
17+
//
18+
// |===
19+
// | Constraint | Restricts placeholder
20+
//
21+
// | kind(literal) | Is a literal (e.g. `42` or `"forty two"`)
22+
// | not(a) | Negates the constraint `a`
23+
// |===
24+
//
1325
// Available via the command `rust-analyzer.ssr`.
1426
//
1527
// ```rust

crates/ra_ssr/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ ra_db = { path = "../ra_db" }
1717
ra_ide_db = { path = "../ra_ide_db" }
1818
hir = { path = "../ra_hir", package = "ra_hir" }
1919
rustc-hash = "1.1.0"
20+
test_utils = { path = "../test_utils" }

crates/ra_ssr/src/matching.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! process of matching, placeholder values are recorded.
33
44
use crate::{
5-
parsing::{Placeholder, SsrTemplate},
5+
parsing::{Constraint, NodeKind, Placeholder, SsrTemplate},
66
SsrMatches, SsrPattern, SsrRule,
77
};
88
use hir::Semantics;
@@ -11,6 +11,7 @@ use ra_syntax::ast::{AstNode, AstToken};
1111
use ra_syntax::{ast, SyntaxElement, SyntaxElementChildren, SyntaxKind, SyntaxNode, SyntaxToken};
1212
use rustc_hash::FxHashMap;
1313
use std::{cell::Cell, iter::Peekable};
14+
use test_utils::mark;
1415

1516
// Creates a match error. If we're currently attempting to match some code that we thought we were
1617
// going to match, as indicated by the --debug-snippet flag, then populate the reason field.
@@ -169,6 +170,9 @@ impl<'db, 'sema> MatchState<'db, 'sema> {
169170
if let Some(placeholder) =
170171
match_inputs.get_placeholder(&SyntaxElement::Node(pattern.clone()))
171172
{
173+
for constraint in &placeholder.constraints {
174+
self.check_constraint(constraint, code)?;
175+
}
172176
if self.match_out.is_none() {
173177
return Ok(());
174178
}
@@ -292,6 +296,24 @@ impl<'db, 'sema> MatchState<'db, 'sema> {
292296
Ok(())
293297
}
294298

299+
fn check_constraint(
300+
&self,
301+
constraint: &Constraint,
302+
code: &SyntaxNode,
303+
) -> Result<(), MatchFailed> {
304+
match constraint {
305+
Constraint::Kind(kind) => {
306+
kind.matches(code)?;
307+
}
308+
Constraint::Not(sub) => {
309+
if self.check_constraint(&*sub, code).is_ok() {
310+
fail_match!("Constraint {:?} failed for '{}'", constraint, code.text());
311+
}
312+
}
313+
}
314+
Ok(())
315+
}
316+
295317
/// We want to allow the records to match in any order, so we have special matching logic for
296318
/// them.
297319
fn attempt_match_record_field_list(
@@ -515,6 +537,21 @@ impl SsrPattern {
515537
}
516538
}
517539

540+
impl NodeKind {
541+
fn matches(&self, node: &SyntaxNode) -> Result<(), MatchFailed> {
542+
let ok = match self {
543+
Self::Literal => {
544+
mark::hit!(literal_constraint);
545+
ast::Literal::can_cast(node.kind())
546+
}
547+
};
548+
if !ok {
549+
fail_match!("Code '{}' isn't of kind {:?}", node.text(), self);
550+
}
551+
Ok(())
552+
}
553+
}
554+
518555
// If `node` contains nothing but an ident then return it, otherwise return None.
519556
fn only_ident(element: SyntaxElement) -> Option<SyntaxToken> {
520557
match element {

crates/ra_ssr/src/parsing.rs

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ pub(crate) struct Placeholder {
3939
pub(crate) ident: SmolStr,
4040
/// A unique name used in place of this placeholder when we parse the pattern as Rust code.
4141
stand_in_name: String,
42+
pub(crate) constraints: Vec<Constraint>,
43+
}
44+
45+
#[derive(Clone, Debug, PartialEq, Eq)]
46+
pub(crate) enum Constraint {
47+
Kind(NodeKind),
48+
Not(Box<Constraint>),
49+
}
50+
51+
#[derive(Clone, Debug, PartialEq, Eq)]
52+
pub(crate) enum NodeKind {
53+
Literal,
4254
}
4355

4456
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -177,6 +189,9 @@ fn validate_rule(rule: &SsrRule) -> Result<(), SsrError> {
177189
if !defined_placeholders.contains(&placeholder.ident) {
178190
undefined.push(format!("${}", placeholder.ident));
179191
}
192+
if !placeholder.constraints.is_empty() {
193+
bail!("Replacement placeholders cannot have constraints");
194+
}
180195
}
181196
}
182197
if !undefined.is_empty() {
@@ -205,23 +220,90 @@ fn tokenize(source: &str) -> Result<Vec<Token>, SsrError> {
205220

206221
fn parse_placeholder(tokens: &mut std::vec::IntoIter<Token>) -> Result<Placeholder, SsrError> {
207222
let mut name = None;
223+
let mut constraints = Vec::new();
208224
if let Some(token) = tokens.next() {
209225
match token.kind {
210226
SyntaxKind::IDENT => {
211227
name = Some(token.text);
212228
}
229+
SyntaxKind::L_CURLY => {
230+
let token =
231+
tokens.next().ok_or_else(|| SsrError::new("Unexpected end of placeholder"))?;
232+
if token.kind == SyntaxKind::IDENT {
233+
name = Some(token.text);
234+
}
235+
loop {
236+
let token = tokens
237+
.next()
238+
.ok_or_else(|| SsrError::new("Placeholder is missing closing brace '}'"))?;
239+
match token.kind {
240+
SyntaxKind::COLON => {
241+
constraints.push(parse_constraint(tokens)?);
242+
}
243+
SyntaxKind::R_CURLY => break,
244+
_ => bail!("Unexpected token while parsing placeholder: '{}'", token.text),
245+
}
246+
}
247+
}
213248
_ => {
214-
bail!("Placeholders should be $name");
249+
bail!("Placeholders should either be $name or ${name:constraints}");
215250
}
216251
}
217252
}
218253
let name = name.ok_or_else(|| SsrError::new("Placeholder ($) with no name"))?;
219-
Ok(Placeholder::new(name))
254+
Ok(Placeholder::new(name, constraints))
255+
}
256+
257+
fn parse_constraint(tokens: &mut std::vec::IntoIter<Token>) -> Result<Constraint, SsrError> {
258+
let constraint_type = tokens
259+
.next()
260+
.ok_or_else(|| SsrError::new("Found end of placeholder while looking for a constraint"))?
261+
.text
262+
.to_string();
263+
match constraint_type.as_str() {
264+
"kind" => {
265+
expect_token(tokens, "(")?;
266+
let t = tokens.next().ok_or_else(|| {
267+
SsrError::new("Unexpected end of constraint while looking for kind")
268+
})?;
269+
if t.kind != SyntaxKind::IDENT {
270+
bail!("Expected ident, found {:?} while parsing kind constraint", t.kind);
271+
}
272+
expect_token(tokens, ")")?;
273+
Ok(Constraint::Kind(NodeKind::from(&t.text)?))
274+
}
275+
"not" => {
276+
expect_token(tokens, "(")?;
277+
let sub = parse_constraint(tokens)?;
278+
expect_token(tokens, ")")?;
279+
Ok(Constraint::Not(Box::new(sub)))
280+
}
281+
x => bail!("Unsupported constraint type '{}'", x),
282+
}
283+
}
284+
285+
fn expect_token(tokens: &mut std::vec::IntoIter<Token>, expected: &str) -> Result<(), SsrError> {
286+
if let Some(t) = tokens.next() {
287+
if t.text == expected {
288+
return Ok(());
289+
}
290+
bail!("Expected {} found {}", expected, t.text);
291+
}
292+
bail!("Expected {} found end of stream");
293+
}
294+
295+
impl NodeKind {
296+
fn from(name: &SmolStr) -> Result<NodeKind, SsrError> {
297+
Ok(match name.as_str() {
298+
"literal" => NodeKind::Literal,
299+
_ => bail!("Unknown node kind '{}'", name),
300+
})
301+
}
220302
}
221303

222304
impl Placeholder {
223-
fn new(name: SmolStr) -> Self {
224-
Self { stand_in_name: format!("__placeholder_{}", name), ident: name }
305+
fn new(name: SmolStr, constraints: Vec<Constraint>) -> Self {
306+
Self { stand_in_name: format!("__placeholder_{}", name), constraints, ident: name }
225307
}
226308
}
227309

@@ -241,7 +323,7 @@ mod tests {
241323
PatternElement::Token(Token { kind, text: SmolStr::new(text) })
242324
}
243325
fn placeholder(name: &str) -> PatternElement {
244-
PatternElement::Placeholder(Placeholder::new(SmolStr::new(name)))
326+
PatternElement::Placeholder(Placeholder::new(SmolStr::new(name), Vec::new()))
245327
}
246328
let result: SsrRule = "foo($a, $b) ==>> bar($b, $a)".parse().unwrap();
247329
assert_eq!(

crates/ra_ssr/src/tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{MatchFinder, SsrRule};
22
use ra_db::{FileId, SourceDatabaseExt};
3+
use test_utils::mark;
34

45
fn parse_error_text(query: &str) -> String {
56
format!("{}", query.parse::<SsrRule>().unwrap_err())
@@ -301,6 +302,22 @@ fn match_pattern() {
301302
assert_matches("Some($a)", "fn f() {if let Some(x) = foo() {}}", &["Some(x)"]);
302303
}
303304

305+
#[test]
306+
fn literal_constraint() {
307+
mark::check!(literal_constraint);
308+
let code = r#"
309+
fn f1() {
310+
let x1 = Some(42);
311+
let x2 = Some("foo");
312+
let x3 = Some(x1);
313+
let x4 = Some(40 + 2);
314+
let x5 = Some(true);
315+
}
316+
"#;
317+
assert_matches("Some(${a:kind(literal)})", code, &["Some(42)", "Some(\"foo\")", "Some(true)"]);
318+
assert_matches("Some(${a:not(kind(literal))})", code, &["Some(x1)", "Some(40 + 2)"]);
319+
}
320+
304321
#[test]
305322
fn match_reordered_struct_instantiation() {
306323
assert_matches(

0 commit comments

Comments
 (0)