Skip to content

Commit

Permalink
feat(Parser): skip comments along with whitespace and commas
Browse files Browse the repository at this point in the history
Fixes #4.
  • Loading branch information
utkarshkukreti committed Aug 3, 2018
1 parent 42f24fa commit eaeaab8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'a> Parser<'a> {
}

pub fn read(&mut self) -> Option<Result<Value, Error>> {
self.advance_while(|ch| ch.is_whitespace() || ch == ',');
self.whitespace();

self.chars.clone().next().map(|(pos, ch)| match (pos, ch) {
(start, '0'...'9') => {
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<'a> Parser<'a> {
self.chars.next();
let mut items = vec![];
loop {
self.advance_while(|ch| ch.is_whitespace() || ch == ',');
self.whitespace();

if self.peek() == Some(close) {
self.chars.next();
Expand Down Expand Up @@ -201,7 +201,8 @@ impl<'a> Parser<'a> {
let close = '}';
let mut items = vec![];
loop {
self.advance_while(|ch| ch.is_whitespace() || ch == ',');
self.whitespace();

if self.peek() == Some(close) {
self.chars.next();
return Ok(Value::Set(items.into_iter().collect()));
Expand Down Expand Up @@ -264,6 +265,21 @@ impl<'a> Parser<'a> {
self.chars.clone().next().map(|(_, ch)| ch)
}

fn whitespace(&mut self) {
loop {
// Skip whitespace.
self.advance_while(|ch| ch.is_whitespace() || ch == ',');
// Skip comment if present.
if self.chars.clone().next().map_or(false, |(_, ch)| ch == ';') {
self.advance_while(|ch| ch != '\n');
self.chars.next();
} else {
// Otherwise we're done.
return;
}
}
}

fn advance_while<F: FnMut(char) -> bool>(&mut self, mut f: F) -> usize {
loop {
match self.chars.clone().next() {
Expand Down
25 changes: 25 additions & 0 deletions tests/parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,28 @@ fn test_tagged_values() {

assert_eq!(parser.read(), None);
}

#[test]
fn test_comments() {
use std::collections::BTreeMap;

let mut parser = Parser::new(
"
; 0
;; ;
0
--;0
+0
[;[]
]
{;}
}
",
);
assert_eq!(parser.read(), Some(Ok(Value::Integer(0))));
assert_eq!(parser.read(), Some(Ok(Value::Symbol("--".into()))));
assert_eq!(parser.read(), Some(Ok(Value::Integer(0))));
assert_eq!(parser.read(), Some(Ok(Value::Vector(Vec::new()))));
assert_eq!(parser.read(), Some(Ok(Value::Map(BTreeMap::new()))));
assert_eq!(parser.read(), None);
}

0 comments on commit eaeaab8

Please sign in to comment.