Skip to content

Commit

Permalink
Continued working on syntax for trait implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Dec 25, 2024
1 parent 526e471 commit 676671a
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 116 deletions.
4 changes: 1 addition & 3 deletions src/ast/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
enumeration::Enum, global_variable::GlobalVar, implementation::Impl, structure::Structure,
type_alias::TypeAlias, Function, Given, HelperExpr, SettingsId, Trait,
type_alias::TypeAlias, Function, HelperExpr, SettingsId, Trait,
};

#[derive(Clone, Debug)]
Expand All @@ -13,7 +13,6 @@ pub struct AstFile {
pub helper_exprs: Vec<HelperExpr>,
pub traits: Vec<Trait>,
pub impls: Vec<Impl>,
pub givens: Vec<Given>,
pub settings: Option<SettingsId>,
}

Expand All @@ -28,7 +27,6 @@ impl AstFile {
helper_exprs: vec![],
traits: vec![],
impls: vec![],
givens: vec![],
settings: None,
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/ast/given.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/ast/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::source_files::Source;

#[derive(Clone, Debug)]
pub struct Impl {
pub for_type: Type,
pub target_trait: Type,
pub name: Option<String>,
pub target: Type,
pub source: Source,
pub body: Vec<Function>,
}
2 changes: 0 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod enumeration;
mod expr;
mod file;
mod function;
mod given;
mod global_variable;
mod helper_expr;
mod implementation;
Expand All @@ -23,7 +22,6 @@ pub use enumeration::*;
pub use expr::*;
pub use file::*;
pub use function::*;
pub use given::*;
pub use global_variable::*;
pub use helper_expr::*;
pub use implementation::*;
Expand Down
1 change: 0 additions & 1 deletion src/lexer/identifier_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl IdentifierState {
"impl" => TokenKind::ImplKeyword,
"for" => TokenKind::ForKeyword,
"is" => TokenKind::IsKeyword,
"given" => TokenKind::GivenKeyword,
_ => TokenKind::Identifier(identifier),
}
.at(self.start_source)
Expand Down
6 changes: 3 additions & 3 deletions src/parser/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ pub enum AnnotationKind {
AbideAbi,
Public,
Template,
Using(Using),
Given(Given),
}

#[derive(Clone, Debug)]
pub struct Using {
pub struct Given {
pub name: Option<String>,
pub ty: ast::Type,
}
Expand All @@ -45,7 +45,7 @@ impl Display for AnnotationKind {
Self::AbideAbi => "abide_abi",
Self::Public => "public",
Self::Template => "template",
Self::Using(_) => "using",
Self::Given(_) => "given",
})
}
}
7 changes: 7 additions & 0 deletions src/parser/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ where
.then(|| self.advance().kind.unwrap_identifier())
}

pub fn eat_polymorph(&mut self) -> Option<String> {
self.peek()
.kind
.is_polymorph()
.then(|| self.advance().kind.unwrap_polymorph())
}

pub fn ignore_newlines(&mut self) {
while self.eat(TokenKind::Newline) {}
}
Expand Down
1 change: 0 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod parse_enum;
mod parse_expr;
mod parse_function;
mod parse_function_parameters;
mod parse_given;
mod parse_global_variable;
mod parse_helper_expr;
mod parse_impl;
Expand Down
16 changes: 11 additions & 5 deletions src/parser/parse_annotation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
annotation::{Annotation, AnnotationKind, Using},
annotation::{Annotation, AnnotationKind, Given},
error::{ParseError, ParseErrorKind},
Parser,
};
Expand Down Expand Up @@ -28,10 +28,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
"abide_abi" => AnnotationKind::AbideAbi,
"public" => AnnotationKind::Public,
"template" => AnnotationKind::Template,
"using" => AnnotationKind::Using(Using {
name: self.parse_optional_name(),
ty: self.parse_type(None::<&str>, Some("for context"))?,
}),
"given" => AnnotationKind::Given(self.parse_given()?),
_ => {
return Err(ParseErrorKind::UnrecognizedAnnotation {
name: annotation_name,
Expand All @@ -52,6 +49,15 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
Ok(annotations)
}

fn parse_given(&mut self) -> Result<Given, ParseError> {
let name = self.input.eat_polymorph();

Ok(Given {
name,
ty: self.parse_type(None::<&str>, Some("for context"))?,
})
}

pub fn parse_optional_name(&mut self) -> Option<String> {
(self.input.peek().is_identifier() && self.input.peek_nth(1).is_identifier())
.then(|| self.input.advance().kind.unwrap_identifier())
Expand Down
16 changes: 12 additions & 4 deletions src/parser/parse_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
// func functionName {
// ^

if !self.input.peek().is_func_keyword() {
return Err(ParseError::expected(
"function",
None::<&str>,
self.input.peek(),
));
}

let source = self.input.advance().source;

let mut is_foreign = false;
let mut abide_abi = false;
let mut privacy = Privacy::Private;
Expand All @@ -25,8 +35,8 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
AnnotationKind::Foreign => is_foreign = true,
AnnotationKind::AbideAbi => abide_abi = true,
AnnotationKind::Public => privacy = Privacy::Public,
AnnotationKind::Using(using) => {
contextual_parameters.push(using);
AnnotationKind::Given(given) => {
contextual_parameters.push(given);
}
_ => return Err(self.unexpected_annotation(&annotation, Some("for function"))),
}
Expand All @@ -37,8 +47,6 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
abide_abi = true;
}

let source = self.input.advance().source;

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

Expand Down
65 changes: 0 additions & 65 deletions src/parser/parse_given.rs

This file was deleted.

22 changes: 7 additions & 15 deletions src/parser/parse_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,14 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {

for annotation in annotations {
match annotation.kind {
_ => return Err(self.unexpected_annotation(&annotation, Some("for impl"))),
}
}

let target_trait = self.parse_type(None::<&str>, Some("trait"))?;

if !self.input.eat(TokenKind::ForKeyword) {
return Err(ParseErrorKind::Expected {
expected: TokenKind::ForKeyword.to_string(),
for_reason: Some("after trait to implement".into()),
got: self.input.peek().to_string(),
_ => {
return Err(self.unexpected_annotation(&annotation, Some("for implementation")))
}
}
.at(self.input.peek().source));
}

let for_type = self.parse_type(None::<&str>, Some("impl target"))?;
let name = self.parse_optional_name();
let target = self.parse_type(None::<&str>, Some("trait"))?;

let mut body = vec![];

Expand Down Expand Up @@ -64,8 +56,8 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
}

Ok(Impl {
for_type,
target_trait,
name,
target,
source,
body,
})
Expand Down
6 changes: 1 addition & 5 deletions src/parser/parse_top_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
ast_file.traits.push(trait_decl);
}
TokenKind::ImplKeyword => {
let impl_decl = self.parse_impl(annotations)?;
ast_file.impls.push(impl_decl);
}
TokenKind::GivenKeyword => {
ast_file.givens.push(self.parse_given(annotations)?);
ast_file.impls.push(self.parse_impl(annotations)?);
}
TokenKind::EndOfFile => {
// End-of-file is only okay if no preceeding annotations
Expand Down

0 comments on commit 676671a

Please sign in to comment.