diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-07-08 19:12:07 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-07-08 19:12:07 +0200 |
| commit | 5a500fb8a7c0ba4b8a59e2622c8cbafdc4ce1fe9 (patch) | |
| tree | 5c22b012cf2c37a96735d6ec84ad19e852259e51 /src/parse | |
| parent | c5635d8a3f45865619d66bc9e296da7d9e9efa5a (diff) | |
Range operator
Diffstat (limited to 'src/parse')
| -rw-r--r-- | src/parse/scanner.rs | 10 | ||||
| -rw-r--r-- | src/parse/tokens.rs | 10 |
2 files changed, 13 insertions, 7 deletions
diff --git a/src/parse/scanner.rs b/src/parse/scanner.rs index d2c2efed..1a0e3045 100644 --- a/src/parse/scanner.rs +++ b/src/parse/scanner.rs @@ -86,11 +86,6 @@ impl<'s> Scanner<'s> { self.rest().chars().next() } - /// Peek at the nth-next char without consuming anything. - pub fn peek_nth(&self, n: usize) -> Option<char> { - self.rest().chars().nth(n) - } - /// Checks whether the next char fulfills a condition. /// /// Returns `false` if there is no next char. @@ -101,6 +96,11 @@ impl<'s> Scanner<'s> { self.peek().map(f).unwrap_or(false) } + /// Checks whether the remaining source starts with the given string. + pub fn starts_with(&self, string: &str) -> bool { + self.rest().starts_with(string) + } + /// The previous index in the source string. pub fn last_index(&self) -> usize { self.eaten() diff --git a/src/parse/tokens.rs b/src/parse/tokens.rs index 522b3136..c9b7dc21 100644 --- a/src/parse/tokens.rs +++ b/src/parse/tokens.rs @@ -216,7 +216,7 @@ impl<'s> Tokens<'s> { self.s.eat_assert(c); Token::Text(&self.s.eaten_from(start)) } - 'u' if self.s.peek_nth(1) == Some('{') => { + 'u' if self.s.starts_with("u{") => { self.s.eat_assert('u'); self.s.eat_assert('{'); Token::UnicodeEscape(UnicodeEscapeToken { @@ -366,7 +366,8 @@ impl<'s> Tokens<'s> { self.s.eat_while(|c| c.is_ascii_digit()); // Read the fractional part if not already done. - if c != '.' && self.s.eat_if('.') { + // Make sure not to confuse a range for the decimal separator. + if c != '.' && !self.s.starts_with("..") && self.s.eat_if('.') { self.s.eat_while(|c| c.is_ascii_digit()); } @@ -905,6 +906,11 @@ mod tests { t!(Code[" /"]: format!("{}{}", s, suffix) => build(v)); } } + + // Multiple dots close the number. + t!(Code[" /"]: "1..2" => Int(1), Dots, Int(2)); + t!(Code[" /"]: "1..2.3" => Int(1), Dots, Float(2.3)); + t!(Code[" /"]: "1.2..3" => Float(1.2), Dots, Int(3)); } #[test] |
