From f402ca05bc7279fe568ebd0910aeb1cc6c748d73 Mon Sep 17 00:00:00 2001 From: IsaacShelton Date: Fri, 27 Sep 2024 21:32:25 -0500 Subject: [PATCH] Started working on visibility modifiers --- src/ast/function/mod.rs | 3 ++- src/lexer/identifier_state.rs | 1 + src/parser/annotation.rs | 2 ++ src/parser/parse_annotation.rs | 1 + src/parser/parse_function.rs | 5 ++++- src/parser/parse_top_level.rs | 7 ++++++- src/resolve/function_search_ctx.rs | 15 ++++++++------- src/token.rs | 3 +++ 8 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/ast/function/mod.rs b/src/ast/function/mod.rs index 3c552c5b..a6e16c32 100644 --- a/src/ast/function/mod.rs +++ b/src/ast/function/mod.rs @@ -1,6 +1,6 @@ mod parameters; -use super::{Stmt, Type}; +use super::{Privacy, Stmt, Type}; use crate::{name::Name, source_files::Source, tag::Tag}; pub use parameters::{Parameter, Parameters}; @@ -14,4 +14,5 @@ pub struct Function { pub source: Source, pub abide_abi: bool, pub tag: Option, + pub privacy: Privacy, } diff --git a/src/lexer/identifier_state.rs b/src/lexer/identifier_state.rs index 288c31de..1ffcf3e4 100644 --- a/src/lexer/identifier_state.rs +++ b/src/lexer/identifier_state.rs @@ -42,6 +42,7 @@ impl IdentifierState { "define" => TokenKind::DefineKeyword, "zeroed" => TokenKind::ZeroedKeyword, "pragma" => TokenKind::PragmaKeyword, + "pub" => TokenKind::PubKeyword, _ => TokenKind::Identifier(identifier), } .at(self.start_source) diff --git a/src/parser/annotation.rs b/src/parser/annotation.rs index 211d52e9..e8d99612 100644 --- a/src/parser/annotation.rs +++ b/src/parser/annotation.rs @@ -20,6 +20,7 @@ pub enum AnnotationKind { Packed, AbideAbi, Namespace(String), + Pub, } impl AnnotationKind { @@ -36,6 +37,7 @@ impl Display for AnnotationKind { Self::Packed => "packed", Self::AbideAbi => "abide_abi", Self::Namespace(_) => "namespace", + Self::Pub => "pub", }) } } diff --git a/src/parser/parse_annotation.rs b/src/parser/parse_annotation.rs index 6dc17c46..3de3cdac 100644 --- a/src/parser/parse_annotation.rs +++ b/src/parser/parse_annotation.rs @@ -28,6 +28,7 @@ impl<'a, I: Inflow> Parser<'a, I> { let namespace = self.parse_identifier(Some("for namespace name"))?; AnnotationKind::Namespace(namespace) } + "pub" => AnnotationKind::Pub, _ => { return Err(ParseErrorKind::UnrecognizedAnnotation { name: annotation_name, diff --git a/src/parser/parse_function.rs b/src/parser/parse_function.rs index 17f79ba2..40678b2e 100644 --- a/src/parser/parse_function.rs +++ b/src/parser/parse_function.rs @@ -4,7 +4,7 @@ use super::{ Parser, }; use crate::{ - ast::{Function, Parameters, TypeKind}, + ast::{Function, Parameters, Privacy, TypeKind}, inflow::Inflow, name::Name, token::{Token, TokenKind}, @@ -18,12 +18,14 @@ impl<'a, I: Inflow> Parser<'a, I> { let mut is_foreign = false; let mut abide_abi = false; let mut namespace = None; + let mut privacy = Privacy::Private; for annotation in annotations { match annotation.kind { AnnotationKind::Foreign => is_foreign = true, AnnotationKind::AbideAbi => abide_abi = true, AnnotationKind::Namespace(new_namespace) => namespace = Some(new_namespace), + AnnotationKind::Pub => privacy = Privacy::Public, _ => return Err(self.unexpected_annotation(&annotation, Some("for function"))), } } @@ -66,6 +68,7 @@ impl<'a, I: Inflow> Parser<'a, I> { source, abide_abi, tag: None, + privacy, }) } } diff --git a/src/parser/parse_top_level.rs b/src/parser/parse_top_level.rs index 34dfee15..06e2abb1 100644 --- a/src/parser/parse_top_level.rs +++ b/src/parser/parse_top_level.rs @@ -1,5 +1,5 @@ use super::{ - annotation::Annotation, + annotation::{Annotation, AnnotationKind}, error::{ParseError, ParseErrorKind}, Parser, }; @@ -27,6 +27,11 @@ impl<'a, I: Inflow> Parser<'a, I> { self.ignore_newlines(); } + // Parse pub keyword + if self.input.peek().is_pub_keyword() { + annotations.push(AnnotationKind::Pub.at(self.input.advance().source)); + } + // Ignore newlines after annotations self.ignore_newlines(); diff --git a/src/resolve/function_search_ctx.rs b/src/resolve/function_search_ctx.rs index b3061b87..0d83f7e5 100644 --- a/src/resolve/function_search_ctx.rs +++ b/src/resolve/function_search_ctx.rs @@ -37,21 +37,22 @@ impl FunctionSearchCtx { } if name.namespace.is_empty() { - let mut matches = self.imported_namespaces.iter().filter_map(|namespace| { - self.available - .get(&ResolvedName::new(&Name::new( + let mut matches = self + .imported_namespaces + .iter() + .filter_map(|namespace| { + self.available.get(&ResolvedName::new(&Name::new( Some(namespace.to_string()), name.basename.clone(), ))) - .and_then(|list| list.first()) - .copied() - }); + }) + .flatten(); if let Some(found) = matches.next() { if matches.next().is_some() { return Err(FindFunctionError::Ambiguous); } else { - return Ok(found); + return Ok(*found); } } } diff --git a/src/token.rs b/src/token.rs index c26e96f0..b219f34b 100644 --- a/src/token.rs +++ b/src/token.rs @@ -80,6 +80,7 @@ pub enum TokenKind { DefineKeyword, ZeroedKeyword, PragmaKeyword, + PubKeyword, Member, Add, Subtract, @@ -168,6 +169,7 @@ impl Display for TokenKind { TokenKind::DefineKeyword => f.write_str("'define' keyword"), TokenKind::ZeroedKeyword => f.write_str("'zeroed' keyword"), TokenKind::PragmaKeyword => f.write_str("'pragma' keyword"), + TokenKind::PubKeyword => f.write_str("'pub' keyword"), TokenKind::Member => f.write_str("'.'"), TokenKind::Add => f.write_str("'+'"), TokenKind::Subtract => f.write_str("'-'"), @@ -294,6 +296,7 @@ impl TokenKind { | TokenKind::DefineKeyword | TokenKind::ZeroedKeyword | TokenKind::PragmaKeyword + | TokenKind::PubKeyword | TokenKind::OpenAngle | TokenKind::Comma | TokenKind::Colon