aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/annotations/AnnotationYamlProtocol.scala
blob: 9018d494860a520c4b3eda4a1d4ff8367563a31c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// See LICENSE for license details.

package firrtl
package annotations

import net.jcazevedo.moultingyaml._

object AnnotationYamlProtocol extends DefaultYamlProtocol {
  // bottom depends on top
  implicit object AnnotationYamlFormat extends YamlFormat[Annotation] {
    def write(a: Annotation) = YamlObject(
      YamlString("targetString") -> YamlString(a.targetString),
      YamlString("transformClass") -> YamlString(a.transformClass),
      YamlString("value") -> YamlString(a.value)
    )

    def read(yamlValue: YamlValue): Annotation = {
      try {
        yamlValue.asYamlObject.getFields(
          YamlString("targetString"),
          YamlString("transformClass"),
          YamlString("value")) match {
          case Seq(YamlString(targetString), YamlString(transformClass), YamlString(value)) =>
            Annotation(
              toTarget(targetString), Class.forName(transformClass).asInstanceOf[Class[_ <: Transform]], value)
          case _ => deserializationError("Annotation expected")
        }
      }
      catch {
        case annotationException: AnnotationException =>
          Utils.error(
            s"Error: ${annotationException.getMessage} while parsing annotation from yaml\n${yamlValue.prettyPrint}")
        case annotationException: FIRRTLException =>
          Utils.error(
            s"Error: ${annotationException.getMessage} while parsing annotation from yaml\n${yamlValue.prettyPrint}")
      }
    }
    def toTarget(string: String): Named = string.split("""\.""", -1).toSeq match {
      case Seq(c) => CircuitName(c)
      case Seq(c, m) => ModuleName(m, CircuitName(c))
      case Nil => Utils.error("BAD")
      case s =>
        val componentString = s.drop(2).mkString(".")
        ComponentName(componentString, ModuleName(s.tail.head, CircuitName(s.head)))
    }
  }
}