Skip to content

Commit

Permalink
Implemented parsing for trait bodies in preparation for user-defined …
Browse files Browse the repository at this point in the history
…traits
  • Loading branch information
IsaacShelton committed Dec 15, 2024
1 parent b465338 commit 63747d1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
48 changes: 46 additions & 2 deletions src/parser/parse_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ use super::{
Parser,
};
use crate::{
ast::{Privacy, Trait},
ast::{Parameters, Privacy, Trait, Type},
inflow::Inflow,
source_files::Source,
token::{Token, TokenKind},
};

pub struct TraitMethod {
pub name: String,
pub parameters: Parameters,
pub return_type: Type,
pub source: Source,
}

impl<'a, I: Inflow<Token>> Parser<'a, I> {
pub fn parse_trait(&mut self, annotations: Vec<Annotation>) -> Result<Trait, ParseError> {
let source = self.source_here();
Expand All @@ -28,9 +36,13 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {

self.ignore_newlines();
self.parse_token(TokenKind::OpenCurly, Some("to begin trait body"))?;
self.ignore_newlines();

let mut methods = vec![];

while !self.input.peek_is_or_eof(TokenKind::CloseCurly) {
todo!("parse trait body");
methods.push(self.parse_trait_method()?);
self.ignore_newlines();
}

self.parse_token(TokenKind::CloseCurly, Some("to end trait body"))?;
Expand All @@ -41,4 +53,36 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
privacy,
})
}

fn parse_trait_method(&mut self) -> Result<TraitMethod, ParseError> {
let source = self.input.peek().source;

if !self.input.eat(TokenKind::FuncKeyword) {
return Err(ParseError::expected(
"'func' keyword",
Some("to begin trait method"),
self.input.peek(),
));
}

let name = self.parse_identifier(Some("after 'func' keyword"))?;
self.ignore_newlines();

let parameters = if self.input.peek_is(TokenKind::OpenParen) {
self.parse_function_parameters()?
} else {
Parameters::default()
};

self.ignore_newlines();

let return_type = self.parse_type(Some("return "), Some("for trait method"))?;

Ok(TraitMethod {
name,
parameters,
return_type,
source,
})
}
}
7 changes: 2 additions & 5 deletions src/resolve/function_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ pub fn create_function_heads(

for (function_i, function) in file.functions.iter().enumerate() {
let name = ResolvedName::new(module_file_id, &function.name);

let fake_constraints = CurrentConstraints {
constraints: Default::default(),
};
let pre_parameters_constraints = CurrentConstraints::default();

let type_ctx = ResolveTypeCtx::new(
&resolved_ast,
module_file_id,
*physical_file_id,
&ctx.types_in_modules,
&fake_constraints,
&pre_parameters_constraints,
);

let is_generic = function.return_type.contains_polymorph().is_some()
Expand Down

0 comments on commit 63747d1

Please sign in to comment.