Skip to content

Commit

Permalink
feat(lexer): null value lexing
Browse files Browse the repository at this point in the history
* including tests for normal null values and invalid ones
  • Loading branch information
Byron committed May 6, 2015
1 parent d4782c8 commit dc2f9a2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,37 @@ impl<I> Iterator for Lexer<I>
let mut first = 0;
let prev_end = self.prev_end;

let mut nb = ['x', 'x', 'x', 'x']; // null buffer
let mut nbi = 0usize; // null buffer index

for c in self.chars.by_ref() {
lcid += 1;

let mut set_cursor = || {
first = prev_end + lcid - 1;
};

if nbi > 0 {
nb[nbi] = c;
if nbi == 3 {
// we know nb[0] is 'n'
if nb[1] == 'u' && nb[2] == 'l' && nb[3] == 'l' {
t = TokenType::NullValue;
}
break;
} else {
nbi += 1;
}
}

match c {
'\\' => {
if in_str {
ign_next = true;
continue; // this is allowed only here !
} else {
// invalid
assert_eq!(t, TokenType::Invalid);
debug_assert!(t == TokenType::Invalid);
set_cursor();
break
}
Expand All @@ -124,6 +140,14 @@ impl<I> Iterator for Lexer<I>
break;
}
},
'n' => {
if !in_str {
debug_assert!(nbi == 0);
nb[0] = c;
nbi = 1;
set_cursor();
}
}
'"' => {
if !ign_next {
if in_str {
Expand Down Expand Up @@ -153,7 +177,7 @@ impl<I> Iterator for Lexer<I>
if in_str {
t = TokenType::Invalid;
}

self.prev_end = self.prev_end + lcid;
Some(Token {
kind: t,
Expand Down
21 changes: 21 additions & 0 deletions tests/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ fn unclosed_string_value() {
}


#[test]
fn null_value() {
let src = r#"{"n":null}"#;
let mut it = Lexer::new(src.chars());

assert_eq!(it.by_ref().skip(3).next(), Some(Token { kind: TokenType::NullValue,
span: Span { first: 5,
end: 9 } }));
}

#[test]
fn unclosed_null_value() {
let src = r#"{"n":nuxl}"#;
let mut it = Lexer::new(src.chars());

// invalid null characters cause it to be invalid
assert_eq!(it.by_ref().skip(3).next(), Some(Token { kind: TokenType::Invalid,
span: Span { first: 5,
end: 9 } }));
}

#[test]
fn multi_line_strings() {
// Add code here
Expand Down

0 comments on commit dc2f9a2

Please sign in to comment.