Skip to content

Commit

Permalink
feat: add comment detection to function declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ph0enixKM committed Feb 3, 2024
1 parent c861d41 commit 4a9628c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/modules/function/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::mem::swap;

use heraclitus_compiler::prelude::*;
use itertools::izip;
use crate::modules::statement::comment_doc::CommentDoc;
use crate::modules::types::Type;
use crate::modules::variable::variable_name_extensions;
use crate::utils::cc_flags::get_ccflag_by_name;
Expand All @@ -24,7 +25,8 @@ pub struct FunctionDeclaration {
pub returns: Type,
pub body: Block,
pub id: usize,
pub is_public: bool
pub is_public: bool,
pub comment: Option<CommentDoc>
}

impl FunctionDeclaration {
Expand Down Expand Up @@ -61,11 +63,18 @@ impl SyntaxModule<ParserMetadata> for FunctionDeclaration {
returns: Type::Generic,
body: Block::new(),
id: 0,
is_public: false
is_public: false,
comment: None
}
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
// Parse the function comment
if is_functions_comment_doc(meta) {
let mut comment = CommentDoc::new();
syntax(meta, &mut comment)?;
self.comment = Some(comment);
}
let mut flags = HashSet::new();
// Get all the user-defined compiler flags
while let Ok(flag) = token_by(meta, |val| val.starts_with("#[")) {
Expand Down Expand Up @@ -168,7 +177,7 @@ impl TranslateModule for FunctionDeclaration {
// Parse the function body
result.push(format!("function {name} {{"));
if let Some(args) = self.set_args_as_variables(meta, function, &self.arg_refs) {
result.push(args);
result.push(args);
}
result.push(function.block.translate(meta));
result.push(meta.gen_indent() + "}");
Expand All @@ -178,4 +187,4 @@ impl TranslateModule for FunctionDeclaration {
// Return the translation
result.join("\n")
}
}
}
39 changes: 38 additions & 1 deletion src/modules/function/declaration_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,43 @@ pub fn skip_function_body(meta: &mut ParserMetadata) -> (usize, usize, bool) {
(index_begin, index_end, is_failable)
}

pub fn is_functions_comment_doc(meta: &mut ParserMetadata) -> bool {
let index = meta.get_index();
let mut is_comment_doc = true;
// Multiple linebreaks are merged by heraclitus, so we need to check for them
let mut last_line = 0;
if let Some(tok) = meta.get_current_token() {
if !tok.word.starts_with("///") {
return false;
}
}
while let Some(tok) = meta.get_current_token() {
// If there was a longer line break, it means the comment ended
if !is_comment_doc && tok.pos.0 != last_line + 1 {
meta.set_index(index);
return false;
}
if tok.word.starts_with("///") {
is_comment_doc = true;
}
if tok.word.starts_with("\n") {
if is_comment_doc {
is_comment_doc = false;
last_line = tok.pos.0;
} else {
meta.set_index(index);
return false;
}
}
if tok.word.starts_with("fun") {
meta.set_index(index);
return true;
}
meta.increment_index();
}
false
}

pub fn handle_existing_function(meta: &mut ParserMetadata, tok: Option<Token>) -> Result<(), Failure> {
let name = tok.as_ref().unwrap().word.clone();
handle_identifier_name(meta, &name, tok.clone())?;
Expand Down Expand Up @@ -60,4 +97,4 @@ pub fn handle_add_function(meta: &mut ParserMetadata, tok: Option<Token>, fun: F
// If the function already exists, show an error
None => error!(meta, tok, format!("Function '{}' already exists", name))
}
}
}
2 changes: 1 addition & 1 deletion src/modules/statement/comment_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::translate::module::TranslateModule;

#[derive(Debug, Clone)]
pub struct CommentDoc {
value: String
pub value: String
}

impl SyntaxModule<ParserMetadata> for CommentDoc {
Expand Down

0 comments on commit 4a9628c

Please sign in to comment.