aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/StringLit.scala
diff options
context:
space:
mode:
authorJack Koenig2017-09-22 15:36:35 -0700
committerGitHub2017-09-22 15:36:35 -0700
commit34e9944aaf3c1fc76fcaaacc02509f217c0c0d63 (patch)
tree387d82ae7d07861609d9f0e4cf7ac249d379f764 /src/main/scala/firrtl/StringLit.scala
parentf04a18efdf4ca88fe1ac77acab30e21290957919 (diff)
Fix string lit (#663)
Refactor StringLit to use String instead of Array[Byte]
Diffstat (limited to 'src/main/scala/firrtl/StringLit.scala')
-rw-r--r--src/main/scala/firrtl/StringLit.scala95
1 files changed, 0 insertions, 95 deletions
diff --git a/src/main/scala/firrtl/StringLit.scala b/src/main/scala/firrtl/StringLit.scala
deleted file mode 100644
index 206fe6a4..00000000
--- a/src/main/scala/firrtl/StringLit.scala
+++ /dev/null
@@ -1,95 +0,0 @@
-// See LICENSE for license details.
-
-package firrtl
-
-import firrtl.ir._
-
-import java.nio.charset.StandardCharsets.UTF_8
-import scala.annotation.tailrec
-
-// Default implementations are for valid FIRRTL Strings
-trait StringLitHandler {
-
- // Apply correct escaping for strings in a given language
- def escape(lit: StringLit): String = {
- @tailrec
- def escape(in: Array[Byte], str: String): String = {
- if (in.isEmpty) str
- else {
- val s = in.head match {
- case 0x5c => "\\\\"
- case 0x0a => "\\n"
- case 0x09 => "\\t"
- case 0x22 => "\\\""
- case c =>
- if (c >= 0x20 && c <= 0x7e) new String(Array(c), UTF_8)
- else throw new FIRRTLException(s"Unsupported byte in StringLit: $c")
- }
- escape(in.tail, str + s)
- }
- }
- escape(lit.array, "")
- }
-
- // Turn raw parsed strings into String Literals
- def unescape(raw: String): StringLit = {
- @tailrec
- def unescape(in: Array[Byte], out: Array[Byte]): Array[Byte] = {
- if (in.isEmpty) out
- else {
- val c = in.head
- if ((c < 0x20) || (c > 0x7e)) { // Range from ' ' to '~'
- val msg = "Unsupported unescaped UTF-8 Byte 0x%02x! ".format(c) +
- "Valid range [0x20-0x7e]"
- throw new InvalidStringLitException(msg)
- }
- // Handle escaped characters
- if (c == 0x5c) { // backslash
- val (n: Int, bytes: Array[Byte]) = in(1) match {
- case 0x5c => (2, Array[Byte](0x5c)) // backslash
- case 0x6e => (2, Array[Byte](0x0a)) // newline
- case 0x74 => (2, Array[Byte](0x09)) // tab
- case 0x27 => (2, Array[Byte](0x27)) // single quote
- case 0x22 => (2, Array[Byte](0x22)) // double quote
- // TODO Finalize supported escapes, implement hex2Bytes
- //case 0x78 => (4, hex2Bytes(in.slice(2, 3)))) // hex escape
- //case 0x75 => (6, hex2Bytes(in.slice(2, 5))) // unicode excape
- case e => // error
- val msg = s"Invalid escape character ${e.toChar}! " +
- "Valid characters [nt'\"\\]"
- throw new InvalidEscapeCharException(msg)
- }
- unescape(in.drop(n), out ++ bytes) // consume n
- } else {
- unescape(in.tail, out :+ in.head)
- }
- }
- }
-
- val byteArray = raw.getBytes(java.nio.charset.StandardCharsets.UTF_8)
- StringLit(unescape(byteArray, Array()))
- }
-
- // Translate from FIRRTL formatting to the specific language's formatting
- def format(lit: StringLit): StringLit = ???
- // Translate from the specific language's formatting to FIRRTL formatting
- def unformat(lit: StringLit): StringLit = ???
-}
-
-object FIRRTLStringLitHandler extends StringLitHandler
-
-object VerilogStringLitHandler extends StringLitHandler {
- // Change FIRRTL format string to Verilog format
- override def format(lit: StringLit): StringLit = {
- @tailrec
- def format(in: Array[Byte], out: Array[Byte], percent: Boolean): Array[Byte] = {
- if (in.isEmpty) out
- else {
- if (percent && in.head == 'x') format(in.tail, out :+ 'h'.toByte, percent = false)
- else format(in.tail, out :+ in.head, in.head == '%' && !percent)
- }
- }
- StringLit(format(lit.array, Array(), percent = false))
- }
-}
-