Implement APIs for parsing expressions, types, paths, patterns and items
This commit is contained in:
@@ -168,6 +168,41 @@ impl SourceFile {
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Path {
|
||||
/// Returns `text`, parsed as a path, but only if it has no errors.
|
||||
pub fn parse(text: &str) -> Result<Self, ()> {
|
||||
parsing::parse_text_fragment(text, ra_parser::FragmentKind::Path)
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Pat {
|
||||
/// Returns `text`, parsed as a pattern, but only if it has no errors.
|
||||
pub fn parse(text: &str) -> Result<Self, ()> {
|
||||
parsing::parse_text_fragment(text, ra_parser::FragmentKind::Pattern)
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::Expr {
|
||||
/// Returns `text`, parsed as an expression, but only if it has no errors.
|
||||
pub fn parse(text: &str) -> Result<Self, ()> {
|
||||
parsing::parse_text_fragment(text, ra_parser::FragmentKind::Expr)
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::ModuleItem {
|
||||
/// Returns `text`, parsed as an item, but only if it has no errors.
|
||||
pub fn parse(text: &str) -> Result<Self, ()> {
|
||||
parsing::parse_text_fragment(text, ra_parser::FragmentKind::Item)
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::TypeRef {
|
||||
/// Returns `text`, parsed as an type reference, but only if it has no errors.
|
||||
pub fn parse(text: &str) -> Result<Self, ()> {
|
||||
parsing::parse_text_fragment(text, ra_parser::FragmentKind::Type)
|
||||
}
|
||||
}
|
||||
|
||||
/// Matches a `SyntaxNode` against an `ast` type.
|
||||
///
|
||||
/// # Example:
|
||||
|
||||
Reference in New Issue
Block a user