From 7f7c1e2000372195793a540c529e2496646656c7 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 23 Jun 2026 12:47:38 +0200 Subject: [PATCH] Fixed mod attributes --- crates/parser/src/grammar.pest | 3 +-- crates/parser/src/parser/items/mod.rs | 7 +++++-- crates/parser/src/parser/mod.rs | 12 ++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 7bcfdf1..d4f09b7 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -343,8 +343,7 @@ function_decl = { // ====================================================== top_level = { - (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl)) - | mod_attribute + mod_attribute | (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl)) } // ====================================================== diff --git a/crates/parser/src/parser/items/mod.rs b/crates/parser/src/parser/items/mod.rs index 1bae6d0..ab4600b 100644 --- a/crates/parser/src/parser/items/mod.rs +++ b/crates/parser/src/parser/items/mod.rs @@ -16,12 +16,15 @@ impl<'a> TryFrom> for TopLevel { type Error = AstError<'a, Self>; fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result { - let mut inner = pair.into_inner(); + let mut inner = pair.clone().into_inner(); let attributes = collect_recovered(inner.next().unwrap().into_inner()); 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, )) } diff --git a/crates/parser/src/parser/mod.rs b/crates/parser/src/parser/mod.rs index 2e73885..6662791 100644 --- a/crates/parser/src/parser/mod.rs +++ b/crates/parser/src/parser/mod.rs @@ -28,6 +28,18 @@ pub fn consume_rule<'a>( if consumed { pairs.next() } else { None } } +impl Spanned { + 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, Error = AstError<'a, T>>> TryFrom> for Spanned {