-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new syntax entity - comment doc
- Loading branch information
Showing
4 changed files
with
82 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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# ")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod stmt; | ||
pub mod stmt; | ||
pub mod comment_doc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters