From 4923b8a74bdffb0a8d1ce3760d00da8959a711c2 Mon Sep 17 00:00:00 2001 From: Victor Song Date: Mon, 12 Feb 2024 23:57:42 -0600 Subject: [PATCH] Return `Option>` from `ast::Literal::parse` --- .../src/server/rust_analyzer_span.rs | 2 +- crates/proc-macro-srv/src/server/token_id.rs | 2 +- crates/syntax/src/lib.rs | 15 +++++++++------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/proc-macro-srv/src/server/rust_analyzer_span.rs b/crates/proc-macro-srv/src/server/rust_analyzer_span.rs index 7313e99bb1..cf6e816d59 100644 --- a/crates/proc-macro-srv/src/server/rust_analyzer_span.rs +++ b/crates/proc-macro-srv/src/server/rust_analyzer_span.rs @@ -71,7 +71,7 @@ impl server::FreeFunctions for RaSpanServer { &mut self, s: &str, ) -> Result, ()> { - let literal = ast::Literal::parse(s); + let literal = ast::Literal::parse(s).ok_or(())?; let literal = literal.tree(); let kind = literal_to_external(literal.kind()).ok_or(())?; diff --git a/crates/proc-macro-srv/src/server/token_id.rs b/crates/proc-macro-srv/src/server/token_id.rs index d16e9d2510..70e577f576 100644 --- a/crates/proc-macro-srv/src/server/token_id.rs +++ b/crates/proc-macro-srv/src/server/token_id.rs @@ -63,7 +63,7 @@ impl server::FreeFunctions for TokenIdServer { &mut self, s: &str, ) -> Result, ()> { - let literal = ast::Literal::parse(s); + let literal = ast::Literal::parse(s).ok_or(())?; let literal = literal.tree(); let kind = literal_to_external(literal.kind()).ok_or(())?; diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 2390cacda2..61d9c65e4f 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -188,7 +188,7 @@ impl SourceFile { } impl ast::Literal { - pub fn parse(text: &str) -> Parse { + pub fn parse(text: &str) -> Option> { let lexed = parser::LexedStr::new(text); let parser_input = lexed.to_input(); let parser_output = parser::TopEntryPoint::Expr.parse(&parser_input); @@ -197,11 +197,14 @@ impl ast::Literal { errors.extend(validation::validate(&root)); - assert_eq!(root.kind(), SyntaxKind::LITERAL); - Parse { - green, - errors: if errors.is_empty() { None } else { Some(errors.into()) }, - _ty: PhantomData, + if root.kind() == SyntaxKind::LITERAL { + Some(Parse { + green, + errors: if errors.is_empty() { None } else { Some(errors.into()) }, + _ty: PhantomData, + }) + } else { + None } } }