remove Item::parse

This commit is contained in:
Aleksey Kladov 2021-12-27 20:33:33 +03:00
parent b468bd6645
commit f0fefde401
6 changed files with 17 additions and 22 deletions

View File

@ -19,3 +19,14 @@ pub(crate) fn ty(s: &str) -> Result<SyntaxNode, ()> {
let node = parse.tree().syntax().descendants().find_map(ast::Type::cast).ok_or(())?;
Ok(node.syntax().clone())
}
pub(crate) fn item(s: &str) -> Result<SyntaxNode, ()> {
let template = "{}";
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::Item::cast).ok_or(())?;
Ok(node.syntax().clone())
}

View File

@ -80,7 +80,7 @@ impl ParsedRule {
builder.try_add(ast::Expr::parse(&raw_pattern), raw_template_stmt.clone());
}
builder.try_add2(fragments::ty(&raw_pattern), raw_template.map(fragments::ty));
builder.try_add(ast::Item::parse(&raw_pattern), raw_template.map(ast::Item::parse));
builder.try_add2(fragments::item(&raw_pattern), raw_template.map(fragments::item));
builder.try_add(ast::Path::parse(&raw_pattern), raw_template.map(ast::Path::parse));
builder.try_add(ast::Pat::parse(&raw_pattern), raw_template.map(ast::Pat::parse));
builder.try_add(ast::Stmt::parse(&raw_pattern), raw_template_stmt);

View File

@ -1,5 +1,6 @@
//! Code for applying replacement templates for matches that have previously been found.
use crate::fragments;
use crate::{resolving::ResolvedRule, Match, SsrMatches};
use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet};
@ -228,9 +229,10 @@ fn parse_as_kind(code: &str, kind: SyntaxKind) -> Option<SyntaxNode> {
if let Ok(expr) = ast::Expr::parse(code) {
return Some(expr.syntax().clone());
}
} else if ast::Item::can_cast(kind) {
if let Ok(item) = ast::Item::parse(code) {
return Some(item.syntax().clone());
}
if ast::Item::can_cast(kind) {
if let Ok(item) = fragments::item(code) {
return Some(item);
}
}
None

View File

@ -142,7 +142,6 @@ pub enum ParserEntryPoint {
Expr,
StatementOptionalSemi,
Pattern,
Item,
Attr,
}
@ -164,7 +163,6 @@ pub fn parse(inp: &Input, entry_point: ParserEntryPoint) -> Output {
ParserEntryPoint::Path => grammar::entry::prefix::path,
ParserEntryPoint::Expr => grammar::entry::prefix::expr,
ParserEntryPoint::Pattern => grammar::entry::prefix::pat,
ParserEntryPoint::Item => grammar::entry::prefix::item,
ParserEntryPoint::StatementOptionalSemi => grammar::entry_points::stmt_optional_semi,
ParserEntryPoint::Attr => grammar::entry_points::attr,
};

View File

@ -194,13 +194,6 @@ impl ast::Expr {
}
}
impl ast::Item {
/// Returns `text`, parsed as an item, but only if it has no errors.
pub fn parse(text: &str) -> Result<Self, ()> {
parsing::parse_text_as(text, parser::ParserEntryPoint::Item)
}
}
impl ast::Attr {
/// Returns `text`, parsed as an attribute, but only if it has no errors.
pub fn parse(text: &str) -> Result<Self, ()> {

View File

@ -86,15 +86,6 @@ fn pattern_parser_tests() {
);
}
#[test]
fn item_parser_tests() {
fragment_parser_dir_test(
&["parser/fragments/item/ok"],
&["parser/fragments/item/err"],
crate::ast::Item::parse,
);
}
#[test]
fn stmt_parser_tests() {
fragment_parser_dir_test(