浏览代码

Fix panic when lexing ".9" at the beginning of a file (#1230)

Signed-off-by: Sean Young <sean@mess.org>
Sean Young 2 年之前
父节点
当前提交
95f58f5086
共有 1 个文件被更改,包括 21 次插入3 次删除
  1. 21 3
      solang-parser/src/lexer.rs

+ 21 - 3
solang-parser/src/lexer.rs

@@ -612,10 +612,10 @@ impl<'input> Lexer<'input> {
             self.chars.next();
         }
         let mut rational_end = end;
-        let mut end_before_rational = end;
+        let mut end_before_rational = end + 1;
         let mut rational_start = end;
         if is_rational {
-            end_before_rational = start - 1;
+            end_before_rational = start;
             rational_start = start + 1;
         }
 
@@ -666,7 +666,7 @@ impl<'input> Lexer<'input> {
         }
 
         if is_rational {
-            let integer = &self.input[start..=end_before_rational];
+            let integer = &self.input[start..end_before_rational];
             let fraction = &self.input[rational_start..=rational_end];
             let exp = &self.input[exp_start..=end];
 
@@ -1705,4 +1705,22 @@ fn lexertest() {
             'g'
         ),)
     );
+
+    let mut errors = Vec::new();
+    let tokens = Lexer::new(".9", 0, &mut comments, &mut errors)
+        .collect::<Vec<Result<(usize, Token, usize), LexicalError>>>();
+
+    assert_eq!(
+        tokens,
+        vec!(Ok((0, Token::RationalNumber("", "9", ""), 2)),)
+    );
+
+    let mut errors = Vec::new();
+    let tokens = Lexer::new(".9e10", 0, &mut comments, &mut errors)
+        .collect::<Vec<Result<(usize, Token, usize), LexicalError>>>();
+
+    assert_eq!(
+        tokens,
+        vec!(Ok((0, Token::RationalNumber("", "9", "10"), 5)),)
+    );
 }