Skip to content

Commit

Permalink
feat(comment): support single-line simple prefix comments (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
pinbraerts authored Feb 1, 2025
1 parent 97776f3 commit 6255eb9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ pub enum Paired {
File, // start, end
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Comment {
Oneline, // // or # or ;
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Lexem {
String,
Expand All @@ -14,6 +19,7 @@ pub enum Lexem {
Colon,
Else,
WhiteSpace,
Comment(Comment),
}

fn joinable(lexem: Lexem) -> bool {
Expand All @@ -39,6 +45,7 @@ impl From<u8> for Lexem {
b',' => Lexem::Comma,
b':' | b'=' => Lexem::Colon,
b'\'' | b'"' | b'`' => Lexem::String,
b';' | b'#' | b'/' => Lexem::Comment(Comment::Oneline),
_ => {
if character.is_ascii_whitespace() {
Lexem::WhiteSpace
Expand Down Expand Up @@ -158,6 +165,17 @@ impl Lexer {
if cfg!(debug_assertions) {
eprintln!("{character} {}", character as char);
}
if let Some(Lexem::Comment(Comment::Oneline)) = self.lexem {
if character == b'\r' || character == b'\n' {
self.lexem = Some(Lexem::WhiteSpace);
return Some((
Lexem::Comment(Comment::Oneline),
std::mem::replace(&mut self.state, vec![character]),
));
}
self.state.push(character);
return None;
}
if let Some(Lexem::String) = self.lexem {
let first_char = self.state.first().cloned().unwrap_or_default();
let last_char = self.state.last().cloned().unwrap_or_default();
Expand Down
3 changes: 3 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ impl Parser {
eprintln!("LEXEM {lexem:?} {value:?}");
}
let mut result = Vec::new();
if let Lexem::Comment(_) = lexem {
return result;
}
if let Lexem::WhiteSpace = lexem {
self.whitespace.extend(value);
return result;
Expand Down
5 changes: 5 additions & 0 deletions tests/input/strip_comment.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
// c-style oneline comment
# python-style oneline comment
; assembler-style comment
}
5 changes: 5 additions & 0 deletions tests/output/strip_comment.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{



}

0 comments on commit 6255eb9

Please sign in to comment.