Top level attributes

This commit is contained in:
2026-05-05 15:08:38 +02:00
parent ce50247895
commit 8793c5d69a
5 changed files with 28 additions and 7 deletions
+1
View File
@@ -62,6 +62,7 @@ pub struct TopLevel(pub TopLevelKind, pub Vec<Attribute>);
#[derive(Debug, Clone, Serialize)]
pub enum TopLevelKind {
ModAttribute,
Include(Path),
StructDecl {
export: bool,
+5 -1
View File
@@ -23,6 +23,10 @@ attribute = {
"#" ~ "[" ~ meta ~ "]"
}
mod_attribute = {
"#" ~ "!" ~ "[" ~ meta ~ "]"
}
meta = {
static_path ~ ("=" ~ primary | "(" ~ meta_list? ~ ")")?
}
@@ -33,7 +37,7 @@ meta_list = {
attributes = { attribute* }
top_level = { attributes ~ (import | function_decl | struct_decl) }
top_level = { (attributes ~ (import | function_decl | struct_decl)) | mod_attribute }
import = { sharp ~ "include" ~ "<" ~ static_path ~ ">" }
+8 -4
View File
@@ -84,9 +84,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for TypeExprKind {
impl From<pest::iterators::Pair<'_, Rule>> for Path {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
match pair.as_rule() {
Rule::static_path => {
Path(pair.into_inner().map(|i| i.as_str().to_string()).collect())
}
Rule::static_path => Path(pair.into_inner().map(|i| i.as_str().to_string()).collect()),
_ => unimplemented!("{pair:#?}"),
}
}
@@ -188,7 +186,13 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevel {
.map(Attribute::from)
.collect::<Vec<_>>();
TopLevel(TopLevelKind::from(inner.next().unwrap()), attributes)
TopLevel(
inner
.next()
.map(TopLevelKind::from)
.unwrap_or(TopLevelKind::ModAttribute),
attributes,
)
}
}