Updated grammar and AST
This commit is contained in:
+10
-14
@@ -18,22 +18,18 @@ pub enum TypePostfix {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Attribute {
|
||||
pub path: StaticPath,
|
||||
pub kind: MetaItemKind,
|
||||
}
|
||||
pub enum Attribute {
|
||||
/// #[test]
|
||||
Path(StaticPath),
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum MetaItemKind {
|
||||
Word,
|
||||
NameValue(Literal),
|
||||
List(Vec<NestedMetaItem>),
|
||||
}
|
||||
/// #[name = "value"]
|
||||
NameValue { path: StaticPath, value: Literal },
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum NestedMetaItem {
|
||||
Literal(Literal),
|
||||
MetaItem(Attribute),
|
||||
/// #[derive(Clone, Copy)]
|
||||
List {
|
||||
path: StaticPath,
|
||||
items: Vec<Attribute>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
||||
+7
-10
@@ -20,19 +20,16 @@ var_decl = { type_expr ~ mutable? ~ identifier }
|
||||
// ================= TOP LEVEL =================
|
||||
|
||||
attribute = {
|
||||
"#" ~ "[" ~ (structured_style | list_style | pair_attr | simple_style) ~ "]"
|
||||
"#" ~ "[" ~ meta ~ "]"
|
||||
}
|
||||
|
||||
// Simple: #[test]
|
||||
simple_style = { static_path }
|
||||
meta = {
|
||||
static_path ~ ("=" ~ primary | "(" ~ meta_list? ~ ")")?
|
||||
}
|
||||
|
||||
// #[derive(PartialEq, PartialOrd)]
|
||||
list_style = { static_path ~ "(" ~ identifier ~ ("," ~ identifier)* ~ ","? ~ ")" }
|
||||
|
||||
// #[link(name = "readline")]
|
||||
structured_style = { static_path ~ "(" ~ pair_attr ~ ("," ~ pair_attr)* ~ ","? ~ ")" }
|
||||
|
||||
pair_attr = { identifier ~ "=" ~ primary }
|
||||
meta_list = {
|
||||
meta ~ ("," ~ meta)* ~ ","?
|
||||
}
|
||||
|
||||
top_level = { attribute* ~ (import | function_decl | struct_decl) }
|
||||
|
||||
|
||||
+2
-33
@@ -128,42 +128,11 @@ impl From<pest::iterators::Pair<'_, Rule>> for Attribute {
|
||||
}
|
||||
|
||||
let mut inner = pair.into_inner();
|
||||
// Every style starts with an identifier (the path)
|
||||
let path = StaticPath::from(inner.next().expect("Path identifier expected"));
|
||||
let path = StaticPath::from(inner.next().unwrap());
|
||||
|
||||
let kind = match rule {
|
||||
Rule::simple_style => MetaItemKind::Word,
|
||||
|
||||
Rule::key_value_style | Rule::pair => {
|
||||
let lit = Literal::from(inner.next().unwrap());
|
||||
MetaItemKind::NameValue(lit)
|
||||
}
|
||||
|
||||
Rule::list_style => {
|
||||
// derive(Debug, Clone) -> NestedMetaItem::MetaItem(Attribute { path: "Debug", kind: Word })
|
||||
let items = inner
|
||||
.map(|p| {
|
||||
NestedMetaItem::MetaItem(Attribute {
|
||||
path: StaticPath::from(p),
|
||||
kind: MetaItemKind::Word,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
MetaItemKind::List(items)
|
||||
}
|
||||
|
||||
Rule::structured_style => {
|
||||
// link(name = "readline") -> NestedMetaItem::MetaItem(Attribute { path: "name", kind: NameValue(...) })
|
||||
let items = inner
|
||||
.map(|p| NestedMetaItem::MetaItem(Attribute::from(p)))
|
||||
.collect();
|
||||
MetaItemKind::List(items)
|
||||
}
|
||||
|
||||
_ => unreachable!("Unexpected rule: {:?}", rule),
|
||||
_ => unimplemented!("{:?}", rule),
|
||||
};
|
||||
|
||||
Attribute { path, kind }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user