Unnamed repository; edit this file 'description' to name the repository.
move the rest of ssr parsing to fragments
Aleksey Kladov 2021-12-28
parent 7e9c74d · commit dacbc6a
-rw-r--r--crates/ide_ssr/src/fragments.rs14
-rw-r--r--crates/ide_ssr/src/parsing.rs40
2 files changed, 24 insertions, 30 deletions
diff --git a/crates/ide_ssr/src/fragments.rs b/crates/ide_ssr/src/fragments.rs
index 4c768a970b..dab214b407 100644
--- a/crates/ide_ssr/src/fragments.rs
+++ b/crates/ide_ssr/src/fragments.rs
@@ -36,6 +36,20 @@ pub(crate) fn item(s: &str) -> Result<SyntaxNode, ()> {
Ok(node.syntax().clone_subtree())
}
+pub(crate) fn pat(s: &str) -> Result<SyntaxNode, ()> {
+ let template = "const _: () = {let {} = ();};";
+ let input = template.replace("{}", s);
+ let parse = syntax::SourceFile::parse(&input);
+ if !parse.errors().is_empty() {
+ return Err(());
+ }
+ let node = parse.tree().syntax().descendants().find_map(ast::Pat::cast).ok_or(())?;
+ if node.to_string() != s {
+ return Err(());
+ }
+ Ok(node.syntax().clone_subtree())
+}
+
pub(crate) fn expr(s: &str) -> Result<SyntaxNode, ()> {
let template = "const _: () = {};";
let input = template.replace("{}", s);
diff --git a/crates/ide_ssr/src/parsing.rs b/crates/ide_ssr/src/parsing.rs
index d70823403e..aaaee576b5 100644
--- a/crates/ide_ssr/src/parsing.rs
+++ b/crates/ide_ssr/src/parsing.rs
@@ -4,12 +4,12 @@
//! 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 rustc_hash::{FxHashMap, FxHashSet};
+use std::{fmt::Display, str::FromStr};
+use syntax::{SmolStr, SyntaxKind, SyntaxNode, T};
use crate::errors::bail;
use crate::{fragments, SsrError, SsrPattern, SsrRule};
-use rustc_hash::{FxHashMap, FxHashSet};
-use std::{fmt::Display, str::FromStr};
-use syntax::{ast, AstNode, SmolStr, SyntaxKind, SyntaxNode, T};
#[derive(Debug)]
pub(crate) struct ParsedRule {
@@ -75,14 +75,14 @@ impl ParsedRule {
let raw_template_stmt = raw_template.map(fragments::stmt);
if let raw_template_expr @ Some(Ok(_)) = raw_template.map(fragments::expr) {
- builder.try_add2(fragments::expr(&raw_pattern), raw_template_expr);
+ builder.try_add(fragments::expr(&raw_pattern), raw_template_expr);
} else {
- builder.try_add2(fragments::expr(&raw_pattern), raw_template_stmt.clone());
+ builder.try_add(fragments::expr(&raw_pattern), raw_template_stmt.clone());
}
- builder.try_add2(fragments::ty(&raw_pattern), raw_template.map(fragments::ty));
- builder.try_add2(fragments::item(&raw_pattern), raw_template.map(fragments::item));
- builder.try_add(ast::Pat::parse(&raw_pattern), raw_template.map(ast::Pat::parse));
- builder.try_add2(fragments::stmt(&raw_pattern), raw_template_stmt);
+ builder.try_add(fragments::ty(&raw_pattern), raw_template.map(fragments::ty));
+ builder.try_add(fragments::item(&raw_pattern), raw_template.map(fragments::item));
+ builder.try_add(fragments::pat(&raw_pattern), raw_template.map(fragments::pat));
+ builder.try_add(fragments::stmt(&raw_pattern), raw_template_stmt);
builder.build()
}
}
@@ -93,27 +93,7 @@ struct RuleBuilder {
}
impl RuleBuilder {
- fn try_add<T: AstNode, T2: AstNode>(
- &mut self,
- pattern: Result<T, ()>,
- template: Option<Result<T2, ()>>,
- ) {
- match (pattern, template) {
- (Ok(pattern), Some(Ok(template))) => self.rules.push(ParsedRule {
- placeholders_by_stand_in: self.placeholders_by_stand_in.clone(),
- pattern: pattern.syntax().clone(),
- template: Some(template.syntax().clone()),
- }),
- (Ok(pattern), None) => self.rules.push(ParsedRule {
- placeholders_by_stand_in: self.placeholders_by_stand_in.clone(),
- pattern: pattern.syntax().clone(),
- template: None,
- }),
- _ => {}
- }
- }
-
- fn try_add2(
+ fn try_add(
&mut self,
pattern: Result<SyntaxNode, ()>,
template: Option<Result<SyntaxNode, ()>>,