Fixed mod attributes

This commit is contained in:
2026-06-23 12:47:38 +02:00
parent 2f6303ba30
commit 7f7c1e2000
3 changed files with 18 additions and 4 deletions
+1 -2
View File
@@ -343,8 +343,7 @@ function_decl = {
// ====================================================== // ======================================================
top_level = { top_level = {
(attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl)) mod_attribute | (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl))
| mod_attribute
} }
// ====================================================== // ======================================================
+5 -2
View File
@@ -16,12 +16,15 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevel {
type Error = AstError<'a, Self>; type Error = AstError<'a, Self>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> { fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let mut inner = pair.into_inner(); let mut inner = pair.clone().into_inner();
let attributes = collect_recovered(inner.next().unwrap().into_inner()); let attributes = collect_recovered(inner.next().unwrap().into_inner());
ast_expr!(TopLevel( ast_expr!(TopLevel(
inner.next().map(Spanned::try_from).unwrap(), inner
.next()
.map(Spanned::try_from)
.unwrap_or_else(move || Ok(Spanned::new_pair(pair, TopLevelKind::ModAttribute))),
attributes, attributes,
)) ))
} }
+12
View File
@@ -28,6 +28,18 @@ pub fn consume_rule<'a>(
if consumed { pairs.next() } else { None } if consumed { pairs.next() } else { None }
} }
impl<T> Spanned<T> {
fn new_pair(pair: pest::iterators::Pair<'_, Rule>, item: T) -> Self {
let span = pair.as_span().start_pos().line_col();
Self {
line: span.0,
column: span.1,
item,
}
}
}
impl<'a, T: TryFrom<pest::iterators::Pair<'a, Rule>, Error = AstError<'a, T>>> impl<'a, T: TryFrom<pest::iterators::Pair<'a, Rule>, Error = AstError<'a, T>>>
TryFrom<pest::iterators::Pair<'a, Rule>> for Spanned<T> TryFrom<pest::iterators::Pair<'a, Rule>> for Spanned<T>
{ {