diff options
| author | Albert Magyar | 2020-03-25 12:24:58 -0700 |
|---|---|---|
| committer | Albert Magyar | 2020-03-26 11:14:28 -0700 |
| commit | 4b3b5442bfe34502862eb070854aeef1e0cfc9c4 (patch) | |
| tree | 49e09570ba220679ca51249ea9cb83f101ef66fa /src/main | |
| parent | 9995f081291d974a6b5ad319e67a9d19cdc4a56d (diff) | |
Support octal and binary literal formats as described in the spec
* Fixes #1464
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/antlr4/FIRRTL.g4 | 20 | ||||
| -rw-r--r-- | src/main/scala/firrtl/Visitor.scala | 4 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/main/antlr4/FIRRTL.g4 b/src/main/antlr4/FIRRTL.g4 index 0035423b..39988e18 100644 --- a/src/main/antlr4/FIRRTL.g4 +++ b/src/main/antlr4/FIRRTL.g4 @@ -186,6 +186,8 @@ intLit : UnsignedInt | SignedInt | HexLit + | OctalLit + | BinaryLit ; lowerBound @@ -320,6 +322,14 @@ HexLit : '"' 'h' ( '+' | '-' )? ( HexDigit )+ '"' ; +OctalLit + : '"' 'o' ( '+' | '-' )? ( OctalDigit )+ '"' + ; + +BinaryLit + : '"' 'b' ( '+' | '-' )? ( BinaryDigit )+ '"' + ; + DoubleLit : ( '+' | '-' )? Digit+ '.' Digit+ ( 'E' ( '+' | '-' )? Digit+ )? ; @@ -334,6 +344,16 @@ HexDigit : [a-fA-F0-9] ; +fragment +OctalDigit + : [0-7] + ; + +fragment +BinaryDigit + : [01] + ; + StringLit : '"' UnquotedString? '"' ; diff --git a/src/main/scala/firrtl/Visitor.scala b/src/main/scala/firrtl/Visitor.scala index 112343d1..084a3006 100644 --- a/src/main/scala/firrtl/Visitor.scala +++ b/src/main/scala/firrtl/Visitor.scala @@ -28,6 +28,8 @@ class Visitor(infoMode: InfoMode) extends AbstractParseTreeVisitor[FirrtlNode] w // These regex have to change if grammar changes private val HexPattern = """\"*h([+\-]?[a-zA-Z0-9]+)\"*""".r + private val OctalPattern = """\"*o([+\-]?[0-7]+)\"*""".r + private val BinaryPattern = """\"*b([+\-]?[01]+)\"*""".r private val DecPattern = """([+\-]?[1-9]\d*)""".r private val ZeroPattern = "0".r private val DecimalPattern = """([+\-]?[0-9]\d*\.[0-9]\d*)""".r @@ -37,6 +39,8 @@ class Visitor(infoMode: InfoMode) extends AbstractParseTreeVisitor[FirrtlNode] w s match { case ZeroPattern(_*) => BigInt(0) case HexPattern(hexdigits) => BigInt(hexdigits, 16) + case OctalPattern(octaldigits) => BigInt(octaldigits, 8) + case BinaryPattern(binarydigits) => BigInt(binarydigits, 2) case DecPattern(num) => BigInt(num, 10) case _ => throw new Exception("Invalid String for conversion to BigInt " + s) } |
