Spanned impl

This commit is contained in:
2026-05-18 14:42:41 +02:00
parent 5648c1da61
commit a88ff4c8dd
3 changed files with 23 additions and 9 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ pub enum Attribute {
}
#[derive(Debug, Clone, Serialize)]
pub struct TopLevel(pub TopLevelKind, pub Vec<Attribute>);
pub struct TopLevel(pub Spanned<TopLevelKind>, pub Vec<Attribute>);
#[derive(Debug, Clone, Serialize)]
pub enum TopLevelKind {
+1 -4
View File
@@ -21,10 +21,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> 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,
))
}
+21 -4
View File
@@ -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<pest::iterators::Pair<'a, Rule>>> TryFrom<pest::iterators::Pair<'a, Rule>>
for Spanned<T>
impl<'a, T: TryFrom<pest::iterators::Pair<'a, Rule>, Error = AstError<'a, T>>>
TryFrom<pest::iterators::Pair<'a, Rule>> for Spanned<T>
{
type Error = T::Error;
type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let span = pair.as_span().start_pos().line_col();
@@ -43,3 +43,20 @@ impl<'a, T: TryFrom<pest::iterators::Pair<'a, Rule>>> TryFrom<pest::iterators::P
})
}
}
impl<'a, T> From<AstError<'a, T>> for AstError<'a, Spanned<T>> {
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,
}),
}
}
}