From 1541b96a5aad7061d5f2ae00e88f0f76776d15d5 Mon Sep 17 00:00:00 2001 From: bdew Date: Mon, 24 Feb 2014 11:28:08 +0200 Subject: [PATCH] Added support for number lists in config --- src/net/bdew/lib/recipes/gencfg/ConfigSection.scala | 5 +++++ .../bdew/lib/recipes/gencfg/GenericConfigParser.scala | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/net/bdew/lib/recipes/gencfg/ConfigSection.scala b/src/net/bdew/lib/recipes/gencfg/ConfigSection.scala index fc0ac03..d71ec0d 100644 --- a/src/net/bdew/lib/recipes/gencfg/ConfigSection.scala +++ b/src/net/bdew/lib/recipes/gencfg/ConfigSection.scala @@ -15,6 +15,8 @@ case class EntryDouble(v: Double) extends CfgEntry case class EntryStr(v: String) extends CfgEntry +case class EntryNumList(v: List[Double]) extends CfgEntry + case class ConfigSection(pfx: String = "") extends CfgEntry with Iterable[(String, CfgEntry)] { var raw = Map.empty[String, CfgEntry].withDefault(x => sys.error("Config value '%s%s' is missing".format(pfx, x))) @@ -33,6 +35,7 @@ case class ConfigSection(pfx: String = "") extends CfgEntry with Iterable[(Strin def getDouble(id: String) = rawget(id, classOf[EntryDouble]).v def getString(id: String) = rawget(id, classOf[EntryStr]).v + def getDoubleList(id: String) = rawget(id, classOf[EntryNumList]).v def getSection(id: String) = rawget(id, classOf[ConfigSection]) final val trueVals = Set("y", "true", "yes", "on") @@ -52,6 +55,8 @@ case class ConfigSection(pfx: String = "") extends CfgEntry with Iterable[(Strin def getInt(id: String) = getDouble(id).round.toInt def getFloat(id: String) = getDouble(id).toFloat + def getIntList(id: String) = getDoubleList(id).map(_.round.toInt).toList + def getFloatList(id: String) = getDoubleList(id).map(_.toFloat).toList } diff --git a/src/net/bdew/lib/recipes/gencfg/GenericConfigParser.scala b/src/net/bdew/lib/recipes/gencfg/GenericConfigParser.scala index da13b42..6ed18e5 100644 --- a/src/net/bdew/lib/recipes/gencfg/GenericConfigParser.scala +++ b/src/net/bdew/lib/recipes/gencfg/GenericConfigParser.scala @@ -15,14 +15,18 @@ trait GenericConfigParser extends RecipeParser { override def statement = statementCfg | super.statement def negativeNumber = "-" ~> decimalNumber ^^ { case x => "-" + x } def signedNumber = decimalNumber | negativeNumber + def cfgStatementNum = ident ~ "=" ~ signedNumber ^^ { case id ~ eq ~ n => CfgVal(id, EntryDouble(n.toDouble)) } def cfgStatementStr = ident ~ "=" ~ str ^^ { case id ~ eq ~ s => CfgVal(id, EntryStr(s)) } + def cfgStatementNumList = ident ~ ("=" ~> "{" ~> signedNumber.* <~ "}") ^^ { case id ~ l => CfgVal(id, EntryNumList(l.map(_.toDouble))) } def cfgStatementSub = cfgBlock ^^ { case (id, st) => CfgSub(id, st) } - def cfgStatement = cfgStatementNum | cfgStatementStr | cfgStatementSub + def cfgStatement = cfgStatementNum | cfgStatementStr | cfgStatementSub | cfgStatementNumList - // A bit of type acrobatics because this is recursive (via cfgStatementSub), the tuple is translated into the proper class in either cfgStatementSub or cfg - def cfgBlock: Parser[(String, List[CfgStatement])] = "cfg" ~> ident ~ ("{" ~> cfgStatement.* <~ "}") ^^ { case id ~ st => (id, st) } + // A bit of type acrobatics because this is recursive (via cfgStatementSub) + // the tuple is translated into the proper class in either cfgStatementSub or cfg + def cfgBlock: Parser[(String, List[CfgStatement])] = + "cfg" ~> ident ~ ("{" ~> cfgStatement.* <~ "}") ^^ { case id ~ st => (id, st) } def statementCfg = cfgBlock ^^ { case (id, st) => StCfg(id, st) } }