Skip to content

Commit cc4d257

Browse files
committed
Basic parsing of FnDefs
1 parent 4e0f1e2 commit cc4d257

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

chalk-parse/src/ast.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct Program {
2121
#[derive(Clone, PartialEq, Eq, Debug)]
2222
pub enum Item {
2323
StructDefn(StructDefn),
24+
FnDefn(FnDefn),
2425
TraitDefn(TraitDefn),
2526
OpaqueTyDefn(OpaqueTyDefn),
2627
Impl(Impl),
@@ -42,6 +43,19 @@ pub struct StructFlags {
4243
pub fundamental: bool,
4344
}
4445

46+
#[derive(Clone, PartialEq, Eq, Debug)]
47+
pub struct FnDefn {
48+
pub name: Identifier,
49+
pub variable_kinds: Vec<VariableKind>,
50+
pub where_clauses: Vec<QuantifiedWhereClause>,
51+
pub argument_types: Vec<Ty>,
52+
pub return_type: Ty,
53+
pub flags: FnDefFlags,
54+
}
55+
56+
#[derive(Clone, PartialEq, Eq, Debug)]
57+
pub struct FnDefFlags {}
58+
4559
#[derive(Clone, PartialEq, Eq, Debug)]
4660
pub struct TraitDefn {
4761
pub name: Identifier,

chalk-parse/src/parser.lalrpop

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Items: Vec<Item> = {
1414
Item: Option<Item> = {
1515
Comment => None,
1616
StructDefn => Some(Item::StructDefn(<>)),
17+
FnDefn => Some(Item::FnDefn(<>)),
1718
TraitDefn => Some(Item::TraitDefn(<>)),
1819
OpaqueTyDefn => Some(Item::OpaqueTyDefn(<>)),
1920
Impl => Some(Item::Impl(<>)),
@@ -67,6 +68,32 @@ StructDefn: StructDefn = {
6768
}
6869
};
6970

71+
FnReturn: Ty = {
72+
"->" <ty:Ty> => ty,
73+
};
74+
75+
FnDefn: FnDefn = {
76+
"fn" <n:Id> <p:Angle<VariableKind>>"(" <args:FnArgs> ")"
77+
<ret_ty:FnReturn?> <w:QuantifiedWhereClauses> ";" => FnDefn
78+
{
79+
name: n,
80+
variable_kinds: p,
81+
where_clauses: w,
82+
argument_types: args,
83+
return_type: ret_ty.unwrap_or_else(|| Ty::Tuple { types: Vec::new() }),
84+
flags: FnDefFlags {
85+
},
86+
}
87+
};
88+
89+
FnArg: Ty = {
90+
Id ":" <arg_ty: Ty> => arg_ty
91+
};
92+
93+
FnArgs: Vec<Ty> = {
94+
<Comma<FnArg>>
95+
};
96+
7097
TraitDefn: TraitDefn = {
7198
<auto:AutoKeyword?> <marker:MarkerKeyword?> <upstream:UpstreamKeyword?> <fundamental:FundamentalKeyword?> <non_enumerable:NonEnumerableKeyword?> <coinductive:CoinductiveKeyword?> <object_safe:ObjectSafeKeyword?> <well_known:WellKnownTrait?> "trait" <n:Id><p:Angle<VariableKind>>
7299
<w:QuantifiedWhereClauses> "{" <a:AssocTyDefn*> "}" => TraitDefn

0 commit comments

Comments
 (0)