diff --git a/compiler/CHANGELOG.md b/compiler/CHANGELOG.md index cd457e3..d817dc5 100644 --- a/compiler/CHANGELOG.md +++ b/compiler/CHANGELOG.md @@ -13,7 +13,8 @@ ## v0.0.5 -- ... +- Scan single line comments +- Refactor String token to include double quotes (") in its content ## v0.0.4 diff --git a/doc-generator/src/highlighter/mod.rs b/doc-generator/src/highlighter/mod.rs index b1d8d37..c994ece 100644 --- a/doc-generator/src/highlighter/mod.rs +++ b/doc-generator/src/highlighter/mod.rs @@ -1,5 +1,22 @@ use misti::TokenType; +#[macro_export] +macro_rules! replace { + ($classes:literal, $token:ident, $offset:ident, $output:ident) => { + { + let start_pos = $token.position; + let end_pos = $token.get_end_position(); + + let range = (start_pos + $offset)..(end_pos + $offset); + let html = format!("{}", $classes, $token.value); + + $offset += 28 + $classes.len(); + + $output.replace_range(range, html.as_str()); + } + }; +} + pub fn highlight(input: &String) -> String { // The tokens come in order let tokens = misti::tokenize(&input); @@ -19,29 +36,8 @@ pub fn highlight(input: &String) -> String { for token in tokens.unwrap() { match &token.token_type { - TokenType::Datatype => { - let start_pos = token.position; - let end_pos = token.get_end_position(); - - let range = (start_pos + offset)..(end_pos + offset); - let html = format!("{}", token.value); - - // 38 is the number of extra characters added to the token - offset += 38; - - output.replace_range(range, html.as_str()); - } - TokenType::Number => { - let start_pos = token.position; - let end_pos = token.get_end_position(); - - let range = (start_pos + offset)..(end_pos + offset); - let html = format!("{}", token.value); - - offset += 34; - - output.replace_range(range, html.as_str()); - } + TokenType::Datatype => replace!("class-name", token, offset, output), + TokenType::Number => replace!("number", token, offset, output), TokenType::String => { let start_pos = token.position; let end_pos = token.get_end_position(); @@ -53,17 +49,8 @@ pub fn highlight(input: &String) -> String { output.replace_range(range, html.as_str()); } - TokenType::Comment => { - let start_pos = token.position; - let end_pos = token.get_end_position(); - - let range = (start_pos + offset)..(end_pos + offset); - let html = format!("{}", token.value); - - offset += 35; - - output.replace_range(range, html.as_str()); - } + TokenType::Comment => replace!("comment", token, offset, output), + TokenType::VAL | TokenType::VAR => replace!("keyword", token, offset, output), _ => {} } } @@ -71,6 +58,7 @@ pub fn highlight(input: &String) -> String { output } + #[cfg(test)] mod tests { use super::*;