diff options
Diffstat (limited to 'src/test/scala/firrtlTests/execution/ParserHelpers.scala')
| -rw-r--r-- | src/test/scala/firrtlTests/execution/ParserHelpers.scala | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/test/scala/firrtlTests/execution/ParserHelpers.scala b/src/test/scala/firrtlTests/execution/ParserHelpers.scala new file mode 100644 index 00000000..3472c19c --- /dev/null +++ b/src/test/scala/firrtlTests/execution/ParserHelpers.scala @@ -0,0 +1,52 @@ +package firrtlTests.execution + +import firrtl._ +import firrtl.ir._ + +class ParserHelperException(val pe: ParserException, input: String) + extends FirrtlUserException(s"Got error ${pe.toString} while parsing input:\n${input}") + +/** + * A utility class that parses a FIRRTL string representing a statement to a sub-AST + */ +object ParseStatement { + private def wrapStmtStr(stmtStr: String): String = { + val indent = " " + val indented = stmtStr.split("\n").mkString(indent, s"\n${indent}", "") + s"""circuit ${DUTRules.dutName} : + | module ${DUTRules.dutName} : + | input clock : Clock + | input reset : UInt<1> + |${indented}""".stripMargin + } + + private def parse(stmtStr: String): Circuit = { + try { + Parser.parseString(wrapStmtStr(stmtStr), Parser.IgnoreInfo) + } catch { + case e: ParserException => throw new ParserHelperException(e, stmtStr) + } + } + + def apply(stmtStr: String): Statement = { + val c = parse(stmtStr) + val stmt = c.modules.collectFirst { case Module(_, _, _, b: Block) => b.stmts.head } + stmt.get + } + + private[execution] def makeDUT(body: String): Circuit = parse(body) +} + +/** + * A utility class that parses a FIRRTL string representing an expression to a sub-AST + */ +object ParseExpression { + def apply(expStr: String): Expression = { + try { + val s = ParseStatement(s"${expStr} is invalid") + s.asInstanceOf[IsInvalid].expr + } catch { + case e: ParserHelperException => throw new ParserHelperException(e.pe, expStr) + } + } +} |
