Return Option<Parse<ast::Literal>> from ast::Literal::parse

This commit is contained in:
Victor Song 2024-02-12 23:57:42 -06:00
parent 1918f9b9e0
commit 4923b8a74b
3 changed files with 11 additions and 8 deletions

View file

@ -71,7 +71,7 @@ impl server::FreeFunctions for RaSpanServer {
&mut self,
s: &str,
) -> Result<bridge::Literal<Self::Span, Self::Symbol>, ()> {
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(())?;

View file

@ -63,7 +63,7 @@ impl server::FreeFunctions for TokenIdServer {
&mut self,
s: &str,
) -> Result<bridge::Literal<Self::Span, Self::Symbol>, ()> {
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(())?;

View file

@ -188,7 +188,7 @@ impl SourceFile {
}
impl ast::Literal {
pub fn parse(text: &str) -> Parse<ast::Literal> {
pub fn parse(text: &str) -> Option<Parse<ast::Literal>> {
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 {
if root.kind() == SyntaxKind::LITERAL {
Some(Parse {
green,
errors: if errors.is_empty() { None } else { Some(errors.into()) },
_ty: PhantomData,
})
} else {
None
}
}
}