Skip to content

Commit

Permalink
Reader can skip comments
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sin committed Feb 6, 2020
1 parent 75db136 commit c816a43
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/tapirlisp/sexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,29 @@ impl Error for ReadError {
}

fn skip_whitespaces(chars: &mut Peekable<Chars>) {
let mut comment = false;
loop {
let ch = chars.peek();
match ch {
Some(';') => {
comment = true;
chars.next();
}
Some(c) => {
if c.is_whitespace() {
if !comment && !c.is_whitespace() {
break;
} else if comment && *c == '\n' {
chars.next();
} else {
break;
} else {
chars.next();
}
}
_ => break,
}
}
}

fn skip_comment(chars: &mut Peekable<Chars>) {
loop {
let ch = chars.peek();
match ch {
Some('\n') => break,
None => break,
_ => chars.next(),
};
}
}

fn read_symbol(chars: &mut Peekable<Chars>) -> Result<Cons, ReadError> {
let mut name = String::new();
loop {
Expand Down Expand Up @@ -127,12 +124,9 @@ fn read_list(chars: &mut Peekable<Chars>) -> Result<Cons, ReadError> {
fn read_exp(chars: &mut Peekable<Chars>) -> Result<Cons, ReadError> {
skip_whitespaces(chars);
let ch = chars.peek();

match ch {
None => Ok(Cons::Nil),
Some(';') => {
skip_comment(chars);
Ok(Cons::Nil)
}
Some(')') => Err(ReadError::UnexpectedCloseParen),
Some('(') => read_list(chars),
Some(c) => {
Expand Down

0 comments on commit c816a43

Please sign in to comment.