Browse Source

feat(parser): Gwei unit

Signed-off-by: Alexey Shekhirin <a.shekhirin@gmail.com>
Alexey Shekhirin 3 years ago
parent
commit
e6add7042d

+ 2 - 0
solang-parser/src/lexer.rs

@@ -163,6 +163,7 @@ pub enum Token<'input> {
     Hours,
     Days,
     Weeks,
+    Gwei,
     Wei,
     Szabo,
     Finney,
@@ -312,6 +313,7 @@ impl<'input> fmt::Display for Token<'input> {
             Token::Hours => write!(f, "hours"),
             Token::Days => write!(f, "days"),
             Token::Weeks => write!(f, "weeks"),
+            Token::Gwei => write!(f, "gwei"),
             Token::Wei => write!(f, "wei"),
             Token::Szabo => write!(f, "szabo"),
             Token::Finney => write!(f, "finney"),

+ 1 - 0
solang-parser/src/pt.rs

@@ -356,6 +356,7 @@ pub enum Unit {
     Days(Loc),
     Weeks(Loc),
     Wei(Loc),
+    Gwei(Loc),
     Szabo(Loc),
     Finney(Loc),
     Ether(Loc),

+ 2 - 0
solang-parser/src/solidity.lalrpop

@@ -522,6 +522,7 @@ Unit: Unit = {
     <@L> "days" <@R> => Unit::Days(Loc::File(file_no, <>)),
     <@L> "weeks" <@R> => Unit::Weeks(Loc::File(file_no, <>)),
     <@L> "wei" <@R> => Unit::Wei(Loc::File(file_no, <>)),
+    <@L> "gwei" <@R> => Unit::Gwei(Loc::File(file_no, <>)),
     <@L> "szabo" <@R> => Unit::Szabo(Loc::File(file_no, <>)),
     <@L> "finney" <@R> => Unit::Finney(Loc::File(file_no, <>)),
     <@L> "ether" <@R> => Unit::Ether(Loc::File(file_no, <>)),
@@ -1122,6 +1123,7 @@ extern {
         "days" => Token::Days,
         "weeks" => Token::Weeks,
         "wei" => Token::Wei,
+        "gwei" => Token::Gwei,
         "szabo" => Token::Szabo,
         "finney" => Token::Finney,
         "ether" => Token::Ether,

+ 2 - 0
src/sema/expression.rs

@@ -1964,6 +1964,7 @@ pub fn expression(
 
             match unit {
                 pt::Unit::Wei(loc)
+                | pt::Unit::Gwei(loc)
                 | pt::Unit::Finney(loc)
                 | pt::Unit::Szabo(loc)
                 | pt::Unit::Ether(loc)
@@ -1986,6 +1987,7 @@ pub fn expression(
                     pt::Unit::Days(_) => BigInt::from(60 * 60 * 24),
                     pt::Unit::Weeks(_) => BigInt::from(60 * 60 * 24 * 7),
                     pt::Unit::Wei(_) => BigInt::from(1),
+                    pt::Unit::Gwei(_) => BigInt::from(10).pow(9u32),
                     pt::Unit::Szabo(_) => BigInt::from(10).pow(12u32),
                     pt::Unit::Finney(_) => BigInt::from(10).pow(15u32),
                     pt::Unit::Ether(_) => BigInt::from(10).pow(18u32),

+ 1 - 0
tests/substrate_tests/primitives.rs

@@ -289,6 +289,7 @@ fn units() {
         contract test {
             function foo() public {
                 assert(10 wei == 10);
+                assert(1 gwei == 1000_000_000);
                 assert(1 szabo == 1000_000_000_000);
                 assert(1 finney == 1000_000_000_000_000);
                 assert(1 ether == 1000_000_000_000_000_000);