diff --git a/src/tapirlisp/sexp.rs b/src/tapirlisp/sexp.rs index 7d01b02..072f3c8 100644 --- a/src/tapirlisp/sexp.rs +++ b/src/tapirlisp/sexp.rs @@ -36,14 +36,22 @@ impl Error for ReadError { } fn skip_whitespaces(chars: &mut Peekable) { + 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, @@ -51,17 +59,6 @@ fn skip_whitespaces(chars: &mut Peekable) { } } -fn skip_comment(chars: &mut Peekable) { - loop { - let ch = chars.peek(); - match ch { - Some('\n') => break, - None => break, - _ => chars.next(), - }; - } -} - fn read_symbol(chars: &mut Peekable) -> Result { let mut name = String::new(); loop { @@ -127,12 +124,9 @@ fn read_list(chars: &mut Peekable) -> Result { fn read_exp(chars: &mut Peekable) -> Result { 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) => {