Implement APIs for parsing expressions, types, paths, patterns and items

This commit is contained in:
David Lattimore 2020-06-19 07:43:19 +10:00
parent 902a9c6da7
commit bc99e95d7d
29 changed files with 222 additions and 1 deletions

View file

@ -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: