From b2d0729ca855af30ac23ccffe003195248c96972 Mon Sep 17 00:00:00 2001 From: Windel Bouwman Date: Fri, 2 Aug 2019 19:08:59 +0200 Subject: [PATCH] Fix lexing of 1else. --- parser/src/lexer.rs | 27 +++++++++++++++++++++++---- tests/snippets/floats.py | 4 ++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/parser/src/lexer.rs b/parser/src/lexer.rs index 6c12e5868..49cc93543 100644 --- a/parser/src/lexer.rs +++ b/parser/src/lexer.rs @@ -67,6 +67,7 @@ pub struct Lexer> { pending: Vec, chr0: Option, chr1: Option, + chr2: Option, location: Location, keywords: HashMap, } @@ -250,10 +251,12 @@ where chr0: None, location: Location::new(0, 0), chr1: None, + chr2: None, keywords: get_keywords(), }; lxr.next_char(); lxr.next_char(); + lxr.next_char(); // Start at top row (=1) left column (=1) lxr.location.reset(); lxr @@ -368,7 +371,7 @@ where } // If float: - if self.chr0 == Some('.') || self.chr0 == Some('e') { + if self.chr0 == Some('.') || self.at_exponent() { // Take '.': if self.chr0 == Some('.') { value_text.push(self.next_char().unwrap()); @@ -378,8 +381,8 @@ where } // 1e6 for example: - if self.chr0 == Some('e') { - value_text.push(self.next_char().unwrap()); + if self.chr0 == Some('e') || self.chr0 == Some('E') { + value_text.push(self.next_char().unwrap().to_ascii_lowercase()); // Optional +/- if self.chr0 == Some('-') || self.chr0 == Some('+') { @@ -423,6 +426,21 @@ where } } + /// Test if we face '[eE][-+]?[0-9]+' + fn at_exponent(&self) -> bool { + match self.chr0 { + Some('e') | Some('E') => match self.chr1 { + Some('+') | Some('-') => match self.chr2 { + Some('0'..='9') => true, + _ => false, + }, + Some('0'..='9') => true, + _ => false, + }, + _ => false, + } + } + /// Skip everything until end of line fn lex_comment(&mut self) { self.next_char(); @@ -1196,7 +1214,8 @@ where let c = self.chr0; let nxt = self.chars.next(); self.chr0 = self.chr1; - self.chr1 = nxt; + self.chr1 = self.chr2; + self.chr2 = nxt; if c == Some('\n') { self.location.newline(); } else { diff --git a/tests/snippets/floats.py b/tests/snippets/floats.py index 101d58cc9..0200e5a36 100644 --- a/tests/snippets/floats.py +++ b/tests/snippets/floats.py @@ -214,3 +214,7 @@ assert float('nan').hex() == 'nan' #for _ in range(10000): # f = random.random() * random.randint(0, 0x10000000000000000) # assert f == float.fromhex(f.hex()) + +# Test float exponent: +assert 1 if 1else 0 == 1 +