Skip to content

Commit

Permalink
frontend: parse super traits for trait definition
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Oct 15, 2024
1 parent 4523034 commit b1fe4fe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 43 deletions.
33 changes: 9 additions & 24 deletions dora-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ impl File {
self.elements[0].to_enum().unwrap()
}

#[cfg(test)]
pub fn alias0(&self) -> &Alias {
self.elements[0].to_alias().unwrap()
}

#[cfg(test)]
pub fn module0(&self) -> &Module {
self.elements[0].to_module().unwrap()
Expand Down Expand Up @@ -103,7 +98,6 @@ pub enum ElemData {
Global(Arc<Global>),
Const(Arc<Const>),
Enum(Arc<Enum>),
Alias(Arc<Alias>),
Module(Arc<Module>),
Use(Arc<Use>),
Extern(Arc<ExternPackage>),
Expand All @@ -122,7 +116,6 @@ impl ElemData {
ElemData::Global(ref node) => node.span,
ElemData::Const(ref node) => node.span,
ElemData::Enum(ref node) => node.span,
ElemData::Alias(ref node) => node.span,
ElemData::Module(ref node) => node.span,
ElemData::Use(ref node) => node.span,
ElemData::Extern(ref node) => node.span,
Expand Down Expand Up @@ -152,13 +145,6 @@ impl ElemData {
}
}

pub fn to_alias(&self) -> Option<&Alias> {
match self {
&ElemData::Alias(ref alias) => Some(alias),
_ => None,
}
}

pub fn to_module(&self) -> Option<&Module> {
match self {
&ElemData::Module(ref module) => Some(module),
Expand Down Expand Up @@ -321,16 +307,6 @@ pub struct EnumVariant {
pub types: Option<Vec<Type>>,
}

#[derive(Clone, Debug)]
pub struct Alias {
pub id: NodeId,
pub span: Span,
pub green: GreenNode,
pub modifiers: Option<ModifierList>,
pub name: Option<Ident>,
pub ty: Type,
}

#[derive(Clone, Debug)]
pub struct Struct {
pub id: NodeId,
Expand Down Expand Up @@ -568,6 +544,14 @@ impl TypeData {
TypeData::Error { id, .. } => id,
}
}

#[cfg(test)]
pub fn name(&self) -> &str {
match *self {
TypeData::Regular(ref regular) => regular.name(),
_ => unreachable!(),
}
}
}

#[derive(Clone, Debug)]
Expand All @@ -593,6 +577,7 @@ pub struct Trait {
pub green: GreenNode,
pub modifiers: Option<ModifierList>,
pub type_params: Option<TypeParams>,
pub bounds: Vec<Type>,
pub where_bounds: Option<WhereBounds>,
pub span: Span,
pub methods: Vec<Elem>,
Expand Down
10 changes: 0 additions & 10 deletions dora-parser/src/ast/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ impl AstDumper {
ElemData::Global(ref node) => self.dump_global(node),
ElemData::Const(ref node) => self.dump_const(node),
ElemData::Enum(ref node) => self.dump_enum(node),
ElemData::Alias(ref node) => self.dump_alias(node),
ElemData::Module(ref node) => self.dump_module(node),
ElemData::Use(ref node) => self.dump_use(node),
ElemData::Extern(ref node) => self.dump_extern(node),
Expand Down Expand Up @@ -103,15 +102,6 @@ impl AstDumper {
dump!(self, "use @ {} {}", node.span, node.id);
}

fn dump_alias(&mut self, alias: &Alias) {
dump!(self, "alias @ {} {}", alias.span, alias.id);
self.dump_ident(&alias.name);

self.indent(|d| {
d.dump_type(&alias.ty);
});
}

fn dump_module(&mut self, module: &Module) {
dump!(self, "module @ {} {}", module.span, module.id);
self.dump_ident(&module.name);
Expand Down
9 changes: 0 additions & 9 deletions dora-parser/src/ast/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ pub trait Visitor: Sized {
walk_enum(self, e);
}

fn visit_alias(&mut self, e: &Arc<Alias>) {
walk_alias(self, e);
}

fn visit_module(&mut self, e: &Arc<Module>) {
walk_module(self, e);
}
Expand Down Expand Up @@ -106,7 +102,6 @@ pub fn walk_elem<V: Visitor>(v: &mut V, e: &ElemData) {
ElemData::Global(ref g) => v.visit_global(g),
ElemData::Const(ref c) => v.visit_const(c),
ElemData::Enum(ref e) => v.visit_enum(e),
ElemData::Alias(ref e) => v.visit_alias(e),
ElemData::Module(ref e) => v.visit_module(e),
ElemData::Use(ref i) => v.visit_use(i),
ElemData::Extern(ref stmt) => v.visit_extern(stmt),
Expand Down Expand Up @@ -150,10 +145,6 @@ pub fn walk_enum<V: Visitor>(_v: &mut V, _e: &Arc<Enum>) {
// nothing to do
}

pub fn walk_alias<V: Visitor>(v: &mut V, a: &Arc<Alias>) {
v.visit_type(&a.ty);
}

pub fn walk_module<V: Visitor>(v: &mut V, node: &Arc<Module>) {
if let Some(ref elements) = node.elements {
for e in elements {
Expand Down
22 changes: 22 additions & 0 deletions dora-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,11 @@ impl Parser {
self.assert(TRAIT_KW);
let name = self.expect_identifier();
let type_params = self.parse_type_params();
let bounds = if self.eat(COLON) {
self.parse_type_bounds()
} else {
Vec::new()
};
let where_bounds = self.parse_where();

self.expect(L_BRACE);
Expand All @@ -555,6 +560,7 @@ impl Parser {
green,
modifiers: modifiers.clone(),
type_params,
bounds,
where_bounds,
span: self.finish_node(),
methods,
Expand Down Expand Up @@ -2466,6 +2472,11 @@ mod tests {

fn parse(code: &'static str) -> Arc<File> {
let (file, errors) = Parser::from_string(code).parse();
if !errors.is_empty() {
for error in &errors {
println!("{} at {}", error.error.message(), error.span);
}
}
assert!(errors.is_empty());
file
}
Expand Down Expand Up @@ -3569,6 +3580,17 @@ mod tests {
assert_eq!(1, trait_.methods.len());
}

#[test]
fn parse_trait_with_bounds() {
let prog = parse("trait Foo: A + B {}");
let trait_ = prog.trait0();

assert_eq!("Foo", trait_.name.as_ref().unwrap().name_as_string);
assert_eq!(2, trait_.bounds.len());
assert_eq!("A", trait_.bounds[0].name());
assert_eq!("B", trait_.bounds[1].name());
}

#[test]
fn parse_trait_with_static_function() {
let prog = parse("trait Foo { static fn empty(); }");
Expand Down

0 comments on commit b1fe4fe

Please sign in to comment.