Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comments #28

Merged
merged 1 commit into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub type Result<T> = ::std::result::Result<T, Error>;
#[derive(Clone, Debug, PartialEq)]
pub enum Error {
Eof,
Syntax,
ExpectedArray,
ExpectedArrayEnd,
ExpectedBoolean,
Expand All @@ -40,6 +39,9 @@ pub enum Error {
Message(String),
Utf8Error(Utf8Error),
TrailingCharacters,

#[doc(hidden)]
__NonExhaustive,
}

impl fmt::Display for Error {
Expand Down Expand Up @@ -448,6 +450,7 @@ impl<'a, 'de> CommaSeparated<'a, 'de> {
Ok(self.de.bytes.peek().ok_or(Error::Eof)? != self.terminator)
} else {
let comma = self.de.bytes.comma();
self.de.bytes.skip_ws();

if self.de.bytes.peek().ok_or(Error::Eof)? == self.terminator {
Ok(false)
Expand Down Expand Up @@ -674,4 +677,14 @@ mod tests {
fn test_escape() {
assert_eq!("\"Quoted\"", from_str::<String>(r#""\"Quoted\"""#).unwrap());
}

#[test]
fn test_comment() {
assert_eq!(MyStruct { x: 1.0, y: 2.0 }, from_str("(
x: 1.0, // x is just 1
// There is another comment in the very next line..
// And y is indeed
y: 2.0 // 2!
)").unwrap());
}
}
16 changes: 16 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ impl<'a> Bytes<'a> {
while self.peek().map(|c| WHITE_SPACE.contains(&c)).unwrap_or(false) {
let _ = self.advance_single();
}

if self.skip_comment() {
self.skip_ws();
}
}

pub fn peek(&self) -> Option<u8> {
Expand Down Expand Up @@ -319,6 +323,18 @@ impl<'a> Bytes<'a> {

Ok(())
}

fn skip_comment(&mut self) -> bool {
if self.consume("//") {
let bytes = self.bytes.iter().take_while(|&&b| b != b'\n').count();

let _ = self.advance(bytes);

true
} else {
false
}
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down