Skip to content

Commit

Permalink
Merge pull request #48 from nix-community/float-starting-with-dot
Browse files Browse the repository at this point in the history
Floats without trailing zeros (e.g. `.5`) are valid Nix code
  • Loading branch information
Ma27 authored Nov 10, 2021
2 parents c917751 + a168ecb commit fa6ddde
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,14 @@ impl<'a> Iterator for Tokenizer<'a> {
'@' => Some((TOKEN_AT, self.string_since(start))),
':' => Some((TOKEN_COLON, self.string_since(start))),
',' => Some((TOKEN_COMMA, self.string_since(start))),
'.' => Some((TOKEN_DOT, self.string_since(start))),
'.' => {
if self.peek().map(|x| x >= '0' && x <= '9').unwrap_or(false) {
self.consume(|c| c >= '0' && c <= '9');
Some((TOKEN_FLOAT, self.string_since(start)))
} else {
Some((TOKEN_DOT, self.string_since(start)))
}
},
'=' => Some((TOKEN_ASSIGN, self.string_since(start))),
'?' => Some((TOKEN_QUESTION, self.string_since(start))),
';' => Some((TOKEN_SEMICOLON, self.string_since(start))),
Expand Down Expand Up @@ -486,6 +493,16 @@ mod tests {
(TOKEN_CURLY_B_CLOSE, "}"),
],
);
assert_eq!(
tokenize(".5 + 0.5"),
tokens![
(TOKEN_FLOAT, ".5"),
(TOKEN_WHITESPACE, " "),
(TOKEN_ADD, "+"),
(TOKEN_WHITESPACE, " "),
(TOKEN_FLOAT, "0.5"),
],
);
assert_eq!(
tokenize("{ scientific = 1.1e4; uppercase = 123.4E-2; }"),
tokens![
Expand Down

0 comments on commit fa6ddde

Please sign in to comment.