Skip to content

Commit

Permalink
feat: add new syntax entity - comment doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Ph0enixKM committed Jan 31, 2024
1 parent 6aaca87 commit c861d41
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/modules/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl SyntaxModule<ParserMetadata> for Block {
continue;
}
// Handle comments
if token.word.starts_with("//") {
if token.word.starts_with("//") && !token.word.starts_with("///") {
meta.increment_index();
continue
}
Expand Down Expand Up @@ -82,4 +82,4 @@ impl TranslateModule for Block {
std::mem::swap(&mut meta.stmt_queue, &mut new_queue);
result
}
}
}
68 changes: 68 additions & 0 deletions src/modules/statement/comment_doc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use heraclitus_compiler::prelude::*;
use itertools::Itertools;
use crate::utils::metadata::ParserMetadata;
use crate::translate::module::TranslateModule;

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

impl SyntaxModule<ParserMetadata> for CommentDoc {
syntax_name!("Parenthesis");

fn new() -> Self {
CommentDoc {
value: String::new()
}
}

fn parse(&mut self, meta: &mut ParserMetadata) -> SyntaxResult {
match meta.get_current_token() {
Some(token) => {
let mut col = token.pos.0;
if token.word.starts_with("///") {
self.value = token.word[3..].trim().to_string();
meta.increment_index();
while let Some(token) = meta.get_current_token() {
let is_token_underneeth = token.pos.0 == col + 1;
let last_char = self.value.chars().last().unwrap_or_else(|| '\n');
// If the token is a newline, we add a newline to the comment
if token.word.starts_with("\n") {
self.value.push_str("\n");
meta.increment_index();
continue;
}
if token.word.starts_with("///") && is_token_underneeth {
// Update the column of the last comment
col = token.pos.0;
meta.increment_index();
// If the comment signifies a paragrah break, we add two newlines
if token.word[3..].trim().len() == 0 {
if last_char == '\n' {
continue;
}
self.value.push_str("\n\n");
continue;
}
let delimiter = if last_char == '\n' { "" } else { " " };
self.value.push_str(&format!("{}{}", delimiter, token.word[3..].trim()));
} else {
break;
}
}
Ok(())
} else {
return Err(Failure::Quiet(PositionInfo::from_token(meta, meta.get_current_token())))
}
}
None => return Err(Failure::Quiet(PositionInfo::from_token(meta, meta.get_current_token())))
}
}
}

impl TranslateModule for CommentDoc {
fn translate(&self, _meta: &mut crate::utils::TranslateMetadata) -> String {
format!("# {}", self.value.trim().split('\n').join("\n# "))
}
}
3 changes: 2 additions & 1 deletion src/modules/statement/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod stmt;
pub mod stmt;
pub mod comment_doc;
20 changes: 10 additions & 10 deletions src/modules/statement/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ use crate::modules::function::{
ret::Return,
fail::Fail
};
use crate::modules::imports::{
import::Import
};
use crate::modules::imports::import::Import;
use crate::modules::main::Main;
use crate::modules::builtin::{
echo::Echo
};
use crate::modules::builtin::echo::Echo;
use super::comment_doc::CommentDoc;

#[derive(Debug, Clone)]
pub enum StatementType {
Expand All @@ -65,7 +62,8 @@ pub enum StatementType {
Import(Import),
Main(Main),
Echo(Echo),
CommandModifier(CommandModifier)
CommandModifier(CommandModifier),
CommentDoc(CommentDoc)
}

#[derive(Debug, Clone)]
Expand All @@ -90,7 +88,9 @@ impl Statement {
ShorthandMul, ShorthandDiv,
ShorthandModulo,
// Command
CommandModifier, CommandStatement, Echo,
CommandModifier, CommandStatement, Echo,
// Comment doc
CommentDoc,
// Expression
Expr
]);
Expand All @@ -104,7 +104,7 @@ impl Statement {
match syntax(meta, &mut module) {
Ok(()) => {
self.value = Some(cb(module));
Ok(())
Ok(())
}
Err(details) => Err(details)
}
Expand All @@ -126,7 +126,7 @@ impl SyntaxModule<ParserMetadata> for Statement {
for statement in statements {
// Handle comments
if let Some(token) = meta.get_current_token() {
if token.word.starts_with("//") {
if token.word.starts_with("//") && !token.word.starts_with("///") {
meta.increment_index();
continue
}
Expand Down

0 comments on commit c861d41

Please sign in to comment.