From a88ff4c8dd3ecca3de4d01f877ad4d7104117d86 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Mon, 18 May 2026 14:42:41 +0200 Subject: [PATCH] Spanned impl --- parser/src/ast/top_level.rs | 2 +- parser/src/parser/items/mod.rs | 5 +---- parser/src/parser/mod.rs | 25 +++++++++++++++++++++---- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/parser/src/ast/top_level.rs b/parser/src/ast/top_level.rs index 04644ca..1771fb6 100644 --- a/parser/src/ast/top_level.rs +++ b/parser/src/ast/top_level.rs @@ -22,7 +22,7 @@ pub enum Attribute { } #[derive(Debug, Clone, Serialize)] -pub struct TopLevel(pub TopLevelKind, pub Vec); +pub struct TopLevel(pub Spanned, pub Vec); #[derive(Debug, Clone, Serialize)] pub enum TopLevelKind { diff --git a/parser/src/parser/items/mod.rs b/parser/src/parser/items/mod.rs index 229ea88..8277660 100644 --- a/parser/src/parser/items/mod.rs +++ b/parser/src/parser/items/mod.rs @@ -21,10 +21,7 @@ impl<'a> TryFrom> for TopLevel { let attributes = collect_recovered(inner.next().unwrap().into_inner()); ast_expr!(TopLevel( - inner - .next() - .map(TopLevelKind::try_from) - .unwrap_or(Ok(TopLevelKind::ModAttribute)), + inner.next().map(Spanned::try_from).unwrap(), attributes, )) } diff --git a/parser/src/parser/mod.rs b/parser/src/parser/mod.rs index 08f20b8..6d1ef65 100644 --- a/parser/src/parser/mod.rs +++ b/parser/src/parser/mod.rs @@ -1,7 +1,7 @@ pub mod common; pub mod items; -use crate::{Rule, ast::Spanned}; +use crate::{Rule, ast::Spanned, error::AstError}; pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool { let consumed = pairs @@ -28,10 +28,10 @@ pub fn consume_rule<'a>( if consumed { pairs.next() } else { None } } -impl<'a, T: TryFrom>> TryFrom> - for Spanned +impl<'a, T: TryFrom, Error = AstError<'a, T>>> + TryFrom> for Spanned { - type Error = T::Error; + type Error = AstError<'a, Self>; fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { let span = pair.as_span().start_pos().line_col(); @@ -43,3 +43,20 @@ impl<'a, T: TryFrom>> TryFrom From> for AstError<'a, Spanned> { + fn from(value: AstError<'a, T>) -> Self { + let span = value.span.start_pos().line_col(); + + Self { + span: value.span, + error_code: value.error_code, + error_message: value.error_message, + recovered: value.recovered.map(|v| Spanned { + line: span.0, + column: span.1, + item: v, + }), + } + } +}